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