Skia
2D Graphics Library
SkPixelRef.h
Go to the documentation of this file.
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"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkSize.h"
17 #include "include/private/SkIDChangeListener.h"
18 #include "include/private/SkMutex.h"
19 #include "include/private/SkTDArray.h"
20 
21 #include <atomic>
22 
23 struct SkIRect;
24 
25 class GrTexture;
26 class SkDiscardableMemory;
27 
33 class SK_API SkPixelRef : public SkRefCnt {
34 public:
35  SkPixelRef(int width, int height, void* addr, size_t rowBytes);
36  ~SkPixelRef() override;
37 
38  SkISize dimensions() const { return {fWidth, fHeight}; }
39  int width() const { return fWidth; }
40  int height() const { return fHeight; }
41  void* pixels() const { return fPixels; }
42  size_t rowBytes() const { return fRowBytes; }
43 
48  uint32_t getGenerationID() const;
49 
56 
60  bool isImmutable() const { return fMutability != kMutable; }
61 
66  void setImmutable();
67 
68  // Register a listener that may be called the next time our generation ID changes.
69  //
70  // We'll only call the listener if we're confident that we are the only SkPixelRef with this
71  // generation ID. If our generation ID changes and we decide not to call the listener, we'll
72  // never call it: you must add a new listener for each generation ID change. We also won't call
73  // the listener when we're certain no one knows what our generation ID is.
74  //
75  // This can be used to invalidate caches keyed by SkPixelRef generation ID.
76  // Takes ownership of listener. Threadsafe.
78 
79  // Call when this pixelref is part of the key to a resourcecache entry. This allows the cache
80  // to know automatically those entries can be purged when this pixelref is changed or deleted.
82  fAddedToCache.store(true);
83  }
84 
85  virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return nullptr; }
86 
87 protected:
88  void android_only_reset(int width, int height, size_t rowBytes);
89 
90 private:
91  int fWidth;
92  int fHeight;
93  void* fPixels;
94  size_t fRowBytes;
95 
96  // Bottom bit indicates the Gen ID is unique.
97  bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); }
98  mutable std::atomic<uint32_t> fTaggedGenID;
99 
100  SkIDChangeListener::List fGenIDChangeListeners;
101 
102  // Set true by caches when they cache content that's derived from the current pixels.
103  std::atomic<bool> fAddedToCache;
104 
105  enum Mutability {
106  kMutable, // PixelRefs begin mutable.
107  kTemporarilyImmutable, // Considered immutable, but can revert to mutable.
108  kImmutable, // Once set to this state, it never leaves.
109  } fMutability : 8; // easily fits inside a byte
110 
111  void needsNewGenID();
112  void callGenIDChangeListeners();
113 
114  void setTemporarilyImmutable();
115  void restoreMutability();
116  friend class SkSurface_Raster; // For the two methods above.
117 
118  void setImmutableWithID(uint32_t genID);
120 
121  using INHERITED = SkRefCnt;
122 };
123 
124 #endif
SkPixelRef::rowBytes
size_t rowBytes() const
Definition: SkPixelRef.h:42
SkSize.h
SkRefCnt
Definition: SkRefCnt.h:118
SkPixelRef::android_only_reset
void android_only_reset(int width, int height, size_t rowBytes)
SkPixelRef::height
int height() const
Definition: SkPixelRef.h:40
SkPixelRef
Definition: SkPixelRef.h:33
SkPixelRef::~SkPixelRef
~SkPixelRef() override
SkBitmap.h
SkPixmap.h
SkPixelRef::diagnostic_only_getDiscardable
virtual SkDiscardableMemory * diagnostic_only_getDiscardable() const
Definition: SkPixelRef.h:85
SkRefCnt.h
SkPixelRef::width
int width() const
Definition: SkPixelRef.h:39
sk_sp
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:215
SkPixelRef::notifyAddedToCache
void notifyAddedToCache()
Definition: SkPixelRef.h:81
SkPixelRef::SkPixelRef
SkPixelRef(int width, int height, void *addr, size_t rowBytes)
SkToBool
static constexpr bool SkToBool(const T &x)
Definition: SkTypes.h:497
SK_API
#define SK_API
Definition: SkTypes.h:181
SkPixelRef::dimensions
SkISize dimensions() const
Definition: SkPixelRef.h:38
SkPixelRef::setImmutable
void setImmutable()
Marks this pixelref is immutable, meaning that the contents of its pixels will not change for the lif...
SkISize
Definition: SkSize.h:13
SkPixelRef::notifyPixelsChanged
void notifyPixelsChanged()
Call this if you have changed the contents of the pixels.
SkPixelRef::isImmutable
bool isImmutable() const
Returns true if this pixelref is marked as immutable, meaning that the contents of its pixels will no...
Definition: SkPixelRef.h:60
SkFilterQuality.h
SkPixelRef::pixels
void * pixels() const
Definition: SkPixelRef.h:41
SkIRect
Definition: SkRect.h:28
SkPixelRef::SkBitmapCache_setImmutableWithID
friend void SkBitmapCache_setImmutableWithID(SkPixelRef *, uint32_t)
SkImageInfo.h
SkPixelRef::addGenIDChangeListener
void addGenIDChangeListener(sk_sp< SkIDChangeListener > listener)
SkPixelRef::getGenerationID
uint32_t getGenerationID() const
Returns a non-zero, unique value corresponding to the pixels in this pixelref.