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 | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 23 | Validator(sk_sp<SharedGenerator>, const SkIRect* subset, const SkColorType* colorType, |
| 24 | sk_sp<SkColorSpace> colorSpace); |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 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 | |
| 38 | SkImageInfo onImageInfo() const override { |
| 39 | return fInfo; |
| 40 | } |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 41 | |
| 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*, |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 50 | const GrSamplerState&, |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 51 | SkScalar scaleAdjust[2]) const override; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 52 | sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4], |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 53 | SkYUVColorSpace*, const void* planes[4]) override; |
Robert Phillips | 4524e25 | 2018-09-21 14:20:38 -0400 | [diff] [blame] | 54 | #endif |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 55 | sk_sp<SkData> onRefEncoded() const override; |
| 56 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
Brian Osman | e50cdf0 | 2018-10-19 13:02:14 -0400 | [diff] [blame] | 57 | bool getROPixels(SkBitmap*, CachingHint) const override; |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 58 | bool onIsLazyGenerated() const override { return true; } |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 59 | sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>) const override; |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 60 | |
| 61 | bool onIsValid(GrContext*) const override; |
| 62 | |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 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, |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 71 | GrTextureMaker::AllowedTexGenType genType) const; |
| 72 | |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 73 | void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) const; |
| 74 | #endif |
| 75 | |
| 76 | private: |
| 77 | class ScopedGenerator; |
| 78 | |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 79 | sk_sp<SharedGenerator> fSharedGenerator; |
| 80 | // Note that fInfo is not necessarily the info from the generator. It may be cropped by |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 81 | // onMakeSubset and its color type/space may be changed by onMakeColorTypeAndColorSpace. |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 82 | const SkImageInfo fInfo; |
| 83 | const SkIPoint fOrigin; |
| 84 | |
| 85 | uint32_t fUniqueID; |
| 86 | |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 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; |
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 |