| 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_KeyContext_DEFINED |
| 9 |
#define skgpu_graphite_KeyContext_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkImageInfo.h" |
| 12 |
#include "include/core/SkM44.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "src/base/SkEnumBitMask.h" |
| 15 |
#include "src/core/SkColorData.h" |
| 16 |
#include "src/core/SkColorSpaceXformSteps.h" |
| 17 |
#include "src/gpu/graphite/TextureProxy.h" |
| 18 |
|
| 19 |
class SkRuntimeEffect; |
| 20 |
|
| 21 |
namespace skgpu::graphite { |
| 22 |
|
| 23 |
class Caps; |
| 24 |
class DrawContext; |
| 25 |
enum class DstReadStrategy : uint8_t; |
| 26 |
class FloatStorageManager; |
| 27 |
class PaintParamsKeyBuilder; |
| 28 |
class PipelineDataGatherer; |
| 29 |
class Recorder; |
| 30 |
class RuntimeEffectDictionary; |
| 31 |
class ShaderCodeDictionary; |
| 32 |
|
| 33 |
enum class KeyGenFlags : uint8_t { |
| 34 |
kDefault = 0b0, |
| 35 |
// By default, linear sampling can be optimized to nearest when it's visually equivalent. |
| 36 |
// This flag disables this behavior. |
| 37 |
kDisableSamplingOptimization = 0b001, |
| 38 |
// By default, identity color conversions map to ColorSpaceTransformPremul as a reasonably |
| 39 |
// performant baseline that avoids shader combinatorics. However, in certain contexts (such as |
| 40 |
// image filters or runtime effects) that sample an image many times *and* perform up front |
| 41 |
// work to ensure there doesn't need to be any color conversion, skipping color space conversion |
| 42 |
// in the shader produces meaningful performance improvements. |
| 43 |
kEnableIdentityColorSpaceXform = 0b010, |
| 44 |
// By default, alpha-only image shaders are colorized by the paint's color. In the context of |
| 45 |
// a runtime effect this is disabled. |
| 46 |
kDisableAlphaOnlyImageColorization = 0b100, |
| 47 |
}; |
| 48 |
SK_MAKE_BITMASK_OPS(KeyGenFlags) |
| 49 |
|
| 50 |
// The key context must always be able to provide a valid ShaderCodeDictionary and |
| 51 |
// SkRuntimeEffectDictionary. Depending on the calling context it can also supply a |
| 52 |
// backend-specific resource providing object (e.g., a Recorder). |
| 53 |
class KeyContext { |
| 54 |
public: |
| 55 |
// Constructor for the pre-compile code path (i.e., no Recorder) |
| 56 |
KeyContext(const Caps*, |
| 57 |
FloatStorageManager*, |
| 58 |
PaintParamsKeyBuilder*, |
| 59 |
PipelineDataGatherer*, |
| 60 |
ShaderCodeDictionary*, |
| 61 |
sk_sp<RuntimeEffectDictionary>, |
| 62 |
const SkColorInfo& dstColorInfo); |
| 63 |
|
| 64 |
// Constructor for the ExtractPaintData code path (i.e., with a Recorder) |
| 65 |
KeyContext(Recorder*, |
| 66 |
DrawContext*, |
| 67 |
FloatStorageManager*, |
| 68 |
PaintParamsKeyBuilder*, |
| 69 |
PipelineDataGatherer*, |
| 70 |
const SkM44& local2Dev, |
| 71 |
const SkColorInfo& dstColorInfo, |
| 72 |
SkEnumBitMask<KeyGenFlags> initialFlags, |
| 73 |
const SkColor4f& paintColor); |
| 74 |
|
| 75 |
KeyContext(const KeyContext&, SkEnumBitMask<KeyGenFlags> xtraFlags=KeyGenFlags::kDefault); |
| 76 |
~KeyContext(); |
| 77 |
|
| 78 |
// Create scoped KeyContexts that allow child effects to be processed differently. |
| 79 |
KeyContext withColorInfo(const SkColorInfo& info) const { |
| 80 |
KeyContext o = *this; |
| 81 |
o.fDstColorInfo = info; |
| 82 |
|
| 83 |
// We want to keep fPaintColor's alpha value but replace the RGB with values in the new |
| 84 |
// color space. By overriding the alpha type of the old and new dst color infos to be |
| 85 |
// kOpaque, SkColorSpaceXformSteps will leave the alpha channel alone. |
| 86 |
SkColorSpaceXformSteps(fDstColorInfo.colorSpace(), kOpaque_SkAlphaType, |
| 87 |
info.colorSpace(), kOpaque_SkAlphaType) |
| 88 |
.apply(o.fPaintColor.vec()); |
| 89 |
SkASSERT(o.fPaintColor.fA == fPaintColor.fA); |
| 90 |
return o; |
| 91 |
} |
| 92 |
|
| 93 |
// The key generation flags vary in the scope of a SkRuntimeEffect per child based on how the |
| 94 |
// RuntimeEffect's SkSL invokes each child. |
| 95 |
KeyContext forRuntimeEffect(const SkRuntimeEffect* effect, int child) const; |
| 96 |
|
| 97 |
KeyContext withExtraFlags(SkEnumBitMask<KeyGenFlags> flags) const { |
| 98 |
return KeyContext(*this, flags); |
| 99 |
} |
| 100 |
|
| 101 |
Recorder* recorder() const { return fRecorder; } |
| 102 |
DrawContext* drawContext() const { return fDC; } |
| 103 |
|
| 104 |
const Caps* caps() const { return fCaps; } |
| 105 |
|
| 106 |
const SkM44& local2Dev() const { return fLocal2Dev; } |
| 107 |
const SkMatrix* localMatrix() const { return fLocalMatrix; } |
| 108 |
|
| 109 |
FloatStorageManager* floatStorageManager() const { return fFloatStorageManager; } |
| 110 |
PaintParamsKeyBuilder* paintParamsKeyBuilder() const { return fPaintParamsKeyBuilder; } |
| 111 |
PipelineDataGatherer* pipelineDataGatherer() const { return fPipelineDataGatherer; } |
| 112 |
ShaderCodeDictionary* dict() const { return fDictionary; } |
| 113 |
|
| 114 |
sk_sp<RuntimeEffectDictionary> rtEffectDict() const; |
| 115 |
|
| 116 |
const SkColorInfo& dstColorInfo() const { return fDstColorInfo; } |
| 117 |
|
| 118 |
const SkPMColor4f& paintColor() const { return fPaintColor; } |
| 119 |
|
| 120 |
SkEnumBitMask<KeyGenFlags> flags() const { return fKeyGenFlags; } |
| 121 |
|
| 122 |
private: |
| 123 |
// Fields which will not change over the course of building a paint key |
| 124 |
const Caps* fCaps; |
| 125 |
Recorder* fRecorder; |
| 126 |
DrawContext* fDC; |
| 127 |
FloatStorageManager* fFloatStorageManager; |
| 128 |
PaintParamsKeyBuilder* fPaintParamsKeyBuilder; |
| 129 |
PipelineDataGatherer* fPipelineDataGatherer; |
| 130 |
ShaderCodeDictionary* fDictionary; |
| 131 |
sk_sp<RuntimeEffectDictionary> fRTEffectDict; |
| 132 |
SkM44 fLocal2Dev; |
| 133 |
|
| 134 |
protected: |
| 135 |
// Fields that can be modified while walking a paint's effects for a key |
| 136 |
SkMatrix* fLocalMatrix = nullptr; |
| 137 |
SkColorInfo fDstColorInfo; |
| 138 |
// Although stored as premul the paint color is actually comprised of an opaque RGB portion |
| 139 |
// and a separate alpha portion. The two portions will never be used together but are stored |
| 140 |
// together to reduce the number of uniforms. |
| 141 |
SkPMColor4f fPaintColor = SK_PMColor4fBLACK; |
| 142 |
SkEnumBitMask<KeyGenFlags> fKeyGenFlags = KeyGenFlags::kDefault; |
| 143 |
}; |
| 144 |
|
| 145 |
class KeyContextWithLocalMatrix : public KeyContext { |
| 146 |
public: |
| 147 |
KeyContextWithLocalMatrix(const KeyContext& other, const SkMatrix& childLM) |
| 148 |
: KeyContext(other) { |
| 149 |
if (fLocalMatrix) { |
| 150 |
fStorage = SkMatrix::Concat(childLM, *fLocalMatrix); |
| 151 |
} else { |
| 152 |
fStorage = childLM; |
| 153 |
} |
| 154 |
|
| 155 |
fLocalMatrix = &fStorage; |
| 156 |
} |
| 157 |
|
| 158 |
private: |
| 159 |
KeyContextWithLocalMatrix(const KeyContextWithLocalMatrix&) = delete; |
| 160 |
KeyContextWithLocalMatrix& operator=(const KeyContextWithLocalMatrix&) = delete; |
| 161 |
|
| 162 |
SkMatrix fStorage; |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
} // namespace skgpu::graphite |
| 167 |
|
| 168 |
#endif // skgpu_graphite_KeyContext_DEFINED |