Merge "Stability/Performance for update buffer index" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / compressed-bitmap-texture.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/gl-resources/compressed-bitmap-texture.h>
20
21 // INTERNAL INCLUDES
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>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 CompressedBitmapTexture::CompressedBitmapTexture(Internal::BitmapCompressed* const bitmap, Context& context, ResourcePolicy::Discardable discardPolicy)
35 : Texture(context,
36           bitmap->GetImageWidth(),
37           bitmap->GetImageHeight(),
38           bitmap->GetImageWidth(),
39           bitmap->GetImageHeight()),
40   mBitmap(bitmap),
41   mDiscardPolicy(discardPolicy),
42   mPixelFormat( bitmap->GetPixelFormat() )
43 {
44   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
45   DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
46 }
47
48 CompressedBitmapTexture::~CompressedBitmapTexture()
49 {
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)
53 }
54
55 bool CompressedBitmapTexture::HasAlphaChannel() const
56 {
57   return Pixel::HasAlpha(mPixelFormat);
58 }
59
60 bool CompressedBitmapTexture::IsFullyOpaque() const
61 {
62   if( mBitmap )
63   {
64     return mBitmap->IsFullyOpaque();
65   }
66   else
67   {
68     return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
69   }
70 }
71
72
73 void CompressedBitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* const pixels, const size_t bufferSize )
74 {
75   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
76   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "CompressedBitmapTexture::AssignBitmap()\n");
77
78   if( generateTexture )
79   {
80     mContext.GenTextures(1, &mId);
81   }
82   DALI_ASSERT_DEBUG( mId != 0 );
83
84   mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
85   mContext.Bind2dTexture(mId);
86
87   GLenum pixelFormat = GL_RGBA;
88   GLenum pixelDataType = GL_UNSIGNED_BYTE;
89   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
90
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);
95
96 }
97
98 void CompressedBitmapTexture::Update( Integration::Bitmap* bitmap )
99 {
100   DALI_ASSERT_DEBUG(bitmap);
101   DALI_ASSERT_DEBUG(mImageWidth == mWidth && mImageHeight == mHeight);
102   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "CompressedBitmapTexture::Update(bitmap:%p )\n", bitmap);
103
104   if( !bitmap )
105   {
106     DALI_LOG_ERROR( "Passed a null bitmap to update this compressed bitmap texture." );
107     return;
108   }
109
110   Internal::BitmapCompressed * const compressedBitmap = dynamic_cast<Dali::Internal::BitmapCompressed*>( bitmap );
111   if( compressedBitmap == 0 )
112   {
113     DALI_LOG_ERROR("CompressedBitmapTexture was passed a non-compressed bitmap to update with.\n");
114     return;
115   }
116   mBitmap = compressedBitmap;
117
118   const unsigned char * const pixels = mBitmap->GetBuffer();
119
120   DALI_ASSERT_DEBUG(pixels != NULL);
121
122   if ( NULL == pixels )
123   {
124     DALI_LOG_ERROR("Bitmap has no data\n");
125   }
126   else
127   {
128     mImageWidth  = mBitmap->GetImageWidth();
129     mImageHeight = mBitmap->GetImageHeight();
130     mWidth       = mImageWidth;
131     mHeight      = mImageHeight;
132     mPixelFormat = mBitmap->GetPixelFormat();
133
134     if ( mId ) // If the texture is already bound
135     {
136       AssignBitmap( false, pixels, mBitmap->GetBufferSize() );
137     }
138   }
139 }
140
141 bool CompressedBitmapTexture::UpdateOnCreate()
142 {
143   return true;
144 }
145
146 bool CompressedBitmapTexture::CreateGlTexture()
147 {
148   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
149   DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
150
151   if( mBitmap )
152   {
153     const unsigned char* pixels = mBitmap->GetBuffer();
154
155     DALI_ASSERT_DEBUG(pixels != NULL);
156
157     if( NULL != pixels )
158     {
159       AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
160       mBitmap->DiscardBuffer();
161     }
162   }
163   else
164   {
165     AssignBitmap( true, NULL, 0 );
166   }
167
168   return mId != 0;
169 }
170
171 bool CompressedBitmapTexture::Init()
172 {
173   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
174   // mBitmap should be initialized by now
175   return (mBitmap != 0);
176 }
177
178 unsigned int CompressedBitmapTexture::GetWidth() const
179 {
180   unsigned int width = mWidth;
181   if( mBitmap )
182   {
183     width = mBitmap->GetImageWidth();
184   }
185   return width;
186 }
187
188 unsigned int CompressedBitmapTexture::GetHeight() const
189 {
190   unsigned int height = mHeight;
191   if( mBitmap )
192   {
193     height = mBitmap->GetImageHeight();
194   }
195   return height;
196 }
197
198 } //namespace Internal
199
200 } //namespace Dali