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 "image-atlas-manager.h"
22 #include <dali/public-api/images/resource-image.h>
35 const uint32_t DEFAULT_ATLAS_SIZE( 1024u ); // this size can fit 8 by 8 images of average size 128*128
36 const uint32_t MAX_ITEM_SIZE( 512u );
37 const uint32_t MAX_ITEM_AREA( MAX_ITEM_SIZE*MAX_ITEM_SIZE );
40 ImageAtlasManager::ImageAtlasManager( Shader shader, const std::string& textureUniformName )
42 mTextureUniformName( textureUniformName ),
47 ImageAtlasManager::~ImageAtlasManager()
51 Material ImageAtlasManager::Add( Vector4& textureRect,
52 const std::string& url,
54 FittingMode::Type fittingMode,
55 bool orientationCorrection )
57 ImageDimensions dimensions = size;
61 dimensions = ResourceImage::GetImageSize( url );
64 // big image, atlasing is not applied
65 if( static_cast<uint32_t>(dimensions.GetWidth()) * static_cast<uint32_t>(dimensions.GetHeight()) > MAX_ITEM_AREA
66 || dimensions.GetWidth()>DEFAULT_ATLAS_SIZE
67 || dimensions.GetHeight()>DEFAULT_ATLAS_SIZE)
73 for( AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
75 if( (*iter).Upload( textureRect, url, size, fittingMode, orientationCorrection ) )
77 return mMaterialList[i];
83 mAtlasList.back().Upload( textureRect, url, size, fittingMode, orientationCorrection );
84 return mMaterialList.back();
87 Material ImageAtlasManager::Add( Vector4& textureRect,
88 PixelDataPtr pixelData )
91 // big buffer, atlasing is not applied
92 if( static_cast<uint32_t>(pixelData->GetWidth()) * static_cast<uint32_t>(pixelData->GetHeight()) > MAX_ITEM_AREA
93 || pixelData->GetWidth()>DEFAULT_ATLAS_SIZE
94 || pixelData->GetHeight()>DEFAULT_ATLAS_SIZE )
100 for( AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
102 if( (*iter).Upload( textureRect, pixelData ) )
104 return mMaterialList[i];
110 mAtlasList.back().Upload( textureRect, pixelData );
111 return mMaterialList.back();
115 void ImageAtlasManager::Remove( Material material, const Vector4& textureRect )
118 for( MaterialContainer::iterator iter = mMaterialList.begin(); iter != mMaterialList.end(); ++iter)
120 if( (*iter) == material )
122 mAtlasList[i].Remove(textureRect);
129 void ImageAtlasManager::SetBrokenImage( const std::string& brokenImageUrl )
131 if( !brokenImageUrl.empty() )
133 mBrokenImageUrl = brokenImageUrl;
137 void ImageAtlasManager::CreateNewAtlas()
139 Toolkit::ImageAtlas newAtlas = Toolkit::ImageAtlas::New( DEFAULT_ATLAS_SIZE, DEFAULT_ATLAS_SIZE );
140 if( !mBrokenImageUrl.empty() )
142 newAtlas.SetBrokenImage( mBrokenImageUrl );
144 mAtlasList.push_back( newAtlas );
145 Material newMaterial = Material::New( mShader );
146 newMaterial.AddTexture( newAtlas.GetAtlas(), mTextureUniformName );
147 mMaterialList.push_back( newMaterial );
150 } // namespace Internal
152 } // namespace Toolkit