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 <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/common/thread-local-storage.h>
25 #include <dali/internal/event/images/image-factory.h>
26 #include <dali/internal/event/resources/resource-client.h>
27 #include <dali/integration-api/bitmap.h>
28 #include <dali/integration-api/platform-abstraction.h>
30 #include <string.h> // for memset()
40 TypeRegistration mType( typeid( Dali::Atlas ), typeid( Dali::Image ), NULL );
43 Atlas* Atlas::New( SizeType width,
45 Pixel::Format pixelFormat,
48 return new Atlas( width, height, pixelFormat, recoverContext );
51 void Atlas::Clear(const Vector4& color)
54 ClearBackground( color );
57 bool Atlas::Upload( BufferImage& bufferImage,
61 bool uploadSuccess( false );
63 if( Compatible(bufferImage.GetPixelFormat(),
64 xOffset + bufferImage.GetWidth(),
65 yOffset + bufferImage.GetHeight() ) )
68 ResourceId destId = GetResourceId();
69 ResourceId srcId = bufferImage.GetResourceId();
73 mResourceClient.UploadBitmap( destId, srcId, xOffset, yOffset );
81 bool Atlas::Upload( const std::string& url,
85 bool uploadSuccess( false );
87 Integration::BitmapPtr bitmap = LoadBitmap( url );
89 if( bitmap && Compatible(bitmap->GetPixelFormat(), xOffset + bitmap->GetImageWidth(), yOffset + bitmap->GetImageHeight()) )
92 ResourceId destId = GetResourceId();
95 mResourceClient.UploadBitmap( destId, bitmap, xOffset, yOffset );
100 mTiles.PushBack( new Tile(xOffset, yOffset, url) );
104 return uploadSuccess;
107 void Atlas::RecoverFromContextLoss()
109 ResourceId destId = GetResourceId();
114 ClearBackground( mClearColor );
117 if( mRecoverContext )
119 // Restore the atlas by re-uploading the url resources
120 Vector< Tile* >::ConstIterator end = mTiles.End();
121 for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
123 Integration::BitmapPtr bitmap = LoadBitmap( (*iter)->url );
124 mResourceClient.UploadBitmap( destId, bitmap, (*iter)->xOffset, (*iter)->yOffset );
135 Atlas::Atlas( SizeType width,
137 Pixel::Format pixelFormat,
138 bool recoverContext )
139 : mResourceClient( ThreadLocalStorage::Get().GetResourceClient() ),
140 mImageFactory( ThreadLocalStorage::Get().GetImageFactory() ),
141 mClearColor( Vector4::ZERO ),
142 mPixelFormat( pixelFormat ),
144 mRecoverContext( recoverContext )
150 void Atlas::Connect()
154 if( mConnectionCount == 1 )
160 void Atlas::Disconnect()
162 if( mConnectionCount )
166 if ( Dali::Image::UNUSED == mReleasePolicy &&
167 mConnectionCount == 0 )
174 bool Atlas::Compatible( Pixel::Format pixelFormat,
178 bool Compatible(false);
180 if( mPixelFormat != pixelFormat )
182 DALI_LOG_ERROR( "Pixel format %d does not match Atlas format %d\n", pixelFormat, mPixelFormat );
186 if( x <= mWidth && y <= mHeight )
192 DALI_LOG_ERROR( "image does not fit within the atlas \n" );
199 void Atlas::AllocateAtlas()
203 mTicket = mResourceClient.AllocateTexture( mWidth, mHeight, mPixelFormat );
204 mImageFactory.RegisterForContextRecovery( this );
208 void Atlas::ReleaseAtlas()
212 mImageFactory.UnregisterFromContextRecovery( this );
215 void Atlas::ClearBackground(const Vector4& color )
218 ResourceId destId = GetResourceId();
221 const unsigned int numPixels = mWidth * mHeight;
222 unsigned int bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
223 BufferImagePtr imageData = BufferImage::New( mWidth, mHeight, mPixelFormat );
224 PixelBuffer* pixbuf = imageData->GetBuffer();
226 // converting color value from float 0.f~1.f to byte 0~255
227 unsigned char r = static_cast<unsigned char>( 255.f * Clamp( color.r, 0.f, 1.f ) );
228 unsigned char g = static_cast<unsigned char>( 255.f * Clamp( color.g, 0.f, 1.f ) );
229 unsigned char b = static_cast<unsigned char>( 255.f * Clamp( color.b, 0.f, 1.f ) );
230 unsigned char a = static_cast<unsigned char>( 255.f * Clamp( color.a, 0.f, 1.f ) );
231 if( mPixelFormat == Pixel::RGBA8888 )
233 // For little-endian byte order, the RGBA channels needs to be reversed for bit shifting.
234 uint32_t clearColor = ( (uint32_t) a<<24 | (uint32_t)b << 16 | (uint32_t)g << 8 | (uint32_t)r );
235 uint32_t* buf = (uint32_t *) pixbuf;
236 for( unsigned int i = 0; i < numPixels; ++i )
241 else if( mPixelFormat == Pixel::RGB888 )
243 for( unsigned int i = 0; i < numPixels; ++i )
245 pixbuf[i*bytesPerPixel] = r;
246 pixbuf[i*bytesPerPixel+1] = g;
247 pixbuf[i*bytesPerPixel+2] = b;
250 else if( mPixelFormat == Pixel::A8 )
252 memset( pixbuf, a, numPixels );
256 imageData->Update(area);
260 mResourceClient.UploadBitmap( destId, imageData->GetResourceId(), 0, 0 );
264 void Atlas::ClearCache()
266 Vector< Tile* >::ConstIterator end = mTiles.End();
267 for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
274 Integration::BitmapPtr Atlas::LoadBitmap( const std::string& url )
276 ImageAttributes loadedAttrs;
277 Integration::BitmapResourceType resourceType( loadedAttrs );
278 Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
280 Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously(resourceType, url);
281 Integration::BitmapPtr bitmap = static_cast<Integration::Bitmap*>( resource.Get());
286 } // namespace Internal