DALi Version 1.1.30
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / image / image-renderer.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_IMAGE_RENDERER_H__
2 #define __DALI_TOOLKIT_INTERNAL_IMAGE_RENDERER_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
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/controls/renderers/control-renderer-impl.h>
23 #include <dali-toolkit/internal/controls/renderers/image-atlas-manager.h>
24
25 // EXTERNAL INCLUDES
26 #include <dali/public-api/images/image.h>
27 #include <dali/public-api/images/image-operations.h>
28 #include <dali/public-api/images/resource-image.h>
29
30 namespace Dali
31 {
32
33 class NativeImage;
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 class ImageRenderer;
42 typedef IntrusivePtr< ImageRenderer > ImageRendererPtr;
43
44 /**
45  * The renderer which renders an image to the control's quad
46  *
47  * The following properties are optional
48  *
49  * | %Property Name          | Type             |
50  * |-------------------------|------------------|
51  * | imageUrl                | STRING           |
52  * | imageFittingMode        | STRING           |
53  * | imageSamplingMode       | STRING           |
54  * | imageDesiredWidth       | INT              |
55  * | imageDesiredHeight      | INT              |
56  *
57  * where imageFittingMode should be one of the following fitting modes:
58  *   "shrinkToFit"
59  *   "scaleToFill"
60  *   "fitWidth"
61  *   "fitHeight"
62  *   "default"
63  *
64  * where imageSamplingMode should be one of the following sampling modes:
65  *   "box"
66  *   "nearest"
67  *   "linear"
68  *   "boxThenNearest"
69  *   "boxThenLinear"
70  *   "noFilter"
71  *   "dontCare"
72  *   "default"
73  *
74  */
75 class ImageRenderer: public ControlRenderer, public ConnectionTracker
76 {
77 public:
78
79   /**
80    * @brief Constructor.
81    *
82    * @param[in] factoryCache The RendererFactoryCache object
83    * @param[in] atlasManager The atlasManager object
84    */
85   ImageRenderer( RendererFactoryCache& factoryCache, ImageAtlasManager& atlasManager );
86
87   /**
88    * @brief A reference counted object may only be deleted by calling Unreference().
89    */
90   ~ImageRenderer();
91
92 public:  // from ControlRenderer
93
94   /**
95    * @copydoc ControlRenderer::SetSize
96    */
97   virtual void SetSize( const Vector2& size );
98
99   /**
100    * @copydoc ControlRenderer::GetNaturalSize
101    */
102   virtual void GetNaturalSize( Vector2& naturalSize ) const;
103
104   /**
105    * @copydoc ControlRenderer::SetClipRect
106    */
107   virtual void SetClipRect( const Rect<int>& clipRect );
108
109   /**
110    * @copydoc ControlRenderer::SetOffset
111    */
112   virtual void SetOffset( const Vector2& offset );
113
114   /**
115    * @copydoc ControlRenderer::CreatePropertyMap
116    */
117   virtual void DoCreatePropertyMap( Property::Map& map ) const;
118
119 protected:
120   /**
121    * @copydoc ControlRenderer::DoInitialize
122    */
123   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
124
125   /**
126    * @copydoc ControlRenderer::DoSetOnStage
127    */
128   virtual void DoSetOnStage( Actor& actor );
129
130   /**
131    * @copydoc ControlRenderer::DoSetOffStage
132    */
133   virtual void DoSetOffStage( Actor& actor );
134
135 public:
136
137   /**
138    * Get the standard image rendering shader.
139    * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
140    */
141   static Shader GetImageShader( RendererFactoryCache& factoryCache );
142
143   /**
144    * @brief Sets the image of this renderer to the resource at imageUrl
145    * The renderer will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
146    *
147    * @param[in] actor The Actor the renderer is applied to if, empty if the renderer has not been applied to any Actor
148    * @param[in] imageUrl The URL to to image resource to use
149    * @param[in] size The width and height to fit the loaded image to.
150    * @param[in] fittingMode The FittingMode of the resource to load
151    * @param[in] samplingMode The SamplingMode of the resource to load
152    */
153   void SetImage( Actor& actor,
154                  const std::string& imageUrl,
155                  ImageDimensions size=ImageDimensions(),
156                  FittingMode::Type fittingMode = FittingMode::DEFAULT,
157                  Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
158
159   /**
160    * @brief Sets the image of this renderer to use
161    *
162    * @param[in] actor The Actor the renderer is applied to if, empty if the renderer has not been applied to any Actor
163    * @param[in] image The image to use
164    */
165   void SetImage( Actor& actor, const Image& image );
166
167 private:
168
169   /**
170    * @brief Applies the image to the material used for this renderer
171    *
172    * @param[in] image The Image to apply to the material used for this renderer
173    */
174   void ApplyImageToSampler( const Image& image );
175
176   /**
177    * @brief Initializes the Dali::Renderer from an image url string
178    *
179    * @param[in] imageUrl The image url string to intialize this ImageRenderer from
180    */
181   void InitializeRenderer( const std::string& imageUrl );
182
183   /**
184    * @brief Initializes the Dali::Renderer from an image handle
185    *
186    * @param[in] image The image handle to intialize this ImageRenderer from
187    */
188   void InitializeRenderer( const Image& image );
189
190   /**
191    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
192    *
193    * @return Returns the created Dali::Renderer
194    */
195   Renderer CreateRenderer() const;
196
197   /**
198    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
199    *
200    * @return Returns the created Dali::Renderer
201    */
202   Renderer CreateNativeImageRenderer() const;
203
204   /**
205    * Callback function of image resource loading succeed
206    * @param[in] image The Image content that we attempted to load from mImageUrl
207    */
208   void OnImageLoaded( ResourceImage image );
209
210   /**
211    * Set the value to the uTextureRect uniform
212    * @param[in] textureRect The texture rectangular area.
213    */
214   void SetTextureRectUniform( const Vector4& textureRect  );
215
216   /**
217    * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
218    */
219   void CleanCache(const std::string& url);
220
221   /**
222    * Set shader code for nativeimage if it exists
223    */
224   void SetNativeFragmentShaderCode( Dali::NativeImage& nativeImage );
225
226 private:
227   Image mImage;
228   ImageAtlasManager& mAtlasManager;
229
230   std::string mImageUrl;
231   Dali::ImageDimensions mDesiredSize;
232   Dali::FittingMode::Type mFittingMode;
233   Dali::SamplingMode::Type mSamplingMode;
234
235   std::string mNativeFragmentShaderCode;
236   bool mNativeImageFlag;
237 };
238
239 } // namespace Internal
240
241 } // namespace Toolkit
242
243 } // namespace Dali
244
245 #endif /* __DALI_TOOLKIT_INTERNAL_IMAGE_RENDERER_H__ */