2 * Copyright (c) 2016 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/bitmap-packed-pixel.h>
28 #include <dali/internal/event/resources/resource-client.h>
29 #include <dali/integration-api/bitmap.h>
30 #include <dali/integration-api/platform-abstraction.h>
41 TypeRegistration mType( typeid( Dali::Atlas ), typeid( Dali::Image ), NULL );
44 Atlas* Atlas::New( SizeType width,
46 Pixel::Format pixelFormat,
49 return new Atlas( width, height, pixelFormat, recoverContext );
52 void Atlas::Clear(const Vector4& color)
55 ClearBackground( color );
58 bool Atlas::Upload( BufferImage& bufferImage,
62 bool uploadSuccess( false );
64 if( IsInside( xOffset + bufferImage.GetWidth(), yOffset + bufferImage.GetHeight() ) )
67 ResourceId destId = GetResourceId();
71 bufferImage.UploadBitmap( destId, xOffset, yOffset );
79 bool Atlas::Upload( const std::string& url,
83 bool uploadSuccess( false );
85 Integration::BitmapPtr bitmap = LoadBitmap( url );
87 if( bitmap && IsInside( xOffset + bitmap->GetImageWidth(), yOffset + bitmap->GetImageHeight()) )
90 ResourceId destId = GetResourceId();
93 mResourceClient.UploadBitmap( destId, bitmap, xOffset, yOffset );
98 mTiles.PushBack( new Tile(xOffset, yOffset, url) );
102 return uploadSuccess;
105 bool Atlas::Upload( PixelDataPtr pixelData,
109 bool uploadSuccess( false );
110 if( IsInside( xOffset + pixelData->GetWidth(), yOffset + pixelData->GetHeight() ) )
113 ResourceId destId = GetResourceId();
117 mResourceClient.UploadBitmap( destId, pixelData, xOffset, yOffset );
118 uploadSuccess = true;
122 return uploadSuccess;
125 void Atlas::RecoverFromContextLoss()
127 ResourceId destId = GetResourceId();
132 ClearBackground( mClearColor );
135 if( mRecoverContext )
137 // Restore the atlas by re-uploading the url resources
138 Vector< Tile* >::ConstIterator end = mTiles.End();
139 for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
141 Integration::BitmapPtr bitmap = LoadBitmap( (*iter)->url );
142 mResourceClient.UploadBitmap( destId, bitmap, (*iter)->xOffset, (*iter)->yOffset );
153 Atlas::Atlas( SizeType width,
155 Pixel::Format pixelFormat,
156 bool recoverContext )
157 : mResourceClient( ThreadLocalStorage::Get().GetResourceClient() ),
158 mClearColor( Vector4::ZERO ),
159 mPixelFormat( pixelFormat ),
161 mRecoverContext( recoverContext )
167 void Atlas::Connect()
171 if( mConnectionCount == 1 )
177 void Atlas::Disconnect()
179 if( mConnectionCount )
183 // Only release the atlas upon destruction
187 bool Atlas::IsInside( SizeType x, SizeType y )
191 if( x <= mWidth && y <= mHeight )
197 DALI_LOG_ERROR( "image does not fit within the atlas \n" );
203 void Atlas::AllocateAtlas()
207 mTicket = mResourceClient.AllocateTexture( mWidth, mHeight, mPixelFormat );
208 mTicket->AddObserver( *this );
212 void Atlas::ReleaseAtlas()
218 void Atlas::ClearBackground(const Vector4& color )
221 ResourceId destId = GetResourceId();
224 const unsigned int numPixels = mWidth * mHeight;
225 unsigned int bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
226 BufferImagePtr imageData = BufferImage::New( mWidth, mHeight, mPixelFormat );
227 PixelBuffer* pixbuf = imageData->GetBuffer();
231 // converting color value from float 0.f~1.f to byte 0~255
232 unsigned char r = static_cast<unsigned char>( 255.f * Clamp( color.r, 0.f, 1.f ) );
233 unsigned char g = static_cast<unsigned char>( 255.f * Clamp( color.g, 0.f, 1.f ) );
234 unsigned char b = static_cast<unsigned char>( 255.f * Clamp( color.b, 0.f, 1.f ) );
235 unsigned char a = static_cast<unsigned char>( 255.f * Clamp( color.a, 0.f, 1.f ) );
236 if( mPixelFormat == Pixel::RGBA8888 )
238 // For little-endian byte order, the RGBA channels needs to be reversed for bit shifting.
239 uint32_t clearColor = ( (uint32_t) a<<24 | (uint32_t)b << 16 | (uint32_t)g << 8 | (uint32_t)r );
240 uint32_t* buf = (uint32_t *) pixbuf;
241 for( unsigned int i = 0; i < numPixels; ++i )
246 else if( mPixelFormat == Pixel::RGB888 )
248 for( unsigned int i = 0; i < numPixels; ++i )
250 pixbuf[i*bytesPerPixel] = r;
251 pixbuf[i*bytesPerPixel+1] = g;
252 pixbuf[i*bytesPerPixel+2] = b;
255 else if( mPixelFormat == Pixel::A8 )
257 memset( pixbuf, a, numPixels );
264 imageData->UploadBitmap( destId, 0, 0 );
268 void Atlas::ClearCache()
270 Vector< Tile* >::ConstIterator end = mTiles.End();
271 for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
278 Integration::BitmapPtr Atlas::LoadBitmap( const std::string& url )
280 Integration::BitmapResourceType resourceType;
281 Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
283 Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously(resourceType, url);
284 Integration::BitmapPtr bitmap = static_cast<Integration::Bitmap*>( resource.Get());
289 } // namespace Internal