• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/core/SkPaint.h"
9 #include "src/core/SkColorFilterBase.h"
10 #include "src/core/SkColorSpacePriv.h"
11 #include "src/core/SkPaintPriv.h"
12 #include "src/core/SkXfermodePriv.h"
13 #include "src/shaders/SkColorFilterShader.h"
14 #include "src/shaders/SkShaderBase.h"
15 
changes_alpha(const SkPaint & paint)16 static bool changes_alpha(const SkPaint& paint) {
17     SkColorFilter* cf = paint.getColorFilter();
18     return cf && !as_CFB(cf)->isAlphaUnchanged();
19 }
20 
Overwrites(const SkPaint * paint,ShaderOverrideOpacity overrideOpacity)21 bool SkPaintPriv::Overwrites(const SkPaint* paint, ShaderOverrideOpacity overrideOpacity) {
22     if (!paint) {
23         // No paint means we default to SRC_OVER, so we overwrite iff our shader-override
24         // is opaque, or we don't have one.
25         return overrideOpacity != kNotOpaque_ShaderOverrideOpacity;
26     }
27 
28     SkXfermode::SrcColorOpacity opacityType = SkXfermode::kUnknown_SrcColorOpacity;
29 
30     if (!changes_alpha(*paint)) {
31         const unsigned paintAlpha = paint->getAlpha();
32         if (0xff == paintAlpha && overrideOpacity != kNotOpaque_ShaderOverrideOpacity &&
33             (!paint->getShader() || paint->getShader()->isOpaque()))
34         {
35             opacityType = SkXfermode::kOpaque_SrcColorOpacity;
36         } else if (0 == paintAlpha) {
37             if (overrideOpacity == kNone_ShaderOverrideOpacity && !paint->getShader()) {
38                 opacityType = SkXfermode::kTransparentBlack_SrcColorOpacity;
39             } else {
40                 opacityType = SkXfermode::kTransparentAlpha_SrcColorOpacity;
41             }
42         }
43     }
44 
45     const auto bm = paint->asBlendMode();
46     if (!bm) {
47         return false;   // don't know for sure, so we play it safe and return false.
48     }
49     return SkXfermode::IsOpaque(bm.value(), opacityType);
50 }
51 
ShouldDither(const SkPaint & p,SkColorType dstCT)52 bool SkPaintPriv::ShouldDither(const SkPaint& p, SkColorType dstCT) {
53     // The paint dither flag can veto.
54     if (!p.isDither()) {
55         return false;
56     }
57 
58     // We always dither 565 or 4444 when requested.
59     if (dstCT == kRGB_565_SkColorType || dstCT == kARGB_4444_SkColorType) {
60         return true;
61     }
62 
63     // Otherwise, dither is only needed for non-const paints.
64     return p.getImageFilter() || p.getMaskFilter() ||
65            (p.getShader() && !as_SB(p.getShader())->isConstant());
66 }
67 
68 // return true if the paint is just a single color (i.e. not a shader). If its
69 // a shader, then we can't compute a const luminance for it :(
just_a_color(const SkPaint & paint,SkColor * color)70 static bool just_a_color(const SkPaint& paint, SkColor* color) {
71     SkColor c = paint.getColor();
72 
73     const auto* shader = as_SB(paint.getShader());
74     if (shader && !shader->asLuminanceColor(&c)) {
75         return false;
76     }
77     if (paint.getColorFilter()) {
78         c = paint.getColorFilter()->filterColor(c);
79     }
80     if (color) {
81         *color = c;
82     }
83     return true;
84 }
85 
ComputeLuminanceColor(const SkPaint & paint)86 SkColor SkPaintPriv::ComputeLuminanceColor(const SkPaint& paint) {
87     SkColor c;
88     if (!just_a_color(paint, &c)) {
89         c = SkColorSetRGB(0x7F, 0x80, 0x7F);
90     }
91     return c;
92 }
93 
RemoveColorFilter(SkPaint * p,SkColorSpace * dstCS)94 void SkPaintPriv::RemoveColorFilter(SkPaint* p, SkColorSpace* dstCS) {
95     if (SkColorFilter* filter = p->getColorFilter()) {
96         if (SkShader* shader = p->getShader()) {
97             // SkColorFilterShader will modulate the shader color by paint alpha
98             // before applying the filter, so we'll reset it to opaque.
99             p->setShader(sk_make_sp<SkColorFilterShader>(sk_ref_sp(shader),
100                                                          p->getAlphaf(),
101                                                          sk_ref_sp(filter)));
102             p->setAlphaf(1.0f);
103         } else {
104             p->setColor(filter->filterColor4f(p->getColor4f(), sk_srgb_singleton(), dstCS), dstCS);
105         }
106         p->setColorFilter(nullptr);
107     }
108 }
109 
ComputeResScaleForStroking(const SkMatrix & matrix)110 SkScalar SkPaintPriv::ComputeResScaleForStroking(const SkMatrix& matrix) {
111     // Not sure how to handle perspective differently, so we just don't try (yet)
112     SkScalar sx = SkPoint::Length(matrix[SkMatrix::kMScaleX], matrix[SkMatrix::kMSkewY]);
113     SkScalar sy = SkPoint::Length(matrix[SkMatrix::kMSkewX],  matrix[SkMatrix::kMScaleY]);
114     if (SkScalarsAreFinite(sx, sy)) {
115         SkScalar scale = std::max(sx, sy);
116         if (scale > 0) {
117             return scale;
118         }
119     }
120     return 1;
121 }
122