1 #ifndef DALI_INTERNAL_SCENE_GRAPH_LAYER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_LAYER_H
5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/internal/event/common/event-thread-services.h>
23 #include <dali/internal/update/nodes/node.h>
24 #include <dali/public-api/actors/layer.h>
30 // value types used by messages
32 struct ParameterType<Dali::Layer::SortFunctionType>
33 : public BasicType<Dali::Layer::SortFunctionType>
37 struct ParameterType<Dali::Layer::Behavior>
38 : public BasicType<Dali::Layer::Behavior>
47 * Pair of node-renderer
51 Renderable() = default;
53 Renderable(Node* node, RendererKey renderer)
60 RendererKey mRenderer{};
63 } // namespace SceneGraph
64 } // Namespace Internal
66 /// Enable Renderable to be used as a trivial type in Dali::Vector.
68 struct TypeTraits<Internal::SceneGraph::Renderable> : public Dali::BasicTypes<Internal::SceneGraph::Renderable>
72 IS_TRIVIAL_TYPE = true
81 using RenderableContainer = Dali::Vector<Renderable>;
84 * Layers have a "depth" relative to all other layers in the scene-graph.
85 * Non-layer child nodes are considered part of the layer.
87 * Layers are rendered separately, and by default the depth buffer is cleared before each layer is rendered.
88 * Objects in higher layers, are rendered after (in front of) objects in lower layers.
90 class Layer : public Node
93 using SortFunctionType = Dali::Layer::SortFunctionType;
98 * Construct a new Layer.
99 * @return A smart-pointer to a newly allocated Node
101 static SceneGraph::Layer* New();
109 * From Node, to convert a node to a layer.
112 Layer* GetLayer() override
118 * Sets the sort-function of a layer.
119 * @param [in] function The new sort-function.
121 void SetSortFunction(Dali::Layer::SortFunctionType function);
124 * Retrieve the function used to sort semi-transparent geometry in this layer.
125 * @return The sort function.
127 Dali::Layer::SortFunctionType GetSortFunction() const
129 return mSortFunction;
133 * Sets whether clipping is enabled for a layer.
134 * @param [in] enabled True if clipping is enabled.
136 void SetClipping(bool enabled);
139 * Query whether clipping is enabled for a layer.
140 * @return True if clipping is enabled.
142 bool IsClipping() const
148 * Sets the clipping box of a layer, in window coordinates.
149 * The contents of the layer will not be visible outside this box, when clipping is
150 * enabled. The default clipping box is empty (0,0,0,0).
151 * @param [in] box The clipping box
153 void SetClippingBox(const ClippingBox& box);
156 * Retrieves the clipping box of a layer, in window coordinates.
157 * @return The clipping box
159 const ClippingBox& GetClippingBox() const
165 * Sets the behavior of the layer
166 * @param [in] behavior The behavior of the layer
168 void SetBehavior(Dali::Layer::Behavior behavior);
171 * Retrieves the behavior of the layer.
172 * @return The behavior
174 Dali::Layer::Behavior GetBehavior() const
180 * @copydoc Dali::Layer::SetDepthTestDisabled()
182 void SetDepthTestDisabled(bool disable);
185 * @copydoc Dali::Layer::IsDepthTestDisabled()
187 bool IsDepthTestDisabled() const;
190 * Enables the reuse of the model view matrices of all renderers for this layer
191 * @param[in] updateBufferIndex The current update buffer index.
192 * @param[in] value to set
194 void SetReuseRenderers(BufferIndex updateBufferIndex, bool value)
196 mAllChildTransformsClean[updateBufferIndex] = value;
200 * Get the reuse of the model view matrices of all renderers for this layer is enabled.
201 * @param[in] updateBufferIndex The current update buffer index.
202 * @return Whether all child transform was clean or not.
204 bool GetReuseRenderers(BufferIndex updateBufferIndex)
206 return mAllChildTransformsClean[updateBufferIndex];
210 * Checks if it is ok to reuse renderers. Renderers can be reused if ModelView transform for all the renderers
211 * has not changed from previous use.
212 * @param[in] camera A pointer to the camera that we want to use to render the list.
213 * @return True if all children transforms have been clean for two consecutive frames and the camera we are going
214 * to use is the same than the one used before ( Otherwise View transform will be different )
217 bool CanReuseRenderers(const Camera* camera)
219 bool bReturn(mAllChildTransformsClean[0] && mAllChildTransformsClean[1] && camera == mLastCamera);
220 mLastCamera = camera;
226 * @return True if default sort function is used
228 bool UsesDefaultSortFunction()
230 return mIsDefaultSortFunction;
234 * Clears all the renderable lists
236 void ClearRenderables();
240 * Private constructor.
241 * See also Layer::New()
245 // Delete copy and move
246 Layer(const Layer&) = delete;
247 Layer(Layer&&) = delete;
248 Layer& operator=(const Layer& rhs) = delete;
249 Layer& operator=(Layer&& rhs) = delete;
251 public: // For update-algorithms
252 RenderableContainer colorRenderables;
253 RenderableContainer overlayRenderables;
256 SortFunctionType mSortFunction; ///< Used to sort semi-transparent geometry
258 ClippingBox mClippingBox; ///< The clipping box, in window coordinates
259 const Camera* mLastCamera; ///< Pointer to the last camera that has rendered the layer
261 Dali::Layer::Behavior mBehavior; ///< The behavior of the layer
263 bool mAllChildTransformsClean[2]; ///< True if all child nodes transforms are clean,
264 /// double buffered as we need two clean frames before we can reuse N-1 for N+1
265 /// this allows us to cache render items when layer is "static"
266 bool mIsClipping : 1; ///< True when clipping is enabled
267 bool mDepthTestDisabled : 1; ///< Whether depth test is disabled.
268 bool mIsDefaultSortFunction : 1; ///< whether the default depth sort function is used
271 // Messages for Layer
274 * Create a message to set the sort-function of a layer
275 * @param[in] layer The layer
276 * @param[in] function The new sort-function.
278 inline void SetSortFunctionMessage(EventThreadServices& eventThreadServices, const Layer& layer, Dali::Layer::SortFunctionType function)
280 using LocalType = MessageValue1<Layer, Dali::Layer::SortFunctionType>;
282 // Reserve some memory inside the message queue
283 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
285 // Construct message in the message queue memory; note that delete should not be called on the return value
286 new(slot) LocalType(&layer, &Layer::SetSortFunction, function);
290 * Create a message for enabling/disabling layer clipping
291 * @param[in] layer The layer
292 * @param[in] enabled True if clipping is enabled
294 inline void SetClippingMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool enabled)
296 using LocalType = MessageValue1<Layer, bool>;
298 // Reserve some memory inside the message queue
299 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
301 // Construct message in the message queue memory; note that delete should not be called on the return value
302 new(slot) LocalType(&layer, &Layer::SetClipping, enabled);
306 * Create a message to set the clipping box of a layer
307 * @param[in] layer The layer
308 * @param[in] clippingbox The clipping box
310 inline void SetClippingBoxMessage(EventThreadServices& eventThreadServices, const Layer& layer, const Dali::ClippingBox& clippingbox)
312 using LocalType = MessageValue1<Layer, Dali::ClippingBox>;
314 // Reserve some memory inside the message queue
315 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
317 // Construct message in the message queue memory; note that delete should not be called on the return value
318 new(slot) LocalType(&layer, &Layer::SetClippingBox, clippingbox);
322 * Create a message to set the behavior of a layer
323 * @param[in] layer The layer
324 * @param[in] behavior The behavior
326 inline void SetBehaviorMessage(EventThreadServices& eventThreadServices,
328 Dali::Layer::Behavior behavior)
330 using LocalType = MessageValue1<Layer, Dali::Layer::Behavior>;
332 // Reserve some memory inside the message queue
333 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
335 // Construct message in the message queue memory; note that delete should not be called on the return value
336 new(slot) LocalType(&layer, &Layer::SetBehavior, behavior);
340 * Create a message for disabling/enabling depth test.
342 * @see Dali::Layer::SetDepthTestDisabled().
344 * @param[in] layer The layer
345 * @param[in] disable \e true disables depth test. \e false sets the default behavior.
347 inline void SetDepthTestDisabledMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool disable)
349 using LocalType = MessageValue1<Layer, bool>;
351 // Reserve some memory inside the message queue
352 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
354 // Construct message in the message queue memory; note that delete should not be called on the return value
355 new(slot) LocalType(&layer, &Layer::SetDepthTestDisabled, disable);
358 } // namespace SceneGraph
360 // Template specialisation for OwnerPointer<Layer>, because delete is protected
362 inline void OwnerPointer<Dali::Internal::SceneGraph::Layer>::Reset()
364 if(mObject != nullptr)
366 Dali::Internal::SceneGraph::Node::Delete(mObject);
370 } // namespace Internal
374 #endif // DALI_INTERNAL_SCENE_GRAPH_LAYER_H