Removed legacy resource tracking / logging
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/render/gl-resources/bitmap-texture.h>
19
20 // EXTERNAL INCLUDES
21 #include <math.h>
22 #include <memory.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/math/rect.h>
26 #include <dali/public-api/math/math-utils.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/internal/render/common/vertex.h>
29 #include <dali/internal/render/common/performance-monitor.h>
30 #include <dali/internal/render/gl-resources/context.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 BitmapTexture::BitmapTexture(Integration::Bitmap* const bitmap, const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixelsProfile, Context& context)
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 {
48   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
49   DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
50 }
51
52 BitmapTexture::BitmapTexture(
53   unsigned int  width,
54   unsigned int  height,
55   Pixel::Format pixelFormat,
56   bool          clearPixels,
57   Context&      context)
58 : Texture(context,
59           width, height,
60           width, height,
61           pixelFormat),
62   mBitmap(NULL),
63   mClearPixels(clearPixels)
64 {
65   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
66 }
67
68 BitmapTexture::~BitmapTexture()
69 {
70   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
71   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
72   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
73 }
74
75 void BitmapTexture::UploadBitmapArray( const BitmapUploadArray& bitmapArray )
76 {
77   if( mId == 0 )
78   {
79     CreateGlTexture();
80   }
81
82   mContext.ActiveTexture(GL_TEXTURE7);  // bind in unused unit so rebind works the first time
83   mContext.Bind2dTexture(mId);
84   mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
85
86   GLenum pixelFormat   = GL_RGBA;
87   GLenum pixelDataType = GL_UNSIGNED_BYTE;
88
89   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
90
91   // go through each bitmap uploading it
92
93   for( BitmapUploadArray::const_iterator iter =  bitmapArray.begin(), endIter = bitmapArray.end(); iter != endIter ; ++iter)
94   {
95     const BitmapUpload& bitmapItem((*iter));
96
97     DALI_ASSERT_ALWAYS(bitmapItem.mPixelData);
98
99     const unsigned char* pixels = bitmapItem.mPixelData;
100
101     DALI_ASSERT_DEBUG( (pixels!=NULL) && "bitmap has no data \n");
102
103     unsigned int bitmapWidth(bitmapItem.mWidth);
104     unsigned int bitmapHeight(bitmapItem.mHeight);
105
106     DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, "upload bitmap to texture :%d y:%d w:%d h:%d\n",
107                             bitmapItem.mXpos,
108                             bitmapItem.mYpos,
109                             bitmapWidth,
110                             bitmapHeight);
111
112      mContext.TexSubImage2D(GL_TEXTURE_2D,
113                             0,                   /* mip map level */
114                             bitmapItem.mXpos,    /* X pos */
115                             bitmapItem.mYpos,    /* Y pos */
116                             bitmapWidth,         /* width */
117                             bitmapHeight,        /* height */
118                             pixelFormat,         /* our bitmap format (should match internal format) */
119                             pixelDataType,       /* pixel data type */
120                             pixels);             /* texture data */
121
122      if( BitmapUpload::DISCARD_PIXEL_DATA == bitmapItem.mDiscard)
123      {
124        delete [] bitmapItem.mPixelData;
125      }
126   }
127 }
128
129 bool BitmapTexture::HasAlphaChannel() const
130 {
131   return Pixel::HasAlpha(mPixelFormat);
132 }
133
134 bool BitmapTexture::IsFullyOpaque() const
135 {
136   if( mBitmap )
137   {
138     return mBitmap->IsFullyOpaque();
139   }
140   else
141   {
142     return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
143   }
144 }
145
146 // Bitmap buffer has been changed. Upload changes to GPU.
147 void BitmapTexture::AreaUpdated( const RectArea& updateArea, const unsigned char* pixels )
148 {
149   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
150   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
151
152   GLenum pixelFormat   = GL_RGBA;
153   GLenum pixelDataType = GL_UNSIGNED_BYTE;
154   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
155
156   mContext.ActiveTexture(GL_TEXTURE7);  // bind in unused unit so rebind works the first time
157
158   mContext.Bind2dTexture(mId);
159
160   if( ! updateArea.IsEmpty() )
161   {
162     mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
163     DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "Update x:%d y:%d w:%d h:%d\n",
164                    updateArea.x, updateArea.y, updateArea.width ,updateArea.height );
165
166     // TODO: obtain pitch of source image, obtain pixel depth of source image.
167     const unsigned int pitchPixels = mImageWidth;
168     const unsigned int pixelDepth = sizeof(unsigned int);
169
170     // If the width of the source update area is the same as the pitch, then can
171     // copy the contents in a single contiguous TexSubImage call.
172     if(updateArea.x == 0 && updateArea.width == pitchPixels)
173     {
174       pixels += updateArea.y * pitchPixels * pixelDepth;
175       mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, updateArea.y,
176                               updateArea.width, updateArea.height,
177                               pixelFormat, pixelDataType, pixels );
178     }
179     else
180     {
181       // Otherwise the source buffer needs to be copied line at a time, as OpenGL ES
182       // does not support source strides. (no GL_UNPACK_ROW_LENGTH supported)
183       unsigned int yBottom = updateArea.y + updateArea.height;
184       pixels += (updateArea.y * pitchPixels + updateArea.x) * pixelDepth;
185
186       for(unsigned int y = updateArea.y; y < yBottom; y++)
187       {
188         mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, y,
189                                 updateArea.width, 1,
190                                 pixelFormat, pixelDataType, pixels );
191         pixels += pitchPixels * pixelDepth;
192       }
193     }
194
195     INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED,
196                  updateArea.Area()* GetBytesPerPixel( mPixelFormat ));
197   }
198 }
199
200
201 void BitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* pixels )
202 {
203   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
204   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AssignBitmap()\n");
205
206   GLenum pixelFormat = GL_RGBA;
207   GLenum pixelDataType = GL_UNSIGNED_BYTE;
208
209   if( generateTexture )
210   {
211     mContext.GenTextures(1, &mId);
212   }
213   DALI_ASSERT_DEBUG( mId != 0 );
214
215   mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
216   mContext.Bind2dTexture(mId);
217   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
218
219   mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
220   mContext.TexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, pixelFormat, pixelDataType, pixels);
221   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
222   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
223   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
224   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
225
226   INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, GetBytesPerPixel(mPixelFormat) * mWidth * mHeight );
227 }
228
229 void BitmapTexture::Update( Integration::Bitmap* bitmap )
230 {
231   DALI_LOG_INFO( Debug::Filter::gGLResource, Debug::General, "BitmapTexture::Update(bitmap:%p )\n", bitmap );
232   DALI_ASSERT_DEBUG( bitmap != 0 );
233   if( !bitmap )
234   {
235     DALI_LOG_ERROR( "Passed a null bitmap to update this bitmap texture." );
236     return;
237   }
238
239   // Only Packed-pixel bitmaps are ever associated with BitmapTextures, so we should never be passed any other kind:
240   const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixels = bitmap->GetPackedPixelsProfile();
241   DALI_ASSERT_DEBUG(bitmapPackedPixels);
242   if( !bitmapPackedPixels )
243   {
244     ///! This should never happen.
245     DALI_LOG_ERROR("Passed an incompatible bitmap type to update this bitmap texture.");
246     return;
247   }
248   mBitmap = bitmap;
249
250   const unsigned char* pixels = mBitmap->GetBuffer();
251
252   DALI_ASSERT_DEBUG( pixels != NULL );
253
254   if ( NULL == pixels )
255   {
256     DALI_LOG_ERROR("bitmap has no data\n");
257     GlCleanup(); // Note, We suicide here in the case of bad input.
258   }
259   else
260   {
261     if( mImageWidth == mBitmap->GetImageWidth() &&
262         mImageHeight == mBitmap->GetImageHeight() &&
263         mWidth  == bitmapPackedPixels->GetBufferWidth() &&
264         mHeight == bitmapPackedPixels->GetBufferHeight() &&
265         mPixelFormat == mBitmap->GetPixelFormat() ) // and size hasn't changed
266     {
267       if ( mId ) // If the texture is already bound
268       {
269         RectArea area(0, 0, mImageWidth, mImageHeight);  // just update whole texture
270         AreaUpdated( area, pixels );
271         mBitmap->DiscardBuffer();
272       }
273     }
274     else
275     {                                           // Otherwise, reload the pixel data
276       mImageWidth  = mBitmap->GetImageWidth();
277       mImageHeight = mBitmap->GetImageHeight();
278       mWidth       = bitmapPackedPixels->GetBufferWidth();
279       mHeight      = bitmapPackedPixels->GetBufferHeight();
280       mPixelFormat = mBitmap->GetPixelFormat();
281
282       if ( mId ) // If the texture is already bound
283       {
284         AssignBitmap( false, pixels );
285         mBitmap->DiscardBuffer();
286       }
287     }
288   }
289 }
290
291 void BitmapTexture::UpdateArea( const RectArea& updateArea )
292 {
293   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "BitmapTexture::UpdateArea()\n");
294
295   if( mBitmap != 0 )
296   {
297     const unsigned char* pixels = mBitmap->GetBuffer();
298     if ( NULL == pixels )
299     {
300       DALI_LOG_ERROR("bitmap has no data\n");
301       GlCleanup(); ///!ToDo: Why do we suicide in the case of bad input?
302     }
303     else
304     {
305       if ( mId ) // If the texture is already bound
306       {
307         if( updateArea.IsEmpty() )
308         {
309           RectArea area;
310           area.x = 0;
311           area.y = 0;
312           area.width = mImageWidth;
313           area.height = mImageHeight;
314           AreaUpdated( area, pixels );
315         }
316         else
317         {
318           AreaUpdated( updateArea, pixels );
319         }
320       }
321     }
322   }
323 }
324
325 void BitmapTexture::ClearAreas( const BitmapClearArray& areaArray, std::size_t blockSize, uint32_t color )
326 {
327   if(mId > 0)
328   {
329     DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
330     DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
331
332     GLenum pixelFormat   = GL_RGBA;
333     GLenum pixelDataType = GL_UNSIGNED_BYTE;
334     Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
335
336     mContext.ActiveTexture(GL_TEXTURE7);  // bind in unused unit so rebind works the first time
337     mContext.Bind2dTexture(mId);
338
339     size_t numPixels = blockSize*blockSize;
340     size_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
341     char* clearPixels = (char*)malloc(numPixels * bytesPerPixel);
342
343     for(size_t i=0; i<numPixels; i++)
344     {
345       memcpy(&clearPixels[i*bytesPerPixel], &color, bytesPerPixel);
346     }
347
348     for( BitmapClearArray::const_iterator iter =  areaArray.begin(), endIter = areaArray.end(); iter != endIter ; ++iter)
349     {
350       const Vector2& clearPos((*iter));
351
352       mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
353       DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "Update x:%0.2f y:%0.2f w:%d h:%d\n",
354                      clearPos.x, clearPos.y, blockSize, blockSize );
355
356       mContext.TexSubImage2D(GL_TEXTURE_2D,
357                              0,
358                              clearPos.x,
359                              clearPos.y,
360                              blockSize,
361                              blockSize,
362                              pixelFormat,         /* our bitmap format (should match internal format) */
363                              pixelDataType,       /* pixel data type */
364                              clearPixels);        /* texture data */
365
366       INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, numPixels * bytesPerPixel );
367     }
368     free(clearPixels);
369   }
370 }
371
372 bool BitmapTexture::UpdateOnCreate()
373 {
374   return true;
375 }
376
377 bool BitmapTexture::CreateGlTexture()
378 {
379   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
380   DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
381
382   if( mBitmap )
383   {
384     const unsigned char* pixels = mBitmap->GetBuffer();
385
386     DALI_ASSERT_DEBUG(pixels != NULL);
387
388     if( NULL != pixels )
389     {
390       AssignBitmap( true, pixels );
391       mBitmap->DiscardBuffer();
392     }
393   }
394   else
395   {
396     const unsigned char* pixels = NULL;
397     std::vector<unsigned char> pixelData;
398     if( ( NULL == pixels ) && ( true == mClearPixels ) )
399     {
400       unsigned int size = mWidth * mHeight * Pixel::GetBytesPerPixel(mPixelFormat);
401       pixelData.resize(size, 0);
402       pixels = &pixelData[0];
403     }
404     AssignBitmap( true, pixels );
405   }
406
407   return mId != 0;
408 }
409
410 bool BitmapTexture::Init()
411 {
412   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
413   // mBitmap should be initialized by now
414   return (mBitmap != 0);
415 }
416
417 unsigned int BitmapTexture::GetWidth() const
418 {
419   unsigned int width = mWidth;
420   if( mBitmap )
421   {
422     width = mBitmap->GetImageWidth();
423   }
424   return width;
425 }
426
427 unsigned int BitmapTexture::GetHeight() const
428 {
429   unsigned int height = mHeight;
430   if( mBitmap )
431   {
432     height = mBitmap->GetImageHeight();
433   }
434   return height;
435 }
436
437 } //namespace Internal
438
439 } //namespace Dali