author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 14 Sep 2010 21:48:24 +0300 | |
branch | RCL_3 |
changeset 59 | 978afdc0236f |
parent 56 | d48ab3b357f1 |
child 72 | a5e7a4f63858 |
permissions | -rw-r--r-- |
56 | 1 |
/* |
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
||
18 |
||
19 |
// INCLUDE FILES |
|
20 |
#include "HgVgEGL.h" |
|
21 |
#include "HgVgConstants.h" |
|
22 |
#include <coecntrl.h> |
|
23 |
||
24 |
||
25 |
using namespace HgVgConstants; |
|
26 |
||
27 |
// ============================ MEMBER FUNCTIONS =============================== |
|
28 |
// ----------------------------------------------------------------------------- |
|
29 |
// CHgVgEGL::NewL() |
|
30 |
// Two-phased constructor. |
|
31 |
// ----------------------------------------------------------------------------- |
|
32 |
// |
|
33 |
CHgVgEGL* CHgVgEGL::NewL(RWindow& aWindow) |
|
34 |
{ |
|
35 |
CHgVgEGL* self = new ( ELeave ) CHgVgEGL(); |
|
36 |
CleanupStack::PushL (self ); |
|
37 |
self->ConstructL(aWindow); |
|
38 |
CleanupStack::Pop (self ); |
|
39 |
return self; |
|
40 |
} |
|
41 |
||
42 |
// ----------------------------------------------------------------------------- |
|
43 |
// CHgVgEGL::ConstructL() |
|
44 |
// Symbian 2nd phase constructor can leave. |
|
45 |
// ----------------------------------------------------------------------------- |
|
46 |
// |
|
47 |
void CHgVgEGL::ConstructL (RWindow& aWindow) |
|
48 |
{ |
|
49 |
InitEGL(aWindow); |
|
59
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
50 |
// Enable flag that is used to determine whether egl has been initialized. |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
51 |
// For example vgImages shouldn't be created before egl has been initialized. |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
52 |
// There will be only one instance of this class so there is no need to check |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
53 |
// whether there is already something in the DLL tls. |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
54 |
TBool* eglInitialized = new TBool(ETrue); |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
55 |
Dll::SetTls((TAny*)eglInitialized); |
56 | 56 |
} |
57 |
||
58 |
// ----------------------------------------------------------------------------- |
|
59 |
// CHgVgEGL::CHgVgEGL() |
|
60 |
// C++ default constructor can NOT contain any code, that might leave. |
|
61 |
// ----------------------------------------------------------------------------- |
|
62 |
// |
|
63 |
CHgVgEGL::CHgVgEGL() |
|
64 |
{ |
|
65 |
} |
|
66 |
||
67 |
// ----------------------------------------------------------------------------- |
|
68 |
// CHgVgEGL::~CHgVgEGL() |
|
69 |
// Destructor. |
|
70 |
// ----------------------------------------------------------------------------- |
|
71 |
// |
|
72 |
CHgVgEGL::~CHgVgEGL ( ) |
|
73 |
{ |
|
74 |
DestroyEGL(); |
|
59
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
75 |
// Disable flag that is used to determine whether egl has been initialized. |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
76 |
// For example vgImages shouldn't be created before egl has been initialized. |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
77 |
delete Dll::Tls(); |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
78 |
Dll::FreeTls(); |
56 | 79 |
} |
80 |
||
81 |
// --------------------------------------------------------------------------- |
|
82 |
// CHgVgEGL::InitWindowSurfaceL() |
|
83 |
// --------------------------------------------------------------------------- |
|
84 |
// |
|
85 |
void CHgVgEGL::InitWindowSurfaceL(RWindow& aWindow) |
|
86 |
{ |
|
87 |
//RDebug::Print(_L("CHgVgEGL::InitWindowSurface")); |
|
88 |
||
89 |
InitEGL(aWindow); |
|
90 |
} |
|
91 |
||
92 |
// --------------------------------------------------------------------------- |
|
93 |
// CHgVgEGL::FreeWindowSurface() |
|
94 |
// --------------------------------------------------------------------------- |
|
95 |
// |
|
96 |
void CHgVgEGL::FreeWindowSurface() |
|
97 |
{ |
|
98 |
//RDebug::Print(_L("CHgVgEGL::FreeWindowSurface")); |
|
99 |
||
100 |
// make sure there is nothing pending. |
|
101 |
vgFinish(); |
|
102 |
||
103 |
// unbind current surface. |
|
104 |
eglMakeCurrent(iDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, iContextVg); |
|
105 |
||
106 |
// destroy surface |
|
107 |
eglDestroySurface(iDisplay, iSurface); |
|
108 |
iSurface = VG_INVALID_HANDLE; |
|
109 |
} |
|
110 |
||
111 |
// --------------------------------------------------------------------------- |
|
112 |
// CHgVgEGL::ReadyToRender() |
|
113 |
// --------------------------------------------------------------------------- |
|
114 |
// |
|
115 |
TBool CHgVgEGL::ReadyToRender() const |
|
116 |
{ |
|
117 |
return iSurface && iDisplay && iContextVg; |
|
118 |
} |
|
119 |
||
120 |
// --------------------------------------------------------------------------- |
|
121 |
// CHgVgEGL::SwapBuffers() |
|
122 |
// --------------------------------------------------------------------------- |
|
123 |
// |
|
124 |
void CHgVgEGL::SwapBuffers() |
|
125 |
{ |
|
126 |
eglSwapBuffers(iDisplay, iSurface); |
|
127 |
} |
|
128 |
||
129 |
// --------------------------------------------------------------------------- |
|
130 |
// CHgVgEGL::Display() |
|
131 |
// --------------------------------------------------------------------------- |
|
132 |
// |
|
133 |
EGLDisplay CHgVgEGL::Display() const |
|
134 |
{ |
|
135 |
return iDisplay; |
|
136 |
} |
|
137 |
||
138 |
// --------------------------------------------------------------------------- |
|
139 |
// CHgVgEGL::Surface |
|
140 |
// --------------------------------------------------------------------------- |
|
141 |
// |
|
142 |
EGLSurface CHgVgEGL::Surface() const |
|
143 |
{ |
|
144 |
return iSurface; |
|
145 |
} |
|
146 |
||
147 |
// --------------------------------------------------------------------------- |
|
148 |
// CHgVgEGL::Context() |
|
149 |
// --------------------------------------------------------------------------- |
|
150 |
// |
|
151 |
EGLContext CHgVgEGL::Context() const |
|
152 |
{ |
|
153 |
return iContextVg; |
|
154 |
} |
|
155 |
||
156 |
// --------------------------------------------------------------------------- |
|
157 |
// CHgVgEGL::CurrentConfig() |
|
158 |
// --------------------------------------------------------------------------- |
|
159 |
// |
|
160 |
EGLConfig CHgVgEGL::CurrentConfig() const |
|
161 |
{ |
|
162 |
return iChosenConfig; |
|
163 |
} |
|
164 |
||
165 |
// --------------------------------------------------------------------------- |
|
166 |
// CHgVgEGL::InitEGL() |
|
167 |
// --------------------------------------------------------------------------- |
|
168 |
// |
|
169 |
void CHgVgEGL::InitEGL(RWindow& aWindow) |
|
170 |
{ |
|
171 |
||
172 |
// if we are just resizing or recreating |
|
173 |
if (iContextVg != VG_INVALID_HANDLE) |
|
174 |
{ |
|
175 |
||
176 |
||
177 |
// save old surface |
|
178 |
EGLSurface oldSurface = VG_INVALID_HANDLE; |
|
179 |
if (iSurface != VG_INVALID_HANDLE) |
|
180 |
{ |
|
181 |
// make sure there is nothing pending. |
|
182 |
vgFinish(); |
|
183 |
||
184 |
oldSurface = iSurface; |
|
185 |
||
186 |
// unbind current surface. |
|
187 |
eglMakeCurrent(iDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, iContextVg); |
|
188 |
} |
|
189 |
||
190 |
// create new surface |
|
191 |
InitEGLWindowSurfaceL(aWindow); |
|
192 |
||
193 |
// bind new surface |
|
194 |
eglMakeCurrent(iDisplay, iSurface, iSurface, iContextVg); |
|
195 |
||
196 |
if (oldSurface != VG_INVALID_HANDLE) |
|
197 |
{ |
|
198 |
// free old surface |
|
199 |
eglDestroySurface(iDisplay,oldSurface); |
|
200 |
} |
|
201 |
||
202 |
} |
|
203 |
else |
|
204 |
{ |
|
205 |
||
206 |
// get display |
|
207 |
iDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
|
208 |
||
209 |
// intialize egl |
|
210 |
eglInitialize(iDisplay, NULL, NULL); |
|
211 |
||
212 |
// find suitable display config |
|
213 |
EGLint numConfigs; |
|
214 |
iChosenConfig = 0; |
|
215 |
||
216 |
const EGLint KColorAttribList[] = |
|
217 |
{ |
|
218 |
EGL_RED_SIZE, KSurfaceRedBits, |
|
219 |
EGL_GREEN_SIZE, KSurfaceGreenBits, |
|
220 |
EGL_BLUE_SIZE, KSurfaceBlueBits, |
|
221 |
EGL_ALPHA_SIZE, KSurfaceAlphaBits, |
|
222 |
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
|
223 |
EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT, |
|
224 |
EGL_NONE |
|
225 |
}; |
|
226 |
||
227 |
eglChooseConfig(iDisplay, KColorAttribList, &iChosenConfig, 1, &numConfigs); |
|
228 |
||
229 |
// we need openvg |
|
230 |
eglBindAPI(EGL_OPENVG_API); |
|
231 |
||
232 |
InitEGLWindowSurfaceL(aWindow); |
|
233 |
||
234 |
// Create context for our thread. |
|
235 |
iContextVg = eglCreateContext(iDisplay, iChosenConfig, EGL_NO_CONTEXT, NULL); |
|
236 |
||
237 |
// bind surface to context |
|
238 |
eglMakeCurrent(iDisplay, iSurface, iSurface, iContextVg); |
|
239 |
||
240 |
} |
|
241 |
||
242 |
} |
|
243 |
||
244 |
// --------------------------------------------------------------------------- |
|
245 |
// CHgVgEGL::InitEGLWindowSurfaceL() |
|
246 |
// --------------------------------------------------------------------------- |
|
247 |
// |
|
248 |
void CHgVgEGL::InitEGLWindowSurfaceL(RWindow& aWindow) |
|
249 |
{ |
|
250 |
||
251 |
||
252 |
// Create surface with premodulated alpha. |
|
253 |
const EGLint KAttriblist[] = |
|
254 |
{ |
|
255 |
EGL_VG_ALPHA_FORMAT, EGL_VG_ALPHA_FORMAT_PRE, |
|
256 |
EGL_NONE |
|
257 |
}; |
|
258 |
iSurface = eglCreateWindowSurface(iDisplay, iChosenConfig, &aWindow, KAttriblist); |
|
259 |
EGLint error = eglGetError(); |
|
260 |
if (error == EGL_BAD_ALLOC) |
|
261 |
{ |
|
262 |
//RDebug::Print(_L("CHgVgEGL:: Can't create surface! (out of memory)")); |
|
263 |
User::Leave(KErrNoMemory); |
|
264 |
} |
|
265 |
else if (error == EGL_BAD_SURFACE || iSurface == EGL_NO_SURFACE) |
|
266 |
{ |
|
267 |
//RDebug::Print(_L("CHgVgEGL:: Can't create surface! (general)")); |
|
268 |
User::Leave(KErrGeneral); |
|
269 |
} |
|
270 |
||
271 |
eglSurfaceAttrib(iDisplay, iSurface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED); |
|
272 |
||
273 |
} |
|
274 |
||
275 |
// --------------------------------------------------------------------------- |
|
276 |
// CHgVgEGL::DestroyEGL() |
|
277 |
// --------------------------------------------------------------------------- |
|
278 |
// |
|
279 |
void CHgVgEGL::DestroyEGL() |
|
280 |
{ |
|
281 |
// deinitialize egl |
|
282 |
eglMakeCurrent(iDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
|
283 |
eglDestroySurface(iDisplay,iSurface); |
|
284 |
eglDestroyContext( iDisplay, iContextVg ); |
|
285 |
eglTerminate(iDisplay); |
|
286 |
eglReleaseThread(); |
|
287 |
} |
|
288 |
||
289 |
||
290 |
// --------------------------------------------------------------------------- |
|
291 |
// CHgVgEGL::GetSurfaceToBitmap() |
|
292 |
// --------------------------------------------------------------------------- |
|
293 |
// |
|
294 |
CFbsBitmap* CHgVgEGL::GetSurfaceToBitmap(const TRect& aRect, TBool aLandscape) const |
|
295 |
{ |
|
296 |
// make sure every thing is in back buffer |
|
297 |
vgFinish(); |
|
298 |
||
299 |
||
300 |
CFbsBitmap* bitmap = new CFbsBitmap(); |
|
301 |
||
302 |
if (bitmap) |
|
303 |
{ |
|
304 |
// create temporary buffer for data |
|
305 |
RBuf8 buf; |
|
306 |
TInt bufCreateErr = buf.Create(aRect.Width() * aRect.Height() * 4); |
|
307 |
if(bufCreateErr != KErrNone) { |
|
308 |
buf.Close(); //just in case |
|
309 |
delete bitmap; |
|
310 |
bitmap = NULL; |
|
311 |
return NULL; |
|
312 |
} |
|
313 |
buf.Fill(0xFF); |
|
314 |
||
315 |
// read data back from vg (this is sloooww) |
|
316 |
VGint dataStride = 0; |
|
317 |
if (aLandscape) |
|
318 |
{ |
|
319 |
dataStride = 4 * aRect.Height(); |
|
320 |
vgReadPixels((void*)buf.Ptr(), |
|
321 |
dataStride, VG_sARGB_8888, 0, 0, aRect.Height(), aRect.Width()); |
|
322 |
} |
|
323 |
else |
|
324 |
{ |
|
325 |
dataStride = 4 * aRect.Width(); |
|
326 |
vgReadPixels((void*)buf.Ptr(), |
|
327 |
dataStride, VG_sARGB_8888, 0, 0, aRect.Width(), aRect.Height()); |
|
328 |
} |
|
329 |
||
330 |
// because of bug in vg driver we need to swap memory using for loop, because |
|
331 |
// negative datastrides cause crash |
|
332 |
TInt bitmapCreateErr = bitmap->Create(TSize(aRect.Width(), aRect.Height()), EColor16MA); |
|
333 |
if(bitmapCreateErr != KErrNone) { |
|
334 |
buf.Close(); |
|
335 |
delete bitmap; |
|
336 |
bitmap = NULL; |
|
337 |
return NULL; |
|
338 |
} |
|
339 |
||
340 |
bitmap->BeginDataAccess(); |
|
341 |
if (!aLandscape) |
|
342 |
{ |
|
343 |
TUint8* ptrTrg = (TUint8*)bitmap->DataAddress(); |
|
344 |
TUint8* ptrSrc = (TUint8*)buf.Ptr(); |
|
345 |
for (TInt i = 0; i < aRect.Height(); i++) |
|
346 |
{ |
|
347 |
Mem::Copy(ptrTrg + dataStride * i, |
|
348 |
ptrSrc + dataStride * (aRect.Height() - i - 1), dataStride); |
|
349 |
} |
|
350 |
} |
|
351 |
else |
|
352 |
{ |
|
353 |
TUint32* ptrTrg = (TUint32*)bitmap->DataAddress(); |
|
354 |
TUint32* ptrSrc = (TUint32*)buf.Ptr(); |
|
355 |
for (TInt i = 0; i < aRect.Height(); i++) |
|
356 |
{ |
|
357 |
for (TInt j = 0; j < aRect.Width(); j++) |
|
358 |
{ |
|
359 |
ptrTrg[(aRect.Height() - i - 1)*aRect.Width()+(aRect.Width()-j-1)] = ptrSrc[j*aRect.Height()+i]; |
|
360 |
} |
|
361 |
} |
|
362 |
} |
|
363 |
bitmap->EndDataAccess(); |
|
364 |
buf.Close(); |
|
365 |
} |
|
366 |
||
367 |
return bitmap; |
|
368 |
} |
|
369 |
||
59
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
370 |
// --------------------------------------------------------------------------- |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
371 |
// CHgVgEGL::EglInitialized() |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
372 |
// --------------------------------------------------------------------------- |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
373 |
// |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
374 |
TBool CHgVgEGL::EglInitialized() |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
375 |
{ |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
376 |
TBool* eglInitialized = (TBool*)Dll::Tls(); |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
377 |
return eglInitialized ? *eglInitialized : EFalse; |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
378 |
} |
978afdc0236f
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
56
diff
changeset
|
379 |
|
56 | 380 |
// End of File |