PaintUtils.h revision bf6f0f260886a04a1680c7f9917124a751322ca4
1/*
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
19#include <utils/Blur.h>
20
21#include <SkColorFilter.h>
22#include <SkXfermode.h>
23
24namespace android {
25namespace uirenderer {
26
27/**
28 * Utility methods for accessing data within SkPaint, and providing defaults
29 * with optional SkPaint pointers.
30 */
31class 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
46    static inline GLenum getFilter(const SkPaint* paint) {
47        if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) {
48            return GL_LINEAR;
49        }
50        return GL_NEAREST;
51    }
52
53    // 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
63                && paint.getLooper() == nullptr
64                && !paint.getColorFilter()
65                && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
66    }
67
68    static bool isBlendedShader(const SkShader* shader) {
69        if (shader == nullptr) {
70            return false;
71        }
72        return !shader->isOpaque();
73    }
74
75    static bool isBlendedColorFilter(const SkColorFilter* filter) {
76        if (filter == nullptr) {
77            return false;
78        }
79        return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
80    }
81
82    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
115}; // class PaintUtils
116
117} /* namespace uirenderer */
118} /* namespace android */
119
120#endif /* PAINT_UTILS_H */
121