8cc31630064c32c9146fc5c40c760a9ceaa625ea
[platform/core/uifw/dali-core.git] / dali / internal / event / images / atlas-impl.cpp
1 /*
2  * Copyright (c) 2015 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/event/images/atlas-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for memset()
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/images/image-factory.h>
28 #include <dali/internal/event/images/bitmap-packed-pixel.h>
29 #include <dali/internal/event/resources/resource-client.h>
30 #include <dali/integration-api/bitmap.h>
31 #include <dali/integration-api/platform-abstraction.h>
32
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42 TypeRegistration mType( typeid( Dali::Atlas ), typeid( Dali::Image ), NULL );
43 }
44
45 Atlas* Atlas::New( SizeType width,
46                    SizeType height,
47                    Pixel::Format pixelFormat,
48                    bool recoverContext)
49 {
50   return new Atlas( width, height, pixelFormat, recoverContext );
51 }
52
53 void Atlas::Clear(const Vector4& color)
54 {
55   ClearCache();
56   ClearBackground( color );
57 }
58
59 bool Atlas::Upload( BufferImage& bufferImage,
60                     SizeType xOffset,
61                     SizeType yOffset )
62 {
63   bool uploadSuccess( false );
64
65   if( IsInside( xOffset + bufferImage.GetWidth(),  yOffset + bufferImage.GetHeight() ) )
66   {
67     AllocateAtlas();
68     ResourceId destId = GetResourceId();
69
70     if( destId )
71     {
72       bufferImage.UploadBitmap( destId, xOffset, yOffset );
73       uploadSuccess = true;
74     }
75   }
76
77   return uploadSuccess;
78 }
79
80 bool Atlas::Upload( const std::string& url,
81                     SizeType xOffset,
82                     SizeType yOffset)
83 {
84   bool uploadSuccess( false );
85
86   Integration::BitmapPtr bitmap = LoadBitmap( url );
87
88   if( bitmap && IsInside( xOffset + bitmap->GetImageWidth(), yOffset + bitmap->GetImageHeight()) )
89   {
90     AllocateAtlas();
91     ResourceId destId = GetResourceId();
92     if( destId )
93     {
94       mResourceClient.UploadBitmap( destId, bitmap, xOffset, yOffset  );
95       uploadSuccess = true;
96
97       if( mRecoverContext )
98       {
99         mTiles.PushBack( new Tile(xOffset, yOffset, url) );
100       }
101     }
102   }
103   return uploadSuccess;
104 }
105
106 bool Atlas::Upload( PixelDataPtr pixelData,
107                     SizeType xOffset,
108                     SizeType yOffset )
109 {
110   bool uploadSuccess( false );
111   if( IsInside( xOffset + pixelData->GetWidth(),  yOffset + pixelData->GetHeight() ) )
112   {
113     AllocateAtlas();
114     ResourceId destId = GetResourceId();
115
116     if( destId )
117     {
118       mResourceClient.UploadBitmap( destId, pixelData, xOffset, yOffset  );
119       uploadSuccess = true;
120     }
121   }
122
123   return uploadSuccess;
124 }
125
126 void Atlas::RecoverFromContextLoss()
127 {
128   ResourceId destId = GetResourceId();
129   if( destId )
130   {
131     if( mClear )
132     {
133       ClearBackground( mClearColor );
134     }
135
136     if( mRecoverContext )
137     {
138       // Restore the atlas by re-uploading the url resources
139       Vector< Tile* >::ConstIterator end = mTiles.End();
140       for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
141       {
142         Integration::BitmapPtr bitmap = LoadBitmap( (*iter)->url );
143         mResourceClient.UploadBitmap( destId, bitmap, (*iter)->xOffset, (*iter)->yOffset  );
144       }
145     }
146   }
147 }
148
149 Atlas::~Atlas()
150 {
151   ReleaseAtlas();
152 }
153
154 Atlas::Atlas( SizeType width,
155               SizeType height,
156               Pixel::Format pixelFormat,
157               bool recoverContext )
158 : mResourceClient( ThreadLocalStorage::Get().GetResourceClient() ),
159   mImageFactory( ThreadLocalStorage::Get().GetImageFactory() ),
160   mClearColor( Vector4::ZERO ),
161   mPixelFormat( pixelFormat ),
162   mClear( false ),
163   mRecoverContext( recoverContext )
164 {
165   mWidth  = width;
166   mHeight = height;
167 }
168
169 void Atlas::Connect()
170 {
171   ++mConnectionCount;
172
173   if( mConnectionCount == 1 )
174   {
175     AllocateAtlas();
176   }
177 }
178
179 void Atlas::Disconnect()
180 {
181   if( mConnectionCount )
182   {
183     --mConnectionCount;
184
185     if ( Dali::Image::UNUSED == mReleasePolicy &&
186          mConnectionCount == 0 )
187     {
188       ReleaseAtlas();
189     }
190   }
191 }
192
193 bool Atlas::IsInside( SizeType x, SizeType y )
194 {
195   bool fit(false);
196
197   if( x <= mWidth  && y <= mHeight )
198   {
199     fit = true;
200   }
201   else
202   {
203     DALI_LOG_ERROR( "image does not fit within the atlas \n" );
204   }
205
206   return fit;
207 }
208
209 void Atlas::AllocateAtlas()
210 {
211   if( !mTicket )
212   {
213     mTicket = mResourceClient.AllocateTexture( mWidth, mHeight, mPixelFormat );
214     mTicket->AddObserver( *this );
215     mImageFactory.RegisterForContextRecovery( this );
216   }
217 }
218
219 void Atlas::ReleaseAtlas()
220 {
221   mTicket.Reset();
222   ClearCache();
223   mImageFactory.UnregisterFromContextRecovery( this );
224 }
225
226 void Atlas::ClearBackground(const Vector4& color )
227 {
228   AllocateAtlas();
229   ResourceId destId = GetResourceId();
230   if( destId )
231   {
232     const unsigned int numPixels = mWidth * mHeight;
233     unsigned int bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
234     BufferImagePtr imageData = BufferImage::New( mWidth, mHeight, mPixelFormat );
235     PixelBuffer* pixbuf = imageData->GetBuffer();
236
237     if( pixbuf )
238     {
239       // converting color value from float 0.f~1.f to byte 0~255
240       unsigned char r =  static_cast<unsigned char>( 255.f * Clamp( color.r, 0.f, 1.f ) );
241       unsigned char g =  static_cast<unsigned char>( 255.f * Clamp( color.g, 0.f, 1.f ) );
242       unsigned char b =  static_cast<unsigned char>( 255.f * Clamp( color.b, 0.f, 1.f ) );
243       unsigned char a =  static_cast<unsigned char>( 255.f * Clamp( color.a, 0.f, 1.f ) );
244       if( mPixelFormat == Pixel::RGBA8888 )
245       {
246         // For little-endian byte order, the RGBA channels needs to be reversed for bit shifting.
247         uint32_t clearColor = ( (uint32_t) a<<24 | (uint32_t)b << 16 | (uint32_t)g << 8 | (uint32_t)r );
248         uint32_t* buf = (uint32_t *) pixbuf;
249         for( unsigned int i = 0; i < numPixels; ++i )
250         {
251           buf[i] = clearColor;
252         }
253       }
254       else if( mPixelFormat == Pixel::RGB888 )
255       {
256         for( unsigned int i = 0; i < numPixels; ++i )
257         {
258           pixbuf[i*bytesPerPixel] = r;
259           pixbuf[i*bytesPerPixel+1] = g;
260           pixbuf[i*bytesPerPixel+2] = b;
261         }
262       }
263       else if( mPixelFormat == Pixel::A8 )
264       {
265         memset( pixbuf, a, numPixels );
266       }
267     }
268
269     mClearColor = color;
270     mClear = true;
271
272     imageData->UploadBitmap( destId, 0, 0 );
273   }
274 }
275
276 void Atlas::ClearCache()
277 {
278   Vector< Tile* >::ConstIterator end = mTiles.End();
279   for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
280   {
281     delete *iter;
282   }
283   mTiles.Clear();
284 }
285
286 Integration::BitmapPtr Atlas::LoadBitmap( const std::string& url )
287 {
288   Integration::BitmapResourceType resourceType;
289   Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
290
291   Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously(resourceType, url);
292   Integration::BitmapPtr bitmap = static_cast<Integration::Bitmap*>( resource.Get());
293
294   return bitmap;
295 }
296
297 } // namespace Internal
298
299 } // namespace Dali