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/SkIDChangeListener.h" 12 #include "include/private/SkMutex.h" 13 #include "src/image/SkImage_Base.h" 14 15 #if SK_SUPPORT_GPU 16 #include "include/core/SkYUVAPixmaps.h" 17 #endif 18 19 class SharedGenerator; 20 21 class SkImage_Lazy : public SkImage_Base { 22 public: 23 struct Validator { 24 Validator(sk_sp<SharedGenerator>, const SkColorType*, sk_sp<SkColorSpace>); 25 26 operator bool() const { return fSharedGenerator.get(); } 27 28 sk_sp<SharedGenerator> fSharedGenerator; 29 SkImageInfo fInfo; 30 sk_sp<SkColorSpace> fColorSpace; 31 uint32_t fUniqueID; 32 }; 33 34 SkImage_Lazy(Validator* validator); 35 onHasMipmaps()36 bool onHasMipmaps() const override { 37 // TODO: Should we defer to the generator? The generator interface currently doesn't have 38 // a way to provide content for levels other than via SkImageGenerator::generateTexture(). 39 return false; 40 } 41 42 bool onReadPixels(GrDirectContext*, const SkImageInfo&, void*, size_t, int srcX, int srcY, 43 CachingHint) const override; 44 sk_sp<SkData> onRefEncoded() const override; 45 sk_sp<SkImage> onMakeSubset(const SkIRect&, GrDirectContext*) const override; 46 bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint) const override; onIsLazyGenerated()47 bool onIsLazyGenerated() const override { return true; } 48 sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>, 49 GrDirectContext*) const override; 50 sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final; 51 52 bool onIsValid(GrRecordingContext*) const override; 53 54 #if SK_SUPPORT_GPU 55 // Returns the texture proxy. CachingHint refers to whether the generator's output should be 56 // cached in CPU memory. We will always cache the generated texture on success. 57 GrSurfaceProxyView lockTextureProxyView(GrRecordingContext*, 58 GrImageTexGenPolicy, 59 GrMipmapped) const; 60 61 // Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This 62 // may be different from the color type on the image in the case where we need up upload CPU 63 // data to a texture but the GPU doesn't support the format of CPU data. In this case we convert 64 // the data to RGBA_8888 unorm on the CPU then upload that. 65 GrColorType colorTypeOfLockTextureProxy(const GrCaps* caps) const; 66 #endif 67 68 private: 69 void addUniqueIDListener(sk_sp<SkIDChangeListener>) const; 70 #if SK_SUPPORT_GPU 71 std::tuple<GrSurfaceProxyView, GrColorType> onAsView(GrRecordingContext*, 72 GrMipmapped, 73 GrImageTexGenPolicy) const override; 74 std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(GrRecordingContext*, 75 SkSamplingOptions, 76 const SkTileMode[], 77 const SkMatrix&, 78 const SkRect*, 79 const SkRect*) const override; 80 81 GrSurfaceProxyView textureProxyViewFromPlanes(GrRecordingContext*, SkBudgeted) const; 82 sk_sp<SkCachedData> getPlanes(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes, 83 SkYUVAPixmaps* pixmaps) const; 84 #endif 85 86 class ScopedGenerator; 87 88 // Note that this->imageInfo() is not necessarily the info from the generator. It may be 89 // cropped by onMakeSubset and its color type/space may be changed by 90 // onMakeColorTypeAndColorSpace. 91 sk_sp<SharedGenerator> fSharedGenerator; 92 93 // Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs 94 // and SkImage_Lazy instances. Cache the result of the last successful call. 95 mutable SkMutex fOnMakeColorTypeAndSpaceMutex; 96 mutable sk_sp<SkImage> fOnMakeColorTypeAndSpaceResult; 97 98 #if SK_SUPPORT_GPU 99 // When the SkImage_Lazy goes away, we will iterate over all the listeners to inform them 100 // of the unique ID's demise. This is used to remove cached textures from GrContext. 101 mutable SkIDChangeListener::List fUniqueIDListeners; 102 #endif 103 104 using INHERITED = SkImage_Base; 105 }; 106 107 #endif 108