2 * Copyright (c) 2022 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/devel-api/adaptor-framework/image-loading.h>
25 #include <dali-toolkit/internal/image-loader/image-atlas-impl.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()
45 ImageAtlasManager::~ImageAtlasManager()
49 bool ImageAtlasManager::CheckAtlasAvailable(const VisualUrl& url, const ImageDimensions& size) const
51 ImageDimensions dimensions = size;
55 dimensions = Dali::GetClosestImageSize(url.GetUrl());
58 // big image, atlasing is not applied
59 if(static_cast<uint32_t>(dimensions.GetWidth()) * static_cast<uint32_t>(dimensions.GetHeight()) > MAX_ITEM_AREA || dimensions.GetWidth() > DEFAULT_ATLAS_SIZE || dimensions.GetHeight() > DEFAULT_ATLAS_SIZE)
66 TextureSet ImageAtlasManager::Add(Vector4& textureRect,
68 ImageDimensions& size,
69 FittingMode::Type fittingMode,
70 bool orientationCorrection,
71 AtlasUploadObserver* atlasUploadObserver)
73 ImageDimensions dimensions = size;
77 dimensions = Dali::GetClosestImageSize(url.GetUrl());
80 // big image, atlasing is not applied
81 if(static_cast<uint32_t>(dimensions.GetWidth()) * static_cast<uint32_t>(dimensions.GetHeight()) > MAX_ITEM_AREA || dimensions.GetWidth() > DEFAULT_ATLAS_SIZE || dimensions.GetHeight() > DEFAULT_ATLAS_SIZE)
88 for(AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
90 if(GetImplementation(*iter).Upload(textureRect, url, size, fittingMode, orientationCorrection, atlasUploadObserver))
92 return mTextureSetList[i];
98 GetImplementation(mAtlasList.back()).Upload(textureRect, url, size, fittingMode, orientationCorrection, atlasUploadObserver);
99 return mTextureSetList.back();
102 TextureSet ImageAtlasManager::Add(Vector4& textureRect,
103 const EncodedImageBuffer& encodedImageBuffer,
104 const ImageDimensions& size,
105 FittingMode::Type fittingMode,
106 bool orientationCorrection,
107 AtlasUploadObserver* atlasUploadObserver)
109 // big image, atlasing is not applied
110 if(static_cast<uint32_t>(size.GetWidth()) * static_cast<uint32_t>(size.GetHeight()) > MAX_ITEM_AREA || size.GetWidth() > DEFAULT_ATLAS_SIZE || size.GetHeight() > DEFAULT_ATLAS_SIZE)
116 for(AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
118 if(GetImplementation(*iter).Upload(textureRect, encodedImageBuffer, size, fittingMode, orientationCorrection, atlasUploadObserver))
120 return mTextureSetList[i];
126 GetImplementation(mAtlasList.back()).Upload(textureRect, encodedImageBuffer, size, fittingMode, orientationCorrection, atlasUploadObserver);
127 return mTextureSetList.back();
130 TextureSet ImageAtlasManager::Add(Vector4& textureRect,
133 // big buffer, atlasing is not applied
134 if(static_cast<uint32_t>(pixelData.GetWidth()) * static_cast<uint32_t>(pixelData.GetHeight()) > MAX_ITEM_AREA || pixelData.GetWidth() > DEFAULT_ATLAS_SIZE || pixelData.GetHeight() > DEFAULT_ATLAS_SIZE)
140 for(AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
142 if((*iter).Upload(textureRect, pixelData))
144 return mTextureSetList[i];
150 mAtlasList.back().Upload(textureRect, pixelData);
151 return mTextureSetList.back();
154 void ImageAtlasManager::Remove(TextureSet textureSet, const Vector4& textureRect)
157 for(TextureSetContainer::iterator iter = mTextureSetList.begin(); iter != mTextureSetList.end(); ++iter)
159 if((*iter) == textureSet)
161 mAtlasList[i].Remove(textureRect);
168 void ImageAtlasManager::SetBrokenImage(const std::string& brokenImageUrl)
170 if(!brokenImageUrl.empty())
172 mBrokenImageUrl = brokenImageUrl;
176 void ImageAtlasManager::CreateNewAtlas()
178 Toolkit::ImageAtlas newAtlas = Toolkit::ImageAtlas::New(DEFAULT_ATLAS_SIZE, DEFAULT_ATLAS_SIZE);
179 if(!mBrokenImageUrl.empty())
181 newAtlas.SetBrokenImage(mBrokenImageUrl);
183 mAtlasList.push_back(newAtlas);
184 TextureSet textureSet = TextureSet::New();
185 textureSet.SetTexture(0u, newAtlas.GetAtlas());
186 mTextureSetList.push_back(textureSet);
189 } // namespace Internal
191 } // namespace Toolkit