Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / graphics / FGrp_NonScale.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_NonScale.cpp
20  * @brief       This is the header file for internal util class.
21  *
22  */
23
24 #include <new>
25 #include <memory>
26
27 #include <FBaseByteBuffer.h>
28
29 #include "FGrp_NonScale.h"
30 #include "FGrp_BitmapImpl.h"
31 #include "FGrp_BitmapCoordHolder.h"
32 #include "FGrp_BitmapUtil.h"
33 #include "FGrp_Bitmap.h"
34 #include "util/FGrp_UtilTemplate.h"
35
36
37 namespace // unnamed
38 {
39
40 Tizen::Graphics::_BitmapCoordHolder*
41 _GetBitmapCoordHolder(const Tizen::Graphics::_BitmapImpl& bitmap)
42 {
43         class BitmapHacked
44                 : public Tizen::Graphics::_BitmapImpl
45         {
46 public:
47                 inline Tizen::Graphics::_BitmapCoordHolder* GetBitmapCoordHolder(void)
48                 {
49                         return this->_sharedItem->coordHolder.get();
50                 }
51         };
52
53         return ((BitmapHacked*) &bitmap)->GetBitmapCoordHolder();
54 }
55
56 }
57
58 namespace Tizen { namespace Graphics
59 {
60
61 _BitmapImpl*
62 _NonScale::CreateBitmapN(const Tizen::Base::ByteBuffer& buffer, const Dimension& rq_dim, BitmapPixelFormat pixelFormat)
63 {
64         Dimension vc_dim = _ResUtil::ConvertToVirCoord(rq_dim);
65
66         return _NonScale::CreateBitmapN(buffer, rq_dim, pixelFormat, vc_dim);
67 }
68
69 _BitmapImpl*
70 _NonScale::CreateBitmapN(const Tizen::Base::ByteBuffer& buffer, const Dimension& rq_dim, BitmapPixelFormat pixelFormat,
71                                                  const Dimension& logicalSize)
72 {
73         result r = E_SUCCESS;
74
75         SysTryReturn(NID_GRP
76                 , rq_dim.width > 0 && rq_dim.height > 0
77                 , null
78                 , E_INVALID_ARG
79                 , "[E_INVALID_ARG] The reqired size(%d, %d) is invalid.", rq_dim.width, rq_dim.height);
80
81         SysTryReturn(NID_GRP
82                 , BITMAP_PIXEL_FORMAT_MIN < pixelFormat && pixelFormat < BITMAP_PIXEL_FORMAT_MAX
83                 , null
84                 , E_INVALID_ARG
85                 , "[E_INVALID_ARG] The given pixel format(%d) is invalid.", pixelFormat);
86
87         {
88                 int bytePerPixel = 0;
89
90                 switch (pixelFormat)
91                 {
92                 case Tizen::Graphics::BITMAP_PIXEL_FORMAT_RGB565:
93                         bytePerPixel = 2;
94                         break;
95                 case Tizen::Graphics::BITMAP_PIXEL_FORMAT_ARGB8888:
96                 case Tizen::Graphics::BITMAP_PIXEL_FORMAT_R8G8B8A8:
97                         bytePerPixel = 4;
98                         break;
99                 default:
100                         SysLogException(NID_GRP, E_INVALID_ARG, "The given pixel format(%d) is not supported.", pixelFormat);
101                         return null;
102                 }
103
104                 SysTryReturn(NID_GRP
105                         , bytePerPixel > 0
106                         , null
107                         , E_UNSUPPORTED_FORMAT
108                         , "[E_UNSUPPORTED_FORMAT] The given bytes-per-pixel(%d) is not supported.", bytePerPixel);
109
110                 int numOfBytes = buffer.GetLimit();
111                 int expectedBufferSize = rq_dim.width * rq_dim.height * bytePerPixel;
112
113                 SysTryReturn(NID_GRP
114                         , expectedBufferSize <= numOfBytes
115                         , null
116                         , E_INVALID_ARG
117                         , "[E_INVALID_ARG] The buffer size is too small. (expected: %d, actual: %d)", expectedBufferSize, numOfBytes);
118         }
119
120         std::auto_ptr <_BitmapImpl> bitmap(new (std::nothrow) _BitmapImpl);
121
122         SysTryReturn(NID_GRP
123                 , bitmap.get()
124                 , null
125                 , E_OUT_OF_MEMORY
126                 , "[E_OUT_OF_MEMORY] _BitmapImpl is not allocated.");
127
128         _Bitmap* pBitmapEx = GetBitmapEx(*bitmap.get());
129
130         SysTryReturn(NID_GRP
131                 , pBitmapEx
132                 , null
133                 , E_OUT_OF_MEMORY
134                 , "[E_OUT_OF_MEMORY] _Bitmap is not allocated.");
135
136         if (_ResUtil::NeedToConvertCoord())
137         {
138                 r = pBitmapEx->Construct(buffer, rq_dim, pixelFormat);
139
140                 SysTryReturn(NID_GRP
141                         , !IsFailed(r)
142                         , null
143                         , r
144                         , "[%s] _Bitmap::Construct() failed.", GetErrorMessage(r));
145
146                 Dimension pc_dim = rq_dim;
147                 Dimension vc_dim = logicalSize;
148
149                 _ResUtil::Rect vc_rect(0, 0, vc_dim.width, vc_dim.height);
150                 _ResUtil::Rect pc_rect(0, 0, pc_dim.width, pc_dim.height);
151
152                 _BitmapCoordHolder* pBitmapCoordHolder = _GetBitmapCoordHolder(*bitmap.get());
153
154                 if (pBitmapCoordHolder)
155                 {
156                         pBitmapCoordHolder->bitmapSize.required = vc_rect;
157                         pBitmapCoordHolder->bitmapSize.phyCoord = pc_rect;
158                         pBitmapCoordHolder->bitmapSize.virCoord = vc_rect;
159                 }
160         }
161         else
162         {
163                 r = pBitmapEx->Construct(buffer, rq_dim, pixelFormat);
164
165                 SysTryReturn(NID_GRP
166                         , !IsFailed(r)
167                         , null
168                         , r
169                         , "[%s] _Bitmap::Construct() failed.", GetErrorMessage(r));
170         }
171
172         return bitmap.release();
173 }
174
175 _BitmapImpl*
176 _NonScale::CreateBitmapN(const Dimension& rq_dim, BitmapPixelFormat pixelFormat)
177 {
178         std::auto_ptr <_BitmapImpl> bitmap(new (std::nothrow) _BitmapImpl);
179
180         SysTryReturn(NID_GRP
181                 , bitmap.get()
182                 , null
183                 , E_OUT_OF_MEMORY
184                 , "[E_OUT_OF_MEMORY] _BitmapImpl::Construct() failed.");
185
186         Dimension pc_dim = rq_dim;
187         Dimension vc_dim = _ResUtil::ConvertToVirCoord(pc_dim);
188
189         result r = bitmap->Construct(vc_dim, pixelFormat);
190
191         SysTryReturn(NID_GRP
192                 , !IsFailed(r)
193                 , null
194                 , r
195                 , "[%s] _Bitmap::Construct() failed.", GetErrorMessage(r));
196
197         return bitmap.release();
198 }
199
200 }} // Tizen::Graphics