blob: ebf2343c5518e389c54a8616dae65e202accaaa4 [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
Stan Iliev564ca3e2018-09-04 22:00:00 +000019#include <GLES2/gl2.h>
Chris Craikbf6f0f22015-10-01 12:36:07 -070020#include <utils/Blur.h>
21
Chris Craik03188872015-02-02 18:39:33 -080022#include <SkColorFilter.h>
Tom Hudsonca4f0b52015-10-05 08:51:49 -040023#include <SkDrawLooper.h>
Mike Reed8cafcc62018-05-03 11:32:46 -040024#include <SkPaint.h>
Chris Craikb565df12015-10-05 13:00:52 -070025#include <SkShader.h>
Chris Craik03188872015-02-02 18:39:33 -080026
Tom Hudson8dfaa492014-12-09 15:03:44 -050027namespace android {
28namespace uirenderer {
29
Chris Craikbf6f0f22015-10-01 12:36:07 -070030/**
31 * Utility methods for accessing data within SkPaint, and providing defaults
32 * with optional SkPaint pointers.
33 */
Tom Hudson8dfaa492014-12-09 15:03:44 -050034class PaintUtils {
35public:
Chris Craik0519c8102015-02-11 13:17:06 -080036 static inline GLenum getFilter(const SkPaint* paint) {
Mike Reed2a1ce8a2015-03-16 11:16:09 -040037 if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) {
Chris Craik0519c8102015-02-11 13:17:06 -080038 return GL_LINEAR;
39 }
40 return GL_NEAREST;
41 }
42
Chris Craik80d2ade2016-03-28 12:54:07 -070043 static bool isOpaquePaint(const SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -070044 if (!paint) return true; // default (paintless) behavior is SrcOver, black
Chris Craik80d2ade2016-03-28 12:54:07 -070045
John Reck1bcacfd2017-11-03 10:12:19 -070046 if (paint->getAlpha() != 0xFF || PaintUtils::isBlendedShader(paint->getShader()) ||
47 PaintUtils::isBlendedColorFilter(paint->getColorFilter())) {
Chris Craik80d2ade2016-03-28 12:54:07 -070048 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();
John Reck1bcacfd2017-11-03 10:12:19 -070053 return mode == SkBlendMode::kSrcOver || mode == SkBlendMode::kSrc;
Chris Craik80d2ade2016-03-28 12:54:07 -070054 }
55
Chris Craik117bdbc2015-02-05 10:12:38 -080056 static bool isBlendedShader(const SkShader* shader) {
57 if (shader == nullptr) {
58 return false;
59 }
60 return !shader->isOpaque();
61 }
62
Tom Hudson8dfaa492014-12-09 15:03:44 -050063 static bool isBlendedColorFilter(const SkColorFilter* filter) {
Chris Craikd41c4d82015-01-05 15:51:13 -080064 if (filter == nullptr) {
Tom Hudson8dfaa492014-12-09 15:03:44 -050065 return false;
66 }
67 return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
68 }
69
Chris Craikbf6f0f22015-10-01 12:36:07 -070070 struct TextShadow {
71 SkScalar radius;
72 float dx;
73 float dy;
74 SkColor color;
75 };
76
77 static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) {
78 SkDrawLooper::BlurShadowRec blur;
79 if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) {
80 if (textShadow) {
81 textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma);
82 textShadow->dx = blur.fOffset.fX;
83 textShadow->dy = blur.fOffset.fY;
84 textShadow->color = blur.fColor;
85 }
86 return true;
87 }
88 return false;
89 }
90
John Reck1bcacfd2017-11-03 10:12:19 -070091 static inline bool hasTextShadow(const SkPaint* paint) { return getTextShadow(paint, nullptr); }
Chris Craikbf6f0f22015-10-01 12:36:07 -070092
Mike Reed260ab722016-10-07 15:59:20 -040093 static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) {
94 return paint ? paint->getBlendMode() : SkBlendMode::kSrcOver;
Chris Craikbf6f0f22015-10-01 12:36:07 -070095 }
96
97 static inline int getAlphaDirect(const SkPaint* paint) {
98 return paint ? paint->getAlpha() : 255;
99 }
100
John Reck1bcacfd2017-11-03 10:12:19 -0700101}; // class PaintUtils
Tom Hudson8dfaa492014-12-09 15:03:44 -0500102
103} /* namespace uirenderer */
104} /* namespace android */
105
106#endif /* PAINT_UTILS_H */