os/graphics/graphicscomposition/openwfcompositionengine/adaptation/src/Platform/Graphics/symbian/owfdisplaycontext.cpp
First public contribution.
1 // Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and/or associated documentation files (the
5 // "Materials"), to deal in the Materials without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Materials, and to
8 // permit persons to whom the Materials are furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Materials.
14 // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
32 #include "wfcdevice.h"
33 #include "owfdisplaycontextgeneral.h"
35 #include "owfdisplaycontext.h"
36 #include <graphics/symbianstream.h>
38 static const TInt updateReceiverPriorityTable[] = {10, 9, 8, 7, 6, 5, 4, 3};
40 /* Arbitrary internal version number for SymbianStream registration. */
41 static const TInt KInternalVersion = 4;
43 OWF_DISPCTX OWF_DisplayContext_Create(TInt screenNum)
45 TInt priorityTableSize = sizeof(updateReceiverPriorityTable) / sizeof(updateReceiverPriorityTable[0]);
48 DPRINT(("++ OWF_DisplayContext_Create(screenNum = %d)", screenNum));
49 OWFDisplayContext *pDispCtx = new OWFDisplayContext;
52 DPRINT(("-- Not enough memory to create OWFDisplayContext object"));
55 pDispCtx->iEventServerUpdate = 0;
56 pDispCtx->iCompositionOngoing = WFC_FALSE;
57 pDispCtx->iPreviousCommit = WFC_FALSE;
58 pDispCtx->iSerialNumber = INITIAL_CONTEXT_SERIAL_NUMBER;
59 pDispCtx->fastpathChecked = WFC_FALSE;
60 pDispCtx->fastpathStream = OWF_INVALID_HANDLE;
61 pDispCtx->iScreenContext = NULL;
62 pDispCtx->iInternalStreamAccessed = WFC_FALSE;
64 OWF_Semaphore_Init(&pDispCtx->iRendezvous, 0);
66 if (screenNum != OWF_RESERVED_BAD_SCREEN_NUMBER)
68 if((screenNum < 0) || (screenNum >= priorityTableSize))
70 DPRINT((" Can't retrieve backend priority for the screen number: %d\n", screenNum));
74 priority = updateReceiverPriorityTable[screenNum];
76 // False positive leaving function is in a TRAPD
77 // coverity[leave_without_push]
78 TInt err = SymbianStreamRegisterScreenNotifications(screenNum, priority, KInternalVersion);
79 if (!(err == KErrNone || err == KErrNotReady || err == KErrAlreadyExists))
81 DPRINT(("-- Register with Surface Update Server failed for screen: %d err=%d\n", screenNum, err));
85 if (err == KErrNotReady)
87 DPRINT((" Surface Update Server is not ready for screen: %d\n", screenNum));
89 if (err == KErrAlreadyExists)
91 DPRINT((" Backend object already exists for screen: %d\n", screenNum));
95 pDispCtx->iScreenContext = NULL;
97 DPRINT(("-- OWF_DisplayContext_Create(screenNum = %d) : %p", screenNum, pDispCtx));
98 return static_cast<OWF_DISPCTX>(pDispCtx);
101 void OWF_DisplayContext_Destroy(TInt screenNum, OWF_DISPCTX dc)
103 DPRINT(("++ OWF_DisplayContext_Destroy(screenNum = %d) : %p", screenNum, dc));
104 if (dc != OWF_INVALID_HANDLE)
106 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
110 if (screenNum != OWF_RESERVED_BAD_SCREEN_NUMBER)
112 TInt err = SymbianStreamUnregisterScreenNotifications(screenNum);
113 if (!(err == KErrNone || err == KErrNotReady))
115 DPRINT(("!! Unregister with Surface Update Server failed for screen: %d with %d\n", screenNum, err));
118 DPRINT(("-- OWF_DisplayContext_Destroy()"));
121 OWFDisplayContext_::~OWFDisplayContext_()
125 void* COwfScratchBuffers::Allocate(TInt aMinSize)
127 Create a chunk for a new scratch buffer that is at least aMinSize bytes large.
128 @param aMinSize The minimum size of the buffer required.
129 @return A pointer to the base of the new buffer if successful; otherwise null is returned.
133 if (buf.iChunk.CreateLocal(aMinSize, aMinSize) != KErrNone)
137 buf.iAddress = buf.iChunk.Base();
139 if(iBuffers.Append(buf) == KErrNone)
150 void COwfScratchBuffers::Destroy(void* aAddress)
152 Destroys the buffer with base address aAddress
153 @param aAddress The address of the buffer to destroy
156 for (TInt i = iBuffers.Count() - 1; i >= 0; --i)
158 if (iBuffers[i].iAddress == static_cast<TUint8*>(aAddress))
160 iBuffers[i].iChunk.Close();
167 COwfScratchBuffers::~COwfScratchBuffers()
170 Close all the chunks.
173 for (TInt i = iBuffers.Count() - 1; i >= 0; --i)
175 iBuffers[i].iChunk.Close();
180 void* OWF_DisplayContext_ScratchBuffer_Allocate(OWF_DISPCTX dc, int size)
184 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
185 return pDispCtx->iBuffers.Allocate(size);
188 void OWF_DisplayContext_ScratchBuffer_Destroy(OWF_DISPCTX dc, void* buffer)
192 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
193 pDispCtx->iBuffers.Destroy(buffer);
196 void OWF_DisplayContext_IncrementSerialNumber(OWF_DISPCTX dc)
200 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
201 pDispCtx->iSerialNumber++;
202 if (pDispCtx->iSerialNumber == INITIAL_CONTEXT_SERIAL_NUMBER)
204 pDispCtx->iSerialNumber++;
208 void OWF_DisplayContext_SetCompositionOngoing(OWF_DISPCTX dc, WFCboolean val)
212 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
213 pDispCtx->iCompositionOngoing = val;
216 void OWF_ComposerThread_Rendezvous(OWF_DISPCTX dc)
220 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
221 OWF_Semaphore_Post(&pDispCtx->iRendezvous);
224 void OWF_ComposerThread_RendezvousWait(OWF_DISPCTX dc)
228 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
229 OWF_Semaphore_Wait(&pDispCtx->iRendezvous);
232 void OWF_ComposerThread_RendezvousDestroy(OWF_DISPCTX dc)
236 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
237 OWF_Semaphore_Destroy(&pDispCtx->iRendezvous);
240 void OWF_DisplayContext_EnableFastpath(OWF_DISPCTX dc, OWFNativeStreamType fastpathStream)
242 DPRINT(("++ OWF_DisplayContext_EnableFastpath(%p, %d)", dc, fastpathStream));
245 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
246 pDispCtx->fastpathStream = fastpathStream;
249 void OWF_DisplayContext_DisableFastpath(OWF_DISPCTX dc)
251 DPRINT(("++ OWF_DisplayContext_DisableFastpath(%p)", dc));
254 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
255 pDispCtx->fastpathStream = OWF_INVALID_HANDLE;
258 WFCboolean OWF_DisplayContext_FastpathEnabled(OWF_DISPCTX dc)
262 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
263 DPRINT(("++ OWF_DisplayContext_FastpathEnabled(%p) : %d", dc, (pDispCtx->fastpathStream != OWF_INVALID_HANDLE)));
264 return (pDispCtx->fastpathStream != OWF_INVALID_HANDLE) ? WFC_TRUE : WFC_FALSE;
267 OWFNativeStreamType OWF_DisplayContext_FastpathStream(OWF_DISPCTX dc)
271 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
272 DPRINT(("++ OWF_DisplayContext_FastpathStream(%p) : %p", dc, pDispCtx->fastpathStream));
273 return pDispCtx->fastpathStream;
276 WFCboolean OWF_DisplayContext_FastpathChecked(OWF_DISPCTX dc)
280 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
281 DPRINT(("++ OWF_DisplayContext_FastpathChecked(%p) : %d", dc, pDispCtx->fastpathChecked));
282 return pDispCtx->fastpathChecked;
285 void OWF_DisplayContext_ResetFastpathCheck(OWF_DISPCTX dc)
287 DPRINT(("++ OWF_DisplayContext_ResetFastpathCheck(%p)", dc));
290 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
291 pDispCtx->fastpathChecked = WFC_FALSE;
294 void OWF_DisplayContext_SetFastpathChecked(OWF_DISPCTX dc)
296 DPRINT(("++ OWF_DisplayContext_SetFastpathCheck(%p)", dc));
299 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
300 pDispCtx->fastpathChecked = WFC_TRUE;
303 void OWF_DisplayContext_FlagInternalStreamAccessed(OWF_DISPCTX dc)
305 DPRINT(("++ OWF_DisplayContext_FlagInternalStreamAccessed(%p)", dc));
308 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
309 pDispCtx->iInternalStreamAccessed = WFC_TRUE;
312 WFCboolean OWF_DisplayContext_InternalStreamAccessed(OWF_DISPCTX dc)
316 OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
317 DPRINT(("++ OWF_DisplayContext_InternalStreamAccessed(%p) : ", dc, pDispCtx->iInternalStreamAccessed));
318 return pDispCtx->iInternalStreamAccessed;
321 OWFboolean OWF_DisplayContext_Check_Fastpathed_Stream(OWF_DISPCTX dc, OWFNativeStreamType fastpathedStream)
328 khronos_int32_t pixelFormat = 0;
329 SymbianStreamGetHeader((SymbianStreamType)fastpathedStream, NULL, NULL, NULL, &pixelFormat, NULL);
331 return ((EUidPixelFormatARGB_8888_PRE == pixelFormat) || (EUidPixelFormatARGB_8888 == pixelFormat) || (EUidPixelFormatXRGB_8888 == pixelFormat)) ? OWF_TRUE : OWF_FALSE;
334 OWFboolean OWF_DisplayContext_CopyFastpathedStreamToTargetStream(void* ctxt)
336 DPRINT(("++ OWF_DisplayContext_CopyFastpathedStreamToTargetStream(%p)", ctxt));
337 WFC_CONTEXT* context = CONTEXT(ctxt);
338 if (!context || !context->displayContext)
340 DPRINT(("!! OWF_DisplayContext_CopyFastpathedStreamToTargetStream(%p) invalid", ctxt));
343 khronos_int32_t dstStride = context->state.targetImage->stride;
344 TInt height = context->state.targetImage->height;
345 void* dstPtr = context->state.targetPixels;
346 OWFDisplayContext* pDispCtx = _OWF_DISPLAYCONTEXT(context->displayContext);
347 SymbianStreamType fastpathedStream = (SymbianStreamType)pDispCtx->fastpathStream;
348 if (!OWF_DisplayContext_Check_Fastpathed_Stream(pDispCtx,pDispCtx->fastpathStream))
349 { //The format does not lend itself to quick copying.
350 //Clients listening for update callbacks will be disappointed.
354 khronos_int32_t srcStride = 0;
355 SymbianStreamBuffer frontBuffer;
356 /* Stride may include unaddressable memory, so only copy valid pixels */
357 khronos_int32_t copyLen = context->state.targetImage->width * context->state.targetImage->pixelSize;
359 SymbianStreamAcquireReadBuffer(fastpathedStream, &frontBuffer);
360 SymbianStreamGetBufferPointer(fastpathedStream, frontBuffer, &srcPtr);
362 SymbianStreamGetHeader(fastpathedStream, NULL, NULL, &srcStride, NULL, NULL);
364 for (TInt i = 0; i < height; i++)
366 Mem::Copy(dstPtr, srcPtr, copyLen);
367 dstPtr = (khronos_uint8_t*)dstPtr + dstStride;
368 srcPtr = (khronos_uint8_t*)srcPtr + srcStride;
371 SymbianStreamReleaseReadBuffer(fastpathedStream, frontBuffer);
372 DPRINT(("-- OWF_DisplayContext_CopyFastpathedStreamToTargetStream(%p)", ctxt));