SkImage_Base.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2012 Google Inc.
  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 SkImage_Base_DEFINED
  8. #define SkImage_Base_DEFINED
  9. #include "include/core/SkImage.h"
  10. #include "include/core/SkSurface.h"
  11. #include <atomic>
  12. #if SK_SUPPORT_GPU
  13. #include "include/private/SkTDArray.h"
  14. #include "src/gpu/GrTextureProxy.h"
  15. class GrRecordingContext;
  16. class GrTexture;
  17. #endif
  18. #include <new>
  19. class GrSamplerState;
  20. class SkCachedData;
  21. struct SkYUVASizeInfo;
  22. enum {
  23. kNeedNewImageUniqueID = 0
  24. };
  25. class SkImage_Base : public SkImage {
  26. public:
  27. virtual ~SkImage_Base();
  28. virtual SkIRect onGetSubset() const {
  29. return { 0, 0, this->width(), this->height() };
  30. }
  31. virtual bool onPeekPixels(SkPixmap*) const { return false; }
  32. virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
  33. virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
  34. int srcX, int srcY, CachingHint) const = 0;
  35. virtual GrContext* context() const { return nullptr; }
  36. #if SK_SUPPORT_GPU
  37. virtual GrSemaphoresSubmitted onFlush(GrContext* context, const GrFlushInfo&) {
  38. return GrSemaphoresSubmitted::kNo;
  39. }
  40. // Return the proxy if this image is backed by a single proxy. For YUVA images, this
  41. // will return nullptr unless the YUVA planes have been converted to RGBA in which case
  42. // that single backing proxy will be returned.
  43. virtual GrTextureProxy* peekProxy() const { return nullptr; }
  44. virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*) const { return nullptr; }
  45. virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*, const GrSamplerState&,
  46. SkScalar scaleAdjust[2]) const = 0;
  47. virtual sk_sp<GrTextureProxy> refPinnedTextureProxy(GrRecordingContext*,
  48. uint32_t* uniqueID) const {
  49. return nullptr;
  50. }
  51. virtual bool isYUVA() const { return false; }
  52. virtual GrTexture* onGetTexture() const { return nullptr; }
  53. #endif
  54. virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
  55. GrSurfaceOrigin* origin) const;
  56. // return a read-only copy of the pixels. We promise to not modify them,
  57. // but only inspect them (or encode them).
  58. virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
  59. virtual sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect&) const = 0;
  60. virtual sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
  61. SkYUVColorSpace*, const void* planes[4]);
  62. virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
  63. virtual bool onAsLegacyBitmap(SkBitmap*) const;
  64. // True for picture-backed and codec-backed
  65. virtual bool onIsLazyGenerated() const { return false; }
  66. // True for images instantiated in GPU memory
  67. virtual bool onIsTextureBacked() const { return false; }
  68. // Call when this image is part of the key to a resourcecache entry. This allows the cache
  69. // to know automatically those entries can be purged when this SkImage deleted.
  70. virtual void notifyAddedToRasterCache() const {
  71. fAddedToRasterCache.store(true);
  72. }
  73. virtual bool onIsValid(GrContext*) const = 0;
  74. virtual bool onPinAsTexture(GrContext*) const { return false; }
  75. virtual void onUnpinAsTexture(GrContext*) const {}
  76. virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
  77. SkColorType, sk_sp<SkColorSpace>) const = 0;
  78. protected:
  79. SkImage_Base(const SkImageInfo& info, uint32_t uniqueID);
  80. private:
  81. // Set true by caches when they cache content that's derived from the current pixels.
  82. mutable std::atomic<bool> fAddedToRasterCache;
  83. typedef SkImage INHERITED;
  84. };
  85. static inline SkImage_Base* as_IB(SkImage* image) {
  86. return static_cast<SkImage_Base*>(image);
  87. }
  88. static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
  89. return static_cast<SkImage_Base*>(image.get());
  90. }
  91. static inline const SkImage_Base* as_IB(const SkImage* image) {
  92. return static_cast<const SkImage_Base*>(image);
  93. }
  94. #endif