xref: /aosp_15_r20/external/skia/modules/svg/src/SkSVGFeLighting.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 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 "modules/svg/include/SkSVGFeLighting.h"
9 
10 #include "include/core/SkImageFilter.h"
11 #include "include/core/SkM44.h"
12 #include "include/core/SkPoint3.h"
13 #include "include/effects/SkImageFilters.h"
14 #include "include/private/base/SkDebug.h"
15 #include "include/private/base/SkTArray.h"
16 #include "modules/svg/include/SkSVGAttributeParser.h"
17 #include "modules/svg/include/SkSVGFeLightSource.h"
18 #include "modules/svg/include/SkSVGFilterContext.h"
19 #include "modules/svg/include/SkSVGRenderContext.h"
20 
parseAndSetAttribute(const char * n,const char * v)21 bool SkSVGFeLighting::parseAndSetAttribute(const char* n, const char* v) {
22     return INHERITED::parseAndSetAttribute(n, v) ||
23            this->setSurfaceScale(
24                    SkSVGAttributeParser::parse<SkSVGNumberType>("surfaceScale", n, v)) ||
25            this->setKernelUnitLength(SkSVGAttributeParser::parse<SkSVGFeLighting::KernelUnitLength>(
26                    "kernelUnitLength", n, v));
27 }
28 
29 template <>
parse(SkSVGFeLighting::KernelUnitLength * kernelUnitLength)30 bool SkSVGAttributeParser::parse<SkSVGFeLighting::KernelUnitLength>(
31         SkSVGFeLighting::KernelUnitLength* kernelUnitLength) {
32     std::vector<SkSVGNumberType> values;
33     if (!this->parse(&values)) {
34         return false;
35     }
36 
37     kernelUnitLength->fDx = values[0];
38     kernelUnitLength->fDy = values.size() > 1 ? values[1] : values[0];
39     return true;
40 }
41 
onMakeImageFilter(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const42 sk_sp<SkImageFilter> SkSVGFeLighting::onMakeImageFilter(const SkSVGRenderContext& ctx,
43                                                         const SkSVGFilterContext& fctx) const {
44     for (const auto& child : fChildren) {
45         switch (child->tag()) {
46             case SkSVGTag::kFeDistantLight:
47                 return this->makeDistantLight(
48                         ctx, fctx, static_cast<const SkSVGFeDistantLight*>(child.get()));
49             case SkSVGTag::kFePointLight:
50                 return this->makePointLight(
51                         ctx, fctx, static_cast<const SkSVGFePointLight*>(child.get()));
52             case SkSVGTag::kFeSpotLight:
53                 return this->makeSpotLight(
54                         ctx, fctx, static_cast<const SkSVGFeSpotLight*>(child.get()));
55             default:
56                 // Ignore unknown children, such as <desc> elements
57                 break;
58         }
59     }
60 
61     SkDEBUGF("lighting filter effect needs exactly one light source\n");
62     return nullptr;
63 }
64 
resolveLightingColor(const SkSVGRenderContext & ctx) const65 SkColor SkSVGFeLighting::resolveLightingColor(const SkSVGRenderContext& ctx) const {
66     const auto color = this->getLightingColor();
67     if (!color.isValue()) {
68         // Uninherited presentation attributes should have a concrete value by now.
69         SkDEBUGF("unhandled: lighting-color has no value\n");
70         return SK_ColorWHITE;
71     }
72 
73     return ctx.resolveSvgColor(*color);
74 }
75 
resolveXYZ(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx,SkSVGNumberType x,SkSVGNumberType y,SkSVGNumberType z) const76 SkPoint3 SkSVGFeLighting::resolveXYZ(const SkSVGRenderContext& ctx,
77                                      const SkSVGFilterContext& fctx,
78                                      SkSVGNumberType x,
79                                      SkSVGNumberType y,
80                                      SkSVGNumberType z) const {
81     const auto obbt = ctx.transformForCurrentOBB(fctx.primitiveUnits());
82     const auto xy = SkV2{x,y} * obbt.scale + obbt.offset;
83     z = SkSVGLengthContext({obbt.scale.x, obbt.scale.y})
84             .resolve(SkSVGLength(z * 100.f, SkSVGLength::Unit::kPercentage),
85                      SkSVGLengthContext::LengthType::kOther);
86     return SkPoint3::Make(xy.x, xy.y, z);
87 }
88 
parseAndSetAttribute(const char * n,const char * v)89 bool SkSVGFeSpecularLighting::parseAndSetAttribute(const char* n, const char* v) {
90     return INHERITED::parseAndSetAttribute(n, v) ||
91            this->setSpecularConstant(
92                    SkSVGAttributeParser::parse<SkSVGNumberType>("specularConstant", n, v)) ||
93            this->setSpecularExponent(
94                    SkSVGAttributeParser::parse<SkSVGNumberType>("specularExponent", n, v));
95 }
96 
makeDistantLight(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx,const SkSVGFeDistantLight * light) const97 sk_sp<SkImageFilter> SkSVGFeSpecularLighting::makeDistantLight(
98         const SkSVGRenderContext& ctx,
99         const SkSVGFilterContext& fctx,
100         const SkSVGFeDistantLight* light) const {
101     const SkPoint3 dir = light->computeDirection();
102     return SkImageFilters::DistantLitSpecular(
103             this->resolveXYZ(ctx, fctx, dir.fX, dir.fY, dir.fZ),
104             this->resolveLightingColor(ctx),
105             this->getSurfaceScale(),
106             fSpecularConstant,
107             fSpecularExponent,
108             fctx.resolveInput(ctx, this->getIn(), this->resolveColorspace(ctx, fctx)),
109             this->resolveFilterSubregion(ctx, fctx));
110 }
111 
makePointLight(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx,const SkSVGFePointLight * light) const112 sk_sp<SkImageFilter> SkSVGFeSpecularLighting::makePointLight(const SkSVGRenderContext& ctx,
113                                                              const SkSVGFilterContext& fctx,
114                                                              const SkSVGFePointLight* light) const {
115     return SkImageFilters::PointLitSpecular(
116             this->resolveXYZ(ctx, fctx, light->getX(), light->getY(), light->getZ()),
117             this->resolveLightingColor(ctx),
118             this->getSurfaceScale(),
119             fSpecularConstant,
120             fSpecularExponent,
121             fctx.resolveInput(ctx, this->getIn(), this->resolveColorspace(ctx, fctx)),
122             this->resolveFilterSubregion(ctx, fctx));
123 }
124 
makeSpotLight(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx,const SkSVGFeSpotLight * light) const125 sk_sp<SkImageFilter> SkSVGFeSpecularLighting::makeSpotLight(const SkSVGRenderContext& ctx,
126                                                             const SkSVGFilterContext& fctx,
127                                                             const SkSVGFeSpotLight* light) const {
128     const auto& limitingConeAngle = light->getLimitingConeAngle();
129     const float cutoffAngle = limitingConeAngle.isValid() ? *limitingConeAngle : 180.f;
130 
131     return SkImageFilters::SpotLitSpecular(
132             this->resolveXYZ(ctx, fctx, light->getX(), light->getY(), light->getZ()),
133             this->resolveXYZ(
134                     ctx, fctx, light->getPointsAtX(), light->getPointsAtY(), light->getPointsAtZ()),
135             light->getSpecularExponent(),
136             cutoffAngle,
137             this->resolveLightingColor(ctx),
138             this->getSurfaceScale(),
139             fSpecularConstant,
140             fSpecularExponent,
141             fctx.resolveInput(ctx, this->getIn(), this->resolveColorspace(ctx, fctx)),
142             this->resolveFilterSubregion(ctx, fctx));
143 }
144 
parseAndSetAttribute(const char * n,const char * v)145 bool SkSVGFeDiffuseLighting::parseAndSetAttribute(const char* n, const char* v) {
146     return INHERITED::parseAndSetAttribute(n, v) ||
147            this->setDiffuseConstant(
148                    SkSVGAttributeParser::parse<SkSVGNumberType>("diffuseConstant", n, v));
149 }
150 
makeDistantLight(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx,const SkSVGFeDistantLight * light) const151 sk_sp<SkImageFilter> SkSVGFeDiffuseLighting::makeDistantLight(
152         const SkSVGRenderContext& ctx,
153         const SkSVGFilterContext& fctx,
154         const SkSVGFeDistantLight* light) const {
155     const SkPoint3 dir = light->computeDirection();
156     return SkImageFilters::DistantLitDiffuse(
157             this->resolveXYZ(ctx, fctx, dir.fX, dir.fY, dir.fZ),
158             this->resolveLightingColor(ctx),
159             this->getSurfaceScale(),
160             this->getDiffuseConstant(),
161             fctx.resolveInput(ctx, this->getIn(), this->resolveColorspace(ctx, fctx)),
162             this->resolveFilterSubregion(ctx, fctx));
163 }
164 
makePointLight(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx,const SkSVGFePointLight * light) const165 sk_sp<SkImageFilter> SkSVGFeDiffuseLighting::makePointLight(const SkSVGRenderContext& ctx,
166                                                             const SkSVGFilterContext& fctx,
167                                                             const SkSVGFePointLight* light) const {
168     return SkImageFilters::PointLitDiffuse(
169             this->resolveXYZ(ctx, fctx, light->getX(), light->getY(), light->getZ()),
170             this->resolveLightingColor(ctx),
171             this->getSurfaceScale(),
172             this->getDiffuseConstant(),
173             fctx.resolveInput(ctx, this->getIn(), this->resolveColorspace(ctx, fctx)),
174             this->resolveFilterSubregion(ctx, fctx));
175 }
176 
makeSpotLight(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx,const SkSVGFeSpotLight * light) const177 sk_sp<SkImageFilter> SkSVGFeDiffuseLighting::makeSpotLight(const SkSVGRenderContext& ctx,
178                                                            const SkSVGFilterContext& fctx,
179                                                            const SkSVGFeSpotLight* light) const {
180     const auto& limitingConeAngle = light->getLimitingConeAngle();
181     const float cutoffAngle = limitingConeAngle.isValid() ? *limitingConeAngle : 180.f;
182 
183     return SkImageFilters::SpotLitDiffuse(
184             this->resolveXYZ(ctx, fctx, light->getX(), light->getY(), light->getZ()),
185             this->resolveXYZ(
186                     ctx, fctx, light->getPointsAtX(), light->getPointsAtY(), light->getPointsAtZ()),
187             light->getSpecularExponent(),
188             cutoffAngle,
189             this->resolveLightingColor(ctx),
190             this->getSurfaceScale(),
191             this->getDiffuseConstant(),
192             fctx.resolveInput(ctx, this->getIn(), this->resolveColorspace(ctx, fctx)),
193             this->resolveFilterSubregion(ctx, fctx));
194 }
195