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