1 #ifndef DALI_INTERNAL_SCENE_GRAPH_LAYER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_LAYER_H
5 * Copyright (c) 2024 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
80 using RenderableContainer = Dali::Vector<Renderable>;
83 * Layers have a "depth" relative to all other layers in the scene-graph.
84 * Non-layer child nodes are considered part of the layer.
86 * Layers are rendered separately, and by default the depth buffer is cleared before each layer is rendered.
87 * Objects in higher layers, are rendered after (in front of) objects in lower layers.
89 class Layer : public Node
92 using SortFunctionType = Dali::Layer::SortFunctionType;
97 * Construct a new Layer.
98 * @return A smart-pointer to a newly allocated Node
100 static SceneGraph::Layer* New();
108 * From Node, to convert a node to a layer.
111 Layer* GetLayer() override
117 * Sets the sort-function of a layer.
118 * @param [in] function The new sort-function.
120 void SetSortFunction(Dali::Layer::SortFunctionType function);
123 * Retrieve the function used to sort semi-transparent geometry in this layer.
124 * @return The sort function.
126 Dali::Layer::SortFunctionType GetSortFunction() const
128 return mSortFunction;
132 * Sets whether clipping is enabled for a layer.
133 * @param [in] enabled True if clipping is enabled.
135 void SetClipping(bool enabled);
138 * Query whether clipping is enabled for a layer.
139 * @return True if clipping is enabled.
141 bool IsClipping() const
147 * Sets the clipping box of a layer, in window coordinates.
148 * The contents of the layer will not be visible outside this box, when clipping is
149 * enabled. The default clipping box is empty (0,0,0,0).
150 * @param [in] box The clipping box
152 void SetClippingBox(const ClippingBox& box);
155 * Retrieves the clipping box of a layer, in window coordinates.
156 * @return The clipping box
158 const ClippingBox& GetClippingBox() const
164 * Sets the behavior of the layer
165 * @param [in] behavior The behavior of the layer
167 void SetBehavior(Dali::Layer::Behavior behavior);
170 * Retrieves the behavior of the layer.
171 * @return The behavior
173 Dali::Layer::Behavior GetBehavior() const
179 * @copydoc Dali::Layer::SetDepthTestDisabled()
181 void SetDepthTestDisabled(bool disable);
184 * @copydoc Dali::Layer::IsDepthTestDisabled()
186 bool IsDepthTestDisabled() const;
189 * Enables the reuse of the model view matrices of all renderers for this layer
190 * @param[in] updateBufferIndex The current update buffer index.
191 * @param[in] value to set
193 void SetReuseRenderers(BufferIndex updateBufferIndex, bool value)
195 mAllChildTransformsClean[updateBufferIndex] = value;
199 * Get the reuse of the model view matrices of all renderers for this layer is enabled.
200 * @param[in] updateBufferIndex The current update buffer index.
201 * @return Whether all child transform was clean or not.
203 bool GetReuseRenderers(BufferIndex updateBufferIndex) const
205 return mAllChildTransformsClean[updateBufferIndex];
209 * Checks if it is ok to reuse renderers. Renderers can be reused if ModelView transform for all the renderers
210 * has not changed from previous use.
211 * @param[in] camera A pointer to the camera that we want to use to render the list.
212 * @return True if all children transforms have been clean for two consecutive frames and the camera we are going
213 * to use is the same than the one used before ( Otherwise View transform will be different )
216 bool CanReuseRenderers(const Camera* camera)
218 bool bReturn(mAllChildTransformsClean[0] && mAllChildTransformsClean[1] && camera == mLastCamera);
219 mLastCamera = camera;
225 * @return True if default sort function is used
227 bool UsesDefaultSortFunction()
229 return mIsDefaultSortFunction;
233 * Clears all the renderable lists
235 void ClearRenderables();
239 * Private constructor.
240 * See also Layer::New()
244 // Delete copy and move
245 Layer(const Layer&) = delete;
246 Layer(Layer&&) = delete;
247 Layer& operator=(const Layer& rhs) = delete;
248 Layer& operator=(Layer&& rhs) = delete;
250 public: // For update-algorithms
251 RenderableContainer colorRenderables;
252 RenderableContainer overlayRenderables;
255 SortFunctionType mSortFunction; ///< Used to sort semi-transparent geometry
257 ClippingBox mClippingBox; ///< The clipping box, in window coordinates
258 const Camera* mLastCamera; ///< Pointer to the last camera that has rendered the layer
260 Dali::Layer::Behavior mBehavior; ///< The behavior of the layer
262 bool mAllChildTransformsClean[2]; ///< True if all child nodes transforms are clean,
263 /// double buffered as we need two clean frames before we can reuse N-1 for N+1
264 /// this allows us to cache render items when layer is "static"
265 bool mIsClipping : 1; ///< True when clipping is enabled
266 bool mDepthTestDisabled : 1; ///< Whether depth test is disabled.
267 bool mIsDefaultSortFunction : 1; ///< whether the default depth sort function is used
270 // Messages for Layer
273 * Create a message to set the sort-function of a layer
274 * @param[in] layer The layer
275 * @param[in] function The new sort-function.
277 inline void SetSortFunctionMessage(EventThreadServices& eventThreadServices, const Layer& layer, Dali::Layer::SortFunctionType function)
279 using LocalType = MessageValue1<Layer, Dali::Layer::SortFunctionType>;
281 // Reserve some memory inside the message queue
282 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
284 // Construct message in the message queue memory; note that delete should not be called on the return value
285 new(slot) LocalType(&layer, &Layer::SetSortFunction, function);
289 * Create a message for enabling/disabling layer clipping
290 * @param[in] layer The layer
291 * @param[in] enabled True if clipping is enabled
293 inline void SetClippingMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool enabled)
295 using LocalType = MessageValue1<Layer, bool>;
297 // Reserve some memory inside the message queue
298 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
300 // Construct message in the message queue memory; note that delete should not be called on the return value
301 new(slot) LocalType(&layer, &Layer::SetClipping, enabled);
305 * Create a message to set the clipping box of a layer
306 * @param[in] layer The layer
307 * @param[in] clippingbox The clipping box
309 inline void SetClippingBoxMessage(EventThreadServices& eventThreadServices, const Layer& layer, const Dali::ClippingBox& clippingbox)
311 using LocalType = MessageValue1<Layer, Dali::ClippingBox>;
313 // Reserve some memory inside the message queue
314 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
316 // Construct message in the message queue memory; note that delete should not be called on the return value
317 new(slot) LocalType(&layer, &Layer::SetClippingBox, clippingbox);
321 * Create a message to set the behavior of a layer
322 * @param[in] layer The layer
323 * @param[in] behavior The behavior
325 inline void SetBehaviorMessage(EventThreadServices& eventThreadServices,
327 Dali::Layer::Behavior behavior)
329 using LocalType = MessageValue1<Layer, Dali::Layer::Behavior>;
331 // Reserve some memory inside the message queue
332 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
334 // Construct message in the message queue memory; note that delete should not be called on the return value
335 new(slot) LocalType(&layer, &Layer::SetBehavior, behavior);
339 * Create a message for disabling/enabling depth test.
341 * @see Dali::Layer::SetDepthTestDisabled().
343 * @param[in] layer The layer
344 * @param[in] disable \e true disables depth test. \e false sets the default behavior.
346 inline void SetDepthTestDisabledMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool disable)
348 using LocalType = MessageValue1<Layer, bool>;
350 // Reserve some memory inside the message queue
351 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
353 // Construct message in the message queue memory; note that delete should not be called on the return value
354 new(slot) LocalType(&layer, &Layer::SetDepthTestDisabled, disable);
357 } // namespace SceneGraph
359 // Template specialisation for OwnerPointer<Layer>, because delete is protected
361 inline void OwnerPointer<Dali::Internal::SceneGraph::Layer>::Reset()
363 if(mObject != nullptr)
365 Dali::Internal::SceneGraph::Node::Delete(mObject);
369 } // namespace Internal
373 #endif // DALI_INTERNAL_SCENE_GRAPH_LAYER_H