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 <GLES2/gl2.h>
20 #include <utils/Blur.h>
21 
22 #include <SkColorFilter.h>
23 #include <SkDrawLooper.h>
24 #include <SkPaint.h>
25 #include <SkShader.h>
26 
27 namespace android {
28 namespace uirenderer {
29 
30 /**
31  * Utility methods for accessing data within SkPaint, and providing defaults
32  * with optional SkPaint pointers.
33  */
34 class PaintUtils {
35 public:
getFilter(const SkPaint * paint)36     static inline GLenum getFilter(const SkPaint* paint) {
37         if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) {
38             return GL_LINEAR;
39         }
40         return GL_NEAREST;
41     }
42 
isOpaquePaint(const SkPaint * paint)43     static bool isOpaquePaint(const SkPaint* paint) {
44         if (!paint) return true;  // default (paintless) behavior is SrcOver, black
45 
46         if (paint->getAlpha() != 0xFF || PaintUtils::isBlendedShader(paint->getShader()) ||
47             PaintUtils::isBlendedColorFilter(paint->getColorFilter())) {
48             return false;
49         }
50 
51         // Only let simple srcOver / src blending modes declare opaque, since behavior is clear.
52         SkBlendMode mode = paint->getBlendMode();
53         return mode == SkBlendMode::kSrcOver || mode == SkBlendMode::kSrc;
54     }
55 
isBlendedShader(const SkShader * shader)56     static bool isBlendedShader(const SkShader* shader) {
57         if (shader == nullptr) {
58             return false;
59         }
60         return !shader->isOpaque();
61     }
62 
isBlendedColorFilter(const SkColorFilter * filter)63     static bool isBlendedColorFilter(const SkColorFilter* filter) {
64         if (filter == nullptr) {
65             return false;
66         }
67         return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
68     }
69 
getBlendModeDirect(const SkPaint * paint)70     static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) {
71         return paint ? paint->getBlendMode() : SkBlendMode::kSrcOver;
72     }
73 
getAlphaDirect(const SkPaint * paint)74     static inline int getAlphaDirect(const SkPaint* paint) {
75         return paint ? paint->getAlpha() : 255;
76     }
77 
78 };  // class PaintUtils
79 
80 } /* namespace uirenderer */
81 } /* namespace android */
82 
83 #endif /* PAINT_UTILS_H */
84