lottie/vector: make all copy_on_write class in vector thread safe. 52/184652/1
authorsubhransu mohanty <sub.mohanty@samsung.com>
Thu, 19 Jul 2018 23:36:43 +0000 (08:36 +0900)
committersubhransu mohanty <sub.mohanty@samsung.com>
Fri, 20 Jul 2018 00:48:09 +0000 (09:48 +0900)
Change-Id: Ibf3781775d0d26c0240970b720a59fbfed43f6b2

src/vector/vbitmap.cpp
src/vector/vglobal.h
src/vector/vmatrix.cpp
src/vector/vpath.cpp
src/vector/vregion.cpp
src/vector/vregion.h
src/vector/vrle.cpp

index a8edd233c5f016ca2ed6943a1899ca83a91028c3..c4a0190c91cbaecc34066e863fefbee32b11092c 100644 (file)
@@ -156,7 +156,7 @@ VBitmap::VBitmap(uchar *data, int w, int h, int bytesPerLine, VBitmap::Format fo
     d->cleanupInfo = nullptr;
     d->ownData = false;
     d->roData = false;
-    d->ref = 1;
+    d->ref.setOwned();
 }
 
 VBitmap VBitmap::copy(const VRect& r) const
index 898460024b48a8358bad719a659d8e94a462d921..0585955991bd89f9b7083b1d2fbeac78e9fd84e6 100644 (file)
@@ -36,42 +36,43 @@ typedef uint8_t          uchar;
 
 #define VECTOR_FALLTHROUGH
 
+#include<atomic>
 class RefCount
 {
 public:
-    inline RefCount(){}
     inline RefCount(int i):atomic(i){}
     inline bool ref() {
-        int count = atomic;
+        int count = atomic.load();
         if (count == 0) // !isSharable
             return false;
         if (count != -1) // !isStatic
-            atomic++;
+            atomic.fetch_add(1);
         return true;
     }
     inline bool deref() {
-        int count = atomic;
+        int count = atomic.load();
         if (count == 0) // !isSharable
             return false;
         if (count == -1) // isStatic
             return true;
-        return --atomic;
+        atomic.fetch_sub(1);
+        return --count;
     }
     bool isShared() const
     {
-        int count = atomic;
+        int count = atomic.load();
         return (count != 1) && (count != 0);
     }
     bool isStatic() const
     {
         // Persistent object, never deleted
-        return atomic == -1;
+        int count = atomic.load();
+        return count == -1;
     }
     inline int count()const{return atomic;}
-    void setOwned() { atomic = 1; }
-    void setUnsharable() { atomic = 0; }
+    void setOwned() { atomic.store(1); }
 private:
-    int atomic;
+    std::atomic<int> atomic;
 };
 
 template <typename T>
index 2a72700a7bdab6088d8c9581e711e72bb4d67965..22ec612092134c622705e2cfc981df0f4893a1b7 100644 (file)
@@ -12,6 +12,12 @@ V_BEGIN_NAMESPACE
  */
 
 struct VMatrixData {
+    VMatrixData(): ref(-1),
+                   type(VMatrix::MatrixType::None),
+                   dirty(VMatrix::MatrixType::None),
+                   m11(1), m12(0), m13(0),
+                   m21(0), m22(1), m23(0),
+                   mtx(0), mty(0), m33(1){}
     RefCount             ref;
     VMatrix::MatrixType type;
     VMatrix::MatrixType dirty;
@@ -19,12 +25,9 @@ struct VMatrixData {
     float m21, m22, m23;
     float mtx, mty, m33;
 };
-static const struct VMatrixData shared_empty = {RefCount(-1),
-                                                 VMatrix::MatrixType::None,
-                                                 VMatrix::MatrixType::None,
-                                                 1, 0, 0,
-                                                 0, 1, 0,
-                                                 0, 0, 1};
+
+static const struct VMatrixData shared_empty;
+
 inline float VMatrix::determinant() const
 {
     return d->m11*(d->m33*d->m22 - d->mty*d->m23) -
@@ -95,7 +98,6 @@ VMatrix::~VMatrix()
 VMatrix::VMatrix(bool init V_UNUSED)
 {
     d = new VMatrixData;
-    memcpy(d, &shared_empty, sizeof(VMatrixData));
     d->ref.setOwned();
 }
 
index ba352ee7f35f68e3187137cc7a1447bbdc18d4e4..592bd9779203a7442e3a951db7fc572912eec91a 100644 (file)
@@ -9,6 +9,13 @@ V_BEGIN_NAMESPACE
 
 struct VPathData
 {
+    VPathData():ref(-1),
+                m_points(),
+                m_elements(),
+                m_segments(0),
+                mStartPoint(),
+                mNewSegment(true){}
+
     void copy(VPathData *o);
     void moveTo(const VPointF &pt);
     void lineTo(const VPointF &pt);
@@ -95,12 +102,7 @@ int  VPathData::segments() const
 }
 
 
-static const struct VPathData shared_empty = {RefCount(-1),
-                                               std::vector<VPointF>(),
-                                               std::vector<VPath::Element>(),
-                                               0,
-                                               VPointF(),
-                                               true};
+static const struct VPathData shared_empty;
 
 inline void VPath::cleanUp(VPathData *d)
 {
@@ -117,7 +119,7 @@ VPath VPath::copy() const
 {
     VPath other;
 
-    other.d = new VPathData(shared_empty);
+    other.d = new VPathData;
     other.d->m_points = d->m_points;
     other.d->m_elements = d->m_elements;
     other.d->m_segments = d->m_segments;
index a0cd00e1bd90cf82d7c0f98215df8b079317a580..7738690c26c1d779c0c55634f1698e803b9f1b92 100644 (file)
@@ -2044,7 +2044,7 @@ typedef region_type_t VRegionPrivate;
 V_BEGIN_NAMESPACE
 
 static VRegionPrivate regionPrivate = {{0,0,0,0}, NULL};
-const VRegion::VRegionData VRegion::shared_empty = {RefCount(-1), &regionPrivate};
+const VRegion::VRegionData VRegion::shared_empty;
 
 inline VRect box_to_rect(box_type_t *box)
 {
index e5986af03563acbfc6982034ddbf6b9b8cd9c0c4..b5d019c65b78ce0f9f7b48c772dcecf1c05fff71 100644 (file)
@@ -55,6 +55,7 @@ private:
     void detach();
 
     struct VRegionData {
+        VRegionData():ref(-1),rgn(nullptr){}
         RefCount ref;
         VRegionPrivate *rgn;
     };
index c41871f4b1c361c2cf2274f7d40a37e67568f74a..1d9ef27dd15d3041ba2ed090d9445864c20a662d 100644 (file)
@@ -555,12 +555,12 @@ void VRleImpl::addSpan(const VRle::Span *span, int count)
 
 struct VRleData
 {
+    VRleData():ref(-1), impl(){}
     RefCount    ref;
     VRleImpl   impl;
 };
 
-static const struct VRleData shared_empty = {RefCount(-1),
-                                              VRleImpl()};
+static const struct VRleData shared_empty;
 
 inline void VRle::cleanUp(VRleData *d)
 {