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/SkMutex.h" 18 #include "include/private/SkTDArray.h" 19 20 #include <atomic> 21 22 struct SkIRect; 23 24 class GrTexture; 25 class SkDiscardableMemory; 26 27 /** \class SkPixelRef 28 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. 31 */ 32 class SK_API SkPixelRef : public SkRefCnt { 33 public: 34 SkPixelRef(int width, int height, void* addr, size_t rowBytes); 35 ~SkPixelRef() override; 36 width()37 int width() const { return fWidth; } height()38 int height() const { return fHeight; } pixels()39 void* pixels() const { return fPixels; } rowBytes()40 size_t rowBytes() const { return fRowBytes; } 41 42 /** Returns a non-zero, unique value corresponding to the pixels in this 43 pixelref. Each time the pixels are changed (and notifyPixelsChanged is 44 called), a different generation ID will be returned. 45 */ 46 uint32_t getGenerationID() const; 47 48 /** 49 * Call this if you have changed the contents of the pixels. This will in- 50 * turn cause a different generation ID value to be returned from 51 * getGenerationID(). 52 */ 53 void notifyPixelsChanged(); 54 55 /** Returns true if this pixelref is marked as immutable, meaning that the 56 contents of its pixels will not change for the lifetime of the pixelref. 57 */ isImmutable()58 bool isImmutable() const { return fMutability != kMutable; } 59 60 /** Marks this pixelref is immutable, meaning that the contents of its 61 pixels will not change for the lifetime of the pixelref. This state can 62 be set on a pixelref, but it cannot be cleared once it is set. 63 */ 64 void setImmutable(); 65 66 // Register a listener that may be called the next time our generation ID changes. 67 // 68 // We'll only call the listener if we're confident that we are the only SkPixelRef with this 69 // generation ID. If our generation ID changes and we decide not to call the listener, we'll 70 // never call it: you must add a new listener for each generation ID change. We also won't call 71 // the listener when we're certain no one knows what our generation ID is. 72 // 73 // This can be used to invalidate caches keyed by SkPixelRef generation ID. 74 struct GenIDChangeListener { ~GenIDChangeListenerGenIDChangeListener75 virtual ~GenIDChangeListener() {} 76 virtual void onChange() = 0; 77 }; 78 79 // Takes ownership of listener. Threadsafe. 80 void addGenIDChangeListener(GenIDChangeListener* listener); 81 82 // Call when this pixelref is part of the key to a resourcecache entry. This allows the cache 83 // to know automatically those entries can be purged when this pixelref is changed or deleted. notifyAddedToCache()84 void notifyAddedToCache() { 85 fAddedToCache.store(true); 86 } 87 diagnostic_only_getDiscardable()88 virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return nullptr; } 89 90 protected: 91 void android_only_reset(int width, int height, size_t rowBytes); 92 93 private: 94 int fWidth; 95 int fHeight; 96 void* fPixels; 97 size_t fRowBytes; 98 99 // Bottom bit indicates the Gen ID is unique. genIDIsUnique()100 bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); } 101 mutable std::atomic<uint32_t> fTaggedGenID; 102 103 SkMutex fGenIDChangeListenersMutex; 104 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned 105 106 // Set true by caches when they cache content that's derived from the current pixels. 107 std::atomic<bool> fAddedToCache; 108 109 enum Mutability { 110 kMutable, // PixelRefs begin mutable. 111 kTemporarilyImmutable, // Considered immutable, but can revert to mutable. 112 kImmutable, // Once set to this state, it never leaves. 113 } fMutability : 8; // easily fits inside a byte 114 115 void needsNewGenID(); 116 void callGenIDChangeListeners(); 117 118 void setTemporarilyImmutable(); 119 void restoreMutability(); 120 friend class SkSurface_Raster; // For the two methods above. 121 122 void setImmutableWithID(uint32_t genID); 123 friend void SkBitmapCache_setImmutableWithID(SkPixelRef*, uint32_t); 124 125 typedef SkRefCnt INHERITED; 126 }; 127 128 #endif 129