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/SkMutex.h" 12 #include "../private/SkTDArray.h" 13 #include "SkBitmap.h" 14 #include "SkFilterQuality.h" 15 #include "SkImageInfo.h" 16 #include "SkPixmap.h" 17 #include "SkRefCnt.h" 18 #include "SkSize.h" 19 #include "SkString.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 width()38 int width() const { return fWidth; } height()39 int height() const { return fHeight; } pixels()40 void* pixels() const { return fPixels; } rowBytes()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 */ isImmutable()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 struct GenIDChangeListener { ~GenIDChangeListenerGenIDChangeListener76 virtual ~GenIDChangeListener() {} 77 virtual void onChange() = 0; 78 }; 79 80 // Takes ownership of listener. Threadsafe. 81 void addGenIDChangeListener(GenIDChangeListener* listener); 82 83 // Call when this pixelref is part of the key to a resourcecache entry. This allows the cache 84 // to know automatically those entries can be purged when this pixelref is changed or deleted. notifyAddedToCache()85 void notifyAddedToCache() { 86 fAddedToCache.store(true); 87 } 88 diagnostic_only_getDiscardable()89 virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return nullptr; } 90 91 protected: 92 void android_only_reset(int width, int height, size_t rowBytes); 93 94 private: 95 int fWidth; 96 int fHeight; 97 void* fPixels; 98 size_t fRowBytes; 99 100 // Bottom bit indicates the Gen ID is unique. genIDIsUnique()101 bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); } 102 mutable std::atomic<uint32_t> fTaggedGenID; 103 104 SkMutex fGenIDChangeListenersMutex; 105 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned 106 107 // Set true by caches when they cache content that's derived from the current pixels. 108 std::atomic<bool> fAddedToCache; 109 110 enum Mutability { 111 kMutable, // PixelRefs begin mutable. 112 kTemporarilyImmutable, // Considered immutable, but can revert to mutable. 113 kImmutable, // Once set to this state, it never leaves. 114 } fMutability : 8; // easily fits inside a byte 115 116 void needsNewGenID(); 117 void callGenIDChangeListeners(); 118 119 void setTemporarilyImmutable(); 120 void restoreMutability(); 121 friend class SkSurface_Raster; // For the two methods above. 122 123 void setImmutableWithID(uint32_t genID); 124 friend void SkBitmapCache_setImmutableWithID(SkPixelRef*, uint32_t); 125 126 typedef SkRefCnt INHERITED; 127 }; 128 129 #endif 130