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 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, bufferSize );
104 void CompressedBitmapTexture::Update( Integration::Bitmap* bitmap )
106 DALI_ASSERT_DEBUG(bitmap);
107 DALI_ASSERT_DEBUG(mImageWidth == mWidth && mImageHeight == mHeight);
108 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "CompressedBitmapTexture::Update(bitmap:%p )\n", bitmap);
112 DALI_LOG_ERROR( "Passed a null bitmap to update this compressed bitmap texture." );
116 Internal::BitmapCompressed * const compressedBitmap = dynamic_cast<Dali::Internal::BitmapCompressed*>( bitmap );
117 if( compressedBitmap == 0 )
119 DALI_LOG_ERROR("CompressedBitmapTexture was passed a non-compressed bitmap to update with.\n");
122 mBitmap = compressedBitmap;
124 const unsigned char * const pixels = mBitmap->GetBuffer();
126 DALI_ASSERT_DEBUG(pixels != NULL);
128 if ( NULL == pixels )
130 DALI_LOG_ERROR("Bitmap has no data\n");
134 mImageWidth = mBitmap->GetImageWidth();
135 mImageHeight = mBitmap->GetImageHeight();
136 mWidth = mImageWidth;
137 mHeight = mImageHeight;
138 mPixelFormat = mBitmap->GetPixelFormat();
140 if ( mId ) // If the texture is already bound
142 AssignBitmap( false, pixels, mBitmap->GetBufferSize() );
143 mBitmap->DiscardBuffer();
148 bool CompressedBitmapTexture::UpdateOnCreate()
153 bool CompressedBitmapTexture::CreateGlTexture()
155 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
156 DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
160 const unsigned char* pixels = mBitmap->GetBuffer();
162 DALI_ASSERT_DEBUG(pixels != NULL);
166 AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
167 mBitmap->DiscardBuffer();
172 AssignBitmap( true, NULL, 0 );
178 bool CompressedBitmapTexture::Init()
180 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
181 // mBitmap should be initialized by now
182 return (mBitmap != 0);
185 unsigned int CompressedBitmapTexture::GetWidth() const
187 unsigned int width = mWidth;
190 width = mBitmap->GetImageWidth();
195 unsigned int CompressedBitmapTexture::GetHeight() const
197 unsigned int height = mHeight;
200 height = mBitmap->GetImageHeight();
205 } //namespace Internal