Add post processor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image-atlas-manager.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "image-atlas-manager.h"
20
21 // EXTERNAL HEADER
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23
24 namespace Dali
25 {
26 namespace Toolkit
27 {
28 namespace Internal
29 {
30 namespace
31 {
32 const uint32_t DEFAULT_ATLAS_SIZE(1024u); // this size can fit 8 by 8 images of average size 128*128
33 const uint32_t MAX_ITEM_SIZE(512u);
34 const uint32_t MAX_ITEM_AREA(MAX_ITEM_SIZE* MAX_ITEM_SIZE);
35 } // namespace
36
37 ImageAtlasManager::ImageAtlasManager()
38 : mBrokenImageUrl("")
39 {
40 }
41
42 ImageAtlasManager::~ImageAtlasManager()
43 {
44 }
45
46 TextureSet ImageAtlasManager::Add(Vector4&             textureRect,
47                                   const std::string&   url,
48                                   ImageDimensions&     size,
49                                   FittingMode::Type    fittingMode,
50                                   bool                 orientationCorrection,
51                                   AtlasUploadObserver* atlasUploadObserver)
52 {
53   ImageDimensions dimensions = size;
54   ImageDimensions zero;
55   if(size == zero)
56   {
57     dimensions = Dali::GetClosestImageSize(url);
58   }
59
60   // big image, atlasing is not applied
61   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)
62   {
63     return TextureSet();
64   }
65   size = dimensions;
66
67   unsigned int i = 0;
68   for(AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
69   {
70     if((*iter).Upload(textureRect, url, size, fittingMode, orientationCorrection, atlasUploadObserver))
71     {
72       return mTextureSetList[i];
73     }
74     i++;
75   }
76
77   CreateNewAtlas();
78   mAtlasList.back().Upload(textureRect, url, size, fittingMode, orientationCorrection, atlasUploadObserver);
79   return mTextureSetList.back();
80 }
81
82 TextureSet ImageAtlasManager::Add(Vector4&  textureRect,
83                                   PixelData pixelData)
84 {
85   // big buffer, atlasing is not applied
86   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)
87   {
88     return TextureSet();
89   }
90
91   unsigned int i = 0;
92   for(AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
93   {
94     if((*iter).Upload(textureRect, pixelData))
95     {
96       return mTextureSetList[i];
97     }
98     i++;
99   }
100
101   CreateNewAtlas();
102   mAtlasList.back().Upload(textureRect, pixelData);
103   return mTextureSetList.back();
104 }
105
106 void ImageAtlasManager::Remove(TextureSet textureSet, const Vector4& textureRect)
107 {
108   unsigned int i = 0;
109   for(TextureSetContainer::iterator iter = mTextureSetList.begin(); iter != mTextureSetList.end(); ++iter)
110   {
111     if((*iter) == textureSet)
112     {
113       mAtlasList[i].Remove(textureRect);
114       return;
115     }
116     i++;
117   }
118 }
119
120 void ImageAtlasManager::SetBrokenImage(const std::string& brokenImageUrl)
121 {
122   if(!brokenImageUrl.empty())
123   {
124     mBrokenImageUrl = brokenImageUrl;
125   }
126 }
127
128 void ImageAtlasManager::CreateNewAtlas()
129 {
130   Toolkit::ImageAtlas newAtlas = Toolkit::ImageAtlas::New(DEFAULT_ATLAS_SIZE, DEFAULT_ATLAS_SIZE);
131   if(!mBrokenImageUrl.empty())
132   {
133     newAtlas.SetBrokenImage(mBrokenImageUrl);
134   }
135   mAtlasList.push_back(newAtlas);
136   TextureSet textureSet = TextureSet::New();
137   textureSet.SetTexture(0u, newAtlas.GetAtlas());
138   mTextureSetList.push_back(textureSet);
139 }
140
141 } // namespace Internal
142
143 } // namespace Toolkit
144
145 } // namespace Dali