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