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/SkFilterQuality.h" 13 #include "include/core/SkImageInfo.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 28 /** \class SkPixelRef 29 30 This class is the smart container for pixel memory, and is used with SkBitmap. 31 This class can be shared/accessed between multiple threads. 32 */ 33 class SK_API SkPixelRef : public SkRefCnt { 34 public: 35 SkPixelRef(int width, int height, void* addr, size_t rowBytes); 36 ~SkPixelRef() override; 37 dimensions()38 SkISize dimensions() const { return {fWidth, fHeight}; } width()39 int width() const { return fWidth; } height()40 int height() const { return fHeight; } pixels()41 void* pixels() const { return fPixels; } rowBytes()42 size_t rowBytes() const { return fRowBytes; } 43 44 /** Returns a non-zero, unique value corresponding to the pixels in this 45 pixelref. Each time the pixels are changed (and notifyPixelsChanged is 46 called), a different generation ID will be returned. 47 */ 48 uint32_t getGenerationID() const; 49 50 /** 51 * Call this if you have changed the contents of the pixels. This will in- 52 * turn cause a different generation ID value to be returned from 53 * getGenerationID(). 54 */ 55 void notifyPixelsChanged(); 56 57 /** Returns true if this pixelref is marked as immutable, meaning that the 58 contents of its pixels will not change for the lifetime of the pixelref. 59 */ isImmutable()60 bool isImmutable() const { return fMutability != kMutable; } 61 62 /** Marks this pixelref is immutable, meaning that the contents of its 63 pixels will not change for the lifetime of the pixelref. This state can 64 be set on a pixelref, but it cannot be cleared once it is set. 65 */ 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. 77 void addGenIDChangeListener(sk_sp<SkIDChangeListener> listener); 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. notifyAddedToCache()81 void notifyAddedToCache() { 82 fAddedToCache.store(true); 83 } 84 diagnostic_only_getDiscardable()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. genIDIsUnique()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); 119 friend void SkBitmapCache_setImmutableWithID(SkPixelRef*, uint32_t); 120 121 using INHERITED = SkRefCnt; 122 }; 123 124 #endif 125