License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.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 RenderDataProvider;
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     GRID_QUAD,
54     GRID_NINE_PATCH
55   };
56
57   /**
58    * Create a new ImageRenderer.
59    * @param dataprovider to render
60    * @return The newly allocated ImageRenderer.
61    */
62   static ImageRenderer* New( RenderDataProvider& dataprovider );
63
64   /**
65    * Virtual destructor
66    */
67   virtual ~ImageRenderer();
68
69   /**
70    * Set the texture used to render.
71    * @param[in] textureId The id of the texture used to render.
72    */
73   void SetTextureId( ResourceId textureId );
74
75   /**
76    * Set the pixel area for the renderer
77    * @param[in] pixelArea An area within the texture, used when building UV coordinates.
78    */
79   void SetPixelArea( const ImageRenderer::PixelArea& pixelArea );
80
81   /**
82    * Set 9 patch border for the image renderer
83    * @param[in] border The border for nine-patch meshes.
84    * @param[in] borderInPixels True if border is in pixels instead of percentages.
85    */
86   void SetNinePatchBorder( const Vector4& border, bool inPixels );
87
88   /**
89    * Calculate the mesh data used by the ImageRenderer.
90    * @param[in] type The type of mesh data required; either quad, nine-patch or grid.
91    * @param[in] targetSize The size which the mesh data should fit inside.
92    * @param[in] usePixelArea whether to use pixel area or not
93    */
94   void CalculateMeshData( MeshType type, const Vector2& targetSize, bool usePixelArea );
95
96   /**
97    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
98    */
99   virtual void GlContextDestroyed();
100
101   /**
102    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
103    */
104   virtual void GlCleanup();
105
106   /**
107    * @copydoc Dali::Internal::SceneGraph::Renderer::RequiresDepthTest()
108    */
109   virtual bool RequiresDepthTest() const;
110
111   /**
112    * @copydoc Dali::Internal::SceneGraph::Renderer::CheckResources()
113    */
114   virtual bool CheckResources();
115
116   /**
117    * @copydoc Dali::Internal::SceneGraph::Renderer::DoRender()
118    */
119   virtual void DoRender( BufferIndex bufferIndex, const Matrix& modelViewMatrix, const Matrix& modelMatrix, const Matrix& viewMatrix, const Matrix& projectionMatrix, const Vector4& color );
120
121 protected: // TextureObserver implementation
122
123   /**
124    * @copydoc Dali::Internal::TextureObserver::TextureDiscarded()
125    */
126   virtual void TextureDiscarded( ResourceId textureId );
127
128 private:
129   /**
130    * Helper to update the vertex buffer.
131    */
132   void UpdateVertexBuffer( GLsizeiptr size, const GLvoid *data );
133
134   /**
135    * Helper to update the index buffer.
136    */
137   void UpdateIndexBuffer( GLsizeiptr size, const GLvoid *data );
138
139   /**
140    * Helper to generate mesh data when required
141    * @param[in] texture Texture from which to get UV data
142    */
143   void GenerateMeshData( Texture* texture );
144
145   /**
146    * Helper to fill vertex/index buffers with quad data.
147    * (Quads are simple meshes, and thus have a specialised mesh generation method)
148    *
149    * @param[in] texture Texture from which to get UV data
150    * @param[in] size
151    * @param[in] pixelArea An area within the texture, used when building UV coordinates. A value of NULL means use default image size.
152    */
153   void SetQuadMeshData( Texture* texture, const Vector2& size, const PixelArea* pixelArea );
154
155   /**
156    * Helper to fill vertex/index buffers with nine-patch data.
157    * (9-Patches are simple meshes, and thus have a specialised mesh generation method)
158    *
159    * @param[in] texture Texture from which to get UV data
160    * @param[in] size The target size.
161    * @param[in] border 9 patch border information.
162    * @param[in] borderInPixels True if border is in pixels instead of percentages.
163    * @param[in] pixelArea An area within the texture, used when building UV coordinates. A value of NULL means use default image size.
164    */
165   void SetNinePatchMeshData( Texture* texture, const Vector2& size, const Vector4& border, bool borderInPixels, const PixelArea* pixelArea );
166
167   /**
168    * Helper to fill vertex/index buffers with grid data.
169    * This can build grid meshes that are either of type Quad or type 9-Patch
170    *
171    * @param[in] size The target size.
172    * @param[in] border 9 patch border information (pass NULL for no border i.e. a standard Quad)
173    * @param[in] borderInPixels True if border is in pixels instead of percentages.
174    * @param[in] pixelArea An area within the texture, used when building UV coordinates. A value of NULL means use default image size.
175    */
176   void SetGridMeshData( Texture* texture, const Vector2& size, const Vector4* border, bool borderInPixels, const PixelArea* pixelArea );
177
178   /**
179    * Helper to fill index buffer with uniform grid data.
180    * @param[in] indices pointer to indices buffer to be filled
181    * @param[in] rectanglesX number of columns for rectangles in grid
182    * @param[in] rectanglesY number of rows for rectangles in grid
183    */
184   void GenerateMeshIndices(GLushort* indices, int rectanglesX, int rectanglesY);
185
186   /**
187    * Private constructor. @see New()
188    */
189   ImageRenderer(RenderDataProvider& dataprovider);
190
191   // Undefined
192   ImageRenderer(const ImageRenderer&);
193
194   // Undefined
195   ImageRenderer& operator=(const ImageRenderer& rhs);
196
197 private:
198
199   Texture*    mTexture;
200
201   OwnerPointer< GpuBuffer > mVertexBuffer;
202   OwnerPointer< GpuBuffer > mIndexBuffer;
203
204   Vector4   mBorder;
205   PixelArea mPixelArea;
206   Vector2   mGeometrySize;
207
208   ResourceId  mTextureId;
209
210   // flags
211   MeshType  mMeshType        : 3; // 4 values fits in 3 bits just fine
212   bool      mIsMeshGenerated : 1;
213   bool      mBorderInPixels  : 1;
214   bool      mUsePixelArea    : 1;
215
216 };
217
218 } // namespace SceneGraph
219
220 } // namespace Internal
221
222 } // namespace Dali
223
224 #endif // __DALI_INTERNAL_SCENE_GRAPH_IMAGE_RENDERER_H__