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/SkColorType.h" 13 #include "include/core/SkImage.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkSamplingOptions.h" 16 #include "src/gpu/ganesh/GrYUVATextureProxies.h" 17 #include "src/gpu/ganesh/image/SkImage_GaneshBase.h" 18 #include "src/image/SkImage_Base.h" 19 20 #include <cstddef> 21 #include <cstdint> 22 #include <memory> 23 #include <tuple> 24 25 class GrDirectContext; 26 class GrFragmentProcessor; 27 class GrImageContext; 28 class GrRecordingContext; 29 class GrSurfaceProxyView; 30 class SkMatrix; 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 static constexpr auto kAssumedColorType = kRGBA_8888_SkColorType; 50 51 SkImage_GaneshYUVA(sk_sp<GrImageContext>, 52 uint32_t uniqueID, 53 GrYUVATextureProxies proxies, 54 sk_sp<SkColorSpace>); 55 56 // From SkImage.h 57 size_t textureSize() const override; 58 59 // From SkImage_Base.h type()60 SkImage_Base::Type type() const override { return SkImage_Base::Type::kGaneshYUVA; } 61 bool onHasMipmaps() const override; 62 bool onIsProtected() const override; 63 64 using SkImage_GaneshBase::onMakeColorTypeAndColorSpace; 65 sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, 66 sk_sp<SkColorSpace>, 67 GrDirectContext*) const final; 68 69 sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final; 70 71 // From SkImage_GaneshBase.h 72 GrSemaphoresSubmitted flush(GrDirectContext*, const GrFlushInfo&) const override; 73 74 std::tuple<GrSurfaceProxyView, GrColorType> asView(GrRecordingContext*, 75 skgpu::Mipmapped, 76 GrImageTexGenPolicy) const override; 77 78 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrRecordingContext*, 79 SkSamplingOptions, 80 const SkTileMode[2], 81 const SkMatrix&, 82 const SkRect*, 83 const SkRect*) const override; 84 85 bool setupMipmapsForPlanes(GrRecordingContext*) const; 86 origin()87 GrSurfaceOrigin origin() const override { return fYUVAProxies.textureOrigin(); } 88 89 private: 90 enum class ColorSpaceMode { 91 kConvert, 92 kReinterpret, 93 }; 94 SkImage_GaneshYUVA(sk_sp<GrImageContext>, 95 const SkImage_GaneshYUVA* image, 96 sk_sp<SkColorSpace> targetCS, 97 ColorSpaceMode csMode); 98 99 mutable GrYUVATextureProxies fYUVAProxies; 100 101 // If this is non-null then the planar data should be converted from fFromColorSpace to 102 // this->colorSpace(). Otherwise we assume the planar data (post YUV->RGB conversion) is already 103 // in this->colorSpace(). 104 const sk_sp<SkColorSpace> fFromColorSpace; 105 106 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and 107 // SkImage_GaneshYUVA instances. Cache the result of the last successful onMakeColorSpace call. 108 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget; 109 mutable sk_sp<SkImage> fOnMakeColorSpaceResult; 110 111 using INHERITED = SkImage_GaneshBase; 112 }; 113 114 #endif 115