Removed some redundant texture features
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / bitmap-texture.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/render/gl-resources/bitmap-texture.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/render/common/vertex.h>
24 #include <dali/internal/render/common/performance-monitor.h>
25 #include <dali/internal/render/gl-resources/context.h>
26 #include <dali/internal/render/gl-resources/texture-units.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 BitmapTexture::BitmapTexture(
35   Integration::Bitmap* const bitmap,
36   const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixelsProfile,
37   Context& context,
38   ResourcePolicy::Discardable policy)
39 : Texture( context,
40            bitmapPackedPixelsProfile->GetBufferWidth(),
41            bitmapPackedPixelsProfile->GetBufferHeight(),
42            bitmap->GetImageWidth(),
43            bitmap->GetImageHeight(),
44            bitmap->GetPixelFormat()),
45   mBitmap(bitmap),
46   mClearPixels(false),
47   mDiscardPolicy(policy)
48 {
49   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
50   DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
51 }
52
53 BitmapTexture::BitmapTexture(
54   unsigned int width,
55   unsigned int height,
56   Pixel::Format pixelFormat,
57   bool clearPixels,
58   Context& context,
59   ResourcePolicy::Discardable policy)
60 : Texture( context,
61            width, height,
62            width, height,
63            pixelFormat),
64   mBitmap(NULL),
65   mClearPixels(clearPixels),
66   mDiscardPolicy(policy)
67 {
68   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
69 }
70
71 BitmapTexture::~BitmapTexture()
72 {
73   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
74
75   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
76   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
77 }
78
79 bool BitmapTexture::HasAlphaChannel() const
80 {
81   return Pixel::HasAlpha(mPixelFormat);
82 }
83
84 bool BitmapTexture::IsFullyOpaque() const
85 {
86   if( mBitmap )
87   {
88     return mBitmap->IsFullyOpaque();
89   }
90   else
91   {
92     return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
93   }
94 }
95
96 // Bitmap buffer has been changed. Upload changes to GPU.
97 void BitmapTexture::AreaUpdated( const RectArea& updateArea, const unsigned char* pixels )
98 {
99   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
100   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
101
102   GLenum pixelFormat   = GL_RGBA;
103   GLenum pixelDataType = GL_UNSIGNED_BYTE;
104   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
105
106   mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
107
108   mContext.Bind2dTexture(mId);
109
110   if( ! updateArea.IsEmpty() )
111   {
112     mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
113     DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "Update x:%d y:%d w:%d h:%d\n",
114                    updateArea.x, updateArea.y, updateArea.width ,updateArea.height );
115
116     // TODO: obtain pitch of source image, obtain pixel depth of source image.
117     const unsigned int pitchPixels = mImageWidth;
118     const unsigned int pixelDepth = sizeof(unsigned int);
119
120     // If the width of the source update area is the same as the pitch, then can
121     // copy the contents in a single contiguous TexSubImage call.
122     if(updateArea.x == 0 && updateArea.width == pitchPixels)
123     {
124       pixels += updateArea.y * pitchPixels * pixelDepth;
125       mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, updateArea.y,
126                               updateArea.width, updateArea.height,
127                               pixelFormat, pixelDataType, pixels );
128     }
129     else
130     {
131       // Otherwise the source buffer needs to be copied line at a time, as OpenGL ES
132       // does not support source strides. (no GL_UNPACK_ROW_LENGTH supported)
133       unsigned int yBottom = updateArea.y + updateArea.height;
134       pixels += (updateArea.y * pitchPixels + updateArea.x) * pixelDepth;
135
136       for(unsigned int y = updateArea.y; y < yBottom; y++)
137       {
138         mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, y,
139                                 updateArea.width, 1,
140                                 pixelFormat, pixelDataType, pixels );
141         pixels += pitchPixels * pixelDepth;
142       }
143     }
144
145     INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED,
146                  updateArea.Area()* GetBytesPerPixel( mPixelFormat ));
147   }
148 }
149
150
151 void BitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* pixels )
152 {
153   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
154   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AssignBitmap()\n");
155
156   GLenum pixelFormat = GL_RGBA;
157   GLenum pixelDataType = GL_UNSIGNED_BYTE;
158
159   if( generateTexture )
160   {
161     mContext.GenTextures(1, &mId);
162   }
163   DALI_ASSERT_DEBUG( mId != 0 );
164
165   mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
166   mContext.Bind2dTexture(mId);
167   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
168
169   mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
170   mContext.TexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, pixelFormat, pixelDataType, pixels);
171   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
172   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
173
174   INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, GetBytesPerPixel(mPixelFormat) * mWidth * mHeight );
175 }
176
177 void BitmapTexture::Update( Integration::Bitmap* bitmap )
178 {
179   DALI_LOG_INFO( Debug::Filter::gGLResource, Debug::General, "BitmapTexture::Update(bitmap:%p )\n", bitmap );
180   DALI_ASSERT_DEBUG( bitmap != 0 );
181   if( !bitmap )
182   {
183     DALI_LOG_ERROR( "Passed a null bitmap to update this bitmap texture." );
184     return;
185   }
186
187   // Only Packed-pixel bitmaps are ever associated with BitmapTextures, so we should never be passed any other kind:
188   const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixels = bitmap->GetPackedPixelsProfile();
189   DALI_ASSERT_DEBUG(bitmapPackedPixels);
190   if( !bitmapPackedPixels )
191   {
192     ///! This should never happen.
193     DALI_LOG_ERROR("Passed an incompatible bitmap type to update this bitmap texture.");
194     return;
195   }
196   mBitmap = bitmap;
197
198   const unsigned char* pixels = mBitmap->GetBuffer();
199
200   // We should never have null pixel data here - resource manager has deliberately loaded/reloaded data
201
202   DALI_ASSERT_DEBUG( pixels != NULL );
203
204   if( NULL == pixels )
205   {
206     DALI_LOG_ERROR("BitmapTexture::Upload() - Bitmap has no pixel data.\n");
207   }
208   else if( mId != 0 )
209   {
210     if( mImageWidth == mBitmap->GetImageWidth() &&
211         mImageHeight == mBitmap->GetImageHeight() &&
212         mWidth  == bitmapPackedPixels->GetBufferWidth() &&
213         mHeight == bitmapPackedPixels->GetBufferHeight() &&
214         mPixelFormat == mBitmap->GetPixelFormat() ) // and size hasn't changed
215     {
216       RectArea area(0, 0, mImageWidth, mImageHeight);  // just update whole texture
217       AreaUpdated( area, pixels );
218       DiscardBitmapBuffer();
219     }
220     else // Otherwise, reload the pixel data
221     {
222       mImageWidth  = mBitmap->GetImageWidth();
223       mImageHeight = mBitmap->GetImageHeight();
224       mWidth       = bitmapPackedPixels->GetBufferWidth();
225       mHeight      = bitmapPackedPixels->GetBufferHeight();
226       mPixelFormat = mBitmap->GetPixelFormat();
227
228       AssignBitmap( false, pixels );
229     }
230   }
231 }
232
233 void BitmapTexture::UpdateArea( const RectArea& updateArea )
234 {
235   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "BitmapTexture::UpdateArea()\n");
236
237   if( mBitmap != 0 )
238   {
239     const unsigned char* pixels = mBitmap->GetBuffer();
240
241     // Pixel data could be null if we've uploaded to GL and discarded the data.
242
243     if( NULL != pixels )
244     {
245       if( mId ) // If the texture is already bound
246       {
247         if( updateArea.IsEmpty() )
248         {
249           RectArea area;
250           area.x = 0;
251           area.y = 0;
252           area.width = mImageWidth;
253           area.height = mImageHeight;
254           AreaUpdated( area, pixels );
255         }
256         else
257         {
258           AreaUpdated( updateArea, pixels );
259         }
260       }
261     }
262   }
263 }
264
265 bool BitmapTexture::UpdateOnCreate()
266 {
267   return true;
268 }
269
270 bool BitmapTexture::CreateGlTexture()
271 {
272   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
273   DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "BitmapTexture::CreateGlTexture() Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
274
275   if( mBitmap )
276   {
277     const unsigned char* pixels = mBitmap->GetBuffer();
278
279     // pixel data could be NULL here if we've had a context loss and we previously discarded
280     // the pixel data on the previous upload. If it is null, then we shouldn't generate a
281     // new GL Texture; leaving mId as zero. Eventually, the bitmap will get reloaded,
282     // and pixels will become non-null.
283
284     if( NULL != pixels )
285     {
286       AssignBitmap( true, pixels );
287       DiscardBitmapBuffer();
288     }
289   }
290   else
291   {
292     const unsigned char* pixels = NULL;
293     std::vector<unsigned char> pixelData;
294     if( ( NULL == pixels ) && ( true == mClearPixels ) )
295     {
296       unsigned int size = mWidth * mHeight * Pixel::GetBytesPerPixel(mPixelFormat);
297       pixelData.resize(size, 0);
298       pixels = &pixelData[0];
299     }
300     AssignBitmap( true, pixels );
301   }
302
303   return mId != 0;
304 }
305
306 bool BitmapTexture::Init()
307 {
308   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
309   // mBitmap should be initialized by now
310   return (mBitmap != 0);
311 }
312
313 unsigned int BitmapTexture::GetWidth() const
314 {
315   unsigned int width = mWidth;
316   if( mBitmap )
317   {
318     width = mBitmap->GetImageWidth();
319   }
320   return width;
321 }
322
323 unsigned int BitmapTexture::GetHeight() const
324 {
325   unsigned int height = mHeight;
326   if( mBitmap )
327   {
328     height = mBitmap->GetImageHeight();
329   }
330   return height;
331 }
332
333 void BitmapTexture::DiscardBitmapBuffer()
334 {
335   DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, "BitmapTexture::DiscardBitmapBuffer() DiscardPolicy: %s\n", mDiscardPolicy == ResourcePolicy::DISCARD?"DISCARD":"RETAIN");
336
337   if( ResourcePolicy::DISCARD == mDiscardPolicy )
338   {
339     DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, "  Discarding bitmap\n");
340     mBitmap->DiscardBuffer();
341   }
342 }
343
344
345 } //namespace Internal
346
347 } //namespace Dali