[3.0] Remove/move experimental features
[platform/core/uifw/dali-core.git] / dali / internal / event / images / atlas-impl.cpp
1 /*
2  * Copyright (c) 2016 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     // Only release the atlas upon destruction
186   }
187 }
188
189 bool Atlas::IsInside( SizeType x, SizeType y )
190 {
191   bool fit(false);
192
193   if( x <= mWidth  && y <= mHeight )
194   {
195     fit = true;
196   }
197   else
198   {
199     DALI_LOG_ERROR( "image does not fit within the atlas \n" );
200   }
201
202   return fit;
203 }
204
205 void Atlas::AllocateAtlas()
206 {
207   if( !mTicket )
208   {
209     mTicket = mResourceClient.AllocateTexture( mWidth, mHeight, mPixelFormat );
210     mTicket->AddObserver( *this );
211     mImageFactory.RegisterForContextRecovery( this );
212   }
213 }
214
215 void Atlas::ReleaseAtlas()
216 {
217   mTicket.Reset();
218   ClearCache();
219   mImageFactory.UnregisterFromContextRecovery( this );
220 }
221
222 void Atlas::ClearBackground(const Vector4& color )
223 {
224   AllocateAtlas();
225   ResourceId destId = GetResourceId();
226   if( destId )
227   {
228     const unsigned int numPixels = mWidth * mHeight;
229     unsigned int bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
230     BufferImagePtr imageData = BufferImage::New( mWidth, mHeight, mPixelFormat );
231     PixelBuffer* pixbuf = imageData->GetBuffer();
232
233     if( pixbuf )
234     {
235       // converting color value from float 0.f~1.f to byte 0~255
236       unsigned char r =  static_cast<unsigned char>( 255.f * Clamp( color.r, 0.f, 1.f ) );
237       unsigned char g =  static_cast<unsigned char>( 255.f * Clamp( color.g, 0.f, 1.f ) );
238       unsigned char b =  static_cast<unsigned char>( 255.f * Clamp( color.b, 0.f, 1.f ) );
239       unsigned char a =  static_cast<unsigned char>( 255.f * Clamp( color.a, 0.f, 1.f ) );
240       if( mPixelFormat == Pixel::RGBA8888 )
241       {
242         // For little-endian byte order, the RGBA channels needs to be reversed for bit shifting.
243         uint32_t clearColor = ( (uint32_t) a<<24 | (uint32_t)b << 16 | (uint32_t)g << 8 | (uint32_t)r );
244         uint32_t* buf = (uint32_t *) pixbuf;
245         for( unsigned int i = 0; i < numPixels; ++i )
246         {
247           buf[i] = clearColor;
248         }
249       }
250       else if( mPixelFormat == Pixel::RGB888 )
251       {
252         for( unsigned int i = 0; i < numPixels; ++i )
253         {
254           pixbuf[i*bytesPerPixel] = r;
255           pixbuf[i*bytesPerPixel+1] = g;
256           pixbuf[i*bytesPerPixel+2] = b;
257         }
258       }
259       else if( mPixelFormat == Pixel::A8 )
260       {
261         memset( pixbuf, a, numPixels );
262       }
263     }
264
265     mClearColor = color;
266     mClear = true;
267
268     imageData->UploadBitmap( destId, 0, 0 );
269   }
270 }
271
272 void Atlas::ClearCache()
273 {
274   Vector< Tile* >::ConstIterator end = mTiles.End();
275   for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
276   {
277     delete *iter;
278   }
279   mTiles.Clear();
280 }
281
282 Integration::BitmapPtr Atlas::LoadBitmap( const std::string& url )
283 {
284   Integration::BitmapResourceType resourceType;
285   Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
286
287   Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously(resourceType, url);
288   Integration::BitmapPtr bitmap = static_cast<Integration::Bitmap*>( resource.Get());
289
290   return bitmap;
291 }
292
293 } // namespace Internal
294
295 } // namespace Dali