Merge "fixed bidi cache(for space)" into tizen_2.2
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_OverlayAgent.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FUiCtrl_OverlayAgent.cpp
20  * @brief               This file contains implementation of _OverlayAgent class
21  */
22
23 #include <new>
24 #include <sys/shm.h>
25 #include <unique_ptr.h>
26 #include <Ecore_Evas.h>
27
28 #include <FBaseResult.h>
29 #include <FUiIOrientationEventListener.h>
30 #include <FUiAnimDisplayContext.h>
31 #include <FGrp_BufferInfoImpl.h>
32 #include <FGrp_Screen.h>
33 #include <FIo_RegistryImpl.h>
34 #include <FMedia_ImageUtil.h>
35 #include "FUi_Control.h"
36 #include "FUi_CoordinateSystemUtils.h"
37 #include "FUi_EcoreEvas.h"
38 #include "FUi_EcoreEvasMgr.h"
39 #include "FUi_Window.h"
40 #include "FUiAnim_EflNode.h"
41 #include "FUiAnim_ControlVisualElement.h"
42 #include "FUiAnim_VisualElementSurfaceImpl.h"
43 #include "FUiAnim_VisualElementImpl.h"
44 #include "FUiCtrl_OverlayAgent.h"
45
46 #define B(c,s) ((((unsigned int)(c)) & 0xff) << (s))
47 #define FOURCC(a,b,c,d) (B(d,24) | B(c,16) | B(b,8) | B(a,0))
48
49 using namespace std;
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Collection;
52 using namespace Tizen::Graphics;
53 using namespace Tizen::Io;
54 using namespace Tizen::Media;
55 using namespace Tizen::System;
56 using namespace Tizen::Ui::Controls;
57 using namespace Tizen::Ui::Animations;
58
59 namespace
60 {
61 bool
62 CheckXvExtension(Display* pDisplay, int* pFirstPort, int* pCountPort)
63 {
64         unsigned int version = 0;
65         unsigned int release = 0;
66         unsigned int requestBase = 0;
67         unsigned int eventBase = 0;
68         unsigned int errorBase = 0;
69
70         // Query and print Xvideo properties
71         int returnValue = XvQueryExtension(pDisplay, &version, &release, &requestBase, &eventBase, &errorBase);
72         if (returnValue != Success)
73         {
74                 if (returnValue == XvBadExtension)
75                 {
76                         SysSecureLog(NID_UI_CTRL, "XvBadExtension returned at XvQueryExtension!");
77                 }
78                 else if (returnValue == XvBadAlloc)
79                 {
80                         SysSecureLog(NID_UI_CTRL, "XvBadAlloc returned at XvQueryExtension!");
81                 }
82                 else
83                 {
84                         SysSecureLog(NID_UI_CTRL, "Other error happened at XvQueryExtension!");
85                 }
86
87                 return false;
88         }
89
90         unsigned int adaptors = 0;
91         XvAdaptorInfo* pAdaptorInfo = null;
92         returnValue = XvQueryAdaptors(pDisplay, DefaultRootWindow(pDisplay), &adaptors, &pAdaptorInfo);
93         if (returnValue != Success)
94         {
95                 if (returnValue == XvBadExtension)
96                 {
97                         SysSecureLog(NID_UI_CTRL, "XvBadExtension returned at XvQueryExtension.");
98                 }
99                 else if (returnValue == XvBadAlloc)
100                 {
101                         SysSecureLog(NID_UI_CTRL, "XvBadAlloc returned at XvQueryExtension.");
102                 }
103                 else
104                 {
105                         SysSecureLog(NID_UI_CTRL, "Other error happaned at XvQueryAdaptors.");
106                 }
107
108                 if (pAdaptorInfo != null)
109                 {
110                         XvFreeAdaptorInfo(pAdaptorInfo);
111                 }
112
113                 return false;
114         }
115
116         int port = -1;
117         if (adaptors > 0 && pAdaptorInfo != null)
118         {
119                 for (unsigned int i = 0; i < adaptors; i++)
120                 {
121                         for (unsigned int j = 0; j < pAdaptorInfo[i].num_formats; j++)
122                         {
123                                 SysSecureLog(NID_UI_CTRL, "The current value of depth is %d and visual is %ld.", pAdaptorInfo[i].formats[j].depth, pAdaptorInfo[i].formats[j].visual_id);
124                         }
125                 }
126
127                 port = pAdaptorInfo[0].base_id;
128
129                 *pFirstPort = port;
130                 *pCountPort = pAdaptorInfo[0].num_ports;
131                 SysSecureLog(NID_UI_CTRL, "The current value of port is %d", port);
132         }
133
134         if (pAdaptorInfo != null)
135         {
136                 XvFreeAdaptorInfo(pAdaptorInfo);
137         }
138
139         if (port == -1)
140         {
141                 return false;
142         }
143
144         return true;
145 }
146
147 Eina_Bool
148 OnPixmapDamaged(void* pData, int type, void* pEvent)
149 {
150         Ecore_X_Event_Damage* pDamageEvent = (Ecore_X_Event_Damage*)pEvent;
151         SysTryReturn(NID_UI_CTRL, pDamageEvent != null, ECORE_CALLBACK_PASS_ON, E_INVALID_DATA, "[E_INVALID_DATA] The current value of DamageEvent is invalid.");
152
153         Tizen::Ui::Controls::_OverlayAgent* pOverlayAgent = static_cast<Tizen::Ui::Controls::_OverlayAgent*>(pData);
154         SysTryReturn(NID_UI_CTRL, pOverlayAgent != null, ECORE_CALLBACK_PASS_ON, E_INVALID_DATA, "[E_INVALID_DATA] Failed to get OverlayAgent from DamagedEvent.");
155
156         if (pDamageEvent->drawable != pOverlayAgent->GetPixmap())
157         {
158                 return ECORE_CALLBACK_PASS_ON;
159         }
160
161         pOverlayAgent->SetRendererFlushNeeded();
162
163         return ECORE_CALLBACK_DONE;
164 }
165
166 void
167 OnEvasImageDamaged(void* pData, Evas_Object* pImageObject)
168 {
169         Tizen::Ui::Controls::_OverlayAgent* pOverlayAgent = static_cast<Tizen::Ui::Controls::_OverlayAgent*>(pData);
170         SysTryReturnVoidResult(NID_UI_CTRL, pOverlayAgent != null, E_INVALID_DATA, "[E_INVALID_DATA] Failed to get OverlayAgent from DamagedEvent.");
171
172         Evas_Object* pRendererImageObject = pOverlayAgent->GetRendererImageObject();
173         SysTryReturnVoidResult(NID_UI_CTRL, pRendererImageObject != null, E_INVALID_DATA, "[E_INVALID_DATA] Failed to get Renderer ImageObject of OverlayAgent.");
174
175         if (pRendererImageObject != pImageObject)
176         {
177                 return;
178         }
179
180         Dimension newImageSize(0,0);
181         evas_object_image_size_get(pImageObject, &newImageSize.width, &newImageSize.height);
182
183         Dimension rendererImageSize(0,0);
184         evas_object_image_size_get(pRendererImageObject, &rendererImageSize.width, &rendererImageSize.height);
185
186         if (!newImageSize.Equals(rendererImageSize))
187         {
188                 SysSecureLog(NID_UI_CTRL,"newImageSize [%d, %d] rendererImageSize [%d, %d]", newImageSize.width, newImageSize.height, rendererImageSize.width, rendererImageSize.height);
189                 pOverlayAgent->SetDestination(FloatDimension(static_cast<float>(newImageSize.width), static_cast<float>(newImageSize.height)));
190         }
191 }
192 } // Anonymous
193
194 namespace Tizen { namespace Ui { namespace Controls
195 {
196
197 int _OverlayAgent::__baseXvPort = 0;
198 bool _OverlayAgent::__isPortGrabbed[]={false,};
199
200 int _OverlayAgent::__overlayAgentCount = 0;
201 int _OverlayAgent::__dstRectMinWidth = -1;
202 int _OverlayAgent::__dstRectMinHegith = -1;
203 int _OverlayAgent::__srcRectMinWidth = -1;
204 int _OverlayAgent::__srcRectMinHegith = -1;
205 int _OverlayAgent::__overlayWidthUnit = -1;
206 int _OverlayAgent::__overlayHeightUnit = -1;
207 int _OverlayAgent::__overlayMaxCount = -1;
208 bool _OverlayAgent::__OverlayAgentBufferPixelFormat[] = {false, };
209
210 struct visualElementDeleter
211 {
212         void operator()(_VisualElement* pVisualElement)
213         {
214                 pVisualElement->Destroy();
215         }
216 };
217
218 _OverlayAgent*
219 _OverlayAgent::CreateInstanceN(_OverlayAgentStyle style, const _Control& parentControl, const FloatRectangle& logicalBounds, const Rectangle& physicalBounds)
220 {
221         unique_ptr<_OverlayAgent> pOverlayAgent(new (std::nothrow) _OverlayAgent(style, parentControl, logicalBounds, physicalBounds));
222         SysTryReturn(NID_UI_CTRL, pOverlayAgent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
223
224         result r = GetLastResult();
225         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
226
227         return pOverlayAgent.release();
228 }
229
230 _OverlayAgent::_OverlayAgent(_OverlayAgentStyle style, const _Control& parentControl, const FloatRectangle& logicalBounds, const Rectangle& physicalBounds)
231         : __pParentVE(null)
232         , __pRendererVE(null)
233         , __pImageObject(null)
234         , __style(style)
235         , __currentRotation(_OVERLAY_AGENT_ROTATION_NONE)
236         , __savedColorFormat(FOURCC('R','G','B','4'))
237         , __currentColorFormat(_OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_ARGB8888)
238         , __currentSourceBufferSize(0,0)
239         , __standardRendererBounds(0.0f, 0.0f, 0.0f, 0.0f)
240         , __standardParentBounds(0.0f, 0.0f, 0.0f, 0.0f)
241         , __needToRellaocImage(false)
242         , __pCurrentSourceBuffer(null)
243         , __pConvertedSourceBuffer(null)
244         , __pixmapDamageHandle(0)
245         , __pPixmapEventHandler(null)
246         , __pixmap(0)
247         , __xvPort(-1)
248         , __pXvImage(null)
249         , __pShmInfo(null)
250 {
251         result r = CreateRendererVisualElement(parentControl, logicalBounds, physicalBounds);
252
253         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
254         SysTryReturnVoidResult(NID_UI_CTRL, __pRendererVE != null, E_SYSTEM, "[E_SYSTEM] The Renderer VisualElement is null.");
255         SysTryReturnVoidResult(NID_UI_CTRL, __pImageObject!= null, E_SYSTEM, "[E_SYSTEM] The Renderder ImageObject is null.");
256
257         __overlayAgentCount++;
258         SysLog(NID_UI_CTRL, "The current count of OverlayAgent is %d", __overlayAgentCount);
259 }
260
261 _OverlayAgent::~_OverlayAgent(void)
262 {
263         SysLog(NID_UI_CTRL, "The %dth OverlayAgent is deleted.", __overlayAgentCount);
264         __overlayAgentCount--;
265
266         UngrabXvPort();
267
268         if (__pPixmapEventHandler)
269         {
270                 ecore_event_handler_del(__pPixmapEventHandler);
271                 __pPixmapEventHandler = null;
272         }
273
274         if (__pixmapDamageHandle)
275         {
276                 ecore_x_damage_free(__pixmapDamageHandle);
277                 __pixmapDamageHandle = 0;
278         }
279
280         if (__pixmap != 0)
281         {
282                 XFreePixmap((Display*)ecore_x_display_get(), __pixmap);
283                 __pixmap = 0;
284         }
285
286         if (__pShmInfo)
287         {
288                 XShmDetach((Display*)ecore_x_display_get(), __pShmInfo);
289                 shmdt(__pShmInfo->shmaddr);
290                 shmctl(__pShmInfo->shmid, IPC_RMID, NULL);
291                 __pShmInfo = null;
292         }
293
294         if (__pXvImage)
295         {
296                 XFree(__pXvImage);
297                 __pXvImage = null;
298         }
299
300         if (__pConvertedSourceBuffer)
301         {
302                 delete __pConvertedSourceBuffer;
303                 __pConvertedSourceBuffer = null;
304         }
305
306         if (__pCurrentSourceBuffer)
307         {
308                 delete __pCurrentSourceBuffer;
309                 __pCurrentSourceBuffer = null;
310         }
311
312         if (__pRendererVE != null)
313         {
314                 __pRendererVE->SetSurface(null);
315                 __pRendererVE->Destroy();
316                 __pRendererVE = null;
317         }
318
319         if (__pImageObject)
320         {
321                 evas_object_del(__pImageObject);
322                 __pImageObject = null;
323         }
324 }
325
326 result
327 _OverlayAgent::GetBufferInfo(BufferInfo& bufferInfo) const
328 {
329         SysTryReturnResult(NID_UI_CTRL, __pImageObject != null, E_SYSTEM, "The Renderer ImageObject is null.");
330         SysTryReturnResult(NID_UI_CTRL, __pRendererVE != null, E_SYSTEM, "The Renderer Visual Element is null.");
331
332         int width = 0;
333         int height = 0;
334         evas_object_geometry_get(__pImageObject, NULL, NULL, &width, &height);
335
336         bufferInfo.width = width;
337         bufferInfo.pitch = width;
338         bufferInfo.height = height;
339         bufferInfo.bitsPerPixel = 32;
340         bufferInfo.pixelFormat = PIXEL_FORMAT_ARGB8888;
341
342         _BufferInfoImpl* pBufferInfoImpl = _BufferInfoImpl::GetInstance(bufferInfo);
343         SysTryReturnResult(NID_UI_CTRL, pBufferInfoImpl != null, E_SYSTEM, "Failed to get instance of buffer info.");
344
345         result r = E_SUCCESS;
346
347         if(__style == _OVERLAY_AGENT_STYLE_REGION_SW || __style == _OVERLAY_AGENT_STYLE_PANEL_SW)
348         {
349                 pBufferInfoImpl->SetHandle(_BufferInfoImpl::HANDLE_TYPE_VE_SURFACE, reinterpret_cast<Handle>(__pImageObject));
350                 SysSecureLog(NID_UI_CTRL,"The returned handle of BufferInfo is a evas image object (HANDLE_TYPE_VE_SURFACE)");
351         }
352         else
353         {
354                 pBufferInfoImpl->SetHandle(_BufferInfoImpl::HANDLE_TYPE_OVERLAY_REGION, __pixmap);
355                 SysSecureLog(NID_UI_CTRL,"The returned handle of BufferInfo is a pixmap (HANDLE_TYPE_OVERLAY_REGION)");
356         }
357
358         pBufferInfoImpl->SetBounds(Rectangle(0, 0, width, height));
359
360         return r;
361 }
362
363 result
364 _OverlayAgent::SetInputBuffer(const ByteBuffer& srcBuffer, const Dimension& srcDim, _OverlayAgentBufferPixelFormat srcFormat)
365 {
366         SysTryReturnResult(NID_UI_CTRL, srcBuffer.GetPointer() != null, E_INVALID_ARG, "The current value of input source buffer is invalid.");
367         SysTryReturnResult(NID_UI_CTRL, srcBuffer.GetCapacity() > 0, E_INVALID_ARG, "The current size of input source buffer is invalid.");
368         SysTryReturnResult(NID_UI_CTRL, srcDim.width >= GetSrcRectMinWidth(), E_INVALID_ARG, "The width of dimension [%d] is under the minimum size.", srcDim.width);
369         SysTryReturnResult(NID_UI_CTRL, srcDim.height >= GetSrcRectMinHeight(), E_INVALID_ARG, "The height of dimension [%d] is under the minimum size.", srcDim.height);
370
371         if (__currentColorFormat != srcFormat)
372         {
373                 unsigned int colorFormat = 0;
374
375                 switch (srcFormat)
376                 {
377                 case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_ARGB8888:
378                         colorFormat = FOURCC('R','G','B','4');
379                         break;
380                 case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_RGB565:
381                         colorFormat = FOURCC('R','G','B','P');
382                         break;
383                 case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_YCbCr420_PLANAR:
384                         colorFormat = FOURCC('I','4','2','0');
385                         break;
386                 case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_NV12:
387                         colorFormat = FOURCC('N','V','1','2');
388                         break;
389                 case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_UYVY:
390                         colorFormat = FOURCC('U','Y','V','Y');
391                         break;
392                 default:
393                         SysLogException(NID_UI_CTRL, E_UNSUPPORTED_FORMAT, "The specified pixel format is not supported.");
394                         return E_UNSUPPORTED_FORMAT;
395                 }
396
397                 __savedColorFormat = colorFormat;
398                 __currentColorFormat = srcFormat;
399                 __needToRellaocImage = true;
400         }
401
402         if (!srcDim.Equals(__currentSourceBufferSize))
403         {
404                 __currentSourceBufferSize.width = srcDim.width;
405                 __currentSourceBufferSize.height = srcDim.height;
406                 __needToRellaocImage = true;
407         }
408
409         __pCurrentSourceBuffer = &srcBuffer;
410
411         if (__needToRellaocImage)
412         {
413                 SysSecureLog(NID_UI_CTRL,"The current buffer pointer is %x, size is %d, color format is %d",
414                         __pCurrentSourceBuffer->GetPointer(), __pCurrentSourceBuffer->GetCapacity(), __savedColorFormat);
415         }
416
417         return E_SUCCESS;
418 }
419
420 result
421 _OverlayAgent::Draw(void)
422 {
423         SysTryReturnResult(NID_UI_CTRL, __pCurrentSourceBuffer->GetPointer() != null, E_SYSTEM, "The current input user buffer is null. (nothing to draw)");
424
425         result r = E_SUCCESS;
426
427         if(__style == _OVERLAY_AGENT_STYLE_REGION_SW || __style == _OVERLAY_AGENT_STYLE_PANEL_SW)
428         {
429                 r = PutEvasImage();
430         }
431         else
432         {
433                 r = PutXvImage();
434         }
435
436         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "Propagating.");
437
438         return r;
439 }
440
441
442 result
443 _OverlayAgent::GrabXvPort(void)
444 {
445         Display* pDisplay = (Display*)ecore_x_display_get();
446         SysTryReturnResult(NID_UI_CTRL, pDisplay != null, E_SYSTEM, "The current value of Display is null");
447
448         int baseXvPort = -1;
449         int portCount = -1;
450         if (!CheckXvExtension(pDisplay, &baseXvPort, &portCount))
451         {
452                 SysTryReturnResult(NID_UI_CTRL, false, E_SYSTEM, "Failed to check XvExtension");
453         }
454
455         SysTryReturnResult(NID_UI_CTRL, baseXvPort != -1, E_SYSTEM, "The current value of base XvPort is invalid.");
456         SysTryReturnResult(NID_UI_CTRL, portCount >= 1 && portCount <= _OVERLAYAGENT_XV_PORT, E_SYSTEM, "The current count of available XvPort[%d] is invalid.", portCount);
457
458         _OverlayAgent::__baseXvPort = baseXvPort;
459
460         for (int index = 0; index < portCount; index++)
461         {
462                 if(!__isPortGrabbed[index])
463                 {
464                         int tempPort = __baseXvPort + index;
465                         if (XvGrabPort(pDisplay, tempPort, 0) != Success)
466                         {
467                                 SysSecureLog(NID_UI_CTRL, "The current XvPort[%d] has been alreadly used by another process", tempPort);
468                         }
469                         else
470                         {
471                                 __xvPort = tempPort;
472                                 __isPortGrabbed[index] = true;
473                                 break;
474                         }
475                 }
476         }
477
478         SysTryReturnResult(NID_UI_CTRL, __xvPort != -1, E_SYSTEM, "Failed to grab pixmap[%d]", __pixmap);
479         SysSecureLog(NID_UI_CTRL, "The current value of grabbed XvPort is [%d] and pixmap is [%d]", __xvPort, __pixmap);
480
481         return E_SUCCESS;
482 }
483
484 void
485 _OverlayAgent::UngrabXvPort(void)
486 {
487         ClearLastResult();
488
489         if (__isPortGrabbed[__xvPort - __baseXvPort])
490         {
491                 Display* pDisplay = (Display*) ecore_x_display_get();
492                 Atom atom = XInternAtom(pDisplay, "_USER_WM_PORT_ATTRIBUTE_STREAM_OFF", False);
493                 XvSetPortAttribute(pDisplay, __xvPort, atom, 1);
494                 XvUngrabPort(pDisplay, __xvPort, 0);
495
496                 SysSecureLog(NID_UI_CTRL, "The current value of ungrabbed XvPort is %d, and pixmap is %d", __xvPort, __pixmap);
497
498                 __isPortGrabbed[__xvPort - __baseXvPort] = false;
499                 __xvPort = -1;
500         }
501 }
502
503 result
504 _OverlayAgent::PutXvImage(void)
505 {
506         SysTryReturnResult(NID_UI_CTRL, __pixmap != 0, E_SYSTEM, "The current value of pixmap is null");
507
508         Display* pDisplay = (Display*)ecore_x_display_get();
509         SysTryReturnResult(NID_UI_CTRL, pDisplay != null, E_SYSTEM, "The current value of Display is null");
510
511         if (__pXvImage == null || __needToRellaocImage)
512                 //||(__pXvImage && __pXvImage->data_size != currentSourceBufferSize->GetCapacity()) )
513         {
514                 if (__xvPort != -1)
515                 {
516                         UngrabXvPort();
517                 }
518
519                 if (__pXvImage != null)
520                 {
521                         if (__pShmInfo)
522                         {
523                                 XShmDetach(pDisplay, __pShmInfo);
524                                 shmdt(__pShmInfo->shmaddr);
525                                 shmctl(__pShmInfo->shmid, IPC_RMID, NULL);
526                                 delete __pShmInfo;
527                                 __pShmInfo = null;
528                         }
529
530                         if (__pXvImage)
531                         {
532                                 XFree(__pXvImage);
533                                 __pXvImage = null;
534                         }
535                 }
536         }
537
538         if (__xvPort == -1)
539         {
540                 result r = GrabXvPort();
541                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "Propagating.");
542         }
543
544         if (__needToRellaocImage)
545         {
546                 XShmSegmentInfo* pShmInfo = new (std::nothrow) XShmSegmentInfo();
547                 SysTryReturnResult(NID_UI_CTRL, pShmInfo != null, E_SYSTEM, "Failed to create XShmSegmentInfo");
548
549                 XvImage* pXvImage = XvShmCreateImage(pDisplay, __xvPort, __savedColorFormat, 0, __currentSourceBufferSize.width, __currentSourceBufferSize.height, pShmInfo);
550                 SysTryReturnResult(NID_UI_CTRL, pXvImage != null, E_SYSTEM, "Failed to create XvImage");
551
552                 pShmInfo->shmid = shmget(IPC_PRIVATE, pXvImage->data_size, IPC_CREAT | 0777);
553                 pShmInfo->shmaddr = pXvImage->data = (char*)shmat(pShmInfo->shmid, 0, 0);
554                 pShmInfo->readOnly = False;
555
556                 if (!XShmAttach(pDisplay, pShmInfo))
557                 {
558                         XFree(pXvImage);
559                         SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Failed to XShmAttach.");
560                         return E_SYSTEM;
561                 }
562
563                 __pXvImage = pXvImage;
564                 __pShmInfo = pShmInfo;
565         }
566
567         if (__pXvImage == null)
568         {
569                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Failed to get XvImage");
570                 return E_SYSTEM;
571         }
572
573         memcpy(__pXvImage->data, __pCurrentSourceBuffer->GetPointer(), __pXvImage->data_size);
574         __pCurrentSourceBuffer = null;
575
576         int imageWidth = 0;
577         int imageHeight = 0;
578         evas_object_image_size_get(__pImageObject, &imageWidth, &imageHeight);
579
580         GC gc= DefaultGCOfScreen(DefaultScreenOfDisplay(pDisplay));
581         XvShmPutImage(pDisplay, __xvPort, __pixmap, gc, __pXvImage, 0, 0, __currentSourceBufferSize.width, __currentSourceBufferSize.height, 0, 0, imageWidth, imageHeight, False);
582         XSync(pDisplay, 0);
583
584         if (__needToRellaocImage)
585         {
586                 SysSecureLog(NID_UI_CTRL, "The current value of source buffer size is [%d,%d]", __currentSourceBufferSize.width, __currentSourceBufferSize.height);
587                 SysSecureLog(NID_UI_CTRL, "The current value of destination XvImage size is [%d,%d]", imageWidth, imageHeight);
588                 __needToRellaocImage = false;
589         }
590
591         result r = __pRendererVE->SetFlushNeeded();
592         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r,"[%s] Propagating.", GetErrorMessage(r));
593
594         return r;
595 }
596
597 result
598 _OverlayAgent::PutEvasImage(void)
599 {
600         SysTryReturnResult(NID_UI_CTRL, __pImageObject != null, E_SYSTEM, "The current value of Renderer ImageObject is null");
601         SysTryReturnResult(NID_UI_CTRL, __pCurrentSourceBuffer->GetPointer() != null, E_SYSTEM, "The current value of Renderer byte buffer is null");
602
603         result r = E_SUCCESS;
604
605         // Create new buffer to convert pixel format
606         if (__needToRellaocImage || __pConvertedSourceBuffer == null ||
607                 !(__pConvertedSourceBuffer && (__pConvertedSourceBuffer->GetCapacity() == __pCurrentSourceBuffer->GetCapacity())))
608         {
609                 if (__pConvertedSourceBuffer != null)
610                 {
611                         __pConvertedSourceBuffer->SetPosition(0);
612                         delete __pConvertedSourceBuffer;
613                         __pConvertedSourceBuffer = null;
614                 }
615
616                 __pConvertedSourceBuffer = new (std::nothrow) ByteBuffer;
617                 SysTryReturnResult(NID_UI_CTRL, __pConvertedSourceBuffer != null, E_OUT_OF_MEMORY, "Failed to create a ByteBuffer for Evas Image Object.");
618
619                 r = __pConvertedSourceBuffer->Construct(__currentSourceBufferSize.width * __currentSourceBufferSize.height * 4);
620                 if (r != E_SUCCESS)
621                 {
622                         delete __pConvertedSourceBuffer;
623                         __pConvertedSourceBuffer = null;
624                         SysLogException(NID_UI_CTRL, r, "[%s] Failed to construct a byte buffer for EvasImageObject.", GetErrorMessage(r));
625
626                         return r;
627                 }
628         }
629
630         __pConvertedSourceBuffer->SetPosition(0);
631
632         switch (__currentColorFormat)
633         {
634         case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_ARGB8888:
635                 r = __pConvertedSourceBuffer->SetArray(__pCurrentSourceBuffer->GetPointer(), 0, __pConvertedSourceBuffer->GetCapacity());
636                 break;
637         case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_RGB565:
638                 r = _ImageUtil::ConvertPixelFormat(*__pCurrentSourceBuffer, MEDIA_PIXEL_FORMAT_RGB565LE, __currentSourceBufferSize.width, __currentSourceBufferSize.height, *__pConvertedSourceBuffer, MEDIA_PIXEL_FORMAT_BGRA8888);
639                 break;
640         case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_YCbCr420_PLANAR:
641                 r = _ImageUtil::ConvertPixelFormat(*__pCurrentSourceBuffer, MEDIA_PIXEL_FORMAT_YUV420P, __currentSourceBufferSize.width, __currentSourceBufferSize.height, *__pConvertedSourceBuffer, MEDIA_PIXEL_FORMAT_BGRA8888);
642                 break;
643         case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_NV12:
644                 r = _ImageUtil::ConvertPixelFormat(*__pCurrentSourceBuffer, MEDIA_PIXEL_FORMAT_NV12, __currentSourceBufferSize.width, __currentSourceBufferSize.height, *__pConvertedSourceBuffer, MEDIA_PIXEL_FORMAT_BGRA8888);
645                 break;
646         case _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_UYVY:
647                 r = _ImageUtil::ConvertPixelFormat(*__pCurrentSourceBuffer, MEDIA_PIXEL_FORMAT_UYVY422, __currentSourceBufferSize.width, __currentSourceBufferSize.height, *__pConvertedSourceBuffer, MEDIA_PIXEL_FORMAT_BGRA8888);
648                 break;
649         default:
650                 SysLogException(NID_UI_CTRL, E_UNSUPPORTED_FORMAT, "[E_UNSUPPORTED_FORMAT] The specified pixel format is not supported.");
651                 return E_UNSUPPORTED_FORMAT;
652         }
653
654         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r,"Propagating.");
655
656         __pCurrentSourceBuffer = null;
657         void* pImageData = (void*)(__pConvertedSourceBuffer->GetPointer());
658         SysTryReturnResult(NID_UI_CTRL, pImageData != null, E_SYSTEM, "The converted image data for Overlay Renderer VisualElement is invalid.");
659
660         evas_object_image_size_set(__pImageObject, __currentSourceBufferSize.width, __currentSourceBufferSize.height);
661         evas_object_image_data_set(__pImageObject, pImageData);
662         evas_object_image_data_update_add(__pImageObject, 0, 0, __currentSourceBufferSize.width, __currentSourceBufferSize.height);
663         evas_object_show(__pImageObject);
664
665         if (__needToRellaocImage)
666         {
667                 SysSecureLog(NID_UI_CTRL, "The current value of source buffer bounds is [%d,%d,%d, %d]", 0, 0, __currentSourceBufferSize.width, __currentSourceBufferSize.height);
668                 __needToRellaocImage = false;
669         }
670
671         r = __pRendererVE->SetFlushNeeded();
672         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r,"[%s] Propagating.", GetErrorMessage(r));
673
674         return r;
675 }
676
677 result
678 _OverlayAgent::SetRendererFlushNeeded(void)
679 {
680         return __pRendererVE->SetFlushNeeded();
681 }
682
683 result
684 _OverlayAgent::SetRotation(_OverlayAgentRotation rotation)
685 {
686         SysTryReturnResult(NID_UI_CTRL, __pRendererVE != null, E_SYSTEM, "The Renderer Visual Element is null");
687         SysTryReturnResult(NID_UI_CTRL, rotation < _OVERLAY_AGENT_ROTATION_MAX, E_INVALID_ARG, "The value of input rotation [%d] is invalid.", rotation);
688
689         if (rotation == __currentRotation)
690         {
691                 return E_SUCCESS;
692         }
693
694         float initialZAngle = __pRendererVE->GetProperty(L"transform.rotation.z").ToFloat();
695         float rotatedZAngle = initialZAngle;
696
697         switch (rotation)
698         {
699         case _OVERLAY_AGENT_ROTATION_NONE: // fall through
700         case _OVERLAY_AGENT_ROTATION_NONE_LR: // fall through
701         case _OVERLAY_AGENT_ROTATION_NONE_UD:
702                 rotatedZAngle = 0.0f;
703                 break;
704
705         case _OVERLAY_AGENT_ROTATION_90: // fall through
706         case _OVERLAY_AGENT_ROTATION_90_LR: // fall through
707         case _OVERLAY_AGENT_ROTATION_90_UD:
708                 rotatedZAngle = 90.0f;
709                 break;
710
711         case _OVERLAY_AGENT_ROTATION_180: // fall through
712         case _OVERLAY_AGENT_ROTATION_180_LR: // fall through
713         case _OVERLAY_AGENT_ROTATION_180_UD:
714                 rotatedZAngle = 180.0f;
715                 break;
716
717         case _OVERLAY_AGENT_ROTATION_270: // fall through
718         case _OVERLAY_AGENT_ROTATION_270_LR: // fall through
719         case _OVERLAY_AGENT_ROTATION_270_UD:
720                 rotatedZAngle = 270.0f;
721                 break;
722
723         default:
724                 break;
725         }
726
727         //Set bounds of rotated renderer
728         FloatRectangle rendererBounds = __standardRendererBounds;
729         if (rotatedZAngle == 90.0f || rotatedZAngle == 270.0f )
730         {
731                 rendererBounds.x = __standardRendererBounds.x - ((__standardRendererBounds.height - __standardRendererBounds.width) / 2.0f);
732                 rendererBounds.y = __standardRendererBounds.y + ((__standardRendererBounds.height - __standardRendererBounds.width) / 2.0f);
733                 std::swap(rendererBounds.height, rendererBounds.width);
734         }
735
736         result r = __pRendererVE->SetBounds(rendererBounds);
737         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
738
739         r = __pRendererVE->SetProperty(L"transform.rotation.z", rotatedZAngle);
740         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
741         SysLog(NID_UI_CTRL, "The current Overlay Renderer is rotated (%d -> %d)", static_cast<int>(initialZAngle), static_cast<int>(rotatedZAngle));
742
743         switch (rotation)
744         {
745         case _OVERLAY_AGENT_ROTATION_NONE: // fall through
746         case _OVERLAY_AGENT_ROTATION_90: // fall through
747         case _OVERLAY_AGENT_ROTATION_180: // fall through
748         case _OVERLAY_AGENT_ROTATION_270:
749                 r = __pRendererVE->SetProperty(L"transform.rotation.x", 0.0f);
750                 r = __pRendererVE->SetProperty(L"transform.rotation.y", 0.0f);
751                 break;
752
753         case _OVERLAY_AGENT_ROTATION_NONE_UD: // fall through
754         case _OVERLAY_AGENT_ROTATION_180_UD: // fall through
755         case _OVERLAY_AGENT_ROTATION_90_LR: // fall through
756         case _OVERLAY_AGENT_ROTATION_270_LR:
757                 r = __pRendererVE->SetProperty(L"transform.rotation.x", 180.0f);
758                 r = __pRendererVE->SetProperty(L"transform.rotation.y", 0.0f);
759                 SysLog(NID_UI_CTRL, "The current Overlay Renderer is flipped over up side down.");
760                 break;
761
762         case _OVERLAY_AGENT_ROTATION_NONE_LR: // fall through
763         case _OVERLAY_AGENT_ROTATION_180_LR: // fall through
764         case _OVERLAY_AGENT_ROTATION_90_UD: // fall through
765         case _OVERLAY_AGENT_ROTATION_270_UD:
766                 r = __pRendererVE->SetProperty(L"transform.rotation.x", 0.0f);
767                 r = __pRendererVE->SetProperty(L"transform.rotation.y", 180.0f);
768                 SysLog(NID_UI_CTRL, "The current Overlay Renderer is flipped over left side right.");
769                 break;
770
771         default:
772                 break;
773         }
774         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
775
776         __currentRotation = rotation;
777
778         r = __pRendererVE->SetFlushNeeded();
779         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
780
781         return r;
782 }
783
784 result
785 _OverlayAgent::SetDestination(const FloatDimension& dimension)
786 {
787         SysTryReturnResult(NID_UI_CTRL, __pParentVE != null, E_SYSTEM, "The Renderer Visual Element is null");
788         SysTryReturnResult(NID_UI_CTRL, __pRendererVE != null, E_SYSTEM, "The Renderer Visual Element is null");
789         if (__standardRendererBounds.width == dimension.width && __standardRendererBounds.height == dimension.height &&
790                 __standardParentBounds.width == __pParentVE->GetBounds().width && __standardParentBounds.height == __pParentVE->GetBounds().height)
791         {
792                 return E_SUCCESS;
793         }
794
795         //Set bounds of saved renderer
796         FloatRectangle parentBounds= __pParentVE->GetBounds();
797
798         if (parentBounds.width >= dimension.width)
799         {
800                 __standardRendererBounds.x = (parentBounds.width - dimension.width) / 2.0f;
801                 __standardRendererBounds.width = dimension.width;
802         }
803         else
804         {
805                 __standardRendererBounds.x = 0.0f;
806                 __standardRendererBounds.width = parentBounds.width;
807         }
808
809         if (parentBounds.height >= dimension.height)
810         {
811                 __standardRendererBounds.y = (parentBounds.height - dimension.height) / 2.0f;
812                 __standardRendererBounds.height = dimension.height;
813         }
814         else
815         {
816                 __standardRendererBounds.y = 0.0f;
817                 __standardRendererBounds.height = parentBounds.height;
818         }
819
820         //Set bounds of resizing renderer
821         float parentWidth = parentBounds.width;
822         float parentHeight = parentBounds.height;
823         FloatDimension inputDimension(dimension.width, dimension.height);
824
825         Variant rotationAngle = __pRendererVE->GetProperty(L"transform.rotation.z");
826         if(rotationAngle.ToFloat() == 90.0f || rotationAngle.ToFloat() == 270.0f)
827         {
828                 std::swap(parentWidth, parentHeight);
829                 std::swap(inputDimension.width, inputDimension.height);
830         }
831
832         FloatRectangle rendererBounds = __standardRendererBounds;
833
834         if (parentWidth >= inputDimension.width)
835         {
836                 rendererBounds.x = (parentBounds.width - inputDimension.width) / 2.0f;
837                 rendererBounds.width = inputDimension.width;
838         }
839         else
840         {
841                 rendererBounds.x = 0.0f;
842                 rendererBounds.width = parentBounds.width;
843         }
844
845         if (parentHeight >= inputDimension.height)
846         {
847                 rendererBounds.y = (parentBounds.height - inputDimension.height) / 2.0f;
848                 rendererBounds.height = inputDimension.height;
849         }
850         else
851         {
852                 rendererBounds.y = 0.0f;
853                 rendererBounds.height = parentBounds.height;
854         }
855         SysLog(NID_UI_CTRL, "The current bounds of resized renderer is [%.3f, %.3f, %.3f, %.3f]",
856                 __standardRendererBounds.x, __standardRendererBounds.y, __standardRendererBounds.width, __standardRendererBounds.height);
857
858         result r = __pRendererVE->SetBounds(rendererBounds);
859         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
860
861         r = __pRendererVE->SetFlushNeeded();
862         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
863
864         __standardParentBounds = __pParentVE->GetBounds();
865
866         return r;
867 }
868
869 result
870 _OverlayAgent::SetAspectRatio(bool enable)
871 {
872         return E_SUCCESS;
873 }
874
875 FloatRectangle
876 _OverlayAgent::GetBounds(void) const
877 {
878         SysTryReturn(NID_UI_CTRL, __pRendererVE != null, FloatRectangle(0.0f, 0.0f, 0.1f, 0.1f), E_SYSTEM, "The Renderer Visual Element is null");
879         return __standardRendererBounds;
880 }
881
882 result
883 _OverlayAgent::CreateRendererVisualElement(const _Control& parentControl, const FloatRectangle& logicalBounds, const Rectangle& physicalBounds)
884 {
885         //Create new VisualElement and insert it to Parent's VisualElement
886         unique_ptr<_VisualElement, visualElementDeleter> pRendererVE(new (std::nothrow) _VisualElement());
887         SysTryReturnResult(NID_UI_CTRL, pRendererVE != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
888
889         result r = pRendererVE->Construct();
890         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
891
892         pRendererVE->SetName("OverlayRenderer");
893         pRendererVE->SetShowState(true);
894         //pRendererVE->SetImplicitAnimationEnabled(true);
895         pRendererVE->SetImplicitAnimationEnabled(false);
896         pRendererVE->SetZOrderGroup(_CONTROL_LAYER_OVERLAY);
897
898         FloatPoint logicalPoint(0.0f, 0.0f);
899         if(__style == _OVERLAY_AGENT_STYLE_REGION_SW || __style == _OVERLAY_AGENT_STYLE_REGION_GL)
900         {
901                 logicalPoint.x = logicalBounds.x;
902                 logicalPoint.y = logicalBounds.y;
903         }
904         pRendererVE->SetBounds(FloatRectangle(logicalPoint.x, logicalPoint.y, logicalBounds.width, logicalBounds.height));
905
906
907         __pParentVE = parentControl.GetVisualElement();
908         SysTryReturnResult(NID_UI_CTRL, __pParentVE != null, E_SYSTEM, "[E_SYSTEM] The current value of OverlayPanel's VisualElement is null.");
909
910         __pParentVE->SetName("OverlayPanel");
911         r = __pParentVE->InsertChild(pRendererVE.get(), null, true);
912         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
913
914         r = __pParentVE->SetSurfaceOpaque(true);
915         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
916
917
918         // Get and Set ImageObject of Renderer VisualElement
919         _EflNode* pEflNode = dynamic_cast<_EflNode*>(pRendererVE->GetNativeNode());
920         SysTryReturnResult(NID_UI_CTRL, pEflNode != null, E_SYSTEM, "Failed to get EflNode from Renderer VisualElement.");
921
922         Evas_Object* pSmartObject = (Evas_Object*) pEflNode->GetGroupContainer();
923         SysTryReturnResult(NID_UI_CTRL, pSmartObject != null, E_SYSTEM, "Failed to get Group Container of Renderer VisualElement.");
924
925         Evas* pEvas = evas_object_evas_get(pSmartObject);
926         SysTryReturnResult(NID_UI_CTRL, pEvas != null, E_SYSTEM, "Failed to get Evas of Renderer VisualElement.");
927
928         Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
929         SysTryReturnResult(NID_UI_CTRL, pEcoreEvas != null, E_SYSTEM, "Failed to get Ecore Evas of Renderer VisualElement.");
930
931         __pImageObject = evas_object_image_add(pEvas);
932         SysTryReturnResult(NID_UI_CTRL, __pImageObject != null, E_SYSTEM, "Failed to get Renderer Image Object.");
933
934         evas_object_name_set(__pImageObject, "OverlayRenderer");
935         evas_object_image_size_set(__pImageObject, physicalBounds.width, physicalBounds.height);
936         evas_object_image_alpha_set(__pImageObject, false);
937         evas_object_image_filled_set(__pImageObject, EINA_TRUE);
938         evas_object_resize(__pImageObject, physicalBounds.width, physicalBounds.height);
939         evas_object_move(__pImageObject, physicalBounds.x, physicalBounds.y);
940         evas_object_show(__pImageObject);
941
942         DisplayContext* pDisplayContext = parentControl.GetRootWindow()->GetDisplayContext();
943         SysTryReturnResult(NID_UI_CTRL, pDisplayContext != null, E_SYSTEM, "[E_SYSTEM] The current value of RootWindow's DisplayContext is null.");
944
945         unique_ptr<VisualElementSurface> pRendererSurface(_VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(*pDisplayContext, (Handle)__pImageObject, Dimension(physicalBounds.width, physicalBounds.height)));
946         SysTryReturnResult(NID_UI_CTRL, pRendererSurface != null, E_SYSTEM, "[E_SYSTEM] The current value of RootWindow's DisplayContext is null.");
947
948         r = pRendererVE->SetSurface(pRendererSurface.release());
949         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
950
951         r = pRendererVE->SetFlushNeeded();
952         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
953
954         pRendererVE->SetAnchor(FloatPoint(0.5f, 0.5f));
955         __pRendererVE = pRendererVE.release();
956         __standardRendererBounds = __pRendererVE->GetBounds();
957
958         if (__style == _OVERLAY_AGENT_STYLE_REGION_SW || __style == _OVERLAY_AGENT_STYLE_PANEL_SW)
959         {
960                 evas_object_image_pixels_get_callback_set(__pImageObject, OnEvasImageDamaged,(void*)this);
961         }
962
963         return r;
964 }
965
966 result
967 _OverlayAgent::CreatePixmap(const Rectangle& physicalBounds)
968 {
969         //Set size of pixmap
970         Dimension pixmapSize(physicalBounds.width, physicalBounds.height);
971         SysTryReturnResult(NID_UI_CTRL, (pixmapSize.width >= GetDstRectMinWidth()) && (pixmapSize.height >= GetDstRectMinHeight()),
972                 E_SYSTEM, "input size (%d, %d) is too small to create a pixmap",pixmapSize.width, pixmapSize.height);
973
974         // Create pixmap
975         Display *pDisplay = (Display*)ecore_x_display_get();
976         SysTryReturnResult(NID_UI_CTRL, pDisplay != null, E_SYSTEM, "The current value of Display is null");
977
978         __pixmap = XCreatePixmap(pDisplay, DefaultRootWindow(pDisplay), pixmapSize.width, pixmapSize.height, DefaultDepth(pDisplay, DefaultScreen(pDisplay)));
979         SysTryReturnResult(NID_UI_CTRL, __pixmap != 0, E_SYSTEM, "Failed to create a pixmap of Overlay Agent");
980
981         GC gc = XCreateGC(pDisplay, __pixmap, 0,0);
982         SysTryReturnResult(NID_UI_CTRL, gc > 0, E_SYSTEM, "Failed to create Graphic Contexts");
983
984         //Set initial color
985         XSetForeground(pDisplay, gc, (long unsigned int)(Color(0xff000000).GetRGB32()));
986         XFillRectangle(pDisplay, __pixmap, gc, 0, 0, pixmapSize.width, pixmapSize.height);
987         XSync(pDisplay, 0);
988         XFreeGC(pDisplay, gc);
989
990         //Set evas native surface
991         Evas_Native_Surface evasNativeSuface = {0,};
992
993         evasNativeSuface.version = EVAS_NATIVE_SURFACE_VERSION;
994         evasNativeSuface.type = EVAS_NATIVE_SURFACE_X11;
995         evasNativeSuface.data.x11.visual = ecore_x_default_visual_get(ecore_x_display_get(), ecore_x_default_screen_get());
996         evasNativeSuface.data.x11.pixmap = __pixmap;
997
998         evas_object_lower(__pImageObject);
999         evas_object_image_native_surface_set(__pImageObject, &evasNativeSuface);
1000
1001         __pixmapDamageHandle = ecore_x_damage_new(__pixmap, ECORE_X_DAMAGE_REPORT_RAW_RECTANGLES);
1002         SysTryCatch(NID_UI_CTRL, __pixmapDamageHandle != 0, , E_SYSTEM, "[E_SYSTEM] Failed to create a damage object");
1003
1004         __pPixmapEventHandler = ecore_event_handler_add(ECORE_X_EVENT_DAMAGE_NOTIFY, OnPixmapDamaged, (void*)this);
1005         SysTryCatch(NID_UI_CTRL, __pPixmapEventHandler != null, , E_SYSTEM, "[E_SYSTEM] Failed to add a pixmap damage event handle");
1006
1007         return E_SUCCESS;
1008
1009 CATCH:
1010         if (__pPixmapEventHandler)
1011         {
1012                 ecore_event_handler_del(__pPixmapEventHandler);
1013                 __pPixmapEventHandler = null;
1014         }
1015
1016         if (__pixmapDamageHandle)
1017         {
1018                 ecore_x_damage_free(__pixmapDamageHandle);
1019                 __pixmapDamageHandle = 0;
1020         }
1021
1022         if (__pixmap)
1023         {
1024                 XFreePixmap((Display*)ecore_x_display_get(), __pixmap);
1025                 __pixmap = 0;
1026         }
1027
1028         return E_SYSTEM;
1029 }
1030
1031 Pixmap
1032 _OverlayAgent::GetPixmap(void) const
1033 {
1034         return __pixmap;
1035 }
1036
1037 Evas_Object*
1038 _OverlayAgent::GetRendererImageObject(void) const
1039 {
1040         return __pImageObject;
1041 }
1042
1043 bool
1044 _OverlayAgent::IsValidDestinationDimension(const FloatDimension& dimension) const
1045 {
1046         FloatDimension physicalDimensionF = _CoordinateSystemUtils::Transform(dimension);
1047         Dimension physicalDimension = _CoordinateSystemUtils::ConvertToInteger(physicalDimensionF);
1048         SysTryReturn(NID_UI_CTRL, (physicalDimension.width >= GetDstRectMinWidth() && physicalDimension.height >= GetDstRectMinHeight()), false, E_INVALID_ARG,
1049                         "[E_INVALID_ARG] The size of input dimension (logical size : [%.3f, %.3f], physical size : [%d,%d]) is under minimun size."
1050                         ,dimension.width, dimension.height, physicalDimension.width, physicalDimension.height);
1051
1052         FloatRectangle parentBounds = __pParentVE->GetBounds();
1053         SysTryReturn(NID_UI_CTRL, (dimension.width <= parentBounds.width || dimension.height <= parentBounds.height), false, E_INVALID_ARG,
1054                         "[E_INVALID_ARG] The size of input dimension[%.3f, %.3f] is over maximum size[%.3f,%.3f]."
1055                         ,dimension.width, dimension.height, parentBounds.width, parentBounds.height);
1056
1057         return true;
1058 }
1059
1060 int
1061 _OverlayAgent::GetOverlayAgentCount(void)
1062 {
1063         return __overlayAgentCount;
1064 }
1065
1066 IListT<bool>*
1067 _OverlayAgent::GetPixelFormatListN(void)
1068 {
1069         if (!__OverlayAgentBufferPixelFormat[0])
1070         {
1071                 const wchar_t* _UI_REGISTRY_PATH = L"/usr/etc/system-info.ini";
1072                 const wchar_t* _UI_INFO_SESSION = L"UiControlInfo";
1073
1074                 _RegistryImpl registry;
1075                 result r = registry.Construct(_UI_REGISTRY_PATH, REG_OPEN_READ_ONLY, null);
1076                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1077
1078                 String pixelFormatList(null);
1079                 r = registry.GetValue(_UI_INFO_SESSION, L"OverlayRegionBufferPixelFormat", pixelFormatList);
1080                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1081
1082                 bool set = false;
1083                 if (pixelFormatList.Contains(L"ARGB8888"))
1084                 {
1085                         __OverlayAgentBufferPixelFormat[_OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_ARGB8888] = true;
1086                         set = true;
1087                 }
1088
1089                 if (pixelFormatList.Contains(L"RGB565"))
1090                 {
1091                         __OverlayAgentBufferPixelFormat[_OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_RGB565] = true;
1092                         set = true;
1093                 }
1094
1095                 if (pixelFormatList.Contains(L"YCbCr420P"))
1096                 {
1097                         __OverlayAgentBufferPixelFormat[_OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_YCbCr420_PLANAR] = true;
1098                         set = true;
1099                 }
1100
1101                 if (pixelFormatList.Contains(L"NV12"))
1102                 {
1103                         __OverlayAgentBufferPixelFormat[_OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_NV12] = true;
1104                         set = true;
1105                 }
1106
1107                 if (pixelFormatList.Contains(L"UYVY"))
1108                 {
1109                         __OverlayAgentBufferPixelFormat[_OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_UYVY] = true;
1110                         set = true;
1111                 }
1112
1113                 SysAssertf(set, "The registry file is an invalid.");
1114
1115                 __OverlayAgentBufferPixelFormat[0] = set;
1116         }
1117
1118         std::unique_ptr< ArrayListT<bool> > pFormatList( new (std::nothrow) ArrayListT<bool>());
1119         SysTryReturn(NID_UI_CTRL, pFormatList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] failed to create a list instance");
1120
1121         result r = pFormatList->Construct();
1122         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1123
1124         for (int index = _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_ARGB8888; index <= _OVERLAY_AGENT_BUFFER_PIXEL_FORMAT_UYVY; index++)
1125         {
1126                 r = pFormatList->Add(__OverlayAgentBufferPixelFormat[index]);
1127         }
1128         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1129
1130         return pFormatList.release();
1131 }
1132
1133 result
1134 _OverlayAgent::EvaluateBounds(OverlayAgentEvaluationOption option, FloatRectangle& rect, bool& modified)
1135 {
1136         modified = false;
1137         SysTryReturnResult(NID_UI_CTRL, rect.width > 0.0f && rect.height > 0.0f,
1138                 E_INVALID_ARG, "The size of input rectangle(%.3f, %.3f) is invalid.", rect.width, rect.height);
1139
1140         Rectangle logicalEvaluatingBounds = _CoordinateSystemUtils::ConvertToInteger(rect);
1141         SysTryReturnResult(NID_UI_CTRL, logicalEvaluatingBounds.width > 0 && logicalEvaluatingBounds.height > 0,
1142                 E_INVALID_ARG, "The size of logical rectangle(%d, %d) is invalid.", logicalEvaluatingBounds.width, logicalEvaluatingBounds.height);
1143
1144         Rectangle physicalRect(_CoordinateSystemUtils::Transform(logicalEvaluatingBounds));
1145         SysTryReturnResult(NID_UI_CTRL, physicalRect.width > 0 && physicalRect.height > 0,
1146                 E_INVALID_ARG, "The size of physical rectangle(%d, %d) is invalid.", physicalRect.width, physicalRect.height);
1147
1148         int physicalMinWidth = GetDstRectMinWidth();
1149         int physicalMinHeight = GetDstRectMinHeight();
1150         SysTryReturnResult(NID_UI_CTRL, physicalMinWidth > 0 && physicalMinHeight > 0, E_SYSTEM, "The value of overlay control's Min width and Min height are invalid.");
1151
1152         const int widthUnit = GetWidthUnit();
1153         const int heightUnit = GetHeightUnit();
1154         SysTryReturnResult(NID_UI_CTRL, widthUnit > 0 && heightUnit > 0, E_SYSTEM, "The value of overlay control's width Unit and height Unit are invalid.");
1155
1156         int screenWidth = _Screen::GetWidth();
1157         int screenHeight = _Screen::GetHeight();
1158         SysTryReturnResult(NID_UI_CTRL, screenWidth > 0 && screenHeight > 0, E_SYSTEM, "The value of screen size is invalid.");
1159
1160         //If orientation status of screen is landscape mode, swap width and height.
1161         {
1162                 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
1163                 int orientation = ecore_evas_rotation_get(pEcoreEvas->GetEcoreEvas());
1164
1165                 if (orientation == 90 || orientation == 270)
1166                 {
1167                         int temp = screenWidth;
1168                         screenWidth = screenHeight;
1169                         screenHeight = temp;
1170                         SysSecureLog(NID_UI_CTRL, "The current value of device screen width is %d, device screen height is %d and orientation is %d", screenWidth, screenHeight, orientation);
1171                 }
1172         }
1173
1174         int physicalMaxWidth= screenWidth - (screenWidth % widthUnit);
1175         int physicalMaxHeight = screenHeight - (screenHeight % heightUnit);
1176         int logicalMaxWidth = _CoordinateSystemUtils::InverseHorizontalTransform(physicalMaxWidth);
1177         int logicalMaxHeight = _CoordinateSystemUtils::InverseVerticalTransform(physicalMaxHeight);
1178
1179         int widthReminder = physicalRect.width % widthUnit;
1180         int heightReminder = physicalRect.height % heightUnit;
1181
1182         //Fix one pixel error when coordinate system transform
1183         if (widthReminder == 1)
1184         {
1185                 physicalRect.width -= 1;
1186                 widthReminder = physicalRect.width % widthUnit;
1187         }
1188
1189         if (heightReminder == 1)
1190         {
1191                 physicalRect.height -= 1;
1192                 heightReminder = physicalRect.height % heightUnit;
1193         }
1194
1195         switch (option)
1196         {
1197         case OVERLAY_AGENT_EVALUATION_OPTION_GREATER_THAN:
1198                 {
1199                         if (logicalEvaluatingBounds.width > logicalMaxWidth || logicalEvaluatingBounds.height > logicalMaxHeight)
1200                         {
1201                                 SysLogException(NID_UI_CTRL, E_UNSUPPORTED_OPTION, "[E_UNSUPPORTED_OPTION] The size of input rect exceeds logical maximun size.");
1202                                 return E_UNSUPPORTED_OPTION;
1203                         }
1204
1205                         if (physicalRect.width > physicalMaxWidth || physicalRect.height > physicalMaxHeight)
1206                         {
1207                                 SysLogException(NID_UI_CTRL, E_UNSUPPORTED_OPTION, "[E_UNSUPPORTED_OPTION] The size of input rect exceeds physical maximun size.");
1208                                 return E_UNSUPPORTED_OPTION;
1209                         }
1210
1211                         if (widthReminder != 0)
1212                         {
1213                                 widthReminder -= widthUnit;
1214                         }
1215
1216                         if (heightReminder != 0)
1217                         {
1218                                 heightReminder -= heightUnit;
1219                         }
1220
1221                         physicalRect.width -= widthReminder;
1222                         physicalRect.height -= heightReminder;
1223
1224                         if (physicalRect.width < physicalMinWidth)
1225                         {
1226                                 physicalRect.width = physicalMinWidth;
1227                         }
1228
1229                         if (physicalRect.height < physicalMinHeight)
1230                         {
1231                                 physicalRect.height = physicalMinHeight;
1232                         }
1233                 }
1234                 break;
1235         case OVERLAY_AGENT_EVALUATION_OPTION_LESS_THAN:
1236                 {
1237                         if (physicalRect.width < physicalMinWidth || physicalRect.height < physicalMinHeight)
1238                         {
1239                                 SysLogException(NID_UI_CTRL, E_UNSUPPORTED_OPTION, "[E_UNSUPPORTED_OPTION] The size of input rect is under minimun size.");
1240
1241                                 return E_UNSUPPORTED_OPTION;
1242                         }
1243
1244                         if (physicalRect.width > physicalMaxWidth)
1245                         {
1246                                 physicalRect.width = physicalMaxWidth + widthReminder;
1247                         }
1248
1249                         if (physicalRect.height > physicalMaxHeight)
1250                         {
1251                                 physicalRect.height = physicalMaxHeight + heightReminder;
1252                         }
1253
1254                         physicalRect.width -= widthReminder;
1255                         physicalRect.height -= heightReminder;
1256                 }
1257                 break;
1258         default:
1259                 {
1260                         SysLogException(NID_UI_CTRL, E_UNSUPPORTED_OPTION, "[E_UNSUPPORTED_OPTION] The input option is an invalid.");
1261
1262                         return E_UNSUPPORTED_OPTION;
1263                 }
1264         }
1265
1266         FloatRectangle originalInputRect(rect);
1267         rect = _CoordinateSystemUtils::ConvertToFloat(_CoordinateSystemUtils::InverseTransform(physicalRect));
1268
1269         if (rect != originalInputRect)
1270         {
1271                 modified = true;
1272                 SysLog(NID_UI_CTRL, "The input bounds [%.3f, %.3f, %.3f, %.3f] is modified to [%.3f, %.3f, %.3f, %.3f]",
1273                                 originalInputRect.x, originalInputRect.y, originalInputRect.width, originalInputRect.height,
1274                                 rect.x, rect.y, rect.width, rect.height);
1275         }
1276         else
1277         {
1278                 SysLog(NID_UI_CTRL, "The evaluated bounds [%.3f, %.3f, %.3f, %.3f] is valid.",
1279                         rect.x, rect.y, rect.width, rect.height);
1280         }
1281
1282         result r = GetLastResult();
1283         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1284
1285         return r;
1286 }
1287
1288 int
1289 _OverlayAgent::GetValueFromRegistry(const String& key)
1290 {
1291         const wchar_t* _UI_REGISTRY_PATH = L"/usr/etc/system-info.ini";
1292         const wchar_t* _UI_INFO_SESSION = L"UiControlInfo";
1293
1294         _RegistryImpl registry;
1295         result r = registry.Construct(_UI_REGISTRY_PATH, REG_OPEN_READ_ONLY, null);
1296         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, -1, r, "[%s] Propagating.", GetErrorMessage(r));
1297
1298         String strValue(null);
1299         r = registry.GetValue(_UI_INFO_SESSION, key, strValue);
1300         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, -1, r, "[%s] Propagating.", GetErrorMessage(r));
1301
1302         int value;
1303         r = Integer::Parse(strValue, value);
1304         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, -1, r, "[%s] Propagating.", GetErrorMessage(r));
1305
1306         return value;
1307 }
1308
1309 int
1310 _OverlayAgent::GetDstRectMinWidth(void)
1311 {
1312         if (__dstRectMinWidth < 0)
1313         {
1314                 int value = GetValueFromRegistry(L"OverlayDstMinWidth");
1315                 SysTryReturn(NID_UI_CTRL, value > 0, -1, E_SYSTEM, "[E_SYSTEM] Failed to get Overlay info from registry");
1316
1317                 __dstRectMinWidth = value;
1318                 SysLog(NID_UI_CTRL, "The overlay control's minimum width of destination is [%d]", __dstRectMinWidth);
1319         }
1320
1321         SetLastResult(E_SUCCESS);
1322
1323         return __dstRectMinWidth;
1324 }
1325
1326 int
1327 _OverlayAgent::GetDstRectMinHeight(void)
1328 {
1329         if (__dstRectMinHegith < 0)
1330         {
1331                 int value = GetValueFromRegistry(L"OverlayDstMinHeight");
1332                 SysTryReturn(NID_UI_CTRL, value > 0, -1, E_SYSTEM, "[E_SYSTEM] Failed to get Overlay info from registry");
1333
1334                 __dstRectMinHegith = value;
1335                 SysLog(NID_UI_CTRL, "The overlay control's minimum height of destination is [%d]", __dstRectMinHegith);
1336         }
1337
1338         SetLastResult(E_SUCCESS);
1339
1340         return __dstRectMinHegith;
1341 }
1342
1343 int
1344 _OverlayAgent::GetSrcRectMinWidth(void)
1345 {
1346         if (__srcRectMinWidth < 0)
1347         {
1348                 int value = GetValueFromRegistry(L"OverlaySrcMinWidth");
1349                 SysTryReturn(NID_UI_CTRL,  value > 0, -1, E_SYSTEM, "[E_SYSTEM] Failed to get Overlay info from registry");
1350
1351                 __srcRectMinWidth = value;
1352                 SysLog(NID_UI_CTRL, "The overlay control's minimum width of source buffer is [%d]", __srcRectMinWidth);
1353         }
1354
1355         SetLastResult(E_SUCCESS);
1356
1357         return __srcRectMinWidth;
1358 }
1359
1360 int
1361 _OverlayAgent::GetSrcRectMinHeight(void)
1362 {
1363         if (__srcRectMinHegith < 0)
1364         {
1365                 int value = GetValueFromRegistry(L"OverlaySrcMinHeight");
1366                 SysTryReturn(NID_UI_CTRL,  value > 0, -1, E_SYSTEM, "[E_SYSTEM] Failed to get Overlay info from registry");
1367
1368                 __srcRectMinHegith = value;
1369                 SysLog(NID_UI_CTRL, "The overlay control's minimum height of source buffer is [%d]", __srcRectMinHegith);
1370         }
1371
1372         SetLastResult(E_SUCCESS);
1373
1374         return __srcRectMinHegith;
1375 }
1376
1377 int
1378 _OverlayAgent::GetWidthUnit(void)
1379 {
1380         if (__overlayWidthUnit < 0)
1381         {
1382                 int value = GetValueFromRegistry(L"OverlayRegionWidthUnit");
1383                 SysTryReturn(NID_UI_CTRL,  value > 0, -1, E_SYSTEM, "[E_SYSTEM] Failed to get Overlay info from registry");
1384
1385                 __overlayWidthUnit = value;
1386                 SysLog(NID_UI_CTRL, "The overlay control's unit width is [%d]", __overlayWidthUnit);
1387         }
1388
1389         SetLastResult(E_SUCCESS);
1390
1391         return __overlayWidthUnit;
1392 }
1393
1394 int
1395 _OverlayAgent::GetHeightUnit(void)
1396 {
1397         if (__overlayHeightUnit < 0)
1398         {
1399                 int value = GetValueFromRegistry(L"OverlayRegionHeightUnit");
1400                 SysTryReturn(NID_UI_CTRL,  value > 0, -1, E_SYSTEM, "[E_SYSTEM] Failed to get Overlay info from registry");
1401
1402                 __overlayHeightUnit = value;
1403                 SysLog(NID_UI_CTRL, "The overlay control's unit height is [%d]", __overlayHeightUnit);
1404         }
1405
1406         SetLastResult(E_SUCCESS);
1407
1408         return __overlayHeightUnit;
1409 }
1410
1411 int
1412 _OverlayAgent::GetMaxCount(void)
1413 {
1414         if (__overlayMaxCount < 0)
1415         {
1416                 int value = GetValueFromRegistry(L"MaxOverlayRegionCount");
1417                 SysTryReturn(NID_UI_CTRL,  value > 0, -1, E_SYSTEM, "[E_SYSTEM] Failed to get Overlay info from registry");
1418
1419                 __overlayMaxCount = value;
1420                 SysLog(NID_UI_CTRL, "The overlay control's maximum count is [%d]", __overlayMaxCount);
1421         }
1422
1423         SetLastResult(E_SUCCESS);
1424
1425         return __overlayMaxCount;
1426 }
1427 }}} // Tizen::Ui
1428