Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / graphics / FGrp_BitmapUtil.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        FGrp_BitmapUtil.cpp
20  * @brief       This is the cpp file for internal util class.
21  *
22  */
23
24 #include <new>
25 #include <memory>
26
27 #include <FGrpCanvas.h>
28
29 #include <FBaseSysLog.h>
30
31 #include "FGrp_BitmapUtil.h"
32 #include "FGrp_Bitmap.h"
33 #include "FGrp_ResUtil.h"
34 #include "util/FGrp_UtilTemplate.h"
35
36
37 namespace Tizen { namespace Graphics
38 {
39
40 unsigned long _GetBitmapTimeStamp(const Tizen::Graphics::_BitmapImpl& bitmap);
41 unsigned long _UpdateBitmapTimeStamp(Tizen::Graphics::_BitmapImpl& bitmap);
42
43 BitmapTemp::BitmapTemp(Canvas& canvas)
44         : _BitmapImpl()
45         , __isValid(false)
46 {
47         BufferInfo canvasDesc;
48         result r = canvas.Lock(canvasDesc);
49
50         if (r == E_SUCCESS)
51         {
52                 {
53                         //
54                         // ykahn 2011/07/25
55                         //
56                         int pixelPerLine = canvasDesc.pitch / (canvasDesc.bitsPerPixel / 8);
57
58                         // 'pitch' can be a negative value
59                         pixelPerLine = (pixelPerLine >= 0) ? pixelPerLine : -pixelPerLine;
60
61                         // new width = max(pixelPerLine, canvasDesc.width)
62                         canvasDesc.width = (pixelPerLine > canvasDesc.width) ? pixelPerLine : canvasDesc.width;
63                 }
64
65                 this->_sharedItem->nativeBitmap->Construct((void*) canvasDesc.pPixels, canvasDesc.width, canvasDesc.height,
66                                                                                                          canvasDesc.bitsPerPixel);
67
68                 Dimension pc_dim(canvasDesc.width, canvasDesc.height);
69                 Dimension vc_dim(canvas.GetBounds().width, canvas.GetBounds().height);
70
71                 _ResUtil::Rect vc_rect(0, 0, vc_dim.width, vc_dim.height);
72                 _ResUtil::Rect pc_rect(0, 0, pc_dim.width, pc_dim.height);
73
74                 this->_sharedItem->coordHolder->bitmapSize.required = vc_rect;
75                 this->_sharedItem->coordHolder->bitmapSize.phyCoord = pc_rect;
76                 this->_sharedItem->coordHolder->bitmapSize.virCoord = vc_rect;
77
78                 canvas.Unlock();
79
80                 __isValid = true;
81         }
82 }
83
84 BitmapTemp::BitmapTemp(void* pBuffer, int width, int height, int depth)
85         : _BitmapImpl()
86         , __isValid(false)
87 {
88         if (pBuffer == null || width <= 0 || height <= 0 || !(depth == 16 || depth == 32))
89         {
90                 return;
91         }
92
93         result r = this->_sharedItem->nativeBitmap->Construct((void*) pBuffer, width, height, depth);
94
95         if (r != E_SUCCESS)
96         {
97                 return;
98         }
99
100         Dimension pc_dim(width, height);
101         Dimension vc_dim = _ResUtil::ConvertToVirCoord(pc_dim);
102
103         _ResUtil::Rect vc_rect(0, 0, vc_dim.width, vc_dim.height);
104         _ResUtil::Rect pc_rect(0, 0, pc_dim.width, pc_dim.height);
105
106         this->_sharedItem->coordHolder->bitmapSize.required = vc_rect;
107         this->_sharedItem->coordHolder->bitmapSize.phyCoord = pc_rect;
108         this->_sharedItem->coordHolder->bitmapSize.virCoord = vc_rect;
109
110         __isValid = true;
111 }
112
113 BitmapTemp::BitmapTemp(Dimension physicalSize, int depth)
114         : _BitmapImpl()
115         , __isValid(false)
116 {
117         if (physicalSize.width <= 0 || physicalSize.height <= 0 || !(depth == 16 || depth == 32))
118         {
119                 return;
120         }
121
122         result r = this->_sharedItem->nativeBitmap->Construct(physicalSize, (depth == 32) ? BITMAP_PIXEL_FORMAT_ARGB8888 : BITMAP_PIXEL_FORMAT_RGB565);
123
124         if (r != E_SUCCESS)
125         {
126                 return;
127         }
128
129         Dimension pcDim(physicalSize.width, physicalSize.height);
130         Dimension vcDim = _ResUtil::ConvertToVirCoord(pcDim);
131
132         _ResUtil::Rect vcRect(0, 0, vcDim.width, vcDim.height);
133         _ResUtil::Rect pcRect(0, 0, pcDim.width, pcDim.height);
134
135         this->_sharedItem->coordHolder->bitmapSize.required = vcRect;
136         this->_sharedItem->coordHolder->bitmapSize.phyCoord = pcRect;
137         this->_sharedItem->coordHolder->bitmapSize.virCoord = vcRect;
138
139         __isValid = true;
140 }
141
142 BitmapTemp::~BitmapTemp()
143 {
144 }
145
146 bool
147 BitmapTemp::IsValid(void)
148 {
149         return __isValid;
150 }
151
152 ////////////////////////////////////////////////////////////////////////////////
153
154 unsigned long
155 Tizen::Graphics::_BitmapUtil::GetTimeStamp(const Tizen::Graphics::Bitmap& bitmap)
156 {
157         const Tizen::Graphics::_BitmapImpl* pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(bitmap);
158
159         if (pBitmapImpl)
160         {
161                 return _GetBitmapTimeStamp(*pBitmapImpl);
162         }
163
164         return 0;
165 }
166
167 unsigned long
168 Tizen::Graphics::_BitmapUtil::UpdateTimeStamp(Tizen::Graphics::Bitmap& bitmap)
169 {
170         Tizen::Graphics::_BitmapImpl* pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(bitmap);
171
172         if (pBitmapImpl)
173         {
174                 return _UpdateBitmapTimeStamp(*pBitmapImpl);
175         }
176
177         return 0;
178 }
179
180 Tizen::Graphics::Bitmap*
181 Tizen::Graphics::_BitmapUtil::CreateBitmapN(_BitmapImpl* pBitmapImpl)
182 {
183         if (pBitmapImpl == null)
184         {
185                 return 0;
186         }
187
188         std::auto_ptr <Tizen::Graphics::Bitmap> bitmap(new (std::nothrow) Tizen::Graphics::Bitmap);
189
190         SysTryReturn(NID_GRP, bitmap.get() != 0, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
191
192         // change the implentation instance of Bitmap class
193         {
194                 // exception of ICLS-CLSIF-03
195                 class _BitmapImplHack
196                         : public _BitmapImpl
197                 {
198                 public:
199                         static _BitmapImpl*& GetBitmapImplRef(Bitmap* pBitmap)
200                         {
201                                 return _BitmapImpl::_GetBitmapImpl(pBitmap);
202                         }
203                 }; // _BitmapImplHack
204
205                 Tizen::Graphics::_BitmapImpl*& pRefBitmapImpl = _BitmapImplHack::GetBitmapImplRef(bitmap.get());
206
207                 delete pRefBitmapImpl;
208                 pRefBitmapImpl = pBitmapImpl;
209         }
210
211         return bitmap.release();
212 }
213
214 Tizen::Graphics::Bitmap*
215 Tizen::Graphics::_BitmapUtil::CreateBitmapN(void* pBuffer, int width, int height, int depth)
216 {
217         std::auto_ptr <Tizen::Graphics::BitmapTemp> bitmapTemp(new (std::nothrow) Tizen::Graphics::BitmapTemp(pBuffer, width, height,
218                                                                                                                                                                                                            depth));
219
220         SysTryReturn(NID_GRP, bitmapTemp.get() && bitmapTemp->IsValid(), 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
221
222         Tizen::Graphics::Bitmap* pBitmap = Tizen::Graphics::_BitmapUtil::CreateBitmapN(bitmapTemp.get());
223
224         if (pBitmap)
225         {
226                 // abandon ownership
227                 bitmapTemp.release();
228         }
229
230         return pBitmap;
231 }
232
233 Tizen::Graphics::Bitmap*
234 Tizen::Graphics::_BitmapUtil::CreateBitmapN(Dimension physicalSize, int depth)
235 {
236         std::auto_ptr <Tizen::Graphics::BitmapTemp> bitmapTemp(new (std::nothrow) Tizen::Graphics::BitmapTemp(physicalSize, depth));
237
238         SysTryReturn(NID_GRP, bitmapTemp.get() && bitmapTemp->IsValid(), 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
239
240         Tizen::Graphics::Bitmap* pBitmap = Tizen::Graphics::_BitmapUtil::CreateBitmapN(bitmapTemp.get());
241
242         if (pBitmap)
243         {
244                 // abandon ownership
245                 bitmapTemp.release();
246         }
247
248         return pBitmap;
249 }
250
251 result
252 Tizen::Graphics::_BitmapUtil::ChangeBuffer(Tizen::Graphics::Bitmap& srcBitmap, void* pBuffer, long bytesPerLine, void (* DestroyCallback)(void*), void* pCallbackParam)
253 {
254         if (_BitmapImpl::GetInstance(srcBitmap) == null)
255         {
256                 return E_SYSTEM;
257         }
258
259         _Bitmap* pNativeBitmap = GetBitmapEx(srcBitmap);
260
261         if (pNativeBitmap == null)
262         {
263                 return E_SYSTEM;
264         }
265
266         if (!SetCallback(srcBitmap, DestroyCallback, pCallbackParam, null, null, null, null))
267         {
268                 return E_SYSTEM;
269         }
270
271         pNativeBitmap->AssignUserBuffer((unsigned char*) pBuffer, bytesPerLine);
272
273         return E_SUCCESS;
274 }
275
276 bool
277 Tizen::Graphics::_BitmapUtil::SetCallback(Tizen::Graphics::Bitmap& bitmap,
278         void (* DestroyCallback)(void*), void* pCallbackParam,
279         void (* LockCallback)(void*), void* pLockParam,
280         void (* UnlockCallback)(void*), void* pUnlockParam)
281 {
282         Tizen::Graphics::_BitmapImpl* pBitmapImpl = _BitmapImpl::GetInstance(bitmap);
283
284         _BitmapImplHack* pBitmapImplHack = static_cast <_BitmapImplHack*>(pBitmapImpl);
285
286         return (pBitmapImplHack) ? pBitmapImplHack->SetCallback(DestroyCallback, pCallbackParam, LockCallback, pLockParam, UnlockCallback, pUnlockParam) : false;
287 }
288
289 void
290 Tizen::Graphics::_BitmapUtil::ResetCallback(Tizen::Graphics::Bitmap& bitmap)
291 {
292         Tizen::Graphics::_BitmapImpl* pBitmapImpl = _BitmapImpl::GetInstance(bitmap);
293
294         _BitmapImplHack* pBitmapImplHack = static_cast <_BitmapImplHack*>(pBitmapImpl);
295
296         if (pBitmapImplHack)
297         {
298                 pBitmapImplHack->ResetCallback();
299         }
300 }
301
302 }} // Tizen::Graphics