Removing rendering backend
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-property-buffer.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_PROPERTY_BUFFER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_PROPERTY_BUFFER_H
3
4 /*
5  * Copyright (c) 2018 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 #include <dali/public-api/actors/sampling.h>
20 #include <dali/public-api/common/vector-wrapper.h>
21 #include <dali/public-api/rendering/sampler.h>
22 #include <dali/graphics-api/graphics-api-accessor.h>
23 #include <dali/graphics-api/graphics-api-buffer.h>
24 #include <dali/integration-api/graphics/graphics.h>
25 #include <dali/internal/common/message.h>
26 #include <dali/internal/common/owner-pointer.h>
27 #include <dali/internal/event/common/event-thread-services.h>
28 #include <dali/internal/update/rendering/scene-graph-sampler.h>
29
30 namespace Dali
31 {
32 namespace Graphics
33 {
34 namespace API
35 {
36 class Controller;
37 } // API
38 } // Graphics
39
40 namespace Internal
41 {
42 namespace SceneGraph
43 {
44
45 class PropertyBuffer
46 {
47 public:
48
49   struct Component
50   {
51     std::string     name;
52     unsigned int    offset;
53     unsigned int    size;
54     Property::Type  type;
55   };
56
57   /**
58    * Structure that holds the meta-data of the format of PropertyBuffer.
59    */
60   struct Format
61   {
62     std::vector<Component> components;
63     unsigned int           size;
64   };
65
66   /**
67    * @brief Default constructor
68    */
69   PropertyBuffer();
70
71   /**
72    * @brief Destructor
73    */
74   ~PropertyBuffer();
75
76   /**
77    * Initialize the shader object with the Graphics API when added to UpdateManager
78    *
79    * @param[in] graphics The Graphics API
80    */
81   void Initialize( Integration::Graphics::Graphics& graphics );
82
83   /**
84    * @brief Set the format of the buffer
85    *
86    * This function takes ownership of the pointer
87    *
88    * @param[in] format The format for the PropertyBuffer
89    */
90   void SetFormat( OwnerPointer<PropertyBuffer::Format>& format );
91
92
93   /**
94    * @brief Set the data of the PropertyBuffer
95    *
96    * This function takes ownership of the pointer
97    * @param[in] data The new data of the PropertyBuffer
98    * @param[in] size The new size of the buffer
99    */
100   void SetData( OwnerPointer< Dali::Vector<char> >& data, size_t size );
101
102   /**
103    * @brief Set the number of elements
104    * @param[in] size The number of elements
105    */
106   void SetSize( unsigned int size );
107
108   /**
109    * Perform the upload of the buffer only when required
110    * @param[in] controller
111    * @return
112    */
113   bool Update( Dali::Graphics::API::Controller& controller );
114
115   /**
116    * Get the number of attributes present in the buffer
117    * @return The number of attributes stored in this buffer
118    */
119   inline unsigned int GetAttributeCount() const
120   {
121     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
122     return mFormat->components.size();
123   }
124
125   /**
126    * Retrieve the i-essim attribute name
127    * @param[in] index The index of the attribute
128    * @return The name of the attribute
129    */
130   inline const std::string& GetAttributeName(unsigned int index ) const
131   {
132     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
133     return mFormat->components[index].name;
134   }
135
136   /**
137    * Retrieve the size of the buffer in bytes
138    * @return The total size of the buffer
139    */
140   inline std::size_t GetDataSize() const
141   {
142     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
143     return mFormat->size * mSize;
144   }
145
146   /**
147    * Retrieve the size of one element of the buffer
148    * @return The size of one element
149    */
150   inline std::size_t GetElementSize() const
151   {
152     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
153     return mFormat->size;
154   }
155
156   /**
157    * Retrieve the number of elements in the buffer
158    * @return The total number of elements
159    */
160   inline unsigned int GetElementCount() const
161   {
162     return mSize;
163   }
164
165   inline const PropertyBuffer::Format* GetFormat() const
166   {
167     return mFormat.Get();
168   }
169   inline Graphics::API::Accessor<Graphics::API::Buffer> GetGfxObject() const
170   {
171     return mGraphicsBuffer;
172   }
173
174   void SetUsage( Graphics::API::Buffer::UsageHint usage );
175
176 private:
177   Integration::Graphics::Graphics*        mGraphics;  ///< Graphics interface object
178   OwnerPointer< PropertyBuffer::Format >  mFormat;    ///< Format of the buffer
179   OwnerPointer< Dali::Vector< char > >    mData;      ///< Data
180
181   size_t mSize;       ///< Number of Elements in the buffer
182   bool mDataChanged;  ///< Flag to know if data has changed in a frame
183
184   // GRAPHICS
185   Graphics::API::Accessor<Graphics::API::Buffer> mGraphicsBuffer;
186   Graphics::API::Buffer::UsageHint mGraphicsBufferUsage;
187 };
188
189
190 inline void SetPropertyBufferFormatMessage( EventThreadServices& eventThreadServices, SceneGraph::PropertyBuffer& propertyBuffer, OwnerPointer< SceneGraph::PropertyBuffer::Format>& format )
191 {
192   // Message has ownership of PropertyBuffer::Format while in transit from event -> update
193   typedef MessageValue1< SceneGraph::PropertyBuffer, OwnerPointer< SceneGraph::PropertyBuffer::Format> > LocalType;
194
195   // Reserve some memory inside the message queue
196   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
197
198   // Construct message in the message queue memory; note that delete should not be called on the return value
199   new (slot) LocalType( &propertyBuffer, &PropertyBuffer::SetFormat, format );
200 }
201
202 inline void SetPropertyBufferDataMessage( EventThreadServices& eventThreadServices, SceneGraph::PropertyBuffer& propertyBuffer, OwnerPointer< Vector<char> >& data, size_t size )
203 {
204   // Message has ownership of PropertyBuffer data while in transit from event -> update
205   typedef MessageValue2< SceneGraph::PropertyBuffer, OwnerPointer< Vector<char> >, size_t  > LocalType;
206
207   // Reserve some memory inside the message queue
208   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
209
210   // Construct message in the message queue memory; note that delete should not be called on the return value
211   new (slot) LocalType( &propertyBuffer, &PropertyBuffer::SetData, data, size );
212 }
213
214 } // namespace SceneGraph
215
216 } // namespace Internal
217
218 } // namespace Dali
219
220
221 #endif //  DALI_INTERNAL_SCENE_GRAPH_PROPERTY_BUFFER_H