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