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/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( Compatible(bufferImage.GetPixelFormat(),
65 xOffset + bufferImage.GetWidth(),
66 yOffset + bufferImage.GetHeight() ) )
69 ResourceId destId = GetResourceId();
70 ResourceId srcId = bufferImage.GetResourceId();
74 mResourceClient.UploadBitmap( destId, srcId, xOffset, yOffset );
82 bool Atlas::Upload( const std::string& url,
86 bool uploadSuccess( false );
88 Integration::BitmapPtr bitmap = LoadBitmap( url );
90 if( bitmap && Compatible(bitmap->GetPixelFormat(), xOffset + bitmap->GetImageWidth(), yOffset + bitmap->GetImageHeight()) )
93 ResourceId destId = GetResourceId();
96 mResourceClient.UploadBitmap( destId, bitmap, xOffset, yOffset );
101 mTiles.PushBack( new Tile(xOffset, yOffset, url) );
105 return uploadSuccess;
108 void Atlas::RecoverFromContextLoss()
110 ResourceId destId = GetResourceId();
115 ClearBackground( mClearColor );
118 if( mRecoverContext )
120 // Restore the atlas by re-uploading the url resources
121 Vector< Tile* >::ConstIterator end = mTiles.End();
122 for( Vector<Tile*>::Iterator iter = mTiles.Begin(); iter != end; iter++ )
124 Integration::BitmapPtr bitmap = LoadBitmap( (*iter)->url );
125 mResourceClient.UploadBitmap( destId, bitmap, (*iter)->xOffset, (*iter)->yOffset );
136 Atlas::Atlas( SizeType width,
138 Pixel::Format pixelFormat,
139 bool recoverContext )
140 : mResourceClient( ThreadLocalStorage::Get().GetResourceClient() ),
141 mImageFactory( ThreadLocalStorage::Get().GetImageFactory() ),
142 mClearColor( Vector4::ZERO ),
143 mPixelFormat( pixelFormat ),
145 mRecoverContext( recoverContext )
151 void Atlas::Connect()
155 if( mConnectionCount == 1 )
161 void Atlas::Disconnect()
163 if( mConnectionCount )
167 if ( Dali::Image::UNUSED == mReleasePolicy &&
168 mConnectionCount == 0 )
175 bool Atlas::Compatible( Pixel::Format pixelFormat,
179 bool Compatible(false);
181 if( mPixelFormat != pixelFormat )
183 DALI_LOG_ERROR( "Pixel format %d does not match Atlas format %d\n", pixelFormat, mPixelFormat );
187 if( x <= mWidth && y <= mHeight )
193 DALI_LOG_ERROR( "image does not fit within the atlas \n" );
200 void Atlas::AllocateAtlas()
204 mTicket = mResourceClient.AllocateTexture( mWidth, mHeight, mPixelFormat );
205 mImageFactory.RegisterForContextRecovery( this );
209 void Atlas::ReleaseAtlas()
213 mImageFactory.UnregisterFromContextRecovery( this );
216 void Atlas::ClearBackground(const Vector4& color )
219 ResourceId destId = GetResourceId();
222 const unsigned int numPixels = mWidth * mHeight;
223 unsigned int bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
224 BufferImagePtr imageData = BufferImage::New( mWidth, mHeight, mPixelFormat );
225 PixelBuffer* pixbuf = imageData->GetBuffer();
229 // converting color value from float 0.f~1.f to byte 0~255
230 unsigned char r = static_cast<unsigned char>( 255.f * Clamp( color.r, 0.f, 1.f ) );
231 unsigned char g = static_cast<unsigned char>( 255.f * Clamp( color.g, 0.f, 1.f ) );
232 unsigned char b = static_cast<unsigned char>( 255.f * Clamp( color.b, 0.f, 1.f ) );
233 unsigned char a = static_cast<unsigned char>( 255.f * Clamp( color.a, 0.f, 1.f ) );
234 if( mPixelFormat == Pixel::RGBA8888 )
236 // For little-endian byte order, the RGBA channels needs to be reversed for bit shifting.
237 uint32_t clearColor = ( (uint32_t) a<<24 | (uint32_t)b << 16 | (uint32_t)g << 8 | (uint32_t)r );
238 uint32_t* buf = (uint32_t *) pixbuf;
239 for( unsigned int i = 0; i < numPixels; ++i )
244 else if( mPixelFormat == Pixel::RGB888 )
246 for( unsigned int i = 0; i < numPixels; ++i )
248 pixbuf[i*bytesPerPixel] = r;
249 pixbuf[i*bytesPerPixel+1] = g;
250 pixbuf[i*bytesPerPixel+2] = b;
253 else if( mPixelFormat == Pixel::A8 )
255 memset( pixbuf, a, numPixels );
260 imageData->Update(area);
264 mResourceClient.UploadBitmap( destId, imageData->GetResourceId(), 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