1 /* 2 * Copyright 2015 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 SkImageShader_DEFINED 9 #define SkImageShader_DEFINED 10 11 #include "include/core/SkImage.h" 12 #include "include/core/SkM44.h" 13 #include "src/shaders/SkBitmapProcShader.h" 14 #include "src/shaders/SkShaderBase.h" 15 16 namespace skgpu { 17 class Swizzle; 18 } 19 20 namespace skgpu::graphite { 21 class KeyContext; 22 enum class ReadSwizzle; 23 } 24 25 class SkImageShader : public SkShaderBase { 26 public: 27 static sk_sp<SkShader> Make(sk_sp<SkImage>, 28 SkTileMode tmx, 29 SkTileMode tmy, 30 const SkSamplingOptions&, 31 const SkMatrix* localMatrix, 32 bool clampAsIfUnpremul = false); 33 34 static sk_sp<SkShader> MakeRaw(sk_sp<SkImage>, 35 SkTileMode tmx, 36 SkTileMode tmy, 37 const SkSamplingOptions&, 38 const SkMatrix* localMatrix); 39 40 // TODO(skbug.com/12784): Requires SkImage to be texture backed, and created SkShader can only 41 // be used on GPU-backed surfaces. 42 static sk_sp<SkShader> MakeSubset(sk_sp<SkImage>, 43 const SkRect& subset, 44 SkTileMode tmx, 45 SkTileMode tmy, 46 const SkSamplingOptions&, 47 const SkMatrix* localMatrix, 48 bool clampAsIfUnpremul = false); 49 50 SkImageShader(sk_sp<SkImage>, 51 const SkRect& subset, 52 SkTileMode tmx, SkTileMode tmy, 53 const SkSamplingOptions&, 54 bool raw, 55 bool clampAsIfUnpremul); 56 57 bool isOpaque() const override; 58 59 #if defined(SK_GANESH) 60 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&, 61 const MatrixRec&) const override; 62 #endif 63 #if defined(SK_GRAPHITE) 64 void addToKey(const skgpu::graphite::KeyContext&, 65 skgpu::graphite::PaintParamsKeyBuilder*, 66 skgpu::graphite::PipelineDataGatherer*) const override; 67 #endif 68 static SkM44 CubicResamplerMatrix(float B, float C); 69 70 private: 71 SK_FLATTENABLE_HOOKS(SkImageShader) 72 73 void flatten(SkWriteBuffer&) const override; 74 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT 75 Context* onMakeContext(const ContextRec&, SkArenaAlloc* storage) const override; 76 #endif 77 SkImage* onIsAImage(SkMatrix*, SkTileMode*) const override; 78 79 bool appendStages(const SkStageRec&, const MatrixRec&) const override; 80 81 skvm::Color program(skvm::Builder*, 82 skvm::Coord device, 83 skvm::Coord local, 84 skvm::Color paint, 85 const MatrixRec&, 86 const SkColorInfo& dst, 87 skvm::Uniforms* uniforms, 88 SkArenaAlloc*) const override; 89 90 sk_sp<SkImage> fImage; 91 const SkSamplingOptions fSampling; 92 const SkTileMode fTileModeX; 93 const SkTileMode fTileModeY; 94 95 // TODO(skbug.com/12784): This is only supported for GPU images currently. 96 // If subset == (0,0,w,h) of the image, then no subset is applied. Subset will not be empty. 97 const SkRect fSubset; 98 99 const bool fRaw; 100 const bool fClampAsIfUnpremul; 101 102 friend class SkShaderBase; 103 using INHERITED = SkShaderBase; 104 }; 105 106 #endif 107