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