Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 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 { |
Brian Osman | 9a97c96 | 2019-01-12 17:38:16 +0000 | [diff] [blame] | 23 | Validator(sk_sp<SharedGenerator>, const SkIRect* subset, sk_sp<SkColorSpace> colorSpace); |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 24 | |
| 25 | operator bool() const { return fSharedGenerator.get(); } |
| 26 | |
| 27 | sk_sp<SharedGenerator> fSharedGenerator; |
| 28 | SkImageInfo fInfo; |
| 29 | SkIPoint fOrigin; |
| 30 | sk_sp<SkColorSpace> fColorSpace; |
| 31 | uint32_t fUniqueID; |
| 32 | }; |
| 33 | |
| 34 | SkImage_Lazy(Validator* validator); |
| 35 | ~SkImage_Lazy() override; |
| 36 | |
| 37 | SkImageInfo onImageInfo() const override { |
| 38 | return fInfo; |
| 39 | } |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 40 | |
| 41 | SkIRect onGetSubset() const override { |
| 42 | return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, fInfo.width(), fInfo.height()); |
| 43 | } |
| 44 | |
| 45 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, |
| 46 | CachingHint) const override; |
| 47 | #if SK_SUPPORT_GPU |
| 48 | sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 49 | const GrSamplerState&, |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 50 | SkScalar scaleAdjust[2]) const override; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 51 | sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4], |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 52 | SkYUVColorSpace*, const void* planes[4]) override; |
Robert Phillips | 4524e25 | 2018-09-21 14:20:38 -0400 | [diff] [blame] | 53 | #endif |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 54 | sk_sp<SkData> onRefEncoded() const override; |
| 55 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
Brian Osman | e50cdf0 | 2018-10-19 13:02:14 -0400 | [diff] [blame] | 56 | bool getROPixels(SkBitmap*, CachingHint) const override; |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 57 | bool onIsLazyGenerated() const override { return true; } |
Brian Osman | 9a97c96 | 2019-01-12 17:38:16 +0000 | [diff] [blame] | 58 | sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>) const override; |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 59 | |
| 60 | bool onIsValid(GrContext*) const override; |
| 61 | |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 62 | #if SK_SUPPORT_GPU |
| 63 | // Returns the texture proxy. If we're going to generate and cache the texture, we should use |
| 64 | // the passed in key (if the key is valid). If genType is AllowedTexGenType::kCheap and the |
| 65 | // texture is not trivial to construct, returns nullptr. |
| 66 | sk_sp<GrTextureProxy> lockTextureProxy(GrContext*, |
| 67 | const GrUniqueKey& key, |
| 68 | SkImage::CachingHint, |
| 69 | bool willBeMipped, |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 70 | GrTextureMaker::AllowedTexGenType genType) const; |
| 71 | |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 72 | void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) const; |
| 73 | #endif |
| 74 | |
| 75 | private: |
| 76 | class ScopedGenerator; |
| 77 | |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 78 | sk_sp<SharedGenerator> fSharedGenerator; |
| 79 | // Note that fInfo is not necessarily the info from the generator. It may be cropped by |
Brian Osman | 9a97c96 | 2019-01-12 17:38:16 +0000 | [diff] [blame] | 80 | // onMakeSubset and its color space may be changed by onMakeColorSpace. |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 81 | const SkImageInfo fInfo; |
| 82 | const SkIPoint fOrigin; |
| 83 | |
| 84 | uint32_t fUniqueID; |
| 85 | |
Brian Osman | 9a97c96 | 2019-01-12 17:38:16 +0000 | [diff] [blame] | 86 | // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and |
| 87 | // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call. |
| 88 | mutable SkMutex fOnMakeColorSpaceMutex; |
| 89 | mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget; |
| 90 | mutable sk_sp<SkImage> fOnMakeColorSpaceResult; |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 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 |