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: isOpaquePaint(const SkPaint * paint)35 static bool isOpaquePaint(const SkPaint* paint) { 36 if (!paint) return true; // default (paintless) behavior is SrcOver, black 37 38 if (paint->getAlpha() != 0xFF || PaintUtils::isBlendedShader(paint->getShader()) || 39 PaintUtils::isBlendedColorFilter(paint->getColorFilter())) { 40 return false; 41 } 42 43 // Only let simple srcOver / src blending modes declare opaque, since behavior is clear. 44 const auto mode = paint->asBlendMode(); 45 return mode == SkBlendMode::kSrcOver || mode == SkBlendMode::kSrc; 46 } 47 isBlendedShader(const SkShader * shader)48 static bool isBlendedShader(const SkShader* shader) { return shader && !shader->isOpaque(); } 49 isBlendedColorFilter(const SkColorFilter * filter)50 static bool isBlendedColorFilter(const SkColorFilter* filter) { 51 return filter && !filter->isAlphaUnchanged(); 52 } 53 getBlendModeDirect(const SkPaint * paint)54 static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) { 55 return paint ? paint->getBlendMode_or(SkBlendMode::kSrcOver) : SkBlendMode::kSrcOver; 56 } 57 getAlphaDirect(const SkPaint * paint)58 static inline int getAlphaDirect(const SkPaint* paint) { 59 return paint ? paint->getAlpha() : 255; 60 } 61 62 }; // class PaintUtils 63 64 } /* namespace uirenderer */ 65 } /* namespace android */ 66 67 #endif /* PAINT_UTILS_H */ 68