[dali_1.2.0] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-geometry.h
1 #ifndef DALI_INTERNAL_RENDER_GEOMETRY_H
2 #define DALI_INTERNAL_RENDER_GEOMETRY_H
3
4 /*
5  * Copyright (c) 2016 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 <dali/public-api/common/dali-vector.h>
21 #include <dali/public-api/rendering/geometry.h>
22 #include <dali/devel-api/common/owner-container.h>
23 #include <dali/internal/common/owner-pointer.h>
24 #include <dali/internal/common/buffer-index.h>
25 #include <dali/internal/common/owner-pointer.h>
26 #include <dali/integration-api/gl-abstraction.h>
27 #include <dali/integration-api/gl-defines.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 class Context;
34 class Program;
35 class GpuBuffer;
36
37 namespace Render
38 {
39 class PropertyBuffer;
40
41 /**
42  * This class encapsulates the GPU buffers. It is used to upload vertex data
43  * to it's GPU buffers, to bind all the buffers and to setup/teardown vertex attribute
44  * bindings
45  */
46 class Geometry
47 {
48 public:
49   typedef Dali::Geometry::Type Type;
50
51   Geometry();
52
53   /**
54    * Destructor
55    */
56   ~Geometry();
57
58   /**
59    * Called on Gl Context created
60    */
61   void GlContextCreated( Context& context );
62
63   /**
64    * Called on Gl Context destroyed.
65    */
66   void GlContextDestroyed();
67
68   /**
69    * Adds a property buffer to the geometry
70    * @param[in] dataProvider The PropertyBuffer data provider
71    */
72   void AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer );
73
74   /**
75    * Set the data for the index buffer to be used by the geometry
76    * @param[in] indices A vector containing the indices
77    */
78   void SetIndexBuffer( Dali::Vector<unsigned short>& indices );
79
80   /**
81    * Obtains pointer to the storage of indexed elements
82    * @return Pointer to the index buffer
83    */
84   const Dali::Vector<unsigned short>* GetIndexBuffer() const;
85
86   /**
87    * Removes a PropertyBuffer from the geometry
88    * @param[in] propertyBuffer The property buffer to be removed
89    */
90   void RemovePropertyBuffer(  const Render::PropertyBuffer* propertyBuffer );
91
92   /**
93    * Returns property buffer at specified index
94    * @param[in] index of the property buffer
95    * @return pointer to the property buffer or NULL
96    */
97   const Render::PropertyBuffer* GetPropertyBuffer( size_t index ) const;
98
99   /**
100    * Gets the attribute locations on the shader for the attributes defined in the geometry RenderBuffers
101    * @param[out] attributeLocation The vector where the attributes locations will be stored
102    * @param[in] program The program
103    * @param[in] bufferIndex The current buffer index
104    */
105   void GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const;
106
107   /**
108    * Called from RenderManager to notify the geometry that current rendering pass has finished.
109    */
110   void OnRenderFinished();
111
112   /**
113    * Chack if the attributes for the geometry have changed
114    * @return True if vertex buffers have been added or removed since last frame, false otherwise
115    */
116   bool AttributesChanged() const
117   {
118     return mAttributesChanged;
119   }
120
121   /**
122    * Sets the geometry type
123    * @param[in] type The new geometry type
124    */
125   void SetType( Type type )
126   {
127     mGeometryType = type;
128   }
129
130   /**
131    * Upload the geometry if it has changed, set up the attributes and perform
132    * the Draw call corresponding to the geometry type
133    * @param[in] context The GL context
134    * @param[in] bufferIndex The current buffer index
135    * @param[in] attributeLocation The location for the attributes in the shader
136    * @param[in] elementBufferOffset The index of first element to draw if index buffer bound
137    * @param[in] elementBufferCount Number of elements to draw if index buffer bound, uses whole buffer when 0
138    */
139   void UploadAndDraw(Context& context,
140                      BufferIndex bufferIndex,
141                      Vector<GLint>& attributeLocation,
142                      size_t elementBufferOffset,
143                      size_t elementBufferCount );
144
145 private:
146
147   // PropertyBuffers
148   Vector< Render::PropertyBuffer* > mVertexBuffers;
149
150   Dali::Vector< unsigned short> mIndices;
151   OwnerPointer< GpuBuffer > mIndexBuffer;
152   Type mGeometryType;
153
154   // Booleans
155   bool mIndicesChanged : 1;
156   bool mHasBeenUpdated : 1;
157   bool mAttributesChanged : 1;
158 };
159
160 } // namespace Render
161 } // namespace Internal
162 } // namespace Dali
163
164 #endif // DALI_INTERNAL_RENDER_GEOMETRY_H