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/compressed-bitmap-texture.h>
26 #include <dali/public-api/math/rect.h>
27 #include <dali/public-api/math/math-utils.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/internal/render/common/vertex.h>
30 #include <dali/internal/render/common/performance-monitor.h>
31 #include <dali/internal/render/gl-resources/context.h>
39 CompressedBitmapTexture::CompressedBitmapTexture(Internal::BitmapCompressed* const bitmap, Context& context)
41 bitmap->GetImageWidth(),
42 bitmap->GetImageHeight(),
43 bitmap->GetImageWidth(),
44 bitmap->GetImageHeight(),
45 bitmap->GetPixelFormat()),
48 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
49 DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
52 CompressedBitmapTexture::~CompressedBitmapTexture()
54 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
55 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
56 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
59 bool CompressedBitmapTexture::HasAlphaChannel() const
61 return Pixel::HasAlpha(mPixelFormat);
64 bool CompressedBitmapTexture::IsFullyOpaque() const
68 return mBitmap->IsFullyOpaque();
72 return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
77 void CompressedBitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* const pixels, const size_t bufferSize )
79 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
80 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "CompressedBitmapTexture::AssignBitmap()\n");
84 mContext.GenTextures(1, &mId);
86 DALI_ASSERT_DEBUG( mId != 0 );
88 mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
89 mContext.Bind2dTexture(mId);
91 GLenum pixelFormat = GL_RGBA;
92 GLenum pixelDataType = GL_UNSIGNED_BYTE;
93 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
95 mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
96 mContext.CompressedTexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, bufferSize, pixels);
97 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
98 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
99 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
100 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
102 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, bufferSize );
105 void CompressedBitmapTexture::Update( Integration::Bitmap* bitmap )
107 DALI_ASSERT_DEBUG(bitmap);
108 DALI_ASSERT_DEBUG(mImageWidth == mWidth && mImageHeight == mHeight);
109 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "CompressedBitmapTexture::Update(bitmap:%p )\n", bitmap);
113 DALI_LOG_ERROR( "Passed a null bitmap to update this compressed bitmap texture." );
117 Internal::BitmapCompressed * const compressedBitmap = dynamic_cast<Dali::Internal::BitmapCompressed*>( bitmap );
118 if( compressedBitmap == 0 )
120 DALI_LOG_ERROR("CompressedBitmapTexture was passed a non-compressed bitmap to update with.\n");
123 mBitmap = compressedBitmap;
125 const unsigned char * const pixels = mBitmap->GetBuffer();
127 DALI_ASSERT_DEBUG(pixels != NULL);
129 if ( NULL == pixels )
131 DALI_LOG_ERROR("Bitmap has no data\n");
135 mImageWidth = mBitmap->GetImageWidth();
136 mImageHeight = mBitmap->GetImageHeight();
137 mWidth = mImageWidth;
138 mHeight = mImageHeight;
139 mPixelFormat = mBitmap->GetPixelFormat();
141 if ( mId ) // If the texture is already bound
143 AssignBitmap( false, pixels, mBitmap->GetBufferSize() );
144 mBitmap->DiscardBuffer();
149 bool CompressedBitmapTexture::UpdateOnCreate()
154 bool CompressedBitmapTexture::CreateGlTexture()
156 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
157 DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
161 const unsigned char* pixels = mBitmap->GetBuffer();
163 DALI_ASSERT_DEBUG(pixels != NULL);
167 AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
168 mBitmap->DiscardBuffer();
173 AssignBitmap( true, NULL, 0 );
179 bool CompressedBitmapTexture::Init()
181 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
182 // mBitmap should be initialized by now
183 return (mBitmap != 0);
186 unsigned int CompressedBitmapTexture::GetWidth() const
188 unsigned int width = mWidth;
191 width = mBitmap->GetImageWidth();
196 unsigned int CompressedBitmapTexture::GetHeight() const
198 unsigned int height = mHeight;
201 height = mBitmap->GetImageHeight();
206 } //namespace Internal