SkImage_Lazy.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2018 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_Lazy_DEFINED
  8. #define SkImage_Lazy_DEFINED
  9. #include "include/private/SkMutex.h"
  10. #include "src/image/SkImage_Base.h"
  11. #if SK_SUPPORT_GPU
  12. #include "src/gpu/GrTextureMaker.h"
  13. #endif
  14. class SharedGenerator;
  15. class SkImage_Lazy : public SkImage_Base {
  16. public:
  17. struct Validator {
  18. Validator(sk_sp<SharedGenerator>, const SkIRect* subset, const SkColorType* colorType,
  19. sk_sp<SkColorSpace> colorSpace);
  20. operator bool() const { return fSharedGenerator.get(); }
  21. sk_sp<SharedGenerator> fSharedGenerator;
  22. SkImageInfo fInfo;
  23. SkIPoint fOrigin;
  24. sk_sp<SkColorSpace> fColorSpace;
  25. uint32_t fUniqueID;
  26. };
  27. SkImage_Lazy(Validator* validator);
  28. ~SkImage_Lazy() override;
  29. SkIRect onGetSubset() const override {
  30. return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, this->width(), this->height());
  31. }
  32. bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY,
  33. CachingHint) const override;
  34. #if SK_SUPPORT_GPU
  35. sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*,
  36. const GrSamplerState&,
  37. SkScalar scaleAdjust[2]) const override;
  38. sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
  39. SkYUVColorSpace*, const void* planes[4]) override;
  40. #endif
  41. sk_sp<SkData> onRefEncoded() const override;
  42. sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect&) const override;
  43. bool getROPixels(SkBitmap*, CachingHint) const override;
  44. bool onIsLazyGenerated() const override { return true; }
  45. sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
  46. SkColorType, sk_sp<SkColorSpace>) const override;
  47. bool onIsValid(GrContext*) const override;
  48. #if SK_SUPPORT_GPU
  49. // Returns the texture proxy. If we're going to generate and cache the texture, we should use
  50. // the passed in key (if the key is valid). If genType is AllowedTexGenType::kCheap and the
  51. // texture is not trivial to construct, returns nullptr.
  52. sk_sp<GrTextureProxy> lockTextureProxy(GrRecordingContext*,
  53. const GrUniqueKey& key,
  54. SkImage::CachingHint,
  55. bool willBeMipped,
  56. GrTextureMaker::AllowedTexGenType genType) const;
  57. void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) const;
  58. #endif
  59. private:
  60. class ScopedGenerator;
  61. // Note that this->imageInfo() is not necessarily the info from the generator. It may be
  62. // cropped by onMakeSubset and its color type/space may be changed by
  63. // onMakeColorTypeAndColorSpace.
  64. sk_sp<SharedGenerator> fSharedGenerator;
  65. const SkIPoint fOrigin;
  66. uint32_t fUniqueID;
  67. // Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs
  68. // and SkImage_Lazy instances. Cache the result of the last successful call.
  69. mutable SkMutex fOnMakeColorTypeAndSpaceMutex;
  70. mutable sk_sp<SkImage> fOnMakeColorTypeAndSpaceResult;
  71. #if SK_SUPPORT_GPU
  72. // When the SkImage_Lazy goes away, we will iterate over all the unique keys we've used and
  73. // send messages to the GrContexts to say the unique keys are no longer valid. The GrContexts
  74. // can then release the resources, conntected with the those unique keys, from their caches.
  75. mutable SkTDArray<GrUniqueKeyInvalidatedMessage*> fUniqueKeyInvalidatedMessages;
  76. #endif
  77. typedef SkImage_Base INHERITED;
  78. };
  79. #endif