SkPixelRef.h 4.5 KB

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