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_GaneshYUVA_DEFINED 9 #define SkImage_GaneshYUVA_DEFINED 10 11 #include "include/core/SkColorSpace.h" 12 #include "include/core/SkImage.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkSamplingOptions.h" 15 #include "src/gpu/ganesh/GrYUVATextureProxies.h" 16 #include "src/gpu/ganesh/image/SkImage_GaneshBase.h" 17 #include "src/image/SkImage_Base.h" 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <memory> 22 #include <tuple> 23 24 class GrDirectContext; 25 class GrFragmentProcessor; 26 class GrImageContext; 27 class GrRecordingContext; 28 class GrSurfaceProxyView; 29 class SkMatrix; 30 enum SkColorType : int; 31 enum class GrColorType; 32 enum class GrImageTexGenPolicy : int; 33 enum class GrSemaphoresSubmitted : bool; 34 enum GrSurfaceOrigin : int; 35 enum class SkTileMode; 36 struct GrFlushInfo; 37 struct SkRect; 38 39 namespace skgpu { 40 enum class Mipmapped : bool; 41 } 42 43 // Wraps the 1 to 4 planes of a YUVA image for consumption by the GPU. 44 // Initially any direct rendering will be done by passing the individual planes to a shader. 45 // Once any method requests a flattened image (e.g., onReadPixels), the flattened RGB 46 // proxy will be stored and used for any future rendering. 47 class SkImage_GaneshYUVA final : public SkImage_GaneshBase { 48 public: 49 SkImage_GaneshYUVA(sk_sp<GrImageContext>, 50 uint32_t uniqueID, 51 GrYUVATextureProxies proxies, 52 sk_sp<SkColorSpace>); 53 54 // From SkImage.h 55 size_t textureSize() const override; 56 57 // From SkImage_Base.h type()58 SkImage_Base::Type type() const override { return SkImage_Base::Type::kGaneshYUVA; } 59 bool onHasMipmaps() const override; 60 bool onIsProtected() const override; 61 62 using SkImage_GaneshBase::onMakeColorTypeAndColorSpace; 63 sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, 64 sk_sp<SkColorSpace>, 65 GrDirectContext*) const final; 66 67 sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final; 68 69 // From SkImage_GaneshBase.h 70 GrSemaphoresSubmitted flush(GrDirectContext*, const GrFlushInfo&) const override; 71 72 std::tuple<GrSurfaceProxyView, GrColorType> asView(GrRecordingContext*, 73 skgpu::Mipmapped, 74 GrImageTexGenPolicy) const override; 75 76 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrRecordingContext*, 77 SkSamplingOptions, 78 const SkTileMode[2], 79 const SkMatrix&, 80 const SkRect*, 81 const SkRect*) const override; 82 83 bool setupMipmapsForPlanes(GrRecordingContext*) const; 84 origin()85 GrSurfaceOrigin origin() const override { return fYUVAProxies.textureOrigin(); } 86 87 private: 88 enum class ColorSpaceMode { 89 kConvert, 90 kReinterpret, 91 }; 92 SkImage_GaneshYUVA(sk_sp<GrImageContext>, 93 const SkImage_GaneshYUVA* image, 94 sk_sp<SkColorSpace> targetCS, 95 ColorSpaceMode csMode); 96 97 mutable GrYUVATextureProxies fYUVAProxies; 98 99 // If this is non-null then the planar data should be converted from fFromColorSpace to 100 // this->colorSpace(). Otherwise we assume the planar data (post YUV->RGB conversion) is already 101 // in this->colorSpace(). 102 const sk_sp<SkColorSpace> fFromColorSpace; 103 104 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and 105 // SkImage_GaneshYUVA instances. Cache the result of the last successful onMakeColorSpace call. 106 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget; 107 mutable sk_sp<SkImage> fOnMakeColorSpaceResult; 108 109 using INHERITED = SkImage_GaneshBase; 110 }; 111 112 #endif 113