PaintUtils.h revision 117bdbcfa3e8306dad21e7e01fa71b00cdfa7265
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 <SkColorFilter.h>
20#include <SkXfermode.h>
21
22namespace android {
23namespace uirenderer {
24
25class PaintUtils {
26public:
27
28   /**
29     * Safely retrieves the mode from the specified xfermode. If the specified
30     * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
31     */
32    static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
33        SkXfermode::Mode resultMode;
34        if (!SkXfermode::AsMode(mode, &resultMode)) {
35            resultMode = SkXfermode::kSrcOver_Mode;
36        }
37        return resultMode;
38    }
39
40    // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()?
41    static inline bool paintWillNotDraw(const SkPaint& paint) {
42        return paint.getAlpha() == 0
43                && !paint.getColorFilter()
44                && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
45    }
46
47    // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()?
48    static inline bool paintWillNotDrawText(const SkPaint& paint) {
49        return paint.getAlpha() == 0
50                && paint.getLooper() == nullptr
51                && !paint.getColorFilter()
52                && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
53    }
54
55    static bool isBlendedShader(const SkShader* shader) {
56        if (shader == nullptr) {
57            return false;
58        }
59        return !shader->isOpaque();
60    }
61
62    static bool isBlendedColorFilter(const SkColorFilter* filter) {
63        if (filter == nullptr) {
64            return false;
65        }
66        return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
67    }
68
69}; // class PaintUtils
70
71} /* namespace uirenderer */
72} /* namespace android */
73
74#endif /* PAINT_UTILS_H */
75