xref: /aosp_15_r20/external/skia/modules/sksg/src/SkSGPaint.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2017 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/core/SkShader.h"
9 #include "include/private/base/SkAssert.h"
10 #include "include/private/base/SkTPin.h"
11 #include "modules/sksg/include/SkSGPaint.h"
12 #include "modules/sksg/include/SkSGRenderEffect.h"
13 
14 #include <utility>
15 
16 class SkMatrix;
17 
18 namespace sksg {
19 
20 // Paint nodes don't generate damage on their own, but via their aggregation ancestor Draw nodes.
PaintNode()21 PaintNode::PaintNode() : INHERITED(kBubbleDamage_Trait) {}
22 
makePaint() const23 SkPaint PaintNode::makePaint() const {
24     SkASSERT(!this->hasInval());
25 
26     SkPaint paint;
27 
28     paint.setAntiAlias(fAntiAlias);
29     paint.setBlendMode(fBlendMode);
30     paint.setStyle(fStyle);
31     paint.setStrokeWidth(fStrokeWidth);
32     paint.setStrokeMiter(fStrokeMiter);
33     paint.setStrokeJoin(fStrokeJoin);
34     paint.setStrokeCap(fStrokeCap);
35 
36     this->onApplyToPaint(&paint);
37 
38     // Compose opacity on top of the subclass value.
39     paint.setAlpha(SkScalarRoundToInt(paint.getAlpha() * SkTPin<SkScalar>(fOpacity, 0, 1)));
40 
41     return paint;
42 }
43 
Make(SkColor c)44 sk_sp<Color> Color::Make(SkColor c) {
45     return sk_sp<Color>(new Color(c));
46 }
47 
Color(SkColor c)48 Color::Color(SkColor c) : fColor(c) {}
49 
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)50 SkRect Color::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
51     SkASSERT(this->hasInval());
52 
53     return SkRect::MakeEmpty();
54 }
55 
onApplyToPaint(SkPaint * paint) const56 void Color::onApplyToPaint(SkPaint* paint) const {
57     paint->setColor(fColor);
58 }
59 
Make(sk_sp<Shader> sh)60 sk_sp<ShaderPaint> ShaderPaint::Make(sk_sp<Shader> sh) {
61     return sh ? sk_sp<ShaderPaint>(new ShaderPaint(std::move(sh)))
62               : nullptr;
63 }
64 
ShaderPaint(sk_sp<Shader> sh)65 ShaderPaint::ShaderPaint(sk_sp<Shader> sh)
66     : fShader(std::move(sh)) {
67     this->observeInval(fShader);
68 }
69 
~ShaderPaint()70 ShaderPaint::~ShaderPaint() {
71     this->unobserveInval(fShader);
72 }
73 
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)74 SkRect ShaderPaint::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
75     SkASSERT(this->hasInval());
76 
77     return fShader->revalidate(ic, ctm);
78 }
79 
onApplyToPaint(SkPaint * paint) const80 void ShaderPaint::onApplyToPaint(SkPaint* paint) const {
81     paint->setShader(fShader->getShader());
82 }
83 
84 } // namespace sksg
85