2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/render/gl-resources/bitmap-texture.h>
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>
38 BitmapTexture::BitmapTexture(Integration::Bitmap* const bitmap, const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixelsProfile, Context& context)
40 bitmapPackedPixelsProfile->GetBufferWidth(),
41 bitmapPackedPixelsProfile->GetBufferHeight(),
42 bitmap->GetImageWidth(),
43 bitmap->GetImageHeight(),
44 bitmap->GetPixelFormat()),
48 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
49 DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
52 BitmapTexture::BitmapTexture(
55 Pixel::Format pixelFormat,
63 mClearPixels(clearPixels)
65 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
68 BitmapTexture::~BitmapTexture()
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)
75 void BitmapTexture::UploadBitmapArray( const BitmapUploadArray& bitmapArray )
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
86 GLenum pixelFormat = GL_RGBA;
87 GLenum pixelDataType = GL_UNSIGNED_BYTE;
89 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
91 // go through each bitmap uploading it
93 for( BitmapUploadArray::const_iterator iter = bitmapArray.begin(), endIter = bitmapArray.end(); iter != endIter ; ++iter)
95 const BitmapUpload& bitmapItem((*iter));
97 DALI_ASSERT_ALWAYS(bitmapItem.mPixelData);
99 const unsigned char* pixels = bitmapItem.mPixelData;
101 DALI_ASSERT_DEBUG( (pixels!=NULL) && "bitmap has no data \n");
103 unsigned int bitmapWidth(bitmapItem.mWidth);
104 unsigned int bitmapHeight(bitmapItem.mHeight);
106 DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, "upload bitmap to texture :%d y:%d w:%d h:%d\n",
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 */
122 if( BitmapUpload::DISCARD_PIXEL_DATA == bitmapItem.mDiscard)
124 delete [] bitmapItem.mPixelData;
129 bool BitmapTexture::HasAlphaChannel() const
131 return Pixel::HasAlpha(mPixelFormat);
134 bool BitmapTexture::IsFullyOpaque() const
138 return mBitmap->IsFullyOpaque();
142 return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
146 // Bitmap buffer has been changed. Upload changes to GPU.
147 void BitmapTexture::AreaUpdated( const RectArea& updateArea, const unsigned char* pixels )
149 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
150 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
152 GLenum pixelFormat = GL_RGBA;
153 GLenum pixelDataType = GL_UNSIGNED_BYTE;
154 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
156 mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
158 mContext.Bind2dTexture(mId);
160 if( ! updateArea.IsEmpty() )
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 );
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);
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)
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 );
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;
186 for(unsigned int y = updateArea.y; y < yBottom; y++)
188 mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, y,
190 pixelFormat, pixelDataType, pixels );
191 pixels += pitchPixels * pixelDepth;
195 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED,
196 updateArea.Area()* GetBytesPerPixel( mPixelFormat ));
201 void BitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* pixels )
203 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
204 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AssignBitmap()\n");
206 GLenum pixelFormat = GL_RGBA;
207 GLenum pixelDataType = GL_UNSIGNED_BYTE;
209 if( generateTexture )
211 mContext.GenTextures(1, &mId);
213 DALI_ASSERT_DEBUG( mId != 0 );
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);
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);
226 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, GetBytesPerPixel(mPixelFormat) * mWidth * mHeight );
229 void BitmapTexture::Update( Integration::Bitmap* bitmap )
231 DALI_LOG_INFO( Debug::Filter::gGLResource, Debug::General, "BitmapTexture::Update(bitmap:%p )\n", bitmap );
232 DALI_ASSERT_DEBUG( bitmap != 0 );
235 DALI_LOG_ERROR( "Passed a null bitmap to update this bitmap texture." );
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 )
244 ///! This should never happen.
245 DALI_LOG_ERROR("Passed an incompatible bitmap type to update this bitmap texture.");
250 const unsigned char* pixels = mBitmap->GetBuffer();
252 DALI_ASSERT_DEBUG( pixels != NULL );
254 if ( NULL == pixels )
256 DALI_LOG_ERROR("bitmap has no data\n");
257 GlCleanup(); // Note, We suicide here in the case of bad input.
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
267 if ( mId ) // If the texture is already bound
269 RectArea area(0, 0, mImageWidth, mImageHeight); // just update whole texture
270 AreaUpdated( area, pixels );
271 mBitmap->DiscardBuffer();
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();
282 if ( mId ) // If the texture is already bound
284 AssignBitmap( false, pixels );
285 mBitmap->DiscardBuffer();
291 void BitmapTexture::UpdateArea( const RectArea& updateArea )
293 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "BitmapTexture::UpdateArea()\n");
297 const unsigned char* pixels = mBitmap->GetBuffer();
298 if ( NULL == pixels )
300 DALI_LOG_ERROR("bitmap has no data\n");
301 GlCleanup(); ///!ToDo: Why do we suicide in the case of bad input?
305 if ( mId ) // If the texture is already bound
307 if( updateArea.IsEmpty() )
312 area.width = mImageWidth;
313 area.height = mImageHeight;
314 AreaUpdated( area, pixels );
318 AreaUpdated( updateArea, pixels );
325 void BitmapTexture::ClearAreas( const BitmapClearArray& areaArray, std::size_t blockSize, uint32_t color )
329 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
330 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
332 GLenum pixelFormat = GL_RGBA;
333 GLenum pixelDataType = GL_UNSIGNED_BYTE;
334 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
336 mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
337 mContext.Bind2dTexture(mId);
339 size_t numPixels = blockSize*blockSize;
340 size_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
341 char* clearPixels = (char*)malloc(numPixels * bytesPerPixel);
343 for(size_t i=0; i<numPixels; i++)
345 memcpy(&clearPixels[i*bytesPerPixel], &color, bytesPerPixel);
348 for( BitmapClearArray::const_iterator iter = areaArray.begin(), endIter = areaArray.end(); iter != endIter ; ++iter)
350 const Vector2& clearPos((*iter));
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 );
356 mContext.TexSubImage2D(GL_TEXTURE_2D,
362 pixelFormat, /* our bitmap format (should match internal format) */
363 pixelDataType, /* pixel data type */
364 clearPixels); /* texture data */
366 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, numPixels * bytesPerPixel );
372 bool BitmapTexture::UpdateOnCreate()
377 bool BitmapTexture::CreateGlTexture()
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));
384 const unsigned char* pixels = mBitmap->GetBuffer();
386 DALI_ASSERT_DEBUG(pixels != NULL);
390 AssignBitmap( true, pixels );
391 mBitmap->DiscardBuffer();
396 const unsigned char* pixels = NULL;
397 std::vector<unsigned char> pixelData;
398 if( ( NULL == pixels ) && ( true == mClearPixels ) )
400 unsigned int size = mWidth * mHeight * Pixel::GetBytesPerPixel(mPixelFormat);
401 pixelData.resize(size, 0);
402 pixels = &pixelData[0];
404 AssignBitmap( true, pixels );
410 bool BitmapTexture::Init()
412 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
413 // mBitmap should be initialized by now
414 return (mBitmap != 0);
417 unsigned int BitmapTexture::GetWidth() const
419 unsigned int width = mWidth;
422 width = mBitmap->GetImageWidth();
427 unsigned int BitmapTexture::GetHeight() const
429 unsigned int height = mHeight;
432 height = mBitmap->GetImageHeight();
437 } //namespace Internal