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