Tizen 2.4.0 rev3 SDK Public Release
[framework/graphics/dali.git] / dali / internal / render / renderers / scene-graph-image-renderer.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_IMAGE_RENDERER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_IMAGE_RENDERER_H__
3
4 /*
5  * Copyright (c) 2014 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/public-api/actors/image-actor.h>
23 #include <dali/internal/common/owner-pointer.h>
24 #include <dali/internal/update/resources/resource-manager-declarations.h>
25 #include <dali/internal/render/gl-resources/context.h>
26 #include <dali/internal/render/gl-resources/texture-observer.h>
27 #include <dali/internal/render/renderers/scene-graph-renderer.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34 class GpuBuffer;
35
36 namespace SceneGraph
37 {
38 class NodeDataProvider;
39
40 /**
41  * Used to render an image.
42  */
43 class ImageRenderer : public Renderer, public TextureObserver
44 {
45 public:
46
47   typedef Dali::ImageActor::PixelArea PixelArea;
48
49   enum MeshType
50   {
51     QUAD,
52     NINE_PATCH,
53     NINE_PATCH_NO_CENTER,
54     GRID_QUAD,
55     GRID_NINE_PATCH,
56     GRID_NINE_PATCH_NO_CENTER ///< Not supported yet.
57   };
58
59   /**
60    * Create a new ImageRenderer.
61    * @param dataprovider to render
62    * @return The newly allocated ImageRenderer.
63    */
64   static ImageRenderer* New( NodeDataProvider& dataprovider );
65
66   /**
67    * Virtual destructor
68    */
69   virtual ~ImageRenderer();
70
71   /**
72    * Called in the render-thread when the renderer is removed
73    */
74   void OnRemove();
75
76   /**
77    * Set the texture used to render.
78    * @param[in] textureId The id of the texture used to render.
79    */
80   void SetTextureId( ResourceId textureId );
81
82   /**
83    * Set the pixel area for the renderer
84    * @param[in] pixelArea An area within the texture, used when building UV coordinates.
85    */
86   void SetPixelArea( const ImageRenderer::PixelArea& pixelArea );
87
88   /**
89    * Set 9 patch border for the image renderer
90    * @param[in] border The border for nine-patch meshes.
91    * @param[in] borderInPixels True if border is in pixels instead of percentages.
92    */
93   void SetNinePatchBorder( const Vector4& border, bool inPixels );
94
95   /**
96    * Set whether the ImageRenderer should use blending
97    * @param[in] useBlend True if blending should be used.
98    */
99   void SetUseBlend( bool useBlend );
100
101   /**
102    * Set the blending options.
103    * @param[in] options A bitmask of blending options.
104    */
105   void SetBlendingOptions( unsigned int options );
106
107   /**
108    * Set the blend color.
109    * @param[in] color The new blend-color.
110    */
111   void SetBlendColor( const Vector4& color );
112
113   /**
114    * Calculate the mesh data used by the ImageRenderer.
115    * @param[in] type The type of mesh data required; either quad, nine-patch or grid.
116    * @param[in] targetSize The size which the mesh data should fit inside.
117    * @param[in] usePixelArea whether to use pixel area or not
118    */
119   void CalculateMeshData( MeshType type, const Vector2& targetSize, bool usePixelArea );
120
121   /**
122    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
123    */
124   virtual void GlContextDestroyed();
125
126   /**
127    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
128    */
129   virtual void GlCleanup();
130
131   /**
132    * @copydoc Dali::Internal::SceneGraph::Renderer::RequiresDepthTest()
133    */
134   virtual bool RequiresDepthTest() const;
135
136   /**
137    * @copydoc Dali::Internal::SceneGraph::Renderer::CheckResources()
138    */
139   virtual bool CheckResources();
140
141   /**
142    * @copydoc Dali::Internal::SceneGraph::Renderer::IsOutsideClipSpace()
143    */
144   virtual bool IsOutsideClipSpace( Context& context, const Matrix& modelMatrix, const Matrix& modelViewProjectionMatrix );
145
146   /**
147    * @copydoc Dali::Internal::SceneGraph::Renderer::DoRender()
148    */
149   virtual void DoRender( Context& context, TextureCache& textureCache, BufferIndex bufferIndex, Program& program, const Matrix& modelViewMatrix, const Matrix& viewMatrix );
150
151   /**
152    * @copydoc Dali::Internal::SceneGraph::Renderer::DoSetBlending()
153    */
154   virtual void DoSetBlending( Context& context, BufferIndex bufferIndex );
155
156 protected: // TextureObserver implementation
157
158   /**
159    * @copydoc Dali::Internal::TextureObserver::TextureDiscarded()
160    */
161   virtual void TextureDiscarded( ResourceId textureId );
162
163 private:
164   /**
165    * Helper to update the vertex buffer.
166    */
167   void UpdateVertexBuffer( Context& context, GLsizeiptr size, const GLvoid *data );
168
169   /**
170    * Helper to update the index buffer.
171    */
172   void UpdateIndexBuffer( Context& context, GLsizeiptr size, const GLvoid *data );
173
174   /**
175    * Helper to generate mesh data when required
176    * @param[in] texture Texture from which to get UV data
177    */
178   void GenerateMeshData( Texture* texture );
179
180   /**
181    * Helper to fill vertex/index buffers with quad data.
182    * (Quads are simple meshes, and thus have a specialised mesh generation method)
183    *
184    * @param[in] texture Texture from which to get UV data
185    * @param[in] size
186    * @param[in] pixelArea An area within the texture, used when building UV coordinates. A value of NULL means use default image size.
187    */
188   void SetQuadMeshData( Texture* texture, const Vector2& size, const PixelArea* pixelArea );
189
190   /**
191    * Helper to fill vertex/index buffers with nine-patch data.
192    * (9-Patches are simple meshes, and thus have a specialised mesh generation method)
193    *
194    * @param[in] texture Texture from which to get UV data
195    * @param[in] size The target size.
196    * @param[in] border 9 patch border information.
197    * @param[in] borderInPixels True if border is in pixels instead of percentages.
198    * @param[in] pixelArea An area within the texture, used when building UV coordinates. A value of NULL means use default image size.
199    * @param[in] noCenter True if the center section of nine patch should not be rendered.
200    */
201   void SetNinePatchMeshData( Texture* texture, const Vector2& size, const Vector4& border, bool borderInPixels, const PixelArea* pixelArea, bool noCenter );
202
203   /**
204    * Helper to fill vertex/index buffers with grid data.
205    * This can build grid meshes that are either of type Quad or type 9-Patch
206    *
207    * @param[in] size The target size.
208    * @param[in] border 9 patch border information (pass NULL for no border i.e. a standard Quad)
209    * @param[in] borderInPixels True if border is in pixels instead of percentages.
210    * @param[in] pixelArea An area within the texture, used when building UV coordinates. A value of NULL means use default image size.
211    */
212   void SetGridMeshData( Texture* texture, const Vector2& size, const Vector4* border, bool borderInPixels, const PixelArea* pixelArea );
213
214   /**
215    * Helper to fill index buffer with uniform grid data.
216    * @param[in] indices pointer to indices buffer to be filled
217    * @param[in] rectanglesX number of columns for rectangles in grid
218    * @param[in] rectanglesY number of rows for rectangles in grid
219    */
220   void GenerateMeshIndices(GLushort* indices, int rectanglesX, int rectanglesY);
221
222   /**
223    * Private constructor. @see New()
224    */
225   ImageRenderer( NodeDataProvider& dataprovider );
226
227   // Undefined
228   ImageRenderer( const ImageRenderer& );
229
230   // Undefined
231   ImageRenderer& operator=( const ImageRenderer& rhs );
232
233 private:
234
235   Texture*    mTexture;
236
237   OwnerPointer< GpuBuffer > mVertexBuffer;
238   OwnerPointer< GpuBuffer > mIndexBuffer;
239
240   Vector4   mBorder;
241   PixelArea mPixelArea;
242   Vector2   mGeometrySize;
243   ResourceId  mTextureId;
244
245   BlendingOptions mBlendingOptions;
246
247   // flags
248   MeshType  mMeshType        : 3; // 4 values fits in 3 bits just fine
249   bool      mIsMeshGenerated : 1;
250   bool      mBorderInPixels  : 1;
251   bool      mUseBlend        : 1; ///< True if blending should be enabled, 1 bit is enough
252   bool      mUsePixelArea    : 1;
253
254 };
255
256 } // namespace SceneGraph
257
258 } // namespace Internal
259
260 } // namespace Dali
261
262 #endif // __DALI_INTERNAL_SCENE_GRAPH_IMAGE_RENDERER_H__