Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / skia / config / sk_ref_cnt_ext_debug.h
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef SKIA_CONFIG_SK_REF_CNT_EXT_DEBUG_H_
6 #define SKIA_CONFIG_SK_REF_CNT_EXT_DEBUG_H_
7
8 #ifdef SKIA_CONFIG_SK_REF_CNT_EXT_RELEASE_H_
9 #error Only one SkRefCnt should be used.
10 #endif
11
12 #include <atomic>
13
14 class SkRefCnt;
15
16 namespace WTF {
17   void adopted(const SkRefCnt*);
18   void requireAdoption(const SkRefCnt*);
19 }
20
21 // Alternate implementation of SkRefCnt for Chromium debug builds
22 class SK_API SkRefCnt : public SkRefCntBase {
23 public:
24   SkRefCnt();
25   ~SkRefCnt() override;
26   void ref() const { SkASSERT(flags_.load() != AdoptionRequired_Flag); SkRefCntBase::ref(); }
27   void deref() const { SkRefCntBase::unref(); }
28 private:
29   void adopted() const { flags_ |= Adopted_Flag; }
30   void requireAdoption() const { flags_ |= AdoptionRequired_Flag; }
31
32   enum {
33     Adopted_Flag = 0x1,
34     AdoptionRequired_Flag = 0x2,
35   };
36
37   mutable std::atomic<int> flags_;
38
39   friend void WTF::adopted(const SkRefCnt*);
40   friend void WTF::requireAdoption(const SkRefCnt*);
41 };
42
43 inline SkRefCnt::SkRefCnt() : flags_(0) { }
44
45 inline SkRefCnt::~SkRefCnt() { }
46
47 // Bootstrap for Blink's WTF::RefPtr
48
49 namespace WTF {
50 inline void adopted(const SkRefCnt* object) {
51   if (!object)
52     return;
53   object->adopted();
54 }
55 inline void requireAdoption(const SkRefCnt* object) {
56   if (!object)
57     return;
58   object->requireAdoption();
59 }
60 }  // namespace WTF
61
62 using WTF::adopted;
63 using WTF::requireAdoption;
64
65 #endif  // SKIA_CONFIG_SK_REF_CNT_EXT_DEBUG_H_