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