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>
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 CompressedBitmapTexture::CompressedBitmapTexture(Internal::BitmapCompressed* const bitmap, Context& context, ResourcePolicy::Discardable discardPolicy)
36 bitmap->GetImageWidth(),
37 bitmap->GetImageHeight(),
38 bitmap->GetImageWidth(),
39 bitmap->GetImageHeight(),
40 bitmap->GetPixelFormat()),
42 mDiscardPolicy(discardPolicy)
44 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
45 DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
48 CompressedBitmapTexture::~CompressedBitmapTexture()
50 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
51 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
52 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
55 bool CompressedBitmapTexture::HasAlphaChannel() const
57 return Pixel::HasAlpha(mPixelFormat);
60 bool CompressedBitmapTexture::IsFullyOpaque() const
64 return mBitmap->IsFullyOpaque();
68 return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
73 void CompressedBitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* const pixels, const size_t bufferSize )
75 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
76 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "CompressedBitmapTexture::AssignBitmap()\n");
80 mContext.GenTextures(1, &mId);
82 DALI_ASSERT_DEBUG( mId != 0 );
84 mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
85 mContext.Bind2dTexture(mId);
87 GLenum pixelFormat = GL_RGBA;
88 GLenum pixelDataType = GL_UNSIGNED_BYTE;
89 Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
91 mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
92 mContext.CompressedTexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, bufferSize, pixels);
93 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
94 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
96 INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, bufferSize );
99 void CompressedBitmapTexture::Update( Integration::Bitmap* bitmap )
101 DALI_ASSERT_DEBUG(bitmap);
102 DALI_ASSERT_DEBUG(mImageWidth == mWidth && mImageHeight == mHeight);
103 DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "CompressedBitmapTexture::Update(bitmap:%p )\n", bitmap);
107 DALI_LOG_ERROR( "Passed a null bitmap to update this compressed bitmap texture." );
111 Internal::BitmapCompressed * const compressedBitmap = dynamic_cast<Dali::Internal::BitmapCompressed*>( bitmap );
112 if( compressedBitmap == 0 )
114 DALI_LOG_ERROR("CompressedBitmapTexture was passed a non-compressed bitmap to update with.\n");
117 mBitmap = compressedBitmap;
119 const unsigned char * const pixels = mBitmap->GetBuffer();
121 DALI_ASSERT_DEBUG(pixels != NULL);
123 if ( NULL == pixels )
125 DALI_LOG_ERROR("Bitmap has no data\n");
129 mImageWidth = mBitmap->GetImageWidth();
130 mImageHeight = mBitmap->GetImageHeight();
131 mWidth = mImageWidth;
132 mHeight = mImageHeight;
133 mPixelFormat = mBitmap->GetPixelFormat();
135 if ( mId ) // If the texture is already bound
137 AssignBitmap( false, pixels, mBitmap->GetBufferSize() );
139 if( mDiscardPolicy == ResourcePolicy::DISCARD )
141 mBitmap->DiscardBuffer();
147 bool CompressedBitmapTexture::UpdateOnCreate()
152 bool CompressedBitmapTexture::CreateGlTexture()
154 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
155 DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
159 const unsigned char* pixels = mBitmap->GetBuffer();
161 DALI_ASSERT_DEBUG(pixels != NULL);
165 AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
166 mBitmap->DiscardBuffer();
171 AssignBitmap( true, NULL, 0 );
177 bool CompressedBitmapTexture::Init()
179 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
180 // mBitmap should be initialized by now
181 return (mBitmap != 0);
184 unsigned int CompressedBitmapTexture::GetWidth() const
186 unsigned int width = mWidth;
189 width = mBitmap->GetImageWidth();
194 unsigned int CompressedBitmapTexture::GetHeight() const
196 unsigned int height = mHeight;
199 height = mBitmap->GetImageHeight();
204 } //namespace Internal