blob: 019e5d3b0e7b595c97ab11bf54f182151cd8b359 [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>
22#include <SkXfermode.h>
23
Tom Hudson8dfaa492014-12-09 15:03:44 -050024namespace android {
25namespace uirenderer {
26
Chris Craikbf6f0f22015-10-01 12:36:07 -070027/**
28 * Utility methods for accessing data within SkPaint, and providing defaults
29 * with optional SkPaint pointers.
30 */
Tom Hudson8dfaa492014-12-09 15:03:44 -050031class PaintUtils {
32public:
33
34 /**
35 * Safely retrieves the mode from the specified xfermode. If the specified
36 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
37 */
38 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
39 SkXfermode::Mode resultMode;
40 if (!SkXfermode::AsMode(mode, &resultMode)) {
41 resultMode = SkXfermode::kSrcOver_Mode;
42 }
43 return resultMode;
44 }
45
Chris Craik0519c8102015-02-11 13:17:06 -080046 static inline GLenum getFilter(const SkPaint* paint) {
Mike Reed2a1ce8a2015-03-16 11:16:09 -040047 if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) {
Chris Craik0519c8102015-02-11 13:17:06 -080048 return GL_LINEAR;
49 }
50 return GL_NEAREST;
51 }
52
Tom Hudson8dfaa492014-12-09 15:03:44 -050053 // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()?
54 static inline bool paintWillNotDraw(const SkPaint& paint) {
55 return paint.getAlpha() == 0
56 && !paint.getColorFilter()
57 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
58 }
59
60 // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()?
61 static inline bool paintWillNotDrawText(const SkPaint& paint) {
62 return paint.getAlpha() == 0
Chris Craikd41c4d82015-01-05 15:51:13 -080063 && paint.getLooper() == nullptr
Tom Hudson8dfaa492014-12-09 15:03:44 -050064 && !paint.getColorFilter()
65 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
66 }
67
Chris Craik117bdbc2015-02-05 10:12:38 -080068 static bool isBlendedShader(const SkShader* shader) {
69 if (shader == nullptr) {
70 return false;
71 }
72 return !shader->isOpaque();
73 }
74
Tom Hudson8dfaa492014-12-09 15:03:44 -050075 static bool isBlendedColorFilter(const SkColorFilter* filter) {
Chris Craikd41c4d82015-01-05 15:51:13 -080076 if (filter == nullptr) {
Tom Hudson8dfaa492014-12-09 15:03:44 -050077 return false;
78 }
79 return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
80 }
81
Chris Craikbf6f0f22015-10-01 12:36:07 -070082 struct TextShadow {
83 SkScalar radius;
84 float dx;
85 float dy;
86 SkColor color;
87 };
88
89 static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) {
90 SkDrawLooper::BlurShadowRec blur;
91 if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) {
92 if (textShadow) {
93 textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma);
94 textShadow->dx = blur.fOffset.fX;
95 textShadow->dy = blur.fOffset.fY;
96 textShadow->color = blur.fColor;
97 }
98 return true;
99 }
100 return false;
101 }
102
103 static inline bool hasTextShadow(const SkPaint* paint) {
104 return getTextShadow(paint, nullptr);
105 }
106
107 static inline SkXfermode::Mode getXfermodeDirect(const SkPaint* paint) {
108 return paint ? getXfermode(paint->getXfermode()) : SkXfermode::kSrcOver_Mode;
109 }
110
111 static inline int getAlphaDirect(const SkPaint* paint) {
112 return paint ? paint->getAlpha() : 255;
113 }
114
Tom Hudson8dfaa492014-12-09 15:03:44 -0500115}; // class PaintUtils
116
117} /* namespace uirenderer */
118} /* namespace android */
119
120#endif /* PAINT_UTILS_H */