blob: 2673be1cb6fa61f02066110c9c8b9e1ec0dab152 [file] [log] [blame]
Tom Hudson8dfaa492014-12-09 15:03:44 -05001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef PAINT_UTILS_H
17#define PAINT_UTILS_H
18
Chris Craikbf6f0f22015-10-01 12:36:07 -070019#include <utils/Blur.h>
20
Chris Craik03188872015-02-02 18:39:33 -080021#include <SkColorFilter.h>
Tom Hudsonca4f0b52015-10-05 08:51:49 -040022#include <SkDrawLooper.h>
Chris Craikb565df12015-10-05 13:00:52 -070023#include <SkShader.h>
Chris Craik03188872015-02-02 18:39:33 -080024
Tom Hudson8dfaa492014-12-09 15:03:44 -050025namespace android {
26namespace uirenderer {
27
Chris Craikbf6f0f22015-10-01 12:36:07 -070028/**
29 * Utility methods for accessing data within SkPaint, and providing defaults
30 * with optional SkPaint pointers.
31 */
Tom Hudson8dfaa492014-12-09 15:03:44 -050032class PaintUtils {
33public:
34
Chris Craik0519c8102015-02-11 13:17:06 -080035 static inline GLenum getFilter(const SkPaint* paint) {
Mike Reed2a1ce8a2015-03-16 11:16:09 -040036 if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) {
Chris Craik0519c8102015-02-11 13:17:06 -080037 return GL_LINEAR;
38 }
39 return GL_NEAREST;
40 }
41
Chris Craik80d2ade2016-03-28 12:54:07 -070042 static bool isOpaquePaint(const SkPaint* paint) {
43 if (!paint) return true; // default (paintless) behavior is SrcOver, black
44
45 if (paint->getAlpha() != 0xFF
46 || PaintUtils::isBlendedShader(paint->getShader())
47 || PaintUtils::isBlendedColorFilter(paint->getColorFilter())) {
48 return false;
49 }
50
51 // Only let simple srcOver / src blending modes declare opaque, since behavior is clear.
Mike Reed260ab722016-10-07 15:59:20 -040052 SkBlendMode mode = paint->getBlendMode();
53 return mode == SkBlendMode::kSrcOver
54 || mode == SkBlendMode::kSrc;
Chris Craik80d2ade2016-03-28 12:54:07 -070055 }
56
Chris Craik117bdbc2015-02-05 10:12:38 -080057 static bool isBlendedShader(const SkShader* shader) {
58 if (shader == nullptr) {
59 return false;
60 }
61 return !shader->isOpaque();
62 }
63
Tom Hudson8dfaa492014-12-09 15:03:44 -050064 static bool isBlendedColorFilter(const SkColorFilter* filter) {
Chris Craikd41c4d82015-01-05 15:51:13 -080065 if (filter == nullptr) {
Tom Hudson8dfaa492014-12-09 15:03:44 -050066 return false;
67 }
68 return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
69 }
70
Chris Craikbf6f0f22015-10-01 12:36:07 -070071 struct TextShadow {
72 SkScalar radius;
73 float dx;
74 float dy;
75 SkColor color;
76 };
77
78 static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) {
79 SkDrawLooper::BlurShadowRec blur;
80 if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) {
81 if (textShadow) {
82 textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma);
83 textShadow->dx = blur.fOffset.fX;
84 textShadow->dy = blur.fOffset.fY;
85 textShadow->color = blur.fColor;
86 }
87 return true;
88 }
89 return false;
90 }
91
92 static inline bool hasTextShadow(const SkPaint* paint) {
93 return getTextShadow(paint, nullptr);
94 }
95
Mike Reed260ab722016-10-07 15:59:20 -040096 static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) {
97 return paint ? paint->getBlendMode() : SkBlendMode::kSrcOver;
Chris Craikbf6f0f22015-10-01 12:36:07 -070098 }
99
100 static inline int getAlphaDirect(const SkPaint* paint) {
101 return paint ? paint->getAlpha() : 255;
102 }
103
Tom Hudson8dfaa492014-12-09 15:03:44 -0500104}; // class PaintUtils
105
106} /* namespace uirenderer */
107} /* namespace android */
108
109#endif /* PAINT_UTILS_H */