2 * Copyright 2008 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkPixelRef_DEFINED
9 #define SkPixelRef_DEFINED
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"
25 class SkDiscardableMemory;
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.
32 class SK_API SkPixelRef : public SkRefCnt {
34 SkPixelRef(int width, int height, void* addr, size_t rowBytes);
35 ~SkPixelRef() override;
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; }
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.
47 uint32_t getGenerationID() const;
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
54 void notifyPixelsChanged();
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.
59 bool isImmutable() const { return fMutability != kMutable; }
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.
67 // Register a listener that may be called the next time our generation ID changes.
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.
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);
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);
84 virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return nullptr; }
87 void android_only_reset(int width, int height, size_t rowBytes);
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;
99 SkIDChangeListener::List fGenIDChangeListeners;
101 // Set true by caches when they cache content that's derived from the current pixels.
102 std::atomic<bool> fAddedToCache;
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
110 void needsNewGenID();
111 void callGenIDChangeListeners();
113 void setTemporarilyImmutable();
114 void restoreMutability();
115 friend class SkSurface_Raster; // For temporary immutable methods above.
117 void setImmutableWithID(uint32_t genID);
118 friend void SkBitmapCache_setImmutableWithID(SkPixelRef*, uint32_t);
120 using INHERITED = SkRefCnt;