Automatic image atlasing
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / renderer-factory-cache.h
1 #ifndef __DALI_TOOLKIT_RENDERER_FACTORY_CACHE_H__
2 #define __DALI_TOOLKIT_RENDERER_FACTORY_CACHE_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <map>
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/devel-api/rendering/geometry.h>
25 #include <dali/devel-api/rendering/shader.h>
26 #include <dali/devel-api/rendering/renderer.h>
27 #include <dali/devel-api/common/owner-container.h>
28 #include <dali/devel-api/object/weak-handle.h>
29
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 /**
41  * Caches shaders and geometries. Owned by RenderFactory.
42  */
43 class RendererFactoryCache : public RefObject
44 {
45 public:
46
47   /**
48    * Type of shader for caching.
49    */
50   enum ShaderType
51   {
52     COLOR_SHADER,
53     BORDER_SHADER,
54     GRADIENT_SHADER_LINEAR_USER_SPACE,
55     GRADIENT_SHADER_LINEAR_BOUNDING_BOX,
56     GRADIENT_SHADER_RADIAL_USER_SPACE,
57     GRADIENT_SHADER_RADIAL_BOUNDING_BOX,
58     IMAGE_SHADER,
59     NINE_PATCH_SHADER,
60     SVG_SHADER,
61     SHADER_TYPE_MAX = SVG_SHADER
62   };
63
64   /**
65    * Type of geometry for caching.
66    */
67   enum GeometryType
68   {
69     QUAD_GEOMETRY,
70     BORDER_GEOMETRY,
71     NINE_PATCH_GEOMETRY,
72     NINE_PATCH_BORDER_GEOMETRY,
73     GEOMETRY_TYPE_MAX = NINE_PATCH_BORDER_GEOMETRY
74   };
75
76 public:
77
78   /**
79    * @brief Constructor
80    */
81   RendererFactoryCache();
82
83   /**
84    * Request geometry of the given type.
85    * @return The geometry of the required type if it exist in the cache. Otherwise, an empty handle is returned.
86    */
87   Geometry GetGeometry( GeometryType type );
88
89   /**
90    * Cache the geometry of the give type.
91    * @param[in] type The geometry type.
92    * @param[in] geometry The geometry for caching.
93    */
94   void SaveGeometry( GeometryType type, Geometry geometry);
95
96   /**
97    * Request shader of the given type.
98    * @return The shader of the required type if it exist in the cache. Otherwise, an empty handle is returned.
99    */
100   Shader GetShader( ShaderType type );
101
102   /**
103    * Cache the geometry of the give type.
104    * @param[in] type The geometry type.
105    * @param[in] geometry The geometry for caching.
106    */
107   void SaveShader( ShaderType type, Shader shader );
108
109   /*
110    * Greate the quad geometry.
111    * Quad geometry is shared by multiple kind of Renderer, so implement it in the factory-cache.
112    */
113   static Geometry CreateQuadGeometry();
114
115 public:
116
117   /**
118    * @brief Request renderer from the url
119    *
120    * @return The cached renderer if exist in the cache. Otherwise an empty handle is returned.
121    */
122   Renderer GetRenderer( const std::string& key ) const;
123
124   /**
125    * @brief Cache the renderer based on the given key.
126    *
127    * If the key already exists in the cache, then the cache will save an additional renderer to the cache.
128    * RemoveRenderer will then need to be called twice to remove both items from the cache.
129    *
130    * @param[in] key The key to use for caching
131    * @param[in] renderer The Renderer to be cached
132    */
133   void SaveRenderer( const std::string& key, Renderer& renderer );
134
135   /**
136    * @brief Cleans the renderer cache by removing the renderer from the cache based on the given key if there are no longer any references to it
137    *
138    * @param[in] key The key used for caching
139    *
140    * @return True if the renderer is no longer used anywhere, false otherwise
141    */
142   bool CleanRendererCache( const std::string& key );
143
144 protected:
145
146   /**
147    * A reference counted object may only be deleted by calling Unreference()
148    */
149   virtual ~RendererFactoryCache();
150
151   /**
152    * Undefined copy constructor.
153    */
154   RendererFactoryCache(const RendererFactoryCache&);
155
156   /**
157    * Undefined assignment operator.
158    */
159   RendererFactoryCache& operator=(const RendererFactoryCache& rhs);
160
161 private:
162   struct CachedRenderer
163   {
164     std::string mKey;
165     WeakHandle< Renderer > mRenderer;
166
167     CachedRenderer( const std::string& key, Renderer& renderer )
168     : mKey( key ),
169       mRenderer( renderer)
170     {}
171   };
172
173   typedef Dali::Vector< std::size_t > HashVector;
174   typedef Dali::OwnerContainer< const CachedRenderer* > CachedRenderers;
175
176   /**
177    * @brief Finds the first index into the cached renderers from the url
178    *
179    * @return Returns the first index into the cached renderer from the url if it exists in the cache, otherwise returns -1
180    */
181   int FindRenderer( const std::string& key ) const;
182
183 private:
184   // ToDo: test whether using the WeakHandle could improve the performance
185   //       With WeakHandle, the resource would be released automatically when no control is using it
186
187   Geometry mGeometry[GEOMETRY_TYPE_MAX+1];
188   Shader mShader[SHADER_TYPE_MAX+1];
189
190   HashVector mRendererHashes;
191   CachedRenderers mRenderers;
192 };
193
194 } // namespace Internal
195
196 } // namespace Toolkit
197
198 } // namespace Dali
199
200 #endif /*__DALI_TOOLKIT_RENDERER_FACTORY_CACHE_H__ */