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