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/public-api/common/dali-vector.h>
23 #include <dali/integration-api/debug.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>
35 BitmapTexture::BitmapTexture(
36 Integration::Bitmap* const bitmap,
37 const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixelsProfile,
39 ResourcePolicy::Discardable policy)
41 bitmapPackedPixelsProfile->GetBufferWidth(),
42 bitmapPackedPixelsProfile->GetBufferHeight(),
43 bitmap->GetImageWidth(),
44 bitmap->GetImageHeight()),
47 mDiscardPolicy(policy),
48 mPixelFormat(bitmap->GetPixelFormat())
50 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
51 DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
54 BitmapTexture::BitmapTexture(
57 Pixel::Format pixelFormat,
60 ResourcePolicy::Discardable policy)
65 mClearPixels(clearPixels),
66 mDiscardPolicy(policy),
67 mPixelFormat( pixelFormat )
69 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
72 BitmapTexture::~BitmapTexture()
74 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
76 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
77 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
80 bool BitmapTexture::HasAlphaChannel() const
82 return Pixel::HasAlpha(mPixelFormat);
85 bool BitmapTexture::IsFullyOpaque() const
89 return mBitmap->IsFullyOpaque();
93 return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
97 // Bitmap buffer has been changed. Upload changes to GPU.
98 void BitmapTexture::AreaUpdated( const RectArea& updateArea, const unsigned char* pixels )
100 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
101 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AreaUpdated()\n");
103 GLenum pixelFormat = GL_RGBA;
104 GLenum pixelDataType = GL_UNSIGNED_BYTE;
105 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
107 mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
109 mContext.Bind2dTexture(mId);
111 if( ! updateArea.IsEmpty() )
113 mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
114 DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "Update x:%d y:%d w:%d h:%d\n",
115 updateArea.x, updateArea.y, updateArea.width ,updateArea.height );
117 const unsigned int pitchPixels = mWidth;
118 const unsigned int pixelDepth = Dali::Pixel::GetBytesPerPixel( mPixelFormat );
120 // If the width of the source update area is the same as the pitch, then can
121 // copy the contents in a single contiguous TexSubImage call.
122 if(updateArea.x == 0 && updateArea.width == pitchPixels)
124 pixels += updateArea.y * pitchPixels * pixelDepth;
125 mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, updateArea.y,
126 updateArea.width, updateArea.height,
127 pixelFormat, pixelDataType, pixels );
131 // Otherwise the source buffer needs to be copied line at a time, as OpenGL ES
132 // does not support source strides. (no GL_UNPACK_ROW_LENGTH supported)
133 unsigned int yBottom = updateArea.y + updateArea.height;
134 pixels += (updateArea.y * pitchPixels + updateArea.x) * pixelDepth;
136 for(unsigned int y = updateArea.y; y < yBottom; y++)
138 mContext.TexSubImage2D( GL_TEXTURE_2D,0, updateArea.x, y,
140 pixelFormat, pixelDataType, pixels );
141 pixels += pitchPixels * pixelDepth;
145 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED,
146 updateArea.Area()* GetBytesPerPixel( mPixelFormat ));
150 void BitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* pixels )
152 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
153 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "BitmapTexture::AssignBitmap()\n");
155 GLenum pixelFormat = GL_RGBA;
156 GLenum pixelDataType = GL_UNSIGNED_BYTE;
158 if( generateTexture )
160 mContext.GenTextures(1, &mId);
162 DALI_ASSERT_DEBUG( mId != 0 );
164 mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
165 mContext.Bind2dTexture(mId);
166 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
168 mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
169 mContext.TexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, pixelFormat, pixelDataType, pixels);
170 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
171 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
173 // If the resource policy is to discard on upload then release buffer
174 DiscardBitmapBuffer();
178 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, GetBytesPerPixel(mPixelFormat) * mWidth * mHeight );
182 void BitmapTexture::Update( Integration::Bitmap* bitmap )
184 DALI_LOG_INFO( Debug::Filter::gGLResource, Debug::General, "BitmapTexture::Update(bitmap:%p )\n", bitmap );
185 DALI_ASSERT_DEBUG( bitmap != 0 );
188 DALI_LOG_ERROR( "Passed a null bitmap to update this bitmap texture." );
192 // Only Packed-pixel bitmaps are ever associated with BitmapTextures, so we should never be passed any other kind:
193 const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixels = bitmap->GetPackedPixelsProfile();
194 DALI_ASSERT_DEBUG(bitmapPackedPixels);
195 if( !bitmapPackedPixels )
197 ///! This should never happen.
198 DALI_LOG_ERROR("Passed an incompatible bitmap type to update this bitmap texture.");
203 const unsigned char* pixels = mBitmap->GetBuffer();
205 // We should never have null pixel data here - resource manager has deliberately loaded/reloaded data
207 DALI_ASSERT_DEBUG( pixels != NULL );
211 DALI_LOG_ERROR("BitmapTexture::Upload() - Bitmap has no pixel data.\n");
215 if( mImageWidth == mBitmap->GetImageWidth() &&
216 mImageHeight == mBitmap->GetImageHeight() &&
217 mWidth == bitmapPackedPixels->GetBufferWidth() &&
218 mHeight == bitmapPackedPixels->GetBufferHeight() &&
219 mPixelFormat == mBitmap->GetPixelFormat() ) // and size hasn't changed
221 RectArea area(0, 0, mImageWidth, mImageHeight); // just update whole texture
222 AreaUpdated( area, pixels );
223 DiscardBitmapBuffer();
225 else // Otherwise, reload the pixel data
227 mImageWidth = mBitmap->GetImageWidth();
228 mImageHeight = mBitmap->GetImageHeight();
229 mWidth = bitmapPackedPixels->GetBufferWidth();
230 mHeight = bitmapPackedPixels->GetBufferHeight();
231 mPixelFormat = mBitmap->GetPixelFormat();
233 AssignBitmap( false, pixels );
238 void BitmapTexture::Update( Integration::Bitmap* srcBitmap, std::size_t xOffset, std::size_t yOffset )
240 if( NULL != srcBitmap )
242 Update( srcBitmap->GetBuffer(), srcBitmap->GetImageWidth(), srcBitmap->GetImageHeight(),
243 srcBitmap->GetPixelFormat(), xOffset, yOffset);
247 void BitmapTexture::Update( PixelData* srcPixelData, std::size_t xOffset, std::size_t yOffset )
249 if( NULL != srcPixelData )
251 Update( srcPixelData->GetBuffer(), srcPixelData->GetWidth(), srcPixelData->GetHeight(),
252 srcPixelData->GetPixelFormat(), xOffset, yOffset);
256 void BitmapTexture::Update( const unsigned char* pixels, std::size_t width, std::size_t height, Pixel::Format pixelFormat, std::size_t xOffset, std::size_t yOffset )
259 GLenum pixelGLFormat = GL_RGBA;
260 GLenum pixelDataType = GL_UNSIGNED_BYTE;
261 Integration::ConvertToGlFormat( mPixelFormat, pixelDataType, pixelGLFormat );
265 mContext.GenTextures( 1, &mId );
267 mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
268 mContext.Bind2dTexture( mId );
269 mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 );
271 mContext.TexImage2D( GL_TEXTURE_2D, 0, pixelGLFormat, mWidth, mHeight, 0, pixelGLFormat, pixelDataType, NULL );
272 mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
273 mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
277 mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
278 mContext.Bind2dTexture( mId );
279 mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 );
282 #if DALI_GLES_VERSION >= 30
283 // for gles 3.0, uploading sub-image with different format is a valid operation
284 Integration::ConvertToGlFormat( srcBitmap->GetPixelFormat(), pixelDataType, pixelGLFormat );
286 // allows RGB888 source bitmap to be added to RGBA8888 texture, need to convert the bitmap format manually
287 if(pixelFormat == Pixel::RGB888 && mPixelFormat == Pixel::RGBA8888 )
289 std::size_t size = width * height;
291 Vector<PixelBuffer> tempBuffer;
292 tempBuffer.Reserve( size*4u );
293 PixelBuffer* data = tempBuffer.Begin();
295 for( std::size_t i=0u; i<size; i++ )
297 data[i*4u] = pixels[i*3u];
298 data[i*4u+1] = pixels[i*3u+1];
299 data[i*4u+2] = pixels[i*3u+2];
303 mContext.TexSubImage2D( GL_TEXTURE_2D, 0,
306 pixelGLFormat, pixelDataType,
313 mContext.TexSubImage2D( GL_TEXTURE_2D, 0,
316 pixelGLFormat, pixelDataType,
320 void BitmapTexture::UpdateArea( const RectArea& updateArea )
322 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "BitmapTexture::UpdateArea()\n");
326 const unsigned char* pixels = mBitmap->GetBuffer();
328 // Pixel data could be null if we've uploaded to GL and discarded the data.
332 if( mId ) // If the texture is already bound
334 if( updateArea.IsEmpty() )
339 area.width = mImageWidth;
340 area.height = mImageHeight;
341 AreaUpdated( area, pixels );
345 AreaUpdated( updateArea, pixels );
352 bool BitmapTexture::UpdateOnCreate()
357 bool BitmapTexture::CreateGlTexture()
359 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
360 DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "BitmapTexture::CreateGlTexture() Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
364 const unsigned char* pixels = mBitmap->GetBuffer();
366 // pixel data could be NULL here if we've had a context loss and we previously discarded
367 // the pixel data on the previous upload. If it is null, then we shouldn't generate a
368 // new GL Texture; leaving mId as zero. Eventually, the bitmap will get reloaded,
369 // and pixels will become non-null.
373 AssignBitmap( true, pixels );
374 DiscardBitmapBuffer();
379 const unsigned char* pixels = NULL;
380 Dali::Vector<unsigned char> pixelData; // Okay to create outside branch as empty vector has no heap allocation.
381 if( true == mClearPixels )
383 unsigned int size = mWidth * mHeight * Pixel::GetBytesPerPixel( mPixelFormat );
384 pixelData.Resize( size, 0 );
385 pixels = &pixelData[0];
387 AssignBitmap( true, pixels );
393 bool BitmapTexture::Init()
395 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
396 // mBitmap should be initialized by now
397 return (mBitmap != 0);
400 unsigned int BitmapTexture::GetWidth() const
402 unsigned int width = mWidth;
405 width = mBitmap->GetImageWidth();
410 unsigned int BitmapTexture::GetHeight() const
412 unsigned int height = mHeight;
415 height = mBitmap->GetImageHeight();
420 void BitmapTexture::DiscardBitmapBuffer()
422 DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, "BitmapTexture::DiscardBitmapBuffer() DiscardPolicy: %s\n", mDiscardPolicy == ResourcePolicy::OWNED_DISCARD?"DISCARD":"RETAIN");
424 if( ResourcePolicy::OWNED_DISCARD == mDiscardPolicy )
426 DALI_LOG_INFO(Debug::Filter::gImage, Debug::General, " Discarding bitmap\n");
429 mBitmap->DiscardBuffer();
435 } //namespace Internal