| 1 |
/* |
| 2 |
* Copyright 2012 The Android Open Source Project |
| 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/SkPoint.h" |
| 15 |
#include "include/core/SkPoint3.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkShader.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/effects/SkRuntimeEffect.h" |
| 22 |
#include "include/private/base/SkCPUTypes.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/SkRectPriv.h" |
| 30 |
#include "src/core/SkWriteBuffer.h" |
| 31 |
#include "src/effects/SkEmbossMaskFilter.h" |
| 32 |
|
| 33 |
#include <optional> |
| 34 |
#include <utility> |
| 35 |
|
| 36 |
struct SkISize; |
| 37 |
|
| 38 |
namespace { |
| 39 |
// The 3D points/vectors used for lighting don't have a great analog for the rest of the image |
| 40 |
// filtering system, and don't have any representation for ParameterSpace and LayerSpace. The |
| 41 |
// SVG spec is also vague on how to handle managing them. Using the principle of least-surprise, |
| 42 |
// the X and Y coordinates are treated as ParameterSpace<SkPoint|Vector> and the Z will be |
| 43 |
// scaled by the average of the X and Y scale factors when tranforming to layer space. For uniform |
| 44 |
// scaling transforms, this has the desirable behavior of uniformly scaling the Z axis as well. |
| 45 |
struct ZValue { |
| 46 |
ZValue() : fZ(0.f) {} |
| 47 |
ZValue(float z) : fZ(z) {} |
| 48 |
operator float() const { return fZ; } |
| 49 |
|
| 50 |
float fZ; |
| 51 |
}; |
| 52 |
} // anonymous namespace |
| 53 |
|
| 54 |
namespace skif { |
| 55 |
template<> |
| 56 |
class LayerSpace<ZValue> { |
| 57 |
public: |
| 58 |
LayerSpace() = default; |
| 59 |
explicit LayerSpace(ZValue z) : fData(z) {} |
| 60 |
|
| 61 |
float val() const { return fData.fZ; } |
| 62 |
|
| 63 |
static LayerSpace<ZValue> Map(const Mapping& mapping, ParameterSpace<ZValue> z) { |
| 64 |
// See comment on ZValue for rationale. |
| 65 |
skif::LayerSpace<skif::Vector> z2d = mapping.paramToLayer( |
| 66 |
skif::ParameterSpace<skif::Vector>({ZValue(z), ZValue(z)})); |
| 67 |
return LayerSpace<ZValue>(sk_float_midpoint(z2d.x(), z2d.y())); |
| 68 |
} |
| 69 |
|
| 70 |
private: |
| 71 |
ZValue fData; |
| 72 |
}; |
| 73 |
} // namespace skif |
| 74 |
|
| 75 |
namespace { |
| 76 |
|
| 77 |
struct Light { |
| 78 |
enum class Type { |
| 79 |
kDistant, |
| 80 |
kPoint, |
| 81 |
kSpot, |
| 82 |
kLast = kSpot |
| 83 |
}; |
| 84 |
|
| 85 |
Type fType; |
| 86 |
SkColor fLightColor; // All lights |
| 87 |
|
| 88 |
// Location and direction are decomposed into typed XY and Z for how they are transformed from |
| 89 |
// parameter space to layer space. |
| 90 |
skif::ParameterSpace<SkPoint> fLocationXY; // Spotlight and point lights only |
| 91 |
skif::ParameterSpace<ZValue> fLocationZ; // "" |
| 92 |
|
| 93 |
skif::ParameterSpace<skif::Vector> fDirectionXY; // Spotlight and distant lights only |
| 94 |
skif::ParameterSpace<ZValue> fDirectionZ; // "" |
| 95 |
|
| 96 |
// Spotlight only (and unchanged by layer matrix) |
| 97 |
float fFalloffExponent; |
| 98 |
float fCosCutoffAngle; |
| 99 |
|
| 100 |
static Light Point(SkColor color, const SkPoint3& location) { |
| 101 |
return {Type::kPoint, |
| 102 |
color, |
| 103 |
skif::ParameterSpace<SkPoint>({location.fX, location.fY}), |
| 104 |
skif::ParameterSpace<ZValue>(location.fZ), |
| 105 |
/*directionXY=*/{}, |
| 106 |
/*directionZ=*/{}, |
| 107 |
/*falloffExponent=*/0.f, |
| 108 |
/*cutoffAngle=*/0.f}; |
| 109 |
} |
| 110 |
|
| 111 |
static Light Distant(SkColor color, const SkPoint3& direction) { |
| 112 |
return {Type::kDistant, |
| 113 |
color, |
| 114 |
/*locationXY=*/{}, |
| 115 |
/*locationZ=*/{}, |
| 116 |
skif::ParameterSpace<skif::Vector>({direction.fX, direction.fY}), |
| 117 |
skif::ParameterSpace<ZValue>(direction.fZ), |
| 118 |
/*falloffExponent=*/0.f, |
| 119 |
/*cutoffAngle=*/0.f}; |
| 120 |
} |
| 121 |
|
| 122 |
static Light Spot(SkColor color, const SkPoint3& location, const SkPoint3& direction, |
| 123 |
float falloffExponent, float cosCutoffAngle) { |
| 124 |
return {Type::kSpot, |
| 125 |
color, |
| 126 |
skif::ParameterSpace<SkPoint>({location.fX, location.fY}), |
| 127 |
skif::ParameterSpace<ZValue>(location.fZ), |
| 128 |
skif::ParameterSpace<skif::Vector>({direction.fX, direction.fY}), |
| 129 |
skif::ParameterSpace<ZValue>(direction.fZ), |
| 130 |
falloffExponent, |
| 131 |
cosCutoffAngle}; |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
struct Material { |
| 136 |
enum class Type { |
| 137 |
kDiffuse, |
| 138 |
kSpecular, |
| 139 |
kEmbossSpecular, |
| 140 |
kLast = kEmbossSpecular |
| 141 |
}; |
| 142 |
|
| 143 |
Type fType; |
| 144 |
// The base scale factor applied to alpha image to go from [0-1] to [0-depth] before computing |
| 145 |
// surface normals. |
| 146 |
skif::ParameterSpace<ZValue> fSurfaceDepth; |
| 147 |
|
| 148 |
// Non-geometric |
| 149 |
float fK; // Reflectance coefficient |
| 150 |
float fShininess; // Specular only |
| 151 |
|
| 152 |
static Material Diffuse(float k, float surfaceDepth) { |
| 153 |
return {Type::kDiffuse, skif::ParameterSpace<ZValue>(surfaceDepth), k, 0.f}; |
| 154 |
} |
| 155 |
|
| 156 |
static Material Specular(float k, float shininess, float surfaceDepth) { |
| 157 |
return {Type::kSpecular, skif::ParameterSpace<ZValue>(surfaceDepth), k, shininess}; |
| 158 |
} |
| 159 |
|
| 160 |
static Material EmbossSpecular(float k, float shininess, float surfaceDepth) { |
| 161 |
return {Type::kEmbossSpecular , skif::ParameterSpace<ZValue>(surfaceDepth), k, shininess}; |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
class SkLightingImageFilter final : public SkImageFilter_Base { |
| 166 |
public: |
| 167 |
SkLightingImageFilter(const Light& light, const Material& material, sk_sp<SkImageFilter> input) |
| 168 |
: SkImageFilter_Base(&input, 1) |
| 169 |
, fLight(light) |
| 170 |
, fMaterial(material) {} |
| 171 |
|
| 172 |
SkRect computeFastBounds(const SkRect& src) const override; |
| 173 |
|
| 174 |
protected: |
| 175 |
void flatten(SkWriteBuffer&) const override; |
| 176 |
|
| 177 |
private: |
| 178 |
friend void ::SkRegisterLightingImageFilterFlattenables(); |
| 179 |
SK_FLATTENABLE_HOOKS(SkLightingImageFilter) |
| 180 |
static Light LegacyDeserializeLight(SkReadBuffer& buffer); |
| 181 |
static sk_sp<SkFlattenable> LegacyDiffuseCreateProc(SkReadBuffer& buffer); |
| 182 |
static sk_sp<SkFlattenable> LegacySpecularCreateProc(SkReadBuffer& buffer); |
| 183 |
|
| 184 |
bool onAffectsTransparentBlack() const override { return true; } |
| 185 |
|
| 186 |
skif::FilterResult onFilterImage(const skif::Context&) const override; |
| 187 |
|
| 188 |
skif::LayerSpace<SkIRect> onGetInputLayerBounds( |
| 189 |
const skif::Mapping& mapping, |
| 190 |
const skif::LayerSpace<SkIRect>& desiredOutput, |
| 191 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const override; |
| 192 |
|
| 193 |
std::optional<skif::LayerSpace<SkIRect>> onGetOutputLayerBounds( |
| 194 |
const skif::Mapping& mapping, |
| 195 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const override; |
| 196 |
|
| 197 |
skif::LayerSpace<SkIRect> requiredInput(const skif::LayerSpace<SkIRect>& desiredOutput) const { |
| 198 |
// We request 1px of padding so that the visible normal map can do a regular Sobel kernel |
| 199 |
// eval. The Sobel kernel is always applied in layer pixels |
| 200 |
skif::LayerSpace<SkIRect> requiredInput = desiredOutput; |
| 201 |
requiredInput.outset(skif::LayerSpace<SkISize>({1, 1})); |
| 202 |
return requiredInput; |
| 203 |
} |
| 204 |
|
| 205 |
Light fLight; |
| 206 |
Material fMaterial; |
| 207 |
}; |
| 208 |
|
| 209 |
// Creates a shader that performs a Sobel filter on the alpha channel of the input image, using |
| 210 |
// 'edgeBounds' to decide how to modify the kernel weights. |
| 211 |
sk_sp<SkShader> make_normal_shader(sk_sp<SkShader> alphaMap, |
| 212 |
const skif::LayerSpace<SkIRect>& edgeBounds, |
| 213 |
skif::LayerSpace<ZValue> surfaceDepth) { |
| 214 |
const SkRuntimeEffect* normalEffect = |
| 215 |
GetKnownRuntimeEffect(SkKnownRuntimeEffects::StableKey::kNormal); |
| 216 |
|
| 217 |
SkRuntimeShaderBuilder builder(sk_ref_sp(normalEffect)); |
| 218 |
builder.child("alphaMap") = std::move(alphaMap); |
| 219 |
builder.uniform("edgeBounds") = SkRect::Make(SkIRect(edgeBounds)).makeInset(0.5f, 0.5f); |
| 220 |
builder.uniform("negSurfaceDepth") = -surfaceDepth.val(); |
| 221 |
|
| 222 |
return builder.makeShader(); |
| 223 |
} |
| 224 |
|
| 225 |
sk_sp<SkShader> make_lighting_shader(sk_sp<SkShader> normalMap, |
| 226 |
Light::Type lightType, |
| 227 |
SkColor lightColor, |
| 228 |
skif::LayerSpace<SkPoint> locationXY, |
| 229 |
skif::LayerSpace<ZValue> locationZ, |
| 230 |
skif::LayerSpace<skif::Vector> directionXY, |
| 231 |
skif::LayerSpace<ZValue> directionZ, |
| 232 |
float falloffExponent, |
| 233 |
float cosCutoffAngle, |
| 234 |
Material::Type matType, |
| 235 |
skif::LayerSpace<ZValue> surfaceDepth, |
| 236 |
float k, |
| 237 |
float shininess) { |
| 238 |
|
| 239 |
const SkRuntimeEffect* lightingEffect = |
| 240 |
GetKnownRuntimeEffect(SkKnownRuntimeEffects::StableKey::kLighting); |
| 241 |
|
| 242 |
SkRuntimeShaderBuilder builder(sk_ref_sp(lightingEffect)); |
| 243 |
builder.child("normalMap") = std::move(normalMap); |
| 244 |
|
| 245 |
builder.uniform("materialAndLightType") = |
| 246 |
SkV4{surfaceDepth.val(), |
| 247 |
shininess, |
| 248 |
static_cast<float>(matType), |
| 249 |
lightType == Light::Type::kPoint ? |
| 250 |
0.f : (lightType == Light::Type::kDistant ? -1.f : 1.f)}; |
| 251 |
builder.uniform("lightPosAndSpotFalloff") = |
| 252 |
SkV4{locationXY.x(), locationXY.y(), locationZ.val(), falloffExponent}; |
| 253 |
|
| 254 |
// Pre-normalize the light direction, but this can be (0,0,0) for point lights, which won't use |
| 255 |
// the uniform anyways. Avoid a division by 0 to keep ASAN happy or in the event that a spot/dir |
| 256 |
// light have bad user input. |
| 257 |
SkV3 dir{directionXY.x(), directionXY.y(), directionZ.val()}; |
| 258 |
float invDirLen = dir.length(); |
| 259 |
invDirLen = invDirLen ? 1.0f / invDirLen : 0.f; |
| 260 |
builder.uniform("lightDirAndSpotCutoff") = |
| 261 |
SkV4{invDirLen*dir.x, invDirLen*dir.y, invDirLen*dir.z, cosCutoffAngle}; |
| 262 |
|
| 263 |
// Historically, the Skia lighting image filter did not apply any color space transformation to |
| 264 |
// the light's color. The SVG spec for the lighting effects does not stipulate how to interpret |
| 265 |
// the color for a light. Overall, it does not have a principled physically based approach, but |
| 266 |
// the closest way to interpret it, is: |
| 267 |
// - the material's K is a uniformly distributed reflectance coefficient |
| 268 |
// - lighting *should* be calculated in a linear color space, which is the default for SVG |
| 269 |
// filters. Chromium manages these color transformations using SkImageFilters::ColorFilter |
| 270 |
// so it's not necessarily reflected in the Context's color space. |
| 271 |
// - it's unspecified in the SVG spec if the light color should be transformed to linear or |
| 272 |
// interpreted as linear already. Regardless, if there was any transformation that needed to |
| 273 |
// occur, Blink took care of it in the past so adding color space management to the light |
| 274 |
// color would be a breaking change. |
| 275 |
// - so for now, leave the color un-modified and apply K up front since no color space |
| 276 |
// transforms need to be performed on the original light color. |
| 277 |
const float colorScale = k / 255.f; |
| 278 |
builder.uniform("lightColor") = SkV3{SkColorGetR(lightColor) * colorScale, |
| 279 |
SkColorGetG(lightColor) * colorScale, |
| 280 |
SkColorGetB(lightColor) * colorScale}; |
| 281 |
|
| 282 |
return builder.makeShader(); |
| 283 |
} |
| 284 |
|
| 285 |
sk_sp<SkImageFilter> make_lighting(const Light& light, |
| 286 |
const Material& material, |
| 287 |
sk_sp<SkImageFilter> input, |
| 288 |
const SkImageFilters::CropRect& cropRect) { |
| 289 |
// According to the spec, ks and kd can be any non-negative number: |
| 290 |
// http://www.w3.org/TR/SVG/filters.html#feSpecularLightingElement |
| 291 |
if (!SkIsFinite(material.fK, material.fShininess, ZValue(material.fSurfaceDepth)) || |
| 292 |
material.fK < 0.f) { |
| 293 |
return nullptr; |
| 294 |
} |
| 295 |
|
| 296 |
// Ensure light values are finite, and the cosine should be between -1 and 1 |
| 297 |
if (!SkPoint(light.fLocationXY).isFinite() || !skif::Vector(light.fDirectionXY).isFinite() || |
| 298 |
!SkIsFinite(light.fFalloffExponent, light.fCosCutoffAngle, |
| 299 |
ZValue(light.fLocationZ), ZValue(light.fDirectionZ)) || |
| 300 |
light.fCosCutoffAngle < -1.f || light.fCosCutoffAngle > 1.f) { |
| 301 |
return nullptr; |
| 302 |
} |
| 303 |
|
| 304 |
// If a crop rect is provided, it clamps both the input (to better match the SVG's normal |
| 305 |
// boundary condition spec) and the output (because otherwise it has infinite bounds). |
| 306 |
sk_sp<SkImageFilter> filter = std::move(input); |
| 307 |
if (cropRect) { |
| 308 |
filter = SkImageFilters::Crop(*cropRect, std::move(filter)); |
| 309 |
} |
| 310 |
filter = sk_sp<SkImageFilter>( |
| 311 |
new SkLightingImageFilter(light, material, std::move(filter))); |
| 312 |
if (cropRect) { |
| 313 |
filter = SkImageFilters::Crop(*cropRect, std::move(filter)); |
| 314 |
} |
| 315 |
return filter; |
| 316 |
} |
| 317 |
|
| 318 |
} // anonymous namespace |
| 319 |
|
| 320 |
sk_sp<SkImageFilter> SkImageFilters::DistantLitDiffuse( |
| 321 |
const SkPoint3& direction, SkColor lightColor, SkScalar surfaceScale, SkScalar kd, |
| 322 |
sk_sp<SkImageFilter> input, const CropRect& cropRect) { |
| 323 |
return make_lighting(Light::Distant(lightColor, direction), |
| 324 |
Material::Diffuse(kd, surfaceScale), |
| 325 |
std::move(input), cropRect); |
| 326 |
} |
| 327 |
|
| 328 |
sk_sp<SkImageFilter> SkImageFilters::PointLitDiffuse( |
| 329 |
const SkPoint3& location, SkColor lightColor, SkScalar surfaceScale, SkScalar kd, |
| 330 |
sk_sp<SkImageFilter> input, const CropRect& cropRect) { |
| 331 |
return make_lighting(Light::Point(lightColor, location), |
| 332 |
Material::Diffuse(kd, surfaceScale), |
| 333 |
std::move(input), cropRect); |
| 334 |
} |
| 335 |
|
| 336 |
sk_sp<SkImageFilter> SkImageFilters::SpotLitDiffuse( |
| 337 |
const SkPoint3& location, const SkPoint3& target, SkScalar falloffExponent, |
| 338 |
SkScalar cutoffAngle, SkColor lightColor, SkScalar surfaceScale, SkScalar kd, |
| 339 |
sk_sp<SkImageFilter> input, const CropRect& cropRect) { |
| 340 |
SkPoint3 dir = target - location; |
| 341 |
float cosCutoffAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); |
| 342 |
return make_lighting(Light::Spot(lightColor, location, dir, falloffExponent, cosCutoffAngle), |
| 343 |
Material::Diffuse(kd, surfaceScale), |
| 344 |
std::move(input), cropRect); |
| 345 |
} |
| 346 |
|
| 347 |
// Private factory method for usage in SkEmbossMaskFilter. |
| 348 |
sk_sp<SkImageFilter> SkEmbossMaskFilter::LegacySpecular( |
| 349 |
const SkPoint3& direction, SkColor lightColor, SkScalar surfaceScale, SkScalar ks, |
| 350 |
SkScalar shininess, sk_sp<SkImageFilter> input) { |
| 351 |
return make_lighting(::Light::Distant(lightColor, direction), |
| 352 |
Material::EmbossSpecular(ks, shininess, surfaceScale), |
| 353 |
std::move(input), {}); |
| 354 |
} |
| 355 |
|
| 356 |
sk_sp<SkImageFilter> SkImageFilters::DistantLitSpecular( |
| 357 |
const SkPoint3& direction, SkColor lightColor, SkScalar surfaceScale, SkScalar ks, |
| 358 |
SkScalar shininess, sk_sp<SkImageFilter> input, const CropRect& cropRect) { |
| 359 |
return make_lighting(Light::Distant(lightColor, direction), |
| 360 |
Material::Specular(ks, shininess, surfaceScale), |
| 361 |
std::move(input), cropRect); |
| 362 |
} |
| 363 |
|
| 364 |
sk_sp<SkImageFilter> SkImageFilters::PointLitSpecular( |
| 365 |
const SkPoint3& location, SkColor lightColor, SkScalar surfaceScale, SkScalar ks, |
| 366 |
SkScalar shininess, sk_sp<SkImageFilter> input, const CropRect& cropRect) { |
| 367 |
return make_lighting(Light::Point(lightColor, location), |
| 368 |
Material::Specular(ks, shininess, surfaceScale), |
| 369 |
std::move(input), cropRect); |
| 370 |
} |
| 371 |
|
| 372 |
sk_sp<SkImageFilter> SkImageFilters::SpotLitSpecular( |
| 373 |
const SkPoint3& location, const SkPoint3& target, SkScalar falloffExponent, |
| 374 |
SkScalar cutoffAngle, SkColor lightColor, SkScalar surfaceScale, SkScalar ks, |
| 375 |
SkScalar shininess, sk_sp<SkImageFilter> input, const CropRect& cropRect) { |
| 376 |
SkPoint3 dir = target - location; |
| 377 |
float cosCutoffAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); |
| 378 |
return make_lighting(Light::Spot(lightColor, location, dir, falloffExponent, cosCutoffAngle), |
| 379 |
Material::Specular(ks, shininess, surfaceScale), |
| 380 |
std::move(input), cropRect); |
| 381 |
} |
| 382 |
|
| 383 |
void SkRegisterLightingImageFilterFlattenables() { |
| 384 |
SK_REGISTER_FLATTENABLE(SkLightingImageFilter); |
| 385 |
// TODO (michaelludwig): Remove after grace period for SKPs to stop using old name |
| 386 |
SkFlattenable::Register("SkDiffuseLightingImageFilter", |
| 387 |
SkLightingImageFilter::LegacyDiffuseCreateProc); |
| 388 |
SkFlattenable::Register("SkSpecularLightingImageFilter", |
| 389 |
SkLightingImageFilter::LegacySpecularCreateProc); |
| 390 |
} |
| 391 |
|
| 392 |
/////////////////////////////////////////////////////////////////////////////// |
| 393 |
|
| 394 |
sk_sp<SkFlattenable> SkLightingImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 395 |
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 396 |
|
| 397 |
Light light; |
| 398 |
light.fType = buffer.read32LE(Light::Type::kLast); |
| 399 |
light.fLightColor = buffer.readColor(); |
| 400 |
|
| 401 |
SkPoint3 lightPos, lightDir; |
| 402 |
buffer.readPoint3(&lightPos); |
| 403 |
light.fLocationXY = skif::ParameterSpace<SkPoint>({lightPos.fX, lightPos.fY}); |
| 404 |
light.fLocationZ = skif::ParameterSpace<ZValue>(lightPos.fZ); |
| 405 |
|
| 406 |
buffer.readPoint3(&lightDir); |
| 407 |
light.fDirectionXY = skif::ParameterSpace<skif::Vector>({lightDir.fX, lightDir.fY}); |
| 408 |
light.fDirectionZ = skif::ParameterSpace<ZValue>(lightDir.fZ); |
| 409 |
|
| 410 |
light.fFalloffExponent = buffer.readScalar(); |
| 411 |
light.fCosCutoffAngle = buffer.readScalar(); |
| 412 |
|
| 413 |
Material material; |
| 414 |
material.fType = buffer.read32LE(Material::Type::kLast); |
| 415 |
material.fSurfaceDepth = skif::ParameterSpace<ZValue>(buffer.readScalar()); |
| 416 |
material.fK = buffer.readScalar(); |
| 417 |
material.fShininess = buffer.readScalar(); |
| 418 |
|
| 419 |
if (!buffer.isValid()) { |
| 420 |
return nullptr; |
| 421 |
} |
| 422 |
|
| 423 |
return make_lighting(light, material, common.getInput(0), common.cropRect()); |
| 424 |
} |
| 425 |
|
| 426 |
Light SkLightingImageFilter::LegacyDeserializeLight(SkReadBuffer& buffer) { |
| 427 |
// Light::Type has the same order as the legacy SkImageFilterLight::LightType enum |
| 428 |
Light::Type lightType = buffer.read32LE(Light::Type::kLast); |
| 429 |
if (!buffer.isValid()) { |
| 430 |
return {}; |
| 431 |
} |
| 432 |
|
| 433 |
// Legacy lights stored just the RGB, but as floats (notably *not* normalized to [0-1]) |
| 434 |
SkColor lightColor = SkColorSetARGB(/*a (ignored)=*/255, |
| 435 |
/*r=*/ (U8CPU) buffer.readScalar(), |
| 436 |
/*g=*/ (U8CPU) buffer.readScalar(), |
| 437 |
/*b=*/ (U8CPU) buffer.readScalar()); |
| 438 |
// Legacy lights only serialized fields specific to that type |
| 439 |
switch (lightType) { |
| 440 |
case Light::Type::kDistant: { |
| 441 |
SkPoint3 dir = {buffer.readScalar(), buffer.readScalar(), buffer.readScalar()}; |
| 442 |
return Light::Distant(lightColor, dir); |
| 443 |
} |
| 444 |
case Light::Type::kPoint: { |
| 445 |
SkPoint3 loc = {buffer.readScalar(), buffer.readScalar(), buffer.readScalar()}; |
| 446 |
return Light::Point(lightColor, loc); |
| 447 |
} |
| 448 |
case Light::Type::kSpot: { |
| 449 |
SkPoint3 loc = {buffer.readScalar(), buffer.readScalar(), buffer.readScalar()}; |
| 450 |
SkPoint3 target = {buffer.readScalar(), buffer.readScalar(), buffer.readScalar()}; |
| 451 |
float falloffExponent = buffer.readScalar(); |
| 452 |
float cosOuterConeAngle = buffer.readScalar(); |
| 453 |
buffer.readScalar(); // skip cosInnerConeAngle, derived from outer cone angle |
| 454 |
buffer.readScalar(); // skip coneScale, which is a constant |
| 455 |
buffer.readScalar(); // skip S, which is normalize(target - loc) |
| 456 |
buffer.readScalar(); // "" |
| 457 |
buffer.readScalar(); // "" |
| 458 |
return Light::Spot(lightColor, loc, target - loc, falloffExponent, cosOuterConeAngle); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
SkUNREACHABLE; // Validation by read32LE() should avoid this |
| 463 |
} |
| 464 |
|
| 465 |
sk_sp<SkFlattenable> SkLightingImageFilter::LegacyDiffuseCreateProc(SkReadBuffer& buffer) { |
| 466 |
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 467 |
|
| 468 |
Light light = LegacyDeserializeLight(buffer); |
| 469 |
|
| 470 |
// Legacy implementations used (scale/255) when filtering, but serialized (fScale*255) so the |
| 471 |
// buffer held the original unmodified surface scale. |
| 472 |
float surfaceScale = buffer.readScalar(); |
| 473 |
float kd = buffer.readScalar(); |
| 474 |
Material material = Material::Diffuse(kd, surfaceScale); |
| 475 |
|
| 476 |
return make_lighting(light, material, common.getInput(0), common.cropRect()); |
| 477 |
} |
| 478 |
|
| 479 |
sk_sp<SkFlattenable> SkLightingImageFilter::LegacySpecularCreateProc(SkReadBuffer& buffer) { |
| 480 |
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 481 |
|
| 482 |
Light light = LegacyDeserializeLight(buffer); |
| 483 |
|
| 484 |
// Legacy implementations used (scale/255) when filtering, but serialized (fScale*255) so the |
| 485 |
// buffer held the original unmodified surface scale. |
| 486 |
float surfaceScale = buffer.readScalar(); |
| 487 |
float ks = buffer.readScalar(); |
| 488 |
float shininess = buffer.readScalar(); |
| 489 |
Material material = Material::Specular(ks, shininess, surfaceScale); |
| 490 |
|
| 491 |
return make_lighting(light, material, common.getInput(0), common.cropRect()); |
| 492 |
} |
| 493 |
|
| 494 |
void SkLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 495 |
this->SkImageFilter_Base::flatten(buffer); |
| 496 |
|
| 497 |
// Light |
| 498 |
buffer.writeInt((int) fLight.fType); |
| 499 |
buffer.writeColor(fLight.fLightColor); |
| 500 |
|
| 501 |
buffer.writePoint(SkPoint(fLight.fLocationXY)); |
| 502 |
buffer.writeScalar(ZValue(fLight.fLocationZ)); |
| 503 |
|
| 504 |
skif::Vector dirXY{fLight.fDirectionXY}; |
| 505 |
buffer.writePoint(SkPoint{dirXY.fX, dirXY.fY}); |
| 506 |
buffer.writeScalar(ZValue(fLight.fDirectionZ)); |
| 507 |
|
| 508 |
buffer.writeScalar(fLight.fFalloffExponent); |
| 509 |
buffer.writeScalar(fLight.fCosCutoffAngle); |
| 510 |
|
| 511 |
// Material |
| 512 |
buffer.writeInt((int) fMaterial.fType); |
| 513 |
buffer.writeScalar(ZValue(fMaterial.fSurfaceDepth)); |
| 514 |
buffer.writeScalar(fMaterial.fK); |
| 515 |
buffer.writeScalar(fMaterial.fShininess); |
| 516 |
} |
| 517 |
|
| 518 |
/////////////////////////////////////////////////////////////////////////////// |
| 519 |
|
| 520 |
skif::FilterResult SkLightingImageFilter::onFilterImage(const skif::Context& ctx) const { |
| 521 |
using ShaderFlags = skif::FilterResult::ShaderFlags; |
| 522 |
|
| 523 |
auto mapZToLayer = [&ctx](skif::ParameterSpace<ZValue> z) { |
| 524 |
return skif::LayerSpace<ZValue>::Map(ctx.mapping(), z); |
| 525 |
}; |
| 526 |
|
| 527 |
// Map lighting and material parameters into layer space |
| 528 |
skif::LayerSpace<ZValue> surfaceDepth = mapZToLayer(fMaterial.fSurfaceDepth); |
| 529 |
skif::LayerSpace<SkPoint> lightLocationXY = ctx.mapping().paramToLayer(fLight.fLocationXY); |
| 530 |
skif::LayerSpace<ZValue> lightLocationZ = mapZToLayer(fLight.fLocationZ); |
| 531 |
skif::LayerSpace<skif::Vector> lightDirXY = ctx.mapping().paramToLayer(fLight.fDirectionXY); |
| 532 |
skif::LayerSpace<ZValue> lightDirZ = mapZToLayer(fLight.fDirectionZ); |
| 533 |
|
| 534 |
// The normal map is determined by a 3x3 kernel, so we request a 1px outset of what should be |
| 535 |
// filled by the lighting equation. Ideally this means there are no boundary conditions visible. |
| 536 |
// If the required input is incomplete, the lighting filter handles the boundaries in two ways: |
| 537 |
// - When the actual child output's edge matches the desired output's edge, it uses clamped |
| 538 |
// tiling at the desired output. This approximates the modified Sobel kernel's specified in |
| 539 |
// https://drafts.fxtf.org/filter-effects/#feDiffuseLightingElement. NOTE: It's identical to |
| 540 |
// the interior kernel and near equal on the 4 edges (only weights are biased differently). |
| 541 |
// The four corners' convolution sums with clamped tiling are not equal, but should not be |
| 542 |
// objectionable since the normals produced are reasonable and still further processed by the |
| 543 |
// lighting equation. The increased complexity is not worth it for just 4 pixels of output. |
| 544 |
// - However, when the desired output is far larger than the produced image, we process the |
| 545 |
// child output with the default decal tiling that the Skia image filter pipeline relies on. |
| 546 |
// This creates a visual bevel at the image boundary but avoids producing streaked normals if |
| 547 |
// the clamped tiling was used in all scenarios. |
| 548 |
skif::LayerSpace<SkIRect> requiredInput = this->requiredInput(ctx.desiredOutput()); |
| 549 |
skif::FilterResult childOutput = |
| 550 |
this->getChildOutput(0, ctx.withNewDesiredOutput(requiredInput)); |
| 551 |
|
| 552 |
skif::LayerSpace<SkIRect> clampRect = requiredInput; // effectively no clamping of normals |
| 553 |
if (!childOutput.layerBounds().contains(requiredInput)) { |
| 554 |
// Adjust clampRect edges to desiredOutput if the actual child output matched the lighting |
| 555 |
// output size (typical SVG case). Otherwise leave coordinates alone to use decal tiling |
| 556 |
// automatically for the pixels outside the child image but inside the desired output. |
| 557 |
auto edgeClamp = [](int actualEdgeValue, int requestedEdgeValue, int outputEdge) { |
| 558 |
return actualEdgeValue == outputEdge ? outputEdge : requestedEdgeValue; |
| 559 |
}; |
| 560 |
auto inputRect = childOutput.layerBounds(); |
| 561 |
auto clampTo = ctx.desiredOutput(); |
| 562 |
clampRect = skif::LayerSpace<SkIRect>({ |
| 563 |
edgeClamp(inputRect.left(), requiredInput.left(), clampTo.left()), |
| 564 |
edgeClamp(inputRect.top(), requiredInput.top(), clampTo.top()), |
| 565 |
edgeClamp(inputRect.right(), requiredInput.right(), clampTo.right()), |
| 566 |
edgeClamp(inputRect.bottom(), requiredInput.bottom(), clampTo.bottom())}); |
| 567 |
} |
| 568 |
|
| 569 |
skif::FilterResult::Builder builder{ctx}; |
| 570 |
builder.add(childOutput, /*sampleBounds=*/clampRect, ShaderFlags::kSampledRepeatedly); |
| 571 |
return builder.eval([&](SkSpan<sk_sp<SkShader>> input) { |
| 572 |
// TODO: Once shaders are deferred in FilterResult, it will likely make sense to have an |
| 573 |
// internal normal map filter that uses this shader, and then have the lighting effects as |
| 574 |
// a separate filter. It's common for multiple lights to use the same input (producing the |
| 575 |
// same normal map) before being merged together. With a separate normal image filter, its |
| 576 |
// output would be automatically cached, and the lighting equation shader would be deferred |
| 577 |
// to the merge's draw operation, making for a maximum of 2 renderpasses instead of N+1. |
| 578 |
sk_sp<SkShader> normals = make_normal_shader(std::move(input[0]), clampRect, surfaceDepth); |
| 579 |
return make_lighting_shader(std::move(normals), |
| 580 |
// Light in layer space |
| 581 |
fLight.fType, |
| 582 |
fLight.fLightColor, |
| 583 |
lightLocationXY, |
| 584 |
lightLocationZ, |
| 585 |
lightDirXY, |
| 586 |
lightDirZ, |
| 587 |
fLight.fFalloffExponent, |
| 588 |
fLight.fCosCutoffAngle, |
| 589 |
// Material in layer space |
| 590 |
fMaterial.fType, |
| 591 |
surfaceDepth, |
| 592 |
fMaterial.fK, |
| 593 |
fMaterial.fShininess); |
| 594 |
}); |
| 595 |
} |
| 596 |
|
| 597 |
skif::LayerSpace<SkIRect> SkLightingImageFilter::onGetInputLayerBounds( |
| 598 |
const skif::Mapping& mapping, |
| 599 |
const skif::LayerSpace<SkIRect>& desiredOutput, |
| 600 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const { |
| 601 |
skif::LayerSpace<SkIRect> requiredInput = this->requiredInput(desiredOutput); |
| 602 |
return this->getChildInputLayerBounds(0, mapping, requiredInput, contentBounds); |
| 603 |
} |
| 604 |
|
| 605 |
std::optional<skif::LayerSpace<SkIRect>> SkLightingImageFilter::onGetOutputLayerBounds( |
| 606 |
const skif::Mapping& mapping, |
| 607 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const { |
| 608 |
// The lighting equation is defined on the entire plane, even if the input image that defines |
| 609 |
// the normal map is bounded. It just is evaluated at a constant normal vector, which can still |
| 610 |
// produce non-constant color since the direction to the eye and light change per pixel. |
| 611 |
return skif::LayerSpace<SkIRect>::Unbounded(); |
| 612 |
} |
| 613 |
|
| 614 |
SkRect SkLightingImageFilter::computeFastBounds(const SkRect& src) const { |
| 615 |
return SkRectPriv::MakeLargeS32(); |
| 616 |
} |