Applied context restoring
[framework/osp/uifw.git] / src / ui / animations / FUiAnim_EflVisualElementSurfaceImpl.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        FUiAnim_EflVisualElementSurfaceImpl.cpp
20  * @brief       This file contains implementation of _EflVisualElementSurfaceImpl class
21  *
22  * This file contains implementation _EflVisualElementSurfaceImpl class.
23  */
24
25 #include <Evas.h>
26 #include <unique_ptr.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseByteBuffer.h>
29 #include <FBaseUtilStringUtil.h>
30 #include <FGrpDimension.h>
31 #include <FGrp_BufferInfoImpl.h>
32
33 #include "FUiAnim_VisualElementCoordinateSystem.h"
34 #include "FUi_EcoreEvas.h"
35 #include "FUi_EcoreEvasMgr.h"
36 #include "FUiAnim_EflNode.h"
37 #include "FUiAnim_EflLayer.h"
38 #include "FUiAnim_EflVisualElementSurfaceImpl.h"
39 #include "FUiAnim_DisplayManager.h"
40
41 using namespace std;
42 using namespace Tizen::Base;
43 using namespace Tizen::Graphics;
44
45 namespace Tizen { namespace Ui { namespace Animations
46 {
47
48 _EflVisualElementSurfaceImpl::_EflVisualElementSurfaceImpl(const Handle layer, const FloatDimension& size)
49         : _VisualElementSurfaceImpl(size)
50         , __pEvasImage(null)
51         , __pBuffer(null)
52         , __fromOutside(false)
53         , __isImageObject(true)
54 {
55         ClearLastResult();
56
57         int stride = 0;
58         _EflLayer* pLayer = reinterpret_cast<_EflLayer*>(layer);
59         __pEvasImage = evas_object_image_filled_add(pLayer->GetEvas());
60         SysTryReturnVoidResult(NID_UI_ANIM, __pEvasImage, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to create a new surface.");
61
62         evas_object_image_alpha_set(__pEvasImage, EINA_TRUE);
63
64         Dimension outputSize;
65         _VisualElementCoordinateSystem::ConvertDimensionToPhysicalIntegral(size.width, size.height, outputSize.width, outputSize.height);
66
67         if (size.width > 0.0f && outputSize.width == 0)
68         {
69                 outputSize.width = 1;
70         }
71
72         if (size.height > 0.0f && outputSize.height == 0)
73         {
74                 outputSize.height = 1;
75         }
76
77         int maxW = 0;
78         int maxH = 0;
79         evas_image_max_size_get(pLayer->GetEvas(), &maxW, &maxH);
80
81         SysTryCatch(NID_UI_ANIM, outputSize.width >= 0 && outputSize.height >= 0, , E_INVALID_ARG,
82                                 "[E_INVALID_ARG] Invalid argument(s) is used. size(%d,%d)", outputSize.width, outputSize.height);
83
84         SysTryCatch(NID_UI_ANIM, outputSize.width <= maxW && outputSize.height <= maxH, , E_OUT_OF_MEMORY,
85                                 "[E_INVALID_ARG] Invalid argument(s) is used. size(%d:%d), Surface size has to be smaller than (%d,%d)", outputSize.width, outputSize.height, maxW, maxH);
86
87         evas_object_image_size_set(__pEvasImage, outputSize.width, outputSize.height);
88
89         stride = evas_object_image_stride_get(__pEvasImage);
90         __pBuffer = new (std::nothrow) unsigned char[stride * outputSize.height];
91         SysTryCatch(NID_UI_ANIM, __pBuffer, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed. size(%d,%d)"
92                                 , outputSize.width, outputSize.height);
93
94         evas_object_resize(__pEvasImage, outputSize.width, outputSize.height);
95         evas_object_image_data_set(__pEvasImage, reinterpret_cast<void*>(__pBuffer));
96
97 #if 1 // Todo : Temporary code(regarding the visibility of evas source object)
98         evas_object_show(__pEvasImage);
99         evas_object_move(__pEvasImage, -999999, -999999);
100 #endif
101
102         _DisplayManager::RestoreContext();
103         return;
104
105 CATCH:
106         evas_object_del(__pEvasImage);
107         __pEvasImage = null;
108         _DisplayManager::RestoreContext();
109 }
110
111 _EflVisualElementSurfaceImpl::_EflVisualElementSurfaceImpl(const Handle layer, const Handle object, const Dimension& size)
112         : _VisualElementSurfaceImpl(size)
113         , __pEvasImage(null)
114         , __pBuffer(null)
115         , __fromOutside(true)
116         , __isImageObject(true)
117 {
118         ClearLastResult();
119
120         _EflLayer* pLayer = reinterpret_cast<_EflLayer*>(layer);
121         Evas* pEvas = evas_object_evas_get(reinterpret_cast<Evas_Object*>(object));
122         if (pLayer->GetEvas() == pEvas)
123         {
124                 __pEvasImage = reinterpret_cast<Evas_Object*>(object);
125         }
126         SysTryReturnVoidResult(NID_UI_ANIM, __pEvasImage, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to create a new surface.");
127
128         // type check
129         const char* pType = evas_object_type_get(__pEvasImage);
130         if (strcmp("image", pType))
131         {
132                 __isImageObject = false;
133         }
134
135 #if 1 // Todo : Temporary code(regarding the visibility of evas source object)
136         evas_object_show(__pEvasImage);
137         evas_object_move(__pEvasImage, -999999, -999999);
138 #endif
139         _DisplayManager::RestoreContext();
140 }
141
142 _EflVisualElementSurfaceImpl::~_EflVisualElementSurfaceImpl(void)
143 {
144         if (__pEvasImage != null)
145         {
146                 evas_object_hide(__pEvasImage);
147
148                 // * DO NOT delete object which has been created from other modules.
149                 if (!__fromOutside)
150                 {
151                         evas_object_del(__pEvasImage);
152                 }
153                 evas_object_smart_member_del(__pEvasImage);
154                 _DisplayManager::RestoreContext();
155         }
156
157         if (__pBuffer != null)
158         {
159                 delete []__pBuffer;
160         }
161
162         __pBuffer = null;
163         __pEvasImage = null;
164 }
165
166 result
167 _EflVisualElementSurfaceImpl::SetImage(const String& fileName)
168 {
169         SysTryReturnResult(NID_UI_ANIM, __pEvasImage, E_SYSTEM, "A system error has been occurred. Surface is null.");
170
171         unique_ptr<ByteBuffer> pBuffer(Tizen::Base::Utility::StringUtil::StringToUtf8N(fileName));
172         SysTryReturnResult(NID_UI_ANIM, pBuffer, E_SYSTEM, "A system error has been occurred. Can not convert the file name.");
173
174         Evas_Object* pSource = evas_object_image_source_get(__pEvasImage);
175         pSource = pSource ? pSource : __pEvasImage;
176
177         evas_object_image_file_set(pSource, (const char*)(pBuffer->GetPointer()), null);
178
179         Evas_Load_Error err = evas_object_image_load_error_get(__pEvasImage);
180         SysTryReturnResult(NID_UI_ANIM, err == EVAS_LOAD_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Image file(%s) load failed . MSG = %s",
181                                            (const char*)(pBuffer->GetPointer()), evas_load_error_str(err));
182
183         if (__pBuffer != null)
184         {
185                 delete []__pBuffer;
186         }
187
188         __pBuffer = null;
189         _DisplayManager::RestoreContext();
190
191         return E_SUCCESS;
192 }
193
194 Handle
195 _EflVisualElementSurfaceImpl::GetNativeHandle(void) const
196 {
197         return (Handle)__pEvasImage;
198 }
199
200 result
201 _EflVisualElementSurfaceImpl::GetBufferInfo(BufferInfo& bufferInfo) const
202 {
203         Evas_Object* pSource = evas_object_image_source_get(__pEvasImage);
204         pSource = pSource ? pSource : __pEvasImage;
205
206         int w = 0;
207         int h = 0;
208         evas_object_image_size_get(pSource, &w, &h);
209         bufferInfo.width = w;
210         bufferInfo.height = h;
211         bufferInfo.pitch = evas_object_image_stride_get(__pEvasImage);
212
213         Evas_Colorspace colorspace = evas_object_image_colorspace_get(__pEvasImage);
214         bufferInfo.bitsPerPixel = GetBytePerPixel(colorspace) * 8;
215         bufferInfo.pixelFormat = GetPixelFormat(colorspace);
216
217         _BufferInfoImpl* pBufferInfoImpl = _BufferInfoImpl::GetInstance(bufferInfo);
218         SysTryReturnResult(NID_UI_ANIM, pBufferInfoImpl, E_OUT_OF_MEMORY, "Memory allocation failed.");
219         pBufferInfoImpl->SetHandle(_BufferInfoImpl::HANDLE_TYPE_VE_SURFACE, reinterpret_cast<Handle>(__pEvasImage));
220         pBufferInfoImpl->SetBounds(Rectangle(0, 0, w, h));
221
222         return E_SUCCESS;
223 }
224
225 Dimension
226 _EflVisualElementSurfaceImpl::GetPhysicalSize(void) const
227 {
228         SysTryReturn(NID_UI_ANIM, __pEvasImage, Dimension(-1, -1), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Surface is null.");
229
230         int w = 0;
231         int h = 0;
232         Evas_Object* pSource = evas_object_image_source_get(__pEvasImage);
233         if (unlikely(!pSource))
234         {
235                 pSource = __pEvasImage;
236         }
237
238         evas_object_image_size_get(pSource, &w, &h);
239
240         return Tizen::Graphics::Dimension(w, h);
241 }
242
243 PixelFormat
244 _EflVisualElementSurfaceImpl::GetPixelFormat(Evas_Colorspace colorspace) const
245 {
246         switch (colorspace)
247         {
248         case EVAS_COLORSPACE_ARGB8888:
249                 return PIXEL_FORMAT_ARGB8888;
250
251         case EVAS_COLORSPACE_YCBCR422P601_PL:
252                 // fall through
253         case EVAS_COLORSPACE_YCBCR422P709_PL:
254                 // fall through
255         case EVAS_COLORSPACE_RGB565_A5P:
256                 // fall through
257         case EVAS_COLORSPACE_GRY8:
258                 // fall through
259         case EVAS_COLORSPACE_YCBCR422601_PL:
260                 // fall through
261         case EVAS_COLORSPACE_YCBCR420NV12601_PL:
262                 // fall through
263         case EVAS_COLORSPACE_YCBCR420TM12601_PL:
264                 // fall through
265         default:
266                 return PIXEL_FORMAT_MIN;
267         }
268 }
269
270 int
271 _EflVisualElementSurfaceImpl::GetBytePerPixel(Evas_Colorspace colorspace) const
272 {
273         switch (colorspace)
274         {
275         case EVAS_COLORSPACE_ARGB8888:
276                 return 4;
277
278         case EVAS_COLORSPACE_YCBCR422P601_PL:
279                 // fall through
280         case EVAS_COLORSPACE_YCBCR422P709_PL:
281                 // fall through
282         case EVAS_COLORSPACE_RGB565_A5P:
283                 // fall through
284         case EVAS_COLORSPACE_GRY8:
285                 // fall through
286         case EVAS_COLORSPACE_YCBCR422601_PL:
287                 // fall through
288         case EVAS_COLORSPACE_YCBCR420NV12601_PL:
289                 // fall through
290         case EVAS_COLORSPACE_YCBCR420TM12601_PL:
291                 // fall through
292         default:
293                 return 0;
294         }
295 }
296
297 }}}
298