fa3a487cbca430abd7cd08d9edb8852950653e9d
[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   DALI_LOG_RESOURCE("[UPLOAD] Uploaded image data from Bitmap %p to Texture %d - size %d bytes (%dx%d)\n",
227                     mBitmap.Get(), mId, Pixel::GetBytesPerPixel(mPixelFormat)*mWidth*mHeight, mWidth, mHeight);
228   INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, GetBytesPerPixel(mPixelFormat) * mWidth * mHeight );
229 }
230
231 void BitmapTexture::Update( Integration::Bitmap* bitmap )
232 {
233   DALI_LOG_INFO( Debug::Filter::gGLResource, Debug::General, "BitmapTexture::Update(bitmap:%p )\n", bitmap );
234   DALI_ASSERT_DEBUG( bitmap != 0 );
235   if( !bitmap )
236   {
237     DALI_LOG_ERROR( "Passed a null bitmap to update this bitmap texture." );
238     return;
239   }
240
241   // Only Packed-pixel bitmaps are ever associated with BitmapTextures, so we should never be passed any other kind:
242   const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixels = bitmap->GetPackedPixelsProfile();
243   DALI_ASSERT_DEBUG(bitmapPackedPixels);
244   if( !bitmapPackedPixels )
245   {
246     ///! This should never happen.
247     DALI_LOG_ERROR("Passed an incompatible bitmap type to update this bitmap texture.");
248     return;
249   }
250   mBitmap = bitmap;
251
252   const unsigned char* pixels = mBitmap->GetBuffer();
253
254   DALI_ASSERT_DEBUG( pixels != NULL );
255
256   if ( NULL == pixels )
257   {
258     DALI_LOG_ERROR("bitmap has no data\n");
259     GlCleanup(); // Note, We suicide here in the case of bad input.
260   }
261   else
262   {
263     if( mImageWidth == mBitmap->GetImageWidth() &&
264         mImageHeight == mBitmap->GetImageHeight() &&
265         mWidth  == bitmapPackedPixels->GetBufferWidth() &&
266         mHeight == bitmapPackedPixels->GetBufferHeight() &&
267         mPixelFormat == mBitmap->GetPixelFormat() ) // and size hasn't changed
268     {
269       if ( mId ) // If the texture is already bound
270       {
271         RectArea area(0, 0, mImageWidth, mImageHeight);  // just update whole texture
272         AreaUpdated( area, pixels );
273         mBitmap->DiscardBuffer();
274       }
275     }
276     else
277     {                                           // Otherwise, reload the pixel data
278       mImageWidth  = mBitmap->GetImageWidth();
279       mImageHeight = mBitmap->GetImageHeight();
280       mWidth       = bitmapPackedPixels->GetBufferWidth();
281       mHeight      = bitmapPackedPixels->GetBufferHeight();
282       mPixelFormat = mBitmap->GetPixelFormat();
283
284       if ( mId ) // If the texture is already bound
285       {
286         AssignBitmap( false, pixels );
287         mBitmap->DiscardBuffer();
288       }
289     }
290   }
291 }
292
293 void BitmapTexture::UpdateArea( const RectArea& updateArea )
294 {
295   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "BitmapTexture::UpdateArea()\n");
296
297   if( mBitmap != 0 )
298   {
299     const unsigned char* pixels = mBitmap->GetBuffer();
300     if ( NULL == pixels )
301     {
302       DALI_LOG_ERROR("bitmap has no data\n");
303       GlCleanup(); ///!ToDo: Why do we suicide in the case of bad input?
304     }
305     else
306     {
307       if ( mId ) // If the texture is already bound
308       {
309         if( updateArea.IsEmpty() )
310         {
311           RectArea area;
312           area.x = 0;
313           area.y = 0;
314           area.width = mImageWidth;
315           area.height = mImageHeight;
316           AreaUpdated( area, pixels );
317         }
318         else
319         {
320           AreaUpdated( updateArea, pixels );
321         }
322       }
323     }
324   }
325 }
326
327 void BitmapTexture::ClearAreas( const BitmapClearArray& areaArray, std::size_t blockSize, uint32_t color )
328 {
329   if(mId > 0)
330   {
331     DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
332     DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
333
334     GLenum pixelFormat   = GL_RGBA;
335     GLenum pixelDataType = GL_UNSIGNED_BYTE;
336     Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
337
338     mContext.ActiveTexture(GL_TEXTURE7);  // bind in unused unit so rebind works the first time
339     mContext.Bind2dTexture(mId);
340
341     size_t numPixels = blockSize*blockSize;
342     size_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
343     char* clearPixels = (char*)malloc(numPixels * bytesPerPixel);
344
345     for(size_t i=0; i<numPixels; i++)
346     {
347       memcpy(&clearPixels[i*bytesPerPixel], &color, bytesPerPixel);
348     }
349
350     for( BitmapClearArray::const_iterator iter =  areaArray.begin(), endIter = areaArray.end(); iter != endIter ; ++iter)
351     {
352       const Vector2& clearPos((*iter));
353
354       mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
355       DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "Update x:%0.2f y:%0.2f w:%d h:%d\n",
356                      clearPos.x, clearPos.y, blockSize, blockSize );
357
358       mContext.TexSubImage2D(GL_TEXTURE_2D,
359                              0,
360                              clearPos.x,
361                              clearPos.y,
362                              blockSize,
363                              blockSize,
364                              pixelFormat,         /* our bitmap format (should match internal format) */
365                              pixelDataType,       /* pixel data type */
366                              clearPixels);        /* texture data */
367
368       INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, numPixels * bytesPerPixel );
369     }
370     free(clearPixels);
371   }
372 }
373
374 bool BitmapTexture::UpdateOnCreate()
375 {
376   return true;
377 }
378
379 bool BitmapTexture::CreateGlTexture()
380 {
381   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
382   DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
383
384   if( mBitmap )
385   {
386     const unsigned char* pixels = mBitmap->GetBuffer();
387
388     DALI_ASSERT_DEBUG(pixels != NULL);
389
390     if( NULL != pixels )
391     {
392       AssignBitmap( true, pixels );
393       mBitmap->DiscardBuffer();
394     }
395   }
396   else
397   {
398     const unsigned char* pixels = NULL;
399     std::vector<unsigned char> pixelData;
400     if( ( NULL == pixels ) && ( true == mClearPixels ) )
401     {
402       unsigned int size = mWidth * mHeight * Pixel::GetBytesPerPixel(mPixelFormat);
403       pixelData.resize(size, 0);
404       pixels = &pixelData[0];
405     }
406     AssignBitmap( true, pixels );
407   }
408
409   return mId != 0;
410 }
411
412 bool BitmapTexture::Init()
413 {
414   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
415   // mBitmap should be initialized by now
416   return (mBitmap != 0);
417 }
418
419 unsigned int BitmapTexture::GetWidth() const
420 {
421   unsigned int width = mWidth;
422   if( mBitmap )
423   {
424     width = mBitmap->GetImageWidth();
425   }
426   return width;
427 }
428
429 unsigned int BitmapTexture::GetHeight() const
430 {
431   unsigned int height = mHeight;
432   if( mBitmap )
433   {
434     height = mBitmap->GetImageHeight();
435   }
436   return height;
437 }
438
439 } //namespace Internal
440
441 } //namespace Dali