NativeImageSource with tbm_surface for tizen 3.0 wayland
[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   /**
168    * @brief Set whether the Pre-multiplied Alpha Blending is required
169    *
170    * @param[in] preMultipled whether alpha is pre-multiplied.
171    */
172   void EnablePreMultipliedAlpha(  bool preMultipled );
173
174 private:
175
176   /**
177    * @brief Applies the image to the material used for this renderer
178    *
179    * @param[in] image The Image to apply to the material used for this renderer
180    */
181   void ApplyImageToSampler( const Image& image );
182
183   /**
184    * @brief Initializes the Dali::Renderer from an image url string
185    *
186    * @param[in] imageUrl The image url string to intialize this ImageRenderer from
187    */
188   void InitializeRenderer( const std::string& imageUrl );
189
190   /**
191    * @brief Initializes the Dali::Renderer from an image handle
192    *
193    * @param[in] image The image handle to intialize this ImageRenderer from
194    */
195   void InitializeRenderer( const Image& image );
196
197   /**
198    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
199    *
200    * @return Returns the created Dali::Renderer
201    */
202   Renderer CreateRenderer() const;
203
204   /**
205    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
206    *
207    * @return Returns the created Dali::Renderer
208    */
209   Renderer CreateNativeImageRenderer() const;
210
211   /**
212    * Callback function of image resource loading succeed
213    * @param[in] image The Image content that we attempted to load from mImageUrl
214    */
215   void OnImageLoaded( ResourceImage image );
216
217   /**
218    * Set the value to the uTextureRect uniform
219    * @param[in] textureRect The texture rectangular area.
220    */
221   void SetTextureRectUniform( const Vector4& textureRect  );
222
223   /**
224    * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
225    */
226   void CleanCache(const std::string& url);
227
228   /**
229    * Set shader code for nativeimage if it exists
230    */
231   void SetNativeFragmentShaderCode( Dali::NativeImage& nativeImage );
232
233 private:
234   Image mImage;
235   ImageAtlasManager& mAtlasManager;
236
237   std::string mImageUrl;
238   Dali::ImageDimensions mDesiredSize;
239   Dali::FittingMode::Type mFittingMode;
240   Dali::SamplingMode::Type mSamplingMode;
241   bool mIsAlphaPreMultiplied;
242
243   std::string mNativeFragmentShaderCode;
244   bool mNativeImageFlag;
245 };
246
247 } // namespace Internal
248
249 } // namespace Toolkit
250
251 } // namespace Dali
252
253 #endif /* __DALI_TOOLKIT_INTERNAL_IMAGE_RENDERER_H__ */