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