Merge "Install default-feedback-theme.json files in styles folder" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image-atlas-manager.cpp
1  /*
2  * Copyright (c) 2016 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/images/texture-set-image.h>
23 #include <dali/public-api/images/resource-image.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Internal
32 {
33
34 namespace
35 {
36 const uint32_t DEFAULT_ATLAS_SIZE( 1024u ); // this size can fit 8 by 8 images of average size 128*128
37 const uint32_t MAX_ITEM_SIZE( 512u  );
38 const uint32_t MAX_ITEM_AREA( MAX_ITEM_SIZE*MAX_ITEM_SIZE  );
39 }
40
41 ImageAtlasManager::ImageAtlasManager()
42 : mBrokenImageUrl( "" )
43 {
44 }
45
46 ImageAtlasManager::~ImageAtlasManager()
47 {
48 }
49
50 TextureSet ImageAtlasManager::Add( Vector4& textureRect,
51                                  const std::string& url,
52                                  ImageDimensions size,
53                                  FittingMode::Type fittingMode,
54                                  bool orientationCorrection )
55 {
56   ImageDimensions dimensions = size;
57   ImageDimensions zero;
58   if( size == zero )
59   {
60     dimensions = ResourceImage::GetImageSize( url );
61   }
62
63   // big image, atlasing is not applied
64   if( static_cast<uint32_t>(dimensions.GetWidth()) * static_cast<uint32_t>(dimensions.GetHeight()) > MAX_ITEM_AREA
65       || dimensions.GetWidth()>DEFAULT_ATLAS_SIZE
66       || dimensions.GetHeight()>DEFAULT_ATLAS_SIZE)
67   {
68     return TextureSet();
69   }
70
71   unsigned int i = 0;
72   for( AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
73   {
74     if( (*iter).Upload( textureRect, url, size, fittingMode, orientationCorrection ) )
75     {
76       return mTextureSetList[i];
77     }
78     i++;
79   }
80
81   CreateNewAtlas();
82   mAtlasList.back().Upload( textureRect, url, size, fittingMode, orientationCorrection );
83   return mTextureSetList.back();
84 }
85
86 TextureSet ImageAtlasManager::Add( Vector4& textureRect,
87                                  PixelData pixelData )
88 {
89
90   // big buffer, atlasing is not applied
91   if( static_cast<uint32_t>(pixelData.GetWidth()) * static_cast<uint32_t>(pixelData.GetHeight()) > MAX_ITEM_AREA
92       || pixelData.GetWidth()>DEFAULT_ATLAS_SIZE
93       || pixelData.GetHeight()>DEFAULT_ATLAS_SIZE )
94   {
95     return TextureSet();
96   }
97
98   unsigned int i = 0;
99   for( AtlasContainer::iterator iter = mAtlasList.begin(); iter != mAtlasList.end(); ++iter)
100   {
101     if( (*iter).Upload( textureRect, pixelData ) )
102     {
103       return mTextureSetList[i];
104     }
105     i++;
106   }
107
108   CreateNewAtlas();
109   mAtlasList.back().Upload( textureRect, pixelData );
110   return mTextureSetList.back();
111
112 }
113
114 void ImageAtlasManager::Remove( TextureSet textureSet, const Vector4& textureRect )
115 {
116   unsigned int i = 0;
117   for( TextureSetContainer::iterator iter = mTextureSetList.begin(); iter != mTextureSetList.end(); ++iter)
118   {
119     if( (*iter) == textureSet )
120     {
121       mAtlasList[i].Remove(textureRect);
122       return;
123     }
124     i++;
125   }
126 }
127
128 void ImageAtlasManager::SetBrokenImage( const std::string& brokenImageUrl )
129 {
130   if( !brokenImageUrl.empty() )
131   {
132     mBrokenImageUrl = brokenImageUrl;
133   }
134 }
135
136 void ImageAtlasManager::CreateNewAtlas()
137 {
138   Toolkit::ImageAtlas newAtlas = Toolkit::ImageAtlas::New( DEFAULT_ATLAS_SIZE, DEFAULT_ATLAS_SIZE  );
139   if( !mBrokenImageUrl.empty() )
140   {
141     newAtlas.SetBrokenImage( mBrokenImageUrl );
142   }
143   mAtlasList.push_back( newAtlas );
144   TextureSet textureSet = TextureSet::New();
145   textureSet.SetTexture( 0u, newAtlas.GetAtlas() );
146   mTextureSetList.push_back( textureSet );
147 }
148
149 } // namespace Internal
150
151 } // namespace Toolkit
152
153 } // namespace Dali