d50ccc5b4f2bea6dcb00db63723280e53a309a4b
[platform/core/uifw/dali-core.git] / dali / internal / update / effects / scene-graph-sampler.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_SAMPLER_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 #include <dali/public-api/shader-effects/sampler.h>
21 #include <dali/integration-api/resource-declarations.h>
22 #include <dali/internal/event/common/event-thread-services.h>
23 #include <dali/internal/update/common/double-buffered.h>
24 #include <dali/internal/update/common/double-buffered-property.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/resources/bitmap-metadata.h>
27 #include <dali/internal/render/data-providers/sampler-data-provider.h>
28
29 #include <string>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace SceneGraph
36 {
37 class SceneController;
38
39 class Sampler : public PropertyOwner, public SamplerDataProvider
40 {
41 public:
42   typedef Dali::Sampler::FilterMode FilterMode;
43   typedef Dali::Sampler::WrapMode   WrapMode;
44
45   /**
46    * Constructor
47    */
48   Sampler( const std::string& samplerName );
49
50   /**
51    * Destructor
52    */
53   virtual ~Sampler();
54
55   /**
56    * Set the uniform name of this sampler. This allows the shader to find the
57    * GL index of this sampler.
58    */
59   void SetUnitName( const std::string& unitName );
60
61   /**
62    * Set the texture identity of this sampler (needs to double buffer this value because
63    * it can be read through the data provider interface in the render thread )
64    * @param[in] bufferIndex The buffer index to use
65    * @param[in] textureId The identity of the texture
66    */
67   void SetTexture( BufferIndex bufferIndex, Integration::ResourceId textureId );
68
69   /**
70    * Set the filter modes for minify and magnify filters
71    * @param[in] bufferIndex The buffer index to use
72    * @param[in] minFilter The minify filter
73    * @param[in] magFilter The magnify filter
74    */
75   void SetFilterMode( BufferIndex bufferIndex, FilterMode minFilter, FilterMode magFilter );
76
77   /**
78    * @param[in] bufferIndex The buffer index to use
79    */
80   void SetWrapMode( BufferIndex bufferIndex, WrapMode uWrap, WrapMode vWrap );
81
82   /**
83    * @param[in] bufferIndex The buffer index to use
84    * @return true if this sampler affects transparency of the material
85    * @note this should only be called from Update thread
86    */
87   bool AffectsTransparency( BufferIndex bufferIndex ) const;
88
89   /**
90    * @param[in] bufferIndex The buffer index to use
91    * @return true if the texture is fully opaque
92    * @note this should only be called from Update thread
93    */
94   bool IsFullyOpaque( BufferIndex bufferIndex ) const;
95
96
97 public: // SamplerDataProvider interface - called from RenderThread
98   /**
99    * Get the texture unit uniform name
100    * @return the name of the texture unit uniform
101    */
102   virtual const std::string& GetUnitName() const;
103
104   /**
105    * Get the texture ID
106    * @param[in] bufferIndex The buffer index to use
107    * @return the identity of the associated texture
108    */
109   virtual Integration::ResourceId GetTextureId(BufferIndex buffer) const;
110
111   /**
112    * Get the filter mode
113    * @param[in] bufferIndex The buffer index to use
114    * @return The minify filter mode
115    */
116   virtual FilterMode GetMinifyFilterMode( BufferIndex bufferIndex ) const;
117
118   /**
119    * Get the filter mode
120    * @param[in] bufferIndex The buffer index to use
121    * @return The magnify filter mode
122    */
123   virtual FilterMode GetMagnifyFilterMode( BufferIndex bufferIndex ) const;
124
125   /**
126    * Get the horizontal wrap mode
127    * @param[in] bufferIndex The buffer index to use
128    * @return The horizontal wrap mode
129    */
130   virtual WrapMode GetUWrapMode( BufferIndex bufferIndex ) const;
131
132   /**
133    * Get the vertical wrap mode
134    * @param[in] bufferIndex The buffer index to use
135    * @return The vertical wrap mode
136    */
137   virtual WrapMode GetVWrapMode( BufferIndex bufferIndex ) const;
138
139   /**
140    * Connect the object to the scene graph
141    *
142    * @param[in] sceneController The scene controller - used for sending messages to render thread
143    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
144    */
145   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
146
147   /**
148    * Disconnect the object from the scene graph
149    * @param[in] sceneController The scene controller - used for sending messages to render thread
150    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
151    */
152   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
153
154 private:
155   std::string mUnitName; ///< The name of the uniform of the texture unit
156
157   // @todo MESH_REWORK Need these to automatically copy
158   // new value into old value on frame change
159
160   ResourceId mTextureId[2]; ///< The identity of the associated texture for this frame (Can be read from RenderThread)
161
162   BitmapMetadata mBitmapMetadata[2]; /// The meta data of the associated texture for this frame (Not needed in RenderThread)
163
164   DoubleBufferedProperty<int> mMinFilter;    ///< The minify filter
165   DoubleBufferedProperty<int> mMagFilter;    ///< The magnify filter
166   DoubleBufferedProperty<int> mUWrapMode;    ///< The horizontal wrap mode
167   DoubleBufferedProperty<int> mVWrapMode;    ///< The vertical wrap mode
168
169   // Note, this is only called from UpdateThread
170   DoubleBufferedProperty<bool>     mAffectsTransparency; ///< If this sampler affects renderer transparency
171 };
172
173 } // namespace SceneGraph
174
175 inline void SetUnitNameMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, const std::string& name )
176 {
177   typedef MessageValue1< SceneGraph::Sampler, std::string > LocalType;
178
179   // Reserve some memory inside the message queue
180   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
181
182   // Construct message in the message queue memory; note that delete should not be called on the return value
183   new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetUnitName, name );
184 }
185
186
187 inline void SetTextureMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, unsigned int resourceId )
188 {
189   typedef MessageDoubleBuffered1< SceneGraph::Sampler, unsigned int > LocalType;
190
191   // Reserve some memory inside the message queue
192   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
193
194   // Construct message in the message queue memory; note that delete should not be called on the return value
195   new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetTexture, resourceId );
196 }
197
198 // Declare enum as a message parameter type outside the SceneGraph namespace
199 template <> struct ParameterType< SceneGraph::Sampler::FilterMode > : public BasicType< SceneGraph::Sampler::FilterMode > {};
200
201
202 inline void SetFilterModeMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, SceneGraph::Sampler::FilterMode minFilter, SceneGraph::Sampler::FilterMode magFilter )
203 {
204   typedef MessageDoubleBuffered2< SceneGraph::Sampler, SceneGraph::Sampler::FilterMode, SceneGraph::Sampler::FilterMode > LocalType;
205
206   // Reserve some memory inside the message queue
207   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
208
209   // Construct message in the message queue memory; note that delete should not be called on the return value
210   new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetFilterMode, minFilter, magFilter );
211 }
212
213 // Declare enum as a message parameter type
214 template <> struct ParameterType< SceneGraph::Sampler::WrapMode > : public BasicType< SceneGraph::Sampler::WrapMode > {};
215
216
217 inline void SetWrapModeMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, SceneGraph::Sampler::WrapMode horizontalWrap, SceneGraph::Sampler::WrapMode verticalWrap )
218 {
219   typedef MessageDoubleBuffered2< SceneGraph::Sampler, SceneGraph::Sampler::WrapMode, SceneGraph::Sampler::WrapMode > LocalType;
220
221   // Reserve some memory inside the message queue
222   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
223
224   // Construct message in the message queue memory; note that delete should not be called on the return value
225   new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetWrapMode, horizontalWrap, verticalWrap );
226 }
227
228 } // namespace Internal
229 } // namespace Dali
230
231
232 #endif //  DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H