Messages and ownership of update objects
[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/internal/common/event-to-update.h>
22 #include <dali/internal/update/common/double-buffered.h>
23 #include <dali/internal/update/common/property-owner.h>
24 #include <dali/internal/render/renderers/sampler-data-provider.h>
25
26 #include <string>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32 namespace SceneGraph
33 {
34
35 class Sampler : public PropertyOwner, public SamplerDataProvider
36 {
37 public:
38   typedef Dali::Sampler::FilterMode FilterMode;
39   typedef Dali::Sampler::WrapMode   WrapMode;
40
41   /**
42    * Constructor
43    */
44   Sampler( const std::string& samplerName );
45
46   /**
47    * Destructor
48    */
49   virtual ~Sampler();
50
51   /**
52    * Set the uniform name of this sampler. This allows the shader to find the
53    * GL index of this sampler.
54    */
55   void SetUniformName( const std::string& samplerName );
56
57   /**
58    * Set the texture identity of this sampler
59    * @param[in] textureId The identity of the texture
60    */
61   void SetTextureId( ResourceId textureId );
62
63   /**
64    * Set the filter modes for minify and magnify filters
65    * @param[in] bufferIndex The buffer index to use
66    * @param[in] minFilter The minify filter
67    * @param[in] magFilter The magnify filter
68    */
69   void SetFilterMode( BufferIndex bufferIndex, FilterMode minFilter, FilterMode magFilter );
70
71   /**
72    * @param[in] bufferIndex The buffer index to use
73    */
74   void SetWrapMode( BufferIndex bufferIndex, WrapMode uWrap, WrapMode vWrap );
75
76 public: // SamplerDataProvider interface
77   /**
78    *
79    */
80   const std::string& GetUniformName();
81
82   /**
83    * Get the texture ID
84    * @return the identity of the associated texture
85    */
86   virtual ResourceId GetTextureId();
87
88   /**
89    * Get the filter mode
90    * @param[in] bufferIndex The buffer index to use
91    * @return The minify filter mode
92    */
93   virtual FilterMode GetMinifyFilterMode( BufferIndex bufferIndex );
94
95   /**
96    * Get the filter mode
97    * @param[in] bufferIndex The buffer index to use
98    * @return The magnify filter mode
99    */
100   virtual FilterMode GetMagnifyFilterMode( BufferIndex bufferIndex );
101
102   /**
103    * Get the horizontal wrap mode
104    * @param[in] bufferIndex The buffer index to use
105    * @return The horizontal wrap mode
106    */
107   virtual WrapMode GetUWrapMode( BufferIndex bufferIndex );
108
109   /**
110    * Get the vertical wrap mode
111    * @param[in] bufferIndex The buffer index to use
112    * @return The vertical wrap mode
113    */
114   virtual WrapMode GetVWrapMode( BufferIndex bufferIndex );
115
116   /**
117    * @return true if the texture is fully opaque.
118    * @todo MESH_REWORK Requires access to ResourceManager::GetBitmapMetadata()
119    */
120   bool IsFullyOpaque();
121
122 private:
123   std::string mUniformName; ///< The name of the uniform referencing this sampler
124   ResourceId mTextureId;    ///< The identity of the associated texture
125
126   DoubleBuffered<FilterMode> mMinFilter;    ///< The minify filter
127   DoubleBuffered<FilterMode> mMagFilter;    ///< The magnify filter
128   DoubleBuffered<WrapMode> mUWrapMode;      ///< The horizontal wrap mode
129   DoubleBuffered<WrapMode> mVWrapMode;      ///< The vertical wrap mode
130 };
131
132 inline void SetUniformNameMessage( EventToUpdate& eventToUpdate, const Sampler& sampler, const std::string& name )
133 {
134   typedef MessageValue1< Sampler, std::string > LocalType;
135
136   // Reserve some memory inside the message queue
137   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
138
139   // Construct message in the message queue memory; note that delete should not be called on the return value
140   new (slot) LocalType( &sampler, &Sampler::SetUniformName, name );
141 }
142
143 } // namespace SceneGraph
144
145 // Declare enum as a message parameter type outside the SceneGraph namespace
146 template <> struct ParameterType< SceneGraph::Sampler::FilterMode > : public BasicType< SceneGraph::Sampler::FilterMode > {};
147
148 namespace SceneGraph
149 {
150
151 inline void SetFilterModeMessage( EventToUpdate& eventToUpdate, const Sampler& sampler, Sampler::FilterMode minFilter, Sampler::FilterMode magFilter )
152 {
153   typedef MessageDoubleBuffered2< Sampler, Sampler::FilterMode, Sampler::FilterMode > LocalType;
154
155   // Reserve some memory inside the message queue
156   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
157
158   // Construct message in the message queue memory; note that delete should not be called on the return value
159   new (slot) LocalType( &sampler, &Sampler::SetFilterMode, minFilter, magFilter );
160 }
161
162 } // namespace SceneGraph
163
164 // Declare enum as a message parameter type
165 template <> struct ParameterType< SceneGraph::Sampler::WrapMode > : public BasicType< SceneGraph::Sampler::WrapMode > {};
166
167 namespace SceneGraph
168 {
169
170 inline void SetWrapModeMessage( EventToUpdate& eventToUpdate, const SceneGraph::Sampler& sampler, SceneGraph::Sampler::WrapMode horizontalWrap, SceneGraph::Sampler::WrapMode verticalWrap )
171 {
172   typedef MessageDoubleBuffered2< Sampler, Sampler::WrapMode, Sampler::WrapMode > LocalType;
173
174   // Reserve some memory inside the message queue
175   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
176
177   // Construct message in the message queue memory; note that delete should not be called on the return value
178   new (slot) LocalType( &sampler, &Sampler::SetWrapMode, horizontalWrap, verticalWrap );
179 }
180
181
182 } // namespace SceneGraph
183 } // namespace Internal
184 } // namespace Dali
185
186
187 #endif //  DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H