1f35368e85cd9821d3a46b85066c27125f559472
[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 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
7 //
8 //     http://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/render/gl-resources/compressed-bitmap-texture.h>
19
20 // EXTERNAL INCLUDES
21 #include <math.h>
22 #include <memory.h>
23
24 // INTERNAL INCLUDES
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>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 CompressedBitmapTexture::CompressedBitmapTexture(Internal::BitmapCompressed* const bitmap, Context& context)
39 : Texture(context,
40           bitmap->GetImageWidth(),
41           bitmap->GetImageHeight(),
42           bitmap->GetImageWidth(),
43           bitmap->GetImageHeight(),
44           bitmap->GetPixelFormat()),
45   mBitmap(bitmap)
46 {
47   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
48   DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
49 }
50
51 CompressedBitmapTexture::~CompressedBitmapTexture()
52 {
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)
56 }
57
58 bool CompressedBitmapTexture::HasAlphaChannel() const
59 {
60   return Pixel::HasAlpha(mPixelFormat);
61 }
62
63 bool CompressedBitmapTexture::IsFullyOpaque() const
64 {
65   if( mBitmap )
66   {
67     return mBitmap->IsFullyOpaque();
68   }
69   else
70   {
71     return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
72   }
73 }
74
75
76 void CompressedBitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* const pixels, const size_t bufferSize )
77 {
78   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
79   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "CompressedBitmapTexture::AssignBitmap()\n");
80
81   if( generateTexture )
82   {
83     mContext.GenTextures(1, &mId);
84   }
85   DALI_ASSERT_DEBUG( mId != 0 );
86
87   mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
88   mContext.Bind2dTexture(mId);
89
90   GLenum pixelFormat = GL_RGBA;
91   GLenum pixelDataType = GL_UNSIGNED_BYTE;
92   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
93
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);
100
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 );
104 }
105
106 void CompressedBitmapTexture::Update( Integration::Bitmap* bitmap )
107 {
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);
111
112   if( !bitmap )
113   {
114     DALI_LOG_ERROR( "Passed a null bitmap to update this compressed bitmap texture." );
115     return;
116   }
117
118   Internal::BitmapCompressed * const compressedBitmap = dynamic_cast<Dali::Internal::BitmapCompressed*>( bitmap );
119   if( compressedBitmap == 0 )
120   {
121     DALI_LOG_ERROR("CompressedBitmapTexture was passed a non-compressed bitmap to update with.\n");
122     return;
123   }
124   mBitmap = compressedBitmap;
125
126   const unsigned char * const pixels = mBitmap->GetBuffer();
127
128   DALI_ASSERT_DEBUG(pixels != NULL);
129
130   if ( NULL == pixels )
131   {
132     DALI_LOG_ERROR("Bitmap has no data\n");
133   }
134   else
135   {
136     mImageWidth  = mBitmap->GetImageWidth();
137     mImageHeight = mBitmap->GetImageHeight();
138     mWidth       = mImageWidth;
139     mHeight      = mImageHeight;
140     mPixelFormat = mBitmap->GetPixelFormat();
141
142     if ( mId ) // If the texture is already bound
143     {
144       AssignBitmap( false, pixels, mBitmap->GetBufferSize() );
145       mBitmap->DiscardBuffer();
146     }
147   }
148 }
149
150 bool CompressedBitmapTexture::UpdateOnCreate()
151 {
152   return true;
153 }
154
155 bool CompressedBitmapTexture::CreateGlTexture()
156 {
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));
159
160   if( mBitmap )
161   {
162     const unsigned char* pixels = mBitmap->GetBuffer();
163
164     DALI_ASSERT_DEBUG(pixels != NULL);
165
166     if( NULL != pixels )
167     {
168       AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
169       mBitmap->DiscardBuffer();
170     }
171   }
172   else
173   {
174     AssignBitmap( true, NULL, 0 );
175   }
176
177   return mId != 0;
178 }
179
180 bool CompressedBitmapTexture::Init()
181 {
182   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
183   // mBitmap should be initialized by now
184   return (mBitmap != 0);
185 }
186
187 unsigned int CompressedBitmapTexture::GetWidth() const
188 {
189   unsigned int width = mWidth;
190   if( mBitmap )
191   {
192     width = mBitmap->GetImageWidth();
193   }
194   return width;
195 }
196
197 unsigned int CompressedBitmapTexture::GetHeight() const
198 {
199   unsigned int height = mHeight;
200   if( mBitmap )
201   {
202     height = mBitmap->GetImageHeight();
203   }
204   return height;
205 }
206
207 } //namespace Internal
208
209 } //namespace Dali