Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "../private/SkAtomics.h"
12 #include "../private/SkMutex.h"
13 #include "../private/SkTDArray.h"
14 #include "SkBitmap.h"
15 #include "SkFilterQuality.h"
16 #include "SkImageInfo.h"
17 #include "SkPixmap.h"
18 #include "SkRefCnt.h"
19 #include "SkSize.h"
20 #include "SkString.h"
21 
22 class SkColorTable;
23 struct SkIRect;
24 
25 class GrTexture;
26 class SkDiscardableMemory;
27 
33 class SK_API SkPixelRef : public SkRefCnt {
34 public:
35 
36  SkPixelRef(int width, int height, void* addr, size_t rowBytes, sk_sp<SkColorTable> = nullptr);
37 
38  ~SkPixelRef() override;
39 
40  int width() const { return fWidth; }
41  int height() const { return fHeight; }
42  void* pixels() const { return fPixels; }
43  SkColorTable* colorTable() const { return fCTable.get(); }
44  size_t rowBytes() const { return fRowBytes; }
45 
50  uint32_t getGenerationID() const;
51 
52 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
53 
61  uint32_t getStableID() const { return fStableID; }
62 #endif
63 
69  void notifyPixelsChanged();
70 
74  bool isImmutable() const { return fMutability != kMutable; }
75 
80  void setImmutable();
81 
82  // Register a listener that may be called the next time our generation ID changes.
83  //
84  // We'll only call the listener if we're confident that we are the only SkPixelRef with this
85  // generation ID. If our generation ID changes and we decide not to call the listener, we'll
86  // never call it: you must add a new listener for each generation ID change. We also won't call
87  // the listener when we're certain no one knows what our generation ID is.
88  //
89  // This can be used to invalidate caches keyed by SkPixelRef generation ID.
91  virtual ~GenIDChangeListener() {}
92  virtual void onChange() = 0;
93  };
94 
95  // Takes ownership of listener.
96  void addGenIDChangeListener(GenIDChangeListener* listener);
97 
98  // Call when this pixelref is part of the key to a resourcecache entry. This allows the cache
99  // to know automatically those entries can be purged when this pixelref is changed or deleted.
100  void notifyAddedToCache() {
101  fAddedToCache.store(true);
102  }
103 
104  virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return NULL; }
105 
106 protected:
107  // default impl does nothing.
108  virtual void onNotifyPixelsChanged();
109 
110 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
111  // This is undefined if there are clients in-flight trying to use us
112  void android_only_reset(int width, int height, size_t rowBytes, sk_sp<SkColorTable>);
113 #endif
114 
115 private:
116  int fWidth;
117  int fHeight;
118  sk_sp<SkColorTable> fCTable;
119  void* fPixels;
120  size_t fRowBytes;
121 
122  // Bottom bit indicates the Gen ID is unique.
123  bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); }
124  mutable SkAtomic<uint32_t> fTaggedGenID;
125 
126 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
127  const uint32_t fStableID;
128 #endif
129 
130  SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned
131 
132  // Set true by caches when they cache content that's derived from the current pixels.
133  SkAtomic<bool> fAddedToCache;
134 
135  enum {
136  kMutable, // PixelRefs begin mutable.
137  kTemporarilyImmutable, // Considered immutable, but can revert to mutable.
138  kImmutable, // Once set to this state, it never leaves.
139  } fMutability : 8; // easily fits inside a byte
140 
141  void needsNewGenID();
142  void callGenIDChangeListeners();
143 
144  void setTemporarilyImmutable();
145  void restoreMutability();
146  friend class SkSurface_Raster; // For the two methods above.
147 
148  friend class SkImage_Raster;
149  friend class SkSpecialImage_Raster;
150 
151  void setImmutableWithID(uint32_t genID);
152  friend class SkImage_Gpu;
153  friend class SkImage_Lazy;
154  friend class SkSpecialImage_Gpu;
155  friend void SkBitmapCache_setImmutableWithID(SkPixelRef*, uint32_t);
156 
157  typedef SkRefCnt INHERITED;
158 };
159 
160 #endif
SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by 8-bit bitmaps...
Definition: SkColorTable.h:25
Definition: SkRefCnt.h:125
#define SkToBool(cond)
Returns 0 or 1 based on the condition.
Definition: SkTypes.h:227
This class is the smart container for pixel memory, and is used with SkBitmap.
Definition: SkPixelRef.h:33
Definition: SkPixelRef.h:90
bool isImmutable() const
Returns true if this pixelref is marked as immutable, meaning that the contents of its pixels will no...
Definition: SkPixelRef.h:74
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
Definition: GrTexture.h:19