Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / include / core / SkPixelRef.h
1 /*
2  * Copyright 2008 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #ifndef SkPixelRef_DEFINED
9 #define SkPixelRef_DEFINED
10
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSize.h"
16 #include "include/private/SkIDChangeListener.h"
17 #include "include/private/SkMutex.h"
18 #include "include/private/SkTDArray.h"
19
20 #include <atomic>
21
22 struct SkIRect;
23
24 class GrTexture;
25 class SkDiscardableMemory;
26
27 /** \class SkPixelRef
28
29     This class is the smart container for pixel memory, and is used with SkBitmap.
30     This class can be shared/accessed between multiple threads.
31 */
32 class SK_API SkPixelRef : public SkRefCnt {
33 public:
34     SkPixelRef(int width, int height, void* addr, size_t rowBytes);
35     ~SkPixelRef() override;
36
37     SkISize dimensions() const { return {fWidth, fHeight}; }
38     int width() const { return fWidth; }
39     int height() const { return fHeight; }
40     void* pixels() const { return fPixels; }
41     size_t rowBytes() const { return fRowBytes; }
42
43     /** Returns a non-zero, unique value corresponding to the pixels in this
44         pixelref. Each time the pixels are changed (and notifyPixelsChanged is
45         called), a different generation ID will be returned.
46     */
47     uint32_t getGenerationID() const;
48
49     /**
50      *  Call this if you have changed the contents of the pixels. This will in-
51      *  turn cause a different generation ID value to be returned from
52      *  getGenerationID().
53      */
54     void notifyPixelsChanged();
55
56     /** Returns true if this pixelref is marked as immutable, meaning that the
57         contents of its pixels will not change for the lifetime of the pixelref.
58     */
59     bool isImmutable() const { return fMutability != kMutable; }
60
61     /** Marks this pixelref is immutable, meaning that the contents of its
62         pixels will not change for the lifetime of the pixelref. This state can
63         be set on a pixelref, but it cannot be cleared once it is set.
64     */
65     void setImmutable();
66
67     // Register a listener that may be called the next time our generation ID changes.
68     //
69     // We'll only call the listener if we're confident that we are the only SkPixelRef with this
70     // generation ID.  If our generation ID changes and we decide not to call the listener, we'll
71     // never call it: you must add a new listener for each generation ID change.  We also won't call
72     // the listener when we're certain no one knows what our generation ID is.
73     //
74     // This can be used to invalidate caches keyed by SkPixelRef generation ID.
75     // Takes ownership of listener.  Threadsafe.
76     void addGenIDChangeListener(sk_sp<SkIDChangeListener> listener);
77
78     // Call when this pixelref is part of the key to a resourcecache entry. This allows the cache
79     // to know automatically those entries can be purged when this pixelref is changed or deleted.
80     void notifyAddedToCache() {
81         fAddedToCache.store(true);
82     }
83
84     virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return nullptr; }
85
86 protected:
87     void android_only_reset(int width, int height, size_t rowBytes);
88
89 private:
90     int                 fWidth;
91     int                 fHeight;
92     void*               fPixels;
93     size_t              fRowBytes;
94
95     // Bottom bit indicates the Gen ID is unique.
96     bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); }
97     mutable std::atomic<uint32_t> fTaggedGenID;
98
99     SkIDChangeListener::List fGenIDChangeListeners;
100
101     // Set true by caches when they cache content that's derived from the current pixels.
102     std::atomic<bool> fAddedToCache;
103
104     enum Mutability {
105         kMutable,               // PixelRefs begin mutable.
106         kTemporarilyImmutable,  // Considered immutable, but can revert to mutable.
107         kImmutable,             // Once set to this state, it never leaves.
108     } fMutability : 8;          // easily fits inside a byte
109
110     void needsNewGenID();
111     void callGenIDChangeListeners();
112
113     void setTemporarilyImmutable();
114     void restoreMutability();
115     friend class SkSurface_Raster;  // For temporary immutable methods above.
116
117     void setImmutableWithID(uint32_t genID);
118     friend void SkBitmapCache_setImmutableWithID(SkPixelRef*, uint32_t);
119
120     using INHERITED = SkRefCnt;
121 };
122
123 #endif