2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/images/atlas-impl.h>
22 #include <cstring> // for memset()
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>
42 TypeRegistration mType( typeid( Dali::Atlas ), typeid( Dali::Image ), NULL );
45 Atlas* Atlas::New( SizeType width,
47 Pixel::Format pixelFormat,
50 return new Atlas( width, height, pixelFormat, recoverContext );
53 void Atlas::Clear(const Vector4& color)
56 ClearBackground( color );
59 bool Atlas::Upload( BufferImage& bufferImage,
63 bool uploadSuccess( false );
65 if( IsInside( xOffset + bufferImage.GetWidth(), yOffset + bufferImage.GetHeight() ) )
68 ResourceId destId = GetResourceId();
72 bufferImage.UploadBitmap( destId, xOffset, yOffset );
80 bool Atlas::Upload( const std::string& url,
84 bool uploadSuccess( false );
86 Integration::BitmapPtr bitmap = LoadBitmap( url );
88 if( bitmap && IsInside( xOffset + bitmap->GetImageWidth(), yOffset + bitmap->GetImageHeight()) )
91 ResourceId destId = GetResourceId();
94 mResourceClient.UploadBitmap( destId, bitmap, xOffset, yOffset );
99 mTiles.PushBack( new Tile(xOffset, yOffset, url) );
103 return uploadSuccess;
106 bool Atlas::Upload( PixelDataPtr pixelData,
110 bool uploadSuccess( false );
111 if( IsInside( xOffset + pixelData->GetWidth(), yOffset + pixelData->GetHeight() ) )
114 ResourceId destId = GetResourceId();
118 mResourceClient.UploadBitmap( destId, pixelData, xOffset, yOffset );
119 uploadSuccess = true;
123 return uploadSuccess;
126 void Atlas::RecoverFromContextLoss()
128 ResourceId destId = GetResourceId();
133 ClearBackground( mClearColor );
136 if( mRecoverContext )
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++ )
142 Integration::BitmapPtr bitmap = LoadBitmap( (*iter)->url );
143 mResourceClient.UploadBitmap( destId, bitmap, (*iter)->xOffset, (*iter)->yOffset );
154 Atlas::Atlas( SizeType width,
156 Pixel::Format pixelFormat,
157 bool recoverContext )
158 : mResourceClient( ThreadLocalStorage::Get().GetResourceClient() ),
159 mImageFactory( ThreadLocalStorage::Get().GetImageFactory() ),
160 mClearColor( Vector4::ZERO ),
161 mPixelFormat( pixelFormat ),
163 mRecoverContext( recoverContext )
169 void Atlas::Connect()
173 if( mConnectionCount == 1 )
179 void Atlas::Disconnect()
181 if( mConnectionCount )
185 if ( Dali::Image::UNUSED == mReleasePolicy &&
186 mConnectionCount == 0 )
193 bool Atlas::IsInside( SizeType x, SizeType y )
197 if( x <= mWidth && y <= mHeight )
203 DALI_LOG_ERROR( "image does not fit within the atlas \n" );
209 void Atlas::AllocateAtlas()
213 mTicket = mResourceClient.AllocateTexture( mWidth, mHeight, mPixelFormat );
214 mTicket->AddObserver( *this );
215 mImageFactory.RegisterForContextRecovery( this );
219 void Atlas::ReleaseAtlas()
223 mImageFactory.UnregisterFromContextRecovery( this );
226 void Atlas::ClearBackground(const Vector4& color )
229 ResourceId destId = GetResourceId();
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();
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 )
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 )
254 else if( mPixelFormat == Pixel::RGB888 )
256 for( unsigned int i = 0; i < numPixels; ++i )
258 pixbuf[i*bytesPerPixel] = r;
259 pixbuf[i*bytesPerPixel+1] = g;
260 pixbuf[i*bytesPerPixel+2] = b;
263 else if( mPixelFormat == Pixel::A8 )
265 memset( pixbuf, a, numPixels );
272 imageData->UploadBitmap( destId, 0, 0 );
276 void Atlas::ClearCache()
278 Vector< Tile* >::ConstIterator end = mTiles.End();
279 for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
286 Integration::BitmapPtr Atlas::LoadBitmap( const std::string& url )
288 Integration::BitmapResourceType resourceType;
289 Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
291 Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously(resourceType, url);
292 Integration::BitmapPtr bitmap = static_cast<Integration::Bitmap*>( resource.Get());
297 } // namespace Internal