2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/render/gl-resources/bitmap-texture.h>
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>
34 BitmapTexture::BitmapTexture(
35 Integration::Bitmap* const bitmap,
36 const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixelsProfile,
38 ResourcePolicy::Discardable policy)
40 bitmapPackedPixelsProfile->GetBufferWidth(),
41 bitmapPackedPixelsProfile->GetBufferHeight(),
42 bitmap->GetImageWidth(),
43 bitmap->GetImageHeight(),
44 bitmap->GetPixelFormat()),
47 mDiscardPolicy(policy)
49 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
50 DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
53 BitmapTexture::BitmapTexture(
56 Pixel::Format pixelFormat,
59 ResourcePolicy::Discardable policy)
65 mClearPixels(clearPixels),
66 mDiscardPolicy(policy)
68 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
71 BitmapTexture::~BitmapTexture()
73 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
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)
79 void BitmapTexture::UploadBitmapArray( const BitmapUploadArray& bitmapArray )
81 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
88 mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
89 mContext.Bind2dTexture(mId);
90 mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
92 GLenum pixelFormat = GL_RGBA;
93 GLenum pixelDataType = GL_UNSIGNED_BYTE;
95 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
97 // go through each bitmap uploading it
99 for( BitmapUploadArray::const_iterator iter = bitmapArray.begin(), endIter = bitmapArray.end(); iter != endIter ; ++iter)
101 const BitmapUpload& bitmapItem((*iter));
103 DALI_ASSERT_ALWAYS(bitmapItem.mPixelData);
105 const unsigned char* pixels = bitmapItem.mPixelData;
107 DALI_ASSERT_DEBUG( (pixels!=NULL) && "bitmap has no data \n");
109 unsigned int bitmapWidth(bitmapItem.mWidth);
110 unsigned int bitmapHeight(bitmapItem.mHeight);
112 DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, "upload bitmap to texture :%d y:%d w:%d h:%d\n",
118 mContext.TexSubImage2D(GL_TEXTURE_2D,
119 0, /* mip map level */
120 bitmapItem.mXpos, /* X pos */
121 bitmapItem.mYpos, /* Y pos */
122 bitmapWidth, /* width */
123 bitmapHeight, /* height */
124 pixelFormat, /* our bitmap format (should match internal format) */
125 pixelDataType, /* pixel data type */
126 pixels); /* texture data */
128 if( BitmapUpload::DISCARD_PIXEL_DATA == bitmapItem.mDiscard)
130 delete [] bitmapItem.mPixelData;
135 bool BitmapTexture::HasAlphaChannel() const
137 return Pixel::HasAlpha(mPixelFormat);
140 bool BitmapTexture::IsFullyOpaque() const
144 return mBitmap->IsFullyOpaque();
148 return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
152 // Bitmap buffer has been changed. Upload changes to GPU.
153 void BitmapTexture::AreaUpdated( const RectArea& updateArea, const unsigned char* pixels )
155 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
156 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
158 GLenum pixelFormat = GL_RGBA;
159 GLenum pixelDataType = GL_UNSIGNED_BYTE;
160 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
162 mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
164 mContext.Bind2dTexture(mId);
166 if( ! updateArea.IsEmpty() )
168 mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
169 DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "Update x:%d y:%d w:%d h:%d\n",
170 updateArea.x, updateArea.y, updateArea.width ,updateArea.height );
172 // TODO: obtain pitch of source image, obtain pixel depth of source image.
173 const unsigned int pitchPixels = mImageWidth;
174 const unsigned int pixelDepth = sizeof(unsigned int);
176 // If the width of the source update area is the same as the pitch, then can
177 // copy the contents in a single contiguous TexSubImage call.
178 if(updateArea.x == 0 && updateArea.width == pitchPixels)
180 pixels += updateArea.y * pitchPixels * pixelDepth;
181 mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, updateArea.y,
182 updateArea.width, updateArea.height,
183 pixelFormat, pixelDataType, pixels );
187 // Otherwise the source buffer needs to be copied line at a time, as OpenGL ES
188 // does not support source strides. (no GL_UNPACK_ROW_LENGTH supported)
189 unsigned int yBottom = updateArea.y + updateArea.height;
190 pixels += (updateArea.y * pitchPixels + updateArea.x) * pixelDepth;
192 for(unsigned int y = updateArea.y; y < yBottom; y++)
194 mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, y,
196 pixelFormat, pixelDataType, pixels );
197 pixels += pitchPixels * pixelDepth;
201 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED,
202 updateArea.Area()* GetBytesPerPixel( mPixelFormat ));
207 void BitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* pixels )
209 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
210 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AssignBitmap()\n");
212 GLenum pixelFormat = GL_RGBA;
213 GLenum pixelDataType = GL_UNSIGNED_BYTE;
215 if( generateTexture )
217 mContext.GenTextures(1, &mId);
219 DALI_ASSERT_DEBUG( mId != 0 );
221 mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
222 mContext.Bind2dTexture(mId);
223 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
225 mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
226 mContext.TexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, pixelFormat, pixelDataType, pixels);
227 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
228 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
230 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, GetBytesPerPixel(mPixelFormat) * mWidth * mHeight );
233 void BitmapTexture::Update( Integration::Bitmap* bitmap )
235 DALI_LOG_INFO( Debug::Filter::gGLResource, Debug::General, "BitmapTexture::Update(bitmap:%p )\n", bitmap );
236 DALI_ASSERT_DEBUG( bitmap != 0 );
239 DALI_LOG_ERROR( "Passed a null bitmap to update this bitmap texture." );
243 // Only Packed-pixel bitmaps are ever associated with BitmapTextures, so we should never be passed any other kind:
244 const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixels = bitmap->GetPackedPixelsProfile();
245 DALI_ASSERT_DEBUG(bitmapPackedPixels);
246 if( !bitmapPackedPixels )
248 ///! This should never happen.
249 DALI_LOG_ERROR("Passed an incompatible bitmap type to update this bitmap texture.");
254 const unsigned char* pixels = mBitmap->GetBuffer();
256 // We should never have null pixel data here - resource manager has deliberately loaded/reloaded data
258 DALI_ASSERT_DEBUG( pixels != NULL );
262 DALI_LOG_ERROR("BitmapTexture::Upload() - Bitmap has no pixel data.\n");
266 if( mImageWidth == mBitmap->GetImageWidth() &&
267 mImageHeight == mBitmap->GetImageHeight() &&
268 mWidth == bitmapPackedPixels->GetBufferWidth() &&
269 mHeight == bitmapPackedPixels->GetBufferHeight() &&
270 mPixelFormat == mBitmap->GetPixelFormat() ) // and size hasn't changed
272 RectArea area(0, 0, mImageWidth, mImageHeight); // just update whole texture
273 AreaUpdated( area, pixels );
274 DiscardBitmapBuffer();
276 else // 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();
284 AssignBitmap( false, pixels );
289 void BitmapTexture::UpdateArea( const RectArea& updateArea )
291 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "BitmapTexture::UpdateArea()\n");
295 const unsigned char* pixels = mBitmap->GetBuffer();
297 // Pixel data could be null if we've uploaded to GL and discarded the data.
301 if( mId ) // If the texture is already bound
303 if( updateArea.IsEmpty() )
308 area.width = mImageWidth;
309 area.height = mImageHeight;
310 AreaUpdated( area, pixels );
314 AreaUpdated( updateArea, pixels );
321 void BitmapTexture::ClearAreas( const BitmapClearArray& areaArray, std::size_t blockSize, uint32_t color )
325 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
326 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
328 GLenum pixelFormat = GL_RGBA;
329 GLenum pixelDataType = GL_UNSIGNED_BYTE;
330 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
332 mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
333 mContext.Bind2dTexture(mId);
335 size_t numPixels = blockSize*blockSize;
336 size_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
337 char* clearPixels = (char*)malloc(numPixels * bytesPerPixel);
339 for(size_t i=0; i<numPixels; i++)
341 memcpy(&clearPixels[i*bytesPerPixel], &color, bytesPerPixel);
344 for( BitmapClearArray::const_iterator iter = areaArray.begin(), endIter = areaArray.end(); iter != endIter ; ++iter)
346 const Vector2& clearPos((*iter));
348 mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
349 DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "Update x:%0.2f y:%0.2f w:%d h:%d\n",
350 clearPos.x, clearPos.y, blockSize, blockSize );
352 mContext.TexSubImage2D(GL_TEXTURE_2D,
358 pixelFormat, /* our bitmap format (should match internal format) */
359 pixelDataType, /* pixel data type */
360 clearPixels); /* texture data */
362 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, numPixels * bytesPerPixel );
368 bool BitmapTexture::UpdateOnCreate()
373 bool BitmapTexture::CreateGlTexture()
375 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
376 DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "BitmapTexture::CreateGlTexture() Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
380 const unsigned char* pixels = mBitmap->GetBuffer();
382 // pixel data could be NULL here if we've had a context loss and we previously discarded
383 // the pixel data on the previous upload. If it is null, then we shouldn't generate a
384 // new GL Texture; leaving mId as zero. Eventually, the bitmap will get reloaded,
385 // and pixels will become non-null.
389 AssignBitmap( true, pixels );
390 DiscardBitmapBuffer();
395 const unsigned char* pixels = NULL;
396 std::vector<unsigned char> pixelData;
397 if( ( NULL == pixels ) && ( true == mClearPixels ) )
399 unsigned int size = mWidth * mHeight * Pixel::GetBytesPerPixel(mPixelFormat);
400 pixelData.resize(size, 0);
401 pixels = &pixelData[0];
403 AssignBitmap( true, pixels );
409 bool BitmapTexture::Init()
411 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
412 // mBitmap should be initialized by now
413 return (mBitmap != 0);
416 unsigned int BitmapTexture::GetWidth() const
418 unsigned int width = mWidth;
421 width = mBitmap->GetImageWidth();
426 unsigned int BitmapTexture::GetHeight() const
428 unsigned int height = mHeight;
431 height = mBitmap->GetImageHeight();
436 void BitmapTexture::DiscardBitmapBuffer()
438 DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, "BitmapTexture::DiscardBitmapBuffer() DiscardPolicy: %s\n", mDiscardPolicy == ResourcePolicy::DISCARD?"DISCARD":"RETAIN");
440 if( ResourcePolicy::DISCARD == mDiscardPolicy )
442 DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, " Discarding bitmap\n");
443 mBitmap->DiscardBuffer();
448 } //namespace Internal