1 /* 2 * Copyright 2022 Google LLC 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 skgpu_graphite_PaintParams_DEFINED 9 #define skgpu_graphite_PaintParams_DEFINED 10 11 #include "include/core/SkColor.h" 12 #include "include/core/SkPaint.h" 13 #include "src/gpu/graphite/Caps.h" 14 #include <functional> // std::function 15 16 class SkColorInfo; 17 class SkShader; 18 19 namespace skgpu::graphite { 20 21 class DrawContext; 22 class KeyContext; 23 class PaintParamsKeyBuilder; 24 class PipelineDataGatherer; 25 class Recorder; 26 class TextureProxy; 27 28 // TBD: If occlusion culling is eliminated as a phase, we can easily move the paint conversion 29 // back to Device when the command is recorded (similar to SkPaint -> GrPaint), and then 30 // PaintParams is not required as an intermediate representation. 31 // NOTE: Only represents the shading state of an SkPaint. Style and complex effects (mask filters, 32 // image filters, path effects) must be handled higher up. AA is not tracked since everything is 33 // assumed to be anti-aliased. 34 class PaintParams { 35 public: 36 explicit PaintParams(const SkPaint&, 37 sk_sp<SkBlender> primitiveBlender, 38 sk_sp<SkShader> clipShader, 39 DstReadRequirement dstReadReq, 40 bool skipColorXform); 41 42 PaintParams(const PaintParams&); 43 ~PaintParams(); 44 45 PaintParams& operator=(const PaintParams&); 46 color()47 SkColor4f color() const { return fColor; } 48 49 std::optional<SkBlendMode> asFinalBlendMode() const; finalBlender()50 SkBlender* finalBlender() const { return fFinalBlender.get(); } 51 sk_sp<SkBlender> refFinalBlender() const; 52 shader()53 SkShader* shader() const { return fShader.get(); } 54 sk_sp<SkShader> refShader() const; 55 colorFilter()56 SkColorFilter* colorFilter() const { return fColorFilter.get(); } 57 sk_sp<SkColorFilter> refColorFilter() const; 58 primitiveBlender()59 SkBlender* primitiveBlender() const { return fPrimitiveBlender.get(); } 60 sk_sp<SkBlender> refPrimitiveBlender() const; 61 dstReadRequirement()62 DstReadRequirement dstReadRequirement() const { return fDstReadReq; } skipColorXform()63 bool skipColorXform() const { return fSkipColorXform; } dither()64 bool dither() const { return fDither; } 65 66 /** Converts an SkColor4f to the destination color space. */ 67 static SkColor4f Color4fPrepForDst(SkColor4f srgb, const SkColorInfo& dstColorInfo); 68 69 void toKey(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const; 70 71 void notifyImagesInUse(Recorder*, DrawContext*) const; 72 73 private: 74 void addPaintColorToKey(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const; 75 void handlePrimitiveColor(const KeyContext&, 76 PaintParamsKeyBuilder*, 77 PipelineDataGatherer*) const; 78 void handlePaintAlpha(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const; 79 void handleColorFilter(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const; 80 void handleDithering(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const; 81 void handleDstRead(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const; 82 83 SkColor4f fColor; 84 sk_sp<SkBlender> fFinalBlender; // A nullptr here means SrcOver blending 85 sk_sp<SkShader> fShader; 86 sk_sp<SkColorFilter> fColorFilter; 87 // A nullptr fPrimitiveBlender means there's no primitive color blending and it is skipped. 88 // In the case where there is primitive blending, the primitive color is the source color and 89 // the dest is the paint's color (or the paint's shader's computed color). 90 sk_sp<SkBlender> fPrimitiveBlender; 91 sk_sp<SkShader> fClipShader; 92 DstReadRequirement fDstReadReq; 93 bool fSkipColorXform; 94 bool fDither; 95 }; 96 97 using AddToKeyFn = std::function<void()>; 98 99 void Blend(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*, 100 AddToKeyFn addBlendToKey, AddToKeyFn addSrcToKey, AddToKeyFn addDstToKey); 101 void Compose(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*, 102 AddToKeyFn addInnerToKey, AddToKeyFn addOuterToKey); 103 // Add a blend mode node for a specific SkBlendMode. 104 void AddKnownModeBlend(const KeyContext&, 105 PaintParamsKeyBuilder*, 106 PipelineDataGatherer*, 107 SkBlendMode); 108 // Add a blend mode node for an SkBlendMode that can vary 109 void AddModeBlend(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*, SkBlendMode); 110 void AddDstReadBlock(const KeyContext&, 111 PaintParamsKeyBuilder*, 112 PipelineDataGatherer*, 113 DstReadRequirement); 114 void AddDitherBlock(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*, SkColorType); 115 116 } // namespace skgpu::graphite 117 118 #endif // skgpu_PaintParams_DEFINED 119