blob: 90248ab5d92483cabd373af2a1473ce0366f7882 [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>
Tom Hudsonca4f0b52015-10-05 08:51:49 -040020#include <SkDrawLooper.h>
Chris Craik03188872015-02-02 18:39:33 -080021#include <SkXfermode.h>
22
Tom Hudson8dfaa492014-12-09 15:03:44 -050023namespace android {
24namespace uirenderer {
25
26class PaintUtils {
27public:
28
29 /**
30 * Safely retrieves the mode from the specified xfermode. If the specified
31 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
32 */
33 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
34 SkXfermode::Mode resultMode;
35 if (!SkXfermode::AsMode(mode, &resultMode)) {
36 resultMode = SkXfermode::kSrcOver_Mode;
37 }
38 return resultMode;
39 }
40
Chris Craik0519c8102015-02-11 13:17:06 -080041 static inline GLenum getFilter(const SkPaint* paint) {
Mike Reed2a1ce8a2015-03-16 11:16:09 -040042 if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) {
Chris Craik0519c8102015-02-11 13:17:06 -080043 return GL_LINEAR;
44 }
45 return GL_NEAREST;
46 }
47
Tom Hudson8dfaa492014-12-09 15:03:44 -050048 // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()?
49 static inline bool paintWillNotDraw(const SkPaint& paint) {
50 return paint.getAlpha() == 0
51 && !paint.getColorFilter()
52 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
53 }
54
55 // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()?
56 static inline bool paintWillNotDrawText(const SkPaint& paint) {
57 return paint.getAlpha() == 0
Chris Craikd41c4d82015-01-05 15:51:13 -080058 && paint.getLooper() == nullptr
Tom Hudson8dfaa492014-12-09 15:03:44 -050059 && !paint.getColorFilter()
60 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
61 }
62
Chris Craik117bdbc2015-02-05 10:12:38 -080063 static bool isBlendedShader(const SkShader* shader) {
64 if (shader == nullptr) {
65 return false;
66 }
67 return !shader->isOpaque();
68 }
69
Tom Hudson8dfaa492014-12-09 15:03:44 -050070 static bool isBlendedColorFilter(const SkColorFilter* filter) {
Chris Craikd41c4d82015-01-05 15:51:13 -080071 if (filter == nullptr) {
Tom Hudson8dfaa492014-12-09 15:03:44 -050072 return false;
73 }
74 return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
75 }
76
77}; // class PaintUtils
78
79} /* namespace uirenderer */
80} /* namespace android */
81
82#endif /* PAINT_UTILS_H */