Merge "Klockwork: remove unreachable code, check iterators" into tizen
[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, ResourcePolicy::Discardable discardPolicy)
40 : Texture(context,
41           bitmap->GetImageWidth(),
42           bitmap->GetImageHeight(),
43           bitmap->GetImageWidth(),
44           bitmap->GetImageHeight(),
45           bitmap->GetPixelFormat()),
46   mBitmap(bitmap),
47   mDiscardPolicy(discardPolicy)
48 {
49   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
50   DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
51 }
52
53 CompressedBitmapTexture::~CompressedBitmapTexture()
54 {
55   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
56   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
57   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
58 }
59
60 bool CompressedBitmapTexture::HasAlphaChannel() const
61 {
62   return Pixel::HasAlpha(mPixelFormat);
63 }
64
65 bool CompressedBitmapTexture::IsFullyOpaque() const
66 {
67   if( mBitmap )
68   {
69     return mBitmap->IsFullyOpaque();
70   }
71   else
72   {
73     return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
74   }
75 }
76
77
78 void CompressedBitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* const pixels, const size_t bufferSize )
79 {
80   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
81   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "CompressedBitmapTexture::AssignBitmap()\n");
82
83   if( generateTexture )
84   {
85     mContext.GenTextures(1, &mId);
86   }
87   DALI_ASSERT_DEBUG( mId != 0 );
88
89   mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
90   mContext.Bind2dTexture(mId);
91
92   GLenum pixelFormat = GL_RGBA;
93   GLenum pixelDataType = GL_UNSIGNED_BYTE;
94   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
95
96   mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
97   mContext.CompressedTexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, bufferSize, pixels);
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
144       if( mDiscardPolicy == ResourcePolicy::DISCARD )
145       {
146         mBitmap->DiscardBuffer();
147       }
148     }
149   }
150 }
151
152 bool CompressedBitmapTexture::UpdateOnCreate()
153 {
154   return true;
155 }
156
157 bool CompressedBitmapTexture::CreateGlTexture()
158 {
159   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
160   DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
161
162   if( mBitmap )
163   {
164     const unsigned char* pixels = mBitmap->GetBuffer();
165
166     DALI_ASSERT_DEBUG(pixels != NULL);
167
168     if( NULL != pixels )
169     {
170       AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
171       mBitmap->DiscardBuffer();
172     }
173   }
174   else
175   {
176     AssignBitmap( true, NULL, 0 );
177   }
178
179   return mId != 0;
180 }
181
182 bool CompressedBitmapTexture::Init()
183 {
184   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
185   // mBitmap should be initialized by now
186   return (mBitmap != 0);
187 }
188
189 unsigned int CompressedBitmapTexture::GetWidth() const
190 {
191   unsigned int width = mWidth;
192   if( mBitmap )
193   {
194     width = mBitmap->GetImageWidth();
195   }
196   return width;
197 }
198
199 unsigned int CompressedBitmapTexture::GetHeight() const
200 {
201   unsigned int height = mHeight;
202   if( mBitmap )
203   {
204     height = mBitmap->GetImageHeight();
205   }
206   return height;
207 }
208
209 } //namespace Internal
210
211 } //namespace Dali