Cleaning up some dead code with geometry scaling, rendering type declarations and...
[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/performance-monitor.h>
24 #include <dali/internal/render/gl-resources/context.h>
25 #include <dali/internal/render/gl-resources/texture-units.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 CompressedBitmapTexture::CompressedBitmapTexture(Internal::BitmapCompressed* const bitmap, Context& context, ResourcePolicy::Discardable discardPolicy)
34 : Texture(context,
35           bitmap->GetImageWidth(),
36           bitmap->GetImageHeight(),
37           bitmap->GetImageWidth(),
38           bitmap->GetImageHeight()),
39   mBitmap(bitmap),
40   mDiscardPolicy(discardPolicy),
41   mPixelFormat( bitmap->GetPixelFormat() )
42 {
43   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
44   DALI_LOG_SET_OBJECT_STRING(this, DALI_LOG_GET_OBJECT_STRING(bitmap));
45 }
46
47 CompressedBitmapTexture::~CompressedBitmapTexture()
48 {
49   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
50   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
51   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
52 }
53
54 bool CompressedBitmapTexture::HasAlphaChannel() const
55 {
56   return Pixel::HasAlpha(mPixelFormat);
57 }
58
59 bool CompressedBitmapTexture::IsFullyOpaque() const
60 {
61   if( mBitmap )
62   {
63     return mBitmap->IsFullyOpaque();
64   }
65   else
66   {
67     return ! HasAlphaChannel(); // Todo: amalgamate updated bitmap's IsFullyOpaque()
68   }
69 }
70
71
72 void CompressedBitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* const pixels, const size_t bufferSize )
73 {
74   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
75   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::Verbose, "CompressedBitmapTexture::AssignBitmap()\n");
76
77   if( generateTexture )
78   {
79     mContext.GenTextures(1, &mId);
80   }
81   DALI_ASSERT_DEBUG( mId != 0 );
82
83   mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
84   mContext.Bind2dTexture(mId);
85
86   GLenum pixelFormat = GL_RGBA;
87   GLenum pixelDataType = GL_UNSIGNED_BYTE;
88   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
89
90   mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
91   mContext.CompressedTexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, bufferSize, pixels);
92   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
93   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
94
95 }
96
97 void CompressedBitmapTexture::Update( Integration::Bitmap* bitmap )
98 {
99   DALI_ASSERT_DEBUG(bitmap);
100   DALI_ASSERT_DEBUG(mImageWidth == mWidth && mImageHeight == mHeight);
101   DALI_LOG_INFO(Debug::Filter::gGLResource, Debug::General, "CompressedBitmapTexture::Update(bitmap:%p )\n", bitmap);
102
103   if( !bitmap )
104   {
105     DALI_LOG_ERROR( "Passed a null bitmap to update this compressed bitmap texture." );
106     return;
107   }
108
109   Internal::BitmapCompressed * const compressedBitmap = dynamic_cast<Dali::Internal::BitmapCompressed*>( bitmap );
110   if( compressedBitmap == 0 )
111   {
112     DALI_LOG_ERROR("CompressedBitmapTexture was passed a non-compressed bitmap to update with.\n");
113     return;
114   }
115   mBitmap = compressedBitmap;
116
117   const unsigned char * const pixels = mBitmap->GetBuffer();
118
119   DALI_ASSERT_DEBUG(pixels != NULL);
120
121   if ( NULL == pixels )
122   {
123     DALI_LOG_ERROR("Bitmap has no data\n");
124   }
125   else
126   {
127     mImageWidth  = mBitmap->GetImageWidth();
128     mImageHeight = mBitmap->GetImageHeight();
129     mWidth       = mImageWidth;
130     mHeight      = mImageHeight;
131     mPixelFormat = mBitmap->GetPixelFormat();
132
133     if ( mId ) // If the texture is already bound
134     {
135       AssignBitmap( false, pixels, mBitmap->GetBufferSize() );
136     }
137   }
138 }
139
140 bool CompressedBitmapTexture::UpdateOnCreate()
141 {
142   return true;
143 }
144
145 bool CompressedBitmapTexture::CreateGlTexture()
146 {
147   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
148   DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Bitmap: %s\n", DALI_LOG_GET_OBJECT_C_STR(this));
149
150   if( mBitmap )
151   {
152     const unsigned char* pixels = mBitmap->GetBuffer();
153
154     DALI_ASSERT_DEBUG(pixels != NULL);
155
156     if( NULL != pixels )
157     {
158       AssignBitmap( true, pixels, mBitmap->GetBufferSize() );
159       mBitmap->DiscardBuffer();
160     }
161   }
162   else
163   {
164     AssignBitmap( true, NULL, 0 );
165   }
166
167   return mId != 0;
168 }
169
170 bool CompressedBitmapTexture::Init()
171 {
172   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
173   // mBitmap should be initialized by now
174   return (mBitmap != 0);
175 }
176
177 unsigned int CompressedBitmapTexture::GetWidth() const
178 {
179   unsigned int width = mWidth;
180   if( mBitmap )
181   {
182     width = mBitmap->GetImageWidth();
183   }
184   return width;
185 }
186
187 unsigned int CompressedBitmapTexture::GetHeight() const
188 {
189   unsigned int height = mHeight;
190   if( mBitmap )
191   {
192     height = mBitmap->GetImageHeight();
193   }
194   return height;
195 }
196
197 } //namespace Internal
198
199 } //namespace Dali