8b3021400729e2feba27aa936a797c6ac3c122ed
[platform/framework/native/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 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        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                 _Util::Dimension<float> vcDimF = { canvas.GetBoundsF().width, canvas.GetBoundsF().height };
69                 _Util::Dimension<int> vcDim = { canvas.GetBounds().width, canvas.GetBounds().height };
70                 _Util::Dimension<int> pcDim = { canvasDesc.width, canvasDesc.height };
71
72                 this->_sharedItem->coordHolder->ResetFromPc(pcDim, vcDim, vcDimF);
73
74                 canvas.Unlock();
75
76                 __isValid = true;
77         }
78 }
79
80 _BitmapTemp::_BitmapTemp(void* pBuffer, int width, int height, int depth)
81         : _BitmapImpl()
82         , __isValid(false)
83 {
84         if (pBuffer == null || width <= 0 || height <= 0 || !(depth == 16 || depth == 32))
85         {
86                 return;
87         }
88
89         result r = this->_sharedItem->nativeBitmap->Construct((void*) pBuffer, width, height, depth);
90
91         if (r != E_SUCCESS)
92         {
93                 return;
94         }
95
96         _Util::Dimension<int> pcDim = { width, height };
97
98         this->_sharedItem->coordHolder->ResetFromPc(pcDim);
99
100         __isValid = true;
101 }
102
103 _BitmapTemp::_BitmapTemp(Dimension physicalSize, int depth)
104         : _BitmapImpl()
105         , __isValid(false)
106 {
107         if (physicalSize.width <= 0 || physicalSize.height <= 0 || !(depth == 16 || depth == 32))
108         {
109                 return;
110         }
111
112         result r = this->_sharedItem->nativeBitmap->Construct(physicalSize, (depth == 32) ? BITMAP_PIXEL_FORMAT_ARGB8888 : BITMAP_PIXEL_FORMAT_RGB565);
113
114         if (r != E_SUCCESS)
115         {
116                 return;
117         }
118
119         _Util::Dimension<int> pcDim = { physicalSize.width, physicalSize.height };
120
121         this->_sharedItem->coordHolder->ResetFromPc(pcDim);
122
123         __isValid = true;
124 }
125
126 _BitmapTemp::~_BitmapTemp()
127 {
128 }
129
130 bool
131 _BitmapTemp::IsValid(void)
132 {
133         return __isValid;
134 }
135
136 ////////////////////////////////////////////////////////////////////////////////
137
138 unsigned long
139 Tizen::Graphics::_BitmapUtil::GetTimeStamp(const Tizen::Graphics::Bitmap& bitmap)
140 {
141         const Tizen::Graphics::_BitmapImpl* pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(bitmap);
142
143         if (pBitmapImpl)
144         {
145                 return _GetBitmapTimeStamp(*pBitmapImpl);
146         }
147
148         return 0;
149 }
150
151 unsigned long
152 Tizen::Graphics::_BitmapUtil::UpdateTimeStamp(Tizen::Graphics::Bitmap& bitmap)
153 {
154         Tizen::Graphics::_BitmapImpl* pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(bitmap);
155
156         if (pBitmapImpl)
157         {
158                 return _UpdateBitmapTimeStamp(*pBitmapImpl);
159         }
160
161         return 0;
162 }
163
164 Tizen::Graphics::Bitmap*
165 Tizen::Graphics::_BitmapUtil::CreateBitmapN(_BitmapImpl* pBitmapImpl)
166 {
167         if (pBitmapImpl == null)
168         {
169                 return 0;
170         }
171
172         std::auto_ptr <Tizen::Graphics::Bitmap> bitmap(new (std::nothrow) Tizen::Graphics::Bitmap);
173
174         SysTryReturn(NID_GRP, bitmap.get() != 0, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
175
176         // change the implentation instance of Bitmap class
177         {
178                 // exception of ICLS-CLSIF-03
179                 class _BitmapImplHack
180                         : public _BitmapImpl
181                 {
182                 public:
183                         static _BitmapImpl*& GetBitmapImplRef(Bitmap* pBitmap)
184                         {
185                                 return _BitmapImpl::_GetBitmapImpl(pBitmap);
186                         }
187                 }; // _BitmapImplHack
188
189                 Tizen::Graphics::_BitmapImpl*& pRefBitmapImpl = _BitmapImplHack::GetBitmapImplRef(bitmap.get());
190
191                 delete pRefBitmapImpl;
192                 pRefBitmapImpl = pBitmapImpl;
193         }
194
195         return bitmap.release();
196 }
197
198 Tizen::Graphics::Bitmap*
199 Tizen::Graphics::_BitmapUtil::CreateBitmapN(void* pBuffer, int width, int height, int depth)
200 {
201         std::auto_ptr <Tizen::Graphics::_BitmapTemp> bitmapTemp(new (std::nothrow) Tizen::Graphics::_BitmapTemp(pBuffer, width, height,
202                                                                                                                                                                                                            depth));
203
204         SysTryReturn(NID_GRP, bitmapTemp.get() && bitmapTemp->IsValid(), 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
205
206         Tizen::Graphics::Bitmap* pBitmap = Tizen::Graphics::_BitmapUtil::CreateBitmapN(bitmapTemp.get());
207
208         if (pBitmap)
209         {
210                 // abandon ownership
211                 bitmapTemp.release();
212         }
213
214         return pBitmap;
215 }
216
217 Tizen::Graphics::Bitmap*
218 Tizen::Graphics::_BitmapUtil::CreateBitmapN(Dimension physicalSize, int depth)
219 {
220         std::auto_ptr <Tizen::Graphics::_BitmapTemp> bitmapTemp(new (std::nothrow) Tizen::Graphics::_BitmapTemp(physicalSize, depth));
221
222         SysTryReturn(NID_GRP, bitmapTemp.get() && bitmapTemp->IsValid(), 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
223
224         Tizen::Graphics::Bitmap* pBitmap = Tizen::Graphics::_BitmapUtil::CreateBitmapN(bitmapTemp.get());
225
226         if (pBitmap)
227         {
228                 // abandon ownership
229                 bitmapTemp.release();
230         }
231
232         return pBitmap;
233 }
234
235 result
236 Tizen::Graphics::_BitmapUtil::ChangeBuffer(Tizen::Graphics::Bitmap& srcBitmap, void* pBuffer, long bytesPerLine, void (* DestroyCallback)(void*), void* pCallbackParam)
237 {
238         if (_BitmapImpl::GetInstance(srcBitmap) == null)
239         {
240                 return E_SYSTEM;
241         }
242
243         _Bitmap* pNativeBitmap = _GetBitmapEx(srcBitmap);
244
245         if (pNativeBitmap == null)
246         {
247                 return E_SYSTEM;
248         }
249
250         if (!SetCallback(srcBitmap, DestroyCallback, pCallbackParam, null, null, null, null, null, null))
251         {
252                 return E_SYSTEM;
253         }
254
255         pNativeBitmap->AssignUserBuffer((unsigned char*) pBuffer, bytesPerLine);
256
257         return E_SUCCESS;
258 }
259
260 bool
261 Tizen::Graphics::_BitmapUtil::SetCallback(Tizen::Graphics::Bitmap& bitmap,
262         void (* DestroyCallback)(void*), void* pCallbackParam,
263         void (* LockCallback)(void*), void* pLockParam,
264         void (* UnlockCallback)(void*), void* pUnlockParam,
265         void (* PostlockCallback)(Tizen::Graphics::BufferInfo& bufferInfo, void*), void* pPostlockParam)
266 {
267         Tizen::Graphics::_BitmapImpl* pBitmapImpl = _BitmapImpl::GetInstance(bitmap);
268
269         _BitmapImplHack* pBitmapImplHack = static_cast <_BitmapImplHack*>(pBitmapImpl);
270
271         return (pBitmapImplHack) ? pBitmapImplHack->SetCallback(DestroyCallback, pCallbackParam, LockCallback, pLockParam, UnlockCallback, pUnlockParam, PostlockCallback, pPostlockParam) : false;
272 }
273
274 void
275 Tizen::Graphics::_BitmapUtil::ResetCallback(Tizen::Graphics::Bitmap& bitmap)
276 {
277         Tizen::Graphics::_BitmapImpl* pBitmapImpl = _BitmapImpl::GetInstance(bitmap);
278
279         _BitmapImplHack* pBitmapImplHack = static_cast <_BitmapImplHack*>(pBitmapImpl);
280
281         if (pBitmapImplHack)
282         {
283                 pBitmapImplHack->ResetCallback();
284         }
285 }
286
287 }} // Tizen::Graphics