Merge changes I8783ad29,I2c860a84 into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / update / common / double-buffered.h
index 1662c3e..65f7cd6 100644 (file)
@@ -1,50 +1,48 @@
-#ifndef __DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H__
-#define __DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H__
-
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+#ifndef DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H
+#define DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H
+
+/*
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // INTERNAL INCLUDES
-#include <dali/public-api/math/matrix.h>
-#include <dali/public-api/math/quaternion.h>
-#include <dali/public-api/math/vector3.h>
+#include <dali/internal/common/buffer-index.h>
+#include <dali/internal/common/owner-pointer.h>
 
 namespace Dali
 {
-
 namespace Internal
 {
-
 // The number of buffers per scene-graph property
 static const unsigned int NUM_SCENE_GRAPH_BUFFERS = 2;
 
-// Buffer index used when reading off-stage values
-static const unsigned int ARBITRARY_OFF_STAGE_BUFFER = 0;
-
 namespace SceneGraph
 {
-
 /**
  * Templated class for a double-buffered value.
- * This simplifies init code, and forces explicit initialization.
  */
-template <typename T>
+template<typename T>
 class DoubleBuffered
 {
 public:
+  DoubleBuffered()
+  : mValue1(),
+    mValue2()
+  {
+  }
 
   DoubleBuffered(const T& val)
   : mValue1(val),
@@ -52,22 +50,21 @@ public:
   {
   }
 
-  inline T& operator[](const size_t i)
+  inline T& operator[](const BufferIndex i)
   {
     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
 
-    return *(&mValue1+i);
+    return *(&mValue1 + i);
   }
 
-  inline const T& operator[](const size_t i) const
+  inline const T& operator[](const BufferIndex i) const
   {
     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
 
-    return *(&mValue1+i);
+    return *(&mValue1 + i);
   }
 
 private:
-
   // Undefined
   DoubleBuffered<T>(const DoubleBuffered<T>&);
 
@@ -75,124 +72,145 @@ private:
   DoubleBuffered<T>& operator=(const DoubleBuffered<T>& rhs);
 
 private:
-
   T mValue1;
   T mValue2;
 };
 
 /**
- * Templated class for a double-buffered value, initialized with 3 value parameters.
- * This simplifies init code, and forces explicit initialization.
+ * @brief Specialization for owner-pointer
+ *
+ * This class takes ownership of the pointers and releases the memory when the pointer
+ * is no longer used by either buffer
  */
-template <typename T, typename P>
-class DoubleBuffered3
+template<typename T>
+class DoubleBuffered<OwnerPointer<T> >
 {
 public:
-
-  DoubleBuffered3(const T& val)
-  : mValue1(val),
-    mValue2(val)
+  /**
+   * Class that deals with setting a value
+   */
+  class Setter
   {
-  }
-
-  DoubleBuffered3(P val1, P val2, P val3)
-  : mValue1(val1, val2, val3),
-    mValue2(val1, val2, val3)
+  public:
+    /**
+     * @brief Assignment operator to that a value that will later
+     * be set in the correct buffer index of the object referenced by the setter.
+     */
+    Setter& operator=(T* value)
+    {
+      mValue = value;
+      return *this;
+    }
+
+    ~Setter()
+    {
+      mObject.Set(mIndex, mValue);
+    }
+
+  private:
+    Setter(DoubleBuffered& object,
+           BufferIndex     i,
+           T*              value)
+    : mObject(object),
+      mIndex(i),
+      mValue(value)
+    {
+    }
+
+    Setter(const Setter& rhs)
+    : mObject(rhs.mObject),
+      mIndex(rhs.mIndex),
+      mValue(rhs.mValue)
+    {
+    }
+
+    DoubleBuffered&   mObject; ///< Double-buffered object that will be changed
+    const BufferIndex mIndex;  ///< Buffer index that will be changed
+    T*                mValue;  ///< Value of the pointer
+
+    friend class DoubleBuffered;
+  };
+
+  DoubleBuffered()
+  : mValue1(NULL),
+    mValue2(NULL)
   {
   }
 
-  inline T& operator[](const size_t i)
+  DoubleBuffered(T* val)
+  : mValue1(val),
+    mValue2(val)
   {
-    DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
-
-    return *(&mValue1+i);
   }
 
-  inline const T& operator[](const size_t i) const
+  ~DoubleBuffered()
   {
-    DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
-
-    return *(&mValue1+i);
+    if(mValue2 != mValue1)
+    {
+      delete mValue2;
+    }
+    delete mValue1;
   }
 
-private:
-
-  // Undefined
-  DoubleBuffered3<T,P>(const DoubleBuffered3<T,P>&);
-
-  // Undefined
-  DoubleBuffered3<T,P>& operator=(const DoubleBuffered3<T,P>& rhs);
-
-private:
-
-  T mValue1;
-  T mValue2;
-};
-
-/**
- * Templated class for a double-buffered value, initialized with 4 value parameters.
- * This simplifies init code, and forces explicit initialization.
- */
-template <typename T, typename P>
-class DoubleBuffered4
-{
-public:
-
-  DoubleBuffered4(const T& val)
-  : mValue1(val),
-    mValue2(val)
+  void Set(BufferIndex i, T* value)
   {
+    T*& current  = *(&mValue1 + i);
+    T*& previous = *(&mValue1 + 1u - i);
+
+    if(current != value && current != previous)
+    {
+      delete current;
+    }
+    current = value;
   }
 
-  DoubleBuffered4(P val1, P val2, P val3, P val4)
-  : mValue1(val1, val2, val3, val4),
-    mValue2(val1, val2, val3, val4)
+  Setter operator[](BufferIndex i)
   {
+    return Setter(*this, i, *(&mValue1 + i));
   }
 
-  inline T& operator[](const size_t i)
+  const T* operator[](BufferIndex i) const
   {
     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
 
-    return *(&mValue1+i);
+    return *(&mValue1 + i);
   }
 
-  inline const T& operator[](const size_t i) const
+  /**
+   * Auto-age the property: if it was set the previous frame,
+   * then copy the value into the current frame's buffer.
+   */
+  void CopyPrevious(BufferIndex i)
   {
     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
 
-    return *(&mValue1+i);
+    T*& current  = *(&mValue1 + i);
+    T*& previous = *(&mValue1 + 1u - i);
+
+    if(current != previous)
+    {
+      delete current;
+    }
+
+    current = previous;
   }
 
 private:
-
   // Undefined
-  DoubleBuffered4<T,P>(const DoubleBuffered4<T,P>&);
+  DoubleBuffered(const DoubleBuffered&);
 
   // Undefined
-  DoubleBuffered4<T,P>& operator=(const DoubleBuffered4<T,P>& rhs);
+  DoubleBuffered& operator=(const DoubleBuffered& rhs);
 
 private:
-
-  T mValue1;
-  T mValue2;
+  T* mValue1;
+  T* mValue2;
 };
 
-typedef DoubleBuffered<int>   DoubleBufferedInt;
-typedef DoubleBuffered<float> DoubleBufferedFloat;
-typedef DoubleBuffered<bool>  DoubleBufferedBool;
-
-typedef DoubleBuffered3<Vector3,float>      DoubleBufferedVector3;
-typedef DoubleBuffered4<Vector4,float>      DoubleBufferedVector4;
-
-typedef DoubleBuffered4<Quaternion, float>  DoubleBufferedQuaternion;
-
-typedef DoubleBuffered<Matrix> DoubleBufferedMatrix;
-
 } // namespace SceneGraph
 
 } // namespace Internal
 
 } // namespace Dali
 
-#endif // __DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H__
+#endif // DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H