Removed legacy resource tracking / logging
[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   INCREASE_BY( PerformanceMonitor::TEXTURE_DATA_UPLOADED, bufferSize );
102 }
103
104 void CompressedBitmapTexture::Update( Integration::Bitmap* bitmap )
105 {
106   DALI_ASSERT_DEBUG(bitmap);
107   DALI_ASSERT_DEBUG(mImageWidth == mWidth && mImageHeight == mHeight);
108   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "CompressedBitmapTexture::Update(bitmap:%p )\n", bitmap);
109
110   if( !bitmap )
111   {
112     DALI_LOG_ERROR( "Passed a null bitmap to update this compressed bitmap texture." );
113     return;
114   }
115
116   Internal::BitmapCompressed * const compressedBitmap = dynamic_cast<Dali::Internal::BitmapCompressed*>( bitmap );
117   if( compressedBitmap == 0 )
118   {
119     DALI_LOG_ERROR("CompressedBitmapTexture was passed a non-compressed bitmap to update with.\n");
120     return;
121   }
122   mBitmap = compressedBitmap;
123
124   const unsigned char * const pixels = mBitmap->GetBuffer();
125
126   DALI_ASSERT_DEBUG(pixels != NULL);
127
128   if ( NULL == pixels )
129   {
130     DALI_LOG_ERROR("Bitmap has no data\n");
131   }
132   else
133   {
134     mImageWidth  = mBitmap->GetImageWidth();
135     mImageHeight = mBitmap->GetImageHeight();
136     mWidth       = mImageWidth;
137     mHeight      = mImageHeight;
138     mPixelFormat = mBitmap->GetPixelFormat();
139
140     if ( mId ) // If the texture is already bound
141     {
142       AssignBitmap( false, pixels, mBitmap->GetBufferSize() );
143       mBitmap->DiscardBuffer();
144     }
145   }
146 }
147
148 bool CompressedBitmapTexture::UpdateOnCreate()
149 {
150   return true;
151 }
152
153 bool CompressedBitmapTexture::CreateGlTexture()
154 {
155   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
156   DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
157
158   if( mBitmap )
159   {
160     const unsigned char* pixels = mBitmap->GetBuffer();
161
162     DALI_ASSERT_DEBUG(pixels != NULL);
163
164     if( NULL != pixels )
165     {
166       AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
167       mBitmap->DiscardBuffer();
168     }
169   }
170   else
171   {
172     AssignBitmap( true, NULL, 0 );
173   }
174
175   return mId != 0;
176 }
177
178 bool CompressedBitmapTexture::Init()
179 {
180   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
181   // mBitmap should be initialized by now
182   return (mBitmap != 0);
183 }
184
185 unsigned int CompressedBitmapTexture::GetWidth() const
186 {
187   unsigned int width = mWidth;
188   if( mBitmap )
189   {
190     width = mBitmap->GetImageWidth();
191   }
192   return width;
193 }
194
195 unsigned int CompressedBitmapTexture::GetHeight() const
196 {
197   unsigned int height = mHeight;
198   if( mBitmap )
199   {
200     height = mBitmap->GetImageHeight();
201   }
202   return height;
203 }
204
205 } //namespace Internal
206
207 } //namespace Dali