blob: 679e2bf5d260417d38635aa4296f643f8f8ec61c [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 Craik03188872015-02-02 18:39:33 -080019#include <SkColorFilter.h>
20#include <SkXfermode.h>
21
Tom Hudson8dfaa492014-12-09 15:03:44 -050022namespace 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
Chris Craikd41c4d82015-01-05 15:51:13 -080050 && paint.getLooper() == nullptr
Tom Hudson8dfaa492014-12-09 15:03:44 -050051 && !paint.getColorFilter()
52 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
53 }
54
Chris Craik117bdbc2015-02-05 10:12:38 -080055 static bool isBlendedShader(const SkShader* shader) {
56 if (shader == nullptr) {
57 return false;
58 }
59 return !shader->isOpaque();
60 }
61
Tom Hudson8dfaa492014-12-09 15:03:44 -050062 static bool isBlendedColorFilter(const SkColorFilter* filter) {
Chris Craikd41c4d82015-01-05 15:51:13 -080063 if (filter == nullptr) {
Tom Hudson8dfaa492014-12-09 15:03:44 -050064 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 */