| 1 |
/* |
| 2 |
* Copyright 2013 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 |
#include "include/effects/SkImageFilters.h" |
| 9 |
|
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkFlattenable.h" |
| 12 |
#include "include/core/SkImageFilter.h" |
| 13 |
#include "include/core/SkM44.h" |
| 14 |
#include "include/core/SkMatrix.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkSamplingOptions.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkShader.h" |
| 20 |
#include "include/core/SkSize.h" |
| 21 |
#include "include/core/SkTypes.h" |
| 22 |
#include "include/effects/SkRuntimeEffect.h" |
| 23 |
#include "include/private/base/SkFloatingPoint.h" |
| 24 |
#include "include/private/base/SkSpan_impl.h" |
| 25 |
#include "src/core/SkImageFilterTypes.h" |
| 26 |
#include "src/core/SkImageFilter_Base.h" |
| 27 |
#include "src/core/SkKnownRuntimeEffects.h" |
| 28 |
#include "src/core/SkReadBuffer.h" |
| 29 |
#include "src/core/SkWriteBuffer.h" |
| 30 |
|
| 31 |
#include <optional> |
| 32 |
#include <utility> |
| 33 |
|
| 34 |
namespace { |
| 35 |
|
| 36 |
class SkDisplacementMapImageFilter final : public SkImageFilter_Base { |
| 37 |
// Input image filter indices |
| 38 |
static constexpr int kDisplacement = 0; |
| 39 |
static constexpr int kColor = 1; |
| 40 |
|
| 41 |
// TODO(skbug.com/40045448): Use nearest to match historical behavior, but eventually this should |
| 42 |
// become a factory option. |
| 43 |
static constexpr SkSamplingOptions kDisplacementSampling{SkFilterMode::kNearest}; |
| 44 |
|
| 45 |
public: |
| 46 |
SkDisplacementMapImageFilter(SkColorChannel xChannel, |
| 47 |
SkColorChannel yChannel, |
| 48 |
SkScalar scale, |
| 49 |
sk_sp<SkImageFilter> inputs[2]) |
| 50 |
: SkImageFilter_Base(inputs, 2) |
| 51 |
, fXChannel(xChannel) |
| 52 |
, fYChannel(yChannel) |
| 53 |
, fScale(scale) { |
| 54 |
SkASSERT(SkIsFinite(fScale)); |
| 55 |
} |
| 56 |
|
| 57 |
SkRect computeFastBounds(const SkRect& src) const override; |
| 58 |
|
| 59 |
protected: |
| 60 |
void flatten(SkWriteBuffer&) const override; |
| 61 |
|
| 62 |
private: |
| 63 |
friend void ::SkRegisterDisplacementMapImageFilterFlattenable(); |
| 64 |
SK_FLATTENABLE_HOOKS(SkDisplacementMapImageFilter) |
| 65 |
|
| 66 |
skif::FilterResult onFilterImage(const skif::Context&) const override; |
| 67 |
|
| 68 |
skif::LayerSpace<SkIRect> onGetInputLayerBounds( |
| 69 |
const skif::Mapping& mapping, |
| 70 |
const skif::LayerSpace<SkIRect>& desiredOutput, |
| 71 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const override; |
| 72 |
|
| 73 |
std::optional<skif::LayerSpace<SkIRect>> onGetOutputLayerBounds( |
| 74 |
const skif::Mapping& mapping, |
| 75 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const override; |
| 76 |
|
| 77 |
skif::LayerSpace<SkIRect> outsetByMaxDisplacement(const skif::Mapping& mapping, |
| 78 |
skif::LayerSpace<SkIRect> bounds) const { |
| 79 |
// For max displacement, we treat 'scale' as a size instead of a vector. The vector offset |
| 80 |
// maps a [0,1] channel value to [-scale/2, scale/2], and treating it as a size |
| 81 |
// automatically accounts for the absolute magnitude when transforming from param to layer. |
| 82 |
skif::LayerSpace<SkSize> maxDisplacement = mapping.paramToLayer( |
| 83 |
skif::ParameterSpace<SkSize>({0.5f * fScale, 0.5f * fScale})); |
| 84 |
bounds.outset(maxDisplacement.ceil()); |
| 85 |
return bounds; |
| 86 |
} |
| 87 |
|
| 88 |
SkColorChannel fXChannel; |
| 89 |
SkColorChannel fYChannel; |
| 90 |
// Scale is really a ParameterSpace<Vector> where width = height = fScale, but we store just the |
| 91 |
// float here for easier serialization and convert to a size in onFilterImage(). |
| 92 |
SkScalar fScale; |
| 93 |
}; |
| 94 |
|
| 95 |
bool channel_selector_type_is_valid(SkColorChannel cst) { |
| 96 |
switch (cst) { |
| 97 |
case SkColorChannel::kR: |
| 98 |
case SkColorChannel::kG: |
| 99 |
case SkColorChannel::kB: |
| 100 |
case SkColorChannel::kA: |
| 101 |
return true; |
| 102 |
default: |
| 103 |
break; |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
sk_sp<SkShader> make_displacement_shader( |
| 109 |
sk_sp<SkShader> displacement, |
| 110 |
sk_sp<SkShader> color, |
| 111 |
skif::LayerSpace<skif::Vector> scale, |
| 112 |
SkColorChannel xChannel, |
| 113 |
SkColorChannel yChannel) { |
| 114 |
if (!color) { |
| 115 |
// Color is fully transparent, so no point in displacing it |
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
if (!displacement) { |
| 119 |
// Somehow we had a valid displacement image but failed to produce a shader |
| 120 |
// (e.g. an internal resolve to a new image failed). Treat the displacement as |
| 121 |
// transparent, but it's too late to switch to the applyTransform() optimization. |
| 122 |
displacement = SkShaders::Color(SK_ColorTRANSPARENT); |
| 123 |
} |
| 124 |
|
| 125 |
const SkRuntimeEffect* displacementEffect = |
| 126 |
GetKnownRuntimeEffect(SkKnownRuntimeEffects::StableKey::kDisplacement); |
| 127 |
|
| 128 |
auto channelSelector = [](SkColorChannel c) { |
| 129 |
return SkV4{c == SkColorChannel::kR ? 1.f : 0.f, |
| 130 |
c == SkColorChannel::kG ? 1.f : 0.f, |
| 131 |
c == SkColorChannel::kB ? 1.f : 0.f, |
| 132 |
c == SkColorChannel::kA ? 1.f : 0.f}; |
| 133 |
}; |
| 134 |
|
| 135 |
SkRuntimeShaderBuilder builder(sk_ref_sp(displacementEffect)); |
| 136 |
builder.child("displMap") = std::move(displacement); |
| 137 |
builder.child("colorMap") = std::move(color); |
| 138 |
builder.uniform("scale") = SkV2{scale.x(), scale.y()}; |
| 139 |
builder.uniform("xSelect") = channelSelector(xChannel); |
| 140 |
builder.uniform("ySelect") = channelSelector(yChannel); |
| 141 |
|
| 142 |
return builder.makeShader(); |
| 143 |
} |
| 144 |
|
| 145 |
} // anonymous namespace |
| 146 |
|
| 147 |
/////////////////////////////////////////////////////////////////////////////// |
| 148 |
|
| 149 |
sk_sp<SkImageFilter> SkImageFilters::DisplacementMap( |
| 150 |
SkColorChannel xChannelSelector, SkColorChannel yChannelSelector, SkScalar scale, |
| 151 |
sk_sp<SkImageFilter> displacement, sk_sp<SkImageFilter> color, const CropRect& cropRect) { |
| 152 |
if (!channel_selector_type_is_valid(xChannelSelector) || |
| 153 |
!channel_selector_type_is_valid(yChannelSelector)) { |
| 154 |
return nullptr; |
| 155 |
} |
| 156 |
if (!SkIsFinite(scale)) { |
| 157 |
return nullptr; |
| 158 |
} |
| 159 |
|
| 160 |
sk_sp<SkImageFilter> inputs[2] = { std::move(displacement), std::move(color) }; |
| 161 |
sk_sp<SkImageFilter> filter(new SkDisplacementMapImageFilter(xChannelSelector, yChannelSelector, |
| 162 |
scale, inputs)); |
| 163 |
if (cropRect) { |
| 164 |
filter = SkImageFilters::Crop(*cropRect, std::move(filter)); |
| 165 |
} |
| 166 |
return filter; |
| 167 |
} |
| 168 |
|
| 169 |
void SkRegisterDisplacementMapImageFilterFlattenable() { |
| 170 |
SK_REGISTER_FLATTENABLE(SkDisplacementMapImageFilter); |
| 171 |
// TODO (michaelludwig) - Remove after grace period for SKPs to stop using old name |
| 172 |
SkFlattenable::Register("SkDisplacementMapEffect", SkDisplacementMapImageFilter::CreateProc); |
| 173 |
SkFlattenable::Register("SkDisplacementMapEffectImpl", |
| 174 |
SkDisplacementMapImageFilter::CreateProc); |
| 175 |
} |
| 176 |
|
| 177 |
sk_sp<SkFlattenable> SkDisplacementMapImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 178 |
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2); |
| 179 |
|
| 180 |
SkColorChannel xsel = buffer.read32LE(SkColorChannel::kLastEnum); |
| 181 |
SkColorChannel ysel = buffer.read32LE(SkColorChannel::kLastEnum); |
| 182 |
SkScalar scale = buffer.readScalar(); |
| 183 |
|
| 184 |
return SkImageFilters::DisplacementMap(xsel, ysel, scale, common.getInput(0), |
| 185 |
common.getInput(1), common.cropRect()); |
| 186 |
} |
| 187 |
|
| 188 |
void SkDisplacementMapImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 189 |
this->SkImageFilter_Base::flatten(buffer); |
| 190 |
buffer.writeInt((int) fXChannel); |
| 191 |
buffer.writeInt((int) fYChannel); |
| 192 |
buffer.writeScalar(fScale); |
| 193 |
} |
| 194 |
|
| 195 |
/////////////////////////////////////////////////////////////////////////////// |
| 196 |
|
| 197 |
skif::FilterResult SkDisplacementMapImageFilter::onFilterImage(const skif::Context& ctx) const { |
| 198 |
skif::LayerSpace<SkIRect> requiredColorInput = |
| 199 |
this->outsetByMaxDisplacement(ctx.mapping(), ctx.desiredOutput()); |
| 200 |
skif::FilterResult colorOutput = |
| 201 |
this->getChildOutput(kColor, ctx.withNewDesiredOutput(requiredColorInput)); |
| 202 |
if (!colorOutput) { |
| 203 |
return {}; // No non-transparent black colors to displace |
| 204 |
} |
| 205 |
|
| 206 |
// When the color image filter is unrestricted, its output will be 'maxDisplacement' larger than |
| 207 |
// this filter's desired output. However, if it is cropped, we can restrict this filter's final |
| 208 |
// output. However it's not simply colorOutput intersected with desiredOutput since we have to |
| 209 |
// account for how the clipped colorOutput might still be displaced. |
| 210 |
skif::LayerSpace<SkIRect> outputBounds = |
| 211 |
this->outsetByMaxDisplacement(ctx.mapping(), colorOutput.layerBounds()); |
| 212 |
// 'outputBounds' has double the max displacement for edges where colorOutput had not been |
| 213 |
// clipped, but that's fine since we intersect with 'desiredOutput'. For edges that were cropped |
| 214 |
// the second max displacement represents how far they can be displaced, which might be inside |
| 215 |
// the original 'desiredOutput'. |
| 216 |
if (!outputBounds.intersect(ctx.desiredOutput())) { |
| 217 |
// None of the non-transparent black colors can be displaced into the desired bounds. |
| 218 |
return {}; |
| 219 |
} |
| 220 |
|
| 221 |
// Creation of the displacement map should happen in a non-colorspace aware context. This |
| 222 |
// texture is a purely mathematical construct, so we want to just operate on the stored |
| 223 |
// values. Consider: |
| 224 |
// |
| 225 |
// User supplies an sRGB displacement map. If we're rendering to a wider gamut, then we could |
| 226 |
// end up filtering the displacement map into that gamut, which has the effect of reducing |
| 227 |
// the amount of displacement that it represents (as encoded values move away from the |
| 228 |
// primaries). |
| 229 |
// |
| 230 |
// With a more complex DAG attached to this input, it's not clear that working in ANY specific |
| 231 |
// color space makes sense, so we ignore color spaces (and gamma) entirely. This may not be |
| 232 |
// ideal, but it's at least consistent and predictable. |
| 233 |
skif::FilterResult displacementOutput = |
| 234 |
this->getChildOutput(kDisplacement, ctx.withNewDesiredOutput(outputBounds) |
| 235 |
.withNewColorSpace(/*cs=*/nullptr)); |
| 236 |
|
| 237 |
// NOTE: The scale is a "vector" not a "size" since we want to preserve negations on the final |
| 238 |
// displacement vector. |
| 239 |
const skif::LayerSpace<skif::Vector> scale = |
| 240 |
ctx.mapping().paramToLayer(skif::ParameterSpace<skif::Vector>({fScale, fScale})); |
| 241 |
if (!displacementOutput) { |
| 242 |
// A null displacement map means its transparent black, but (0,0,0,0) becomes the vector |
| 243 |
// (-scale/2, -scale/2) applied to the color image, so represent the displacement as a |
| 244 |
// simple transform. |
| 245 |
skif::LayerSpace<SkMatrix> constantDisplacement{SkMatrix::Translate(-0.5f * scale.x(), |
| 246 |
-0.5f * scale.y())}; |
| 247 |
return colorOutput.applyTransform(ctx, constantDisplacement, kDisplacementSampling); |
| 248 |
} |
| 249 |
|
| 250 |
// If we made it this far, then we actually have per-pixel displacement affecting the color |
| 251 |
// image. We need to evaluate each pixel within 'outputBounds'. |
| 252 |
using ShaderFlags = skif::FilterResult::ShaderFlags; |
| 253 |
|
| 254 |
skif::FilterResult::Builder builder{ctx}; |
| 255 |
builder.add(displacementOutput, /*sampleBounds=*/outputBounds); |
| 256 |
builder.add(colorOutput, |
| 257 |
/*sampleBounds=*/requiredColorInput, |
| 258 |
ShaderFlags::kNonTrivialSampling, |
| 259 |
kDisplacementSampling); |
| 260 |
return builder.eval( |
| 261 |
[&](SkSpan<sk_sp<SkShader>> inputs) { |
| 262 |
return make_displacement_shader(inputs[kDisplacement], inputs[kColor], |
| 263 |
scale, fXChannel, fYChannel); |
| 264 |
}, outputBounds); |
| 265 |
} |
| 266 |
|
| 267 |
skif::LayerSpace<SkIRect> SkDisplacementMapImageFilter::onGetInputLayerBounds( |
| 268 |
const skif::Mapping& mapping, |
| 269 |
const skif::LayerSpace<SkIRect>& desiredOutput, |
| 270 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const { |
| 271 |
// Pixels up to the maximum displacement away from 'desiredOutput' can be moved into those |
| 272 |
// bounds, depending on how the displacement map renders. To ensure those colors are defined, |
| 273 |
// we require that outset buffer around 'desiredOutput' from the color map. |
| 274 |
skif::LayerSpace<SkIRect> requiredInput = this->outsetByMaxDisplacement(mapping, desiredOutput); |
| 275 |
requiredInput = this->getChildInputLayerBounds(kColor, mapping, requiredInput, contentBounds); |
| 276 |
|
| 277 |
// Accumulate the required input for the displacement filter to cover the original desired out |
| 278 |
requiredInput.join(this->getChildInputLayerBounds( |
| 279 |
kDisplacement, mapping, desiredOutput, contentBounds)); |
| 280 |
return requiredInput; |
| 281 |
} |
| 282 |
|
| 283 |
std::optional<skif::LayerSpace<SkIRect>> SkDisplacementMapImageFilter::onGetOutputLayerBounds( |
| 284 |
const skif::Mapping& mapping, |
| 285 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const { |
| 286 |
auto colorOutput = this->getChildOutputLayerBounds(kColor, mapping, contentBounds); |
| 287 |
if (colorOutput) { |
| 288 |
return this->outsetByMaxDisplacement(mapping, *colorOutput); |
| 289 |
} else { |
| 290 |
return skif::LayerSpace<SkIRect>::Unbounded(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
SkRect SkDisplacementMapImageFilter::computeFastBounds(const SkRect& src) const { |
| 295 |
SkRect colorBounds = this->getInput(kColor) ? this->getInput(kColor)->computeFastBounds(src) |
| 296 |
: src; |
| 297 |
float maxDisplacement = 0.5f * SkScalarAbs(fScale); |
| 298 |
return colorBounds.makeOutset(maxDisplacement, maxDisplacement); |
| 299 |
} |