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/compressed-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 CompressedBitmapTexture::CompressedBitmapTexture(Internal::BitmapCompressed* const bitmap, Context& context)
40 bitmap->GetImageWidth(),
41 bitmap->GetImageHeight(),
42 bitmap->GetImageWidth(),
43 bitmap->GetImageHeight(),
44 bitmap->GetPixelFormat()),
47 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
48 DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
51 CompressedBitmapTexture::~CompressedBitmapTexture()
53 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
54 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
55 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
58 bool CompressedBitmapTexture::HasAlphaChannel() const
60 return Pixel::HasAlpha(mPixelFormat);
63 bool CompressedBitmapTexture::IsFullyOpaque() const
67 return mBitmap->IsFullyOpaque();
71 return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
76 void CompressedBitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* const pixels, const size_t bufferSize )
78 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
79 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "CompressedBitmapTexture::AssignBitmap()\n");
83 mContext.GenTextures(1, &mId);
85 DALI_ASSERT_DEBUG( mId != 0 );
87 mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
88 mContext.Bind2dTexture(mId);
90 GLenum pixelFormat = GL_RGBA;
91 GLenum pixelDataType = GL_UNSIGNED_BYTE;
92 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
94 mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
95 mContext.CompressedTexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, bufferSize, pixels);
96 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
97 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
98 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
99 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
101 DALI_LOG_RESOURCE("[UPLOAD] Uploaded image data from Bitmap %p to Texture %d - size %d bytes (%dx%d)\n",
102 mBitmap.Get(), mId, bufferSize, mWidth, mHeight);
103 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, bufferSize );
106 void CompressedBitmapTexture::Update( Integration::Bitmap* bitmap )
108 DALI_ASSERT_DEBUG(bitmap);
109 DALI_ASSERT_DEBUG(mImageWidth == mWidth && mImageHeight == mHeight);
110 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "CompressedBitmapTexture::Update(bitmap:%p )\n", bitmap);
114 DALI_LOG_ERROR( "Passed a null bitmap to update this compressed bitmap texture." );
118 Internal::BitmapCompressed * const compressedBitmap = dynamic_cast<Dali::Internal::BitmapCompressed*>( bitmap );
119 if( compressedBitmap == 0 )
121 DALI_LOG_ERROR("CompressedBitmapTexture was passed a non-compressed bitmap to update with.\n");
124 mBitmap = compressedBitmap;
126 const unsigned char * const pixels = mBitmap->GetBuffer();
128 DALI_ASSERT_DEBUG(pixels != NULL);
130 if ( NULL == pixels )
132 DALI_LOG_ERROR("Bitmap has no data\n");
136 mImageWidth = mBitmap->GetImageWidth();
137 mImageHeight = mBitmap->GetImageHeight();
138 mWidth = mImageWidth;
139 mHeight = mImageHeight;
140 mPixelFormat = mBitmap->GetPixelFormat();
142 if ( mId ) // If the texture is already bound
144 AssignBitmap( false, pixels, mBitmap->GetBufferSize() );
145 mBitmap->DiscardBuffer();
150 bool CompressedBitmapTexture::UpdateOnCreate()
155 bool CompressedBitmapTexture::CreateGlTexture()
157 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
158 DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
162 const unsigned char* pixels = mBitmap->GetBuffer();
164 DALI_ASSERT_DEBUG(pixels != NULL);
168 AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
169 mBitmap->DiscardBuffer();
174 AssignBitmap( true, NULL, 0 );
180 bool CompressedBitmapTexture::Init()
182 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
183 // mBitmap should be initialized by now
184 return (mBitmap != 0);
187 unsigned int CompressedBitmapTexture::GetWidth() const
189 unsigned int width = mWidth;
192 width = mBitmap->GetImageWidth();
197 unsigned int CompressedBitmapTexture::GetHeight() const
199 unsigned int height = mHeight;
202 height = mBitmap->GetImageHeight();
207 } //namespace Internal