Ensure synchronous buffer decode when using encoded buffer image.
[platform/core/uifw/dali-core.git] / dali / internal / event / images / bitmap-packed-pixel.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/images/bitmap-packed-pixel.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/ref-object.h>
23 #include <dali/internal/common/core-impl.h>
24 #include <dali/integration-api/debug.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31 using namespace Dali::Pixel;
32
33
34 BitmapPackedPixel::BitmapPackedPixel( ResourcePolicy::Discardable discardable, Dali::Integration::PixelBuffer* pixBuf )
35 : Bitmap( discardable, pixBuf ),
36   mBufferWidth(0),
37   mBufferHeight(0),
38   mBytesPerPixel(0)
39 {
40 }
41
42 // use power of two bufferWidth and bufferHeight for better performance
43 Dali::Integration::PixelBuffer* BitmapPackedPixel::ReserveBuffer(Pixel::Format pixelFormat,
44                                     unsigned int  width,
45                                     unsigned int  height,
46                                     unsigned int  bufferWidth,
47                                     unsigned int  bufferHeight)
48 {
49   // delete existing buffer
50   DeletePixelBuffer();
51
52   Initialize(pixelFormat, width, height, bufferWidth, bufferHeight);
53
54   //allocate buffer
55   unsigned int bufSize = mBufferWidth * mBufferHeight * mBytesPerPixel;
56
57   mData = new Dali::Integration::PixelBuffer[bufSize];
58
59   return mData;
60 }
61
62 void BitmapPackedPixel::AssignBuffer(Pixel::Format pixelFormat,
63                                      Dali::Integration::PixelBuffer* buffer,
64                                      std::size_t bufferSize,
65                                      unsigned int width,
66                                      unsigned int height,
67                                      unsigned int bufferWidth,
68                                      unsigned int bufferHeight)
69 {
70   DALI_ASSERT_DEBUG( buffer );
71
72   // delete existing buffer
73   DeletePixelBuffer();
74
75   Initialize( pixelFormat, width, height, bufferWidth, bufferHeight );
76
77    // make sure the buffer size matches what is being passed in
78   DALI_ASSERT_DEBUG( bufferSize ==  (mBufferWidth * mBufferHeight * mBytesPerPixel))
79
80   mData = buffer;
81 }
82
83 void BitmapPackedPixel::TestForTransparency()
84 {
85   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
86
87   mAlphaChannelUsed = false;
88
89   if(HasAlphaChannel())
90   {
91     unsigned char* pixelBuffer=GetBuffer();
92     if(pixelBuffer != NULL)
93     {
94       unsigned char* row = pixelBuffer;
95
96       int byte; int bits;
97       Pixel::GetAlphaOffsetAndMask(mPixelFormat, byte, bits);
98
99       int stride       = mBufferWidth * mBytesPerPixel;
100       int pixelsPerRow = mImageWidth;
101
102       for(size_t j=0; j<mImageHeight; j++)
103       {
104         unsigned char* pixels = row;
105         for(int i=0; i<pixelsPerRow; i++)
106         {
107           if((pixels[byte] & bits) != bits)
108           {
109             mAlphaChannelUsed = true;
110             j=mImageHeight; // break out of outer loop
111             break;
112           }
113           pixels+=mBytesPerPixel;
114         }
115         row += stride;
116       }
117     }
118   }
119 }
120
121 BitmapPackedPixel::~BitmapPackedPixel()
122 {
123   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
124 }
125
126 void BitmapPackedPixel::Initialize( Pixel::Format pixelFormat,
127                           unsigned int width,
128                           unsigned int height,
129                           unsigned int bufferWidth,
130                           unsigned int bufferHeight)
131 {
132   Dali::Integration::Bitmap::Initialize(pixelFormat, width, height);
133   mBufferWidth  = (bufferWidth  != 0) ? bufferWidth  : width;
134   mBufferHeight = (bufferHeight != 0) ? bufferHeight : height;
135   mBytesPerPixel = Pixel::GetBytesPerPixel(pixelFormat);
136   DALI_ASSERT_DEBUG(mBufferWidth >= mImageWidth && mBufferHeight >= mImageHeight);
137 }
138
139 unsigned int BitmapPackedPixel::GetBufferStride() const
140 {
141   return mBufferWidth*mBytesPerPixel;
142 }
143
144
145
146 } //namespace Integration
147
148 } //namespace Dali