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 70 struct TextShadow { 71 SkScalar radius; 72 float dx; 73 float dy; 74 SkColor color; 75 }; 76 getTextShadow(const SkPaint * paint,TextShadow * textShadow)77 static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) { 78 SkDrawLooper::BlurShadowRec blur; 79 if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) { 80 if (textShadow) { 81 textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma); 82 textShadow->dx = blur.fOffset.fX; 83 textShadow->dy = blur.fOffset.fY; 84 textShadow->color = blur.fColor; 85 } 86 return true; 87 } 88 return false; 89 } 90 hasTextShadow(const SkPaint * paint)91 static inline bool hasTextShadow(const SkPaint* paint) { return getTextShadow(paint, nullptr); } 92 getBlendModeDirect(const SkPaint * paint)93 static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) { 94 return paint ? paint->getBlendMode() : SkBlendMode::kSrcOver; 95 } 96 getAlphaDirect(const SkPaint * paint)97 static inline int getAlphaDirect(const SkPaint* paint) { 98 return paint ? paint->getAlpha() : 255; 99 } 100 101 }; // class PaintUtils 102 103 } /* namespace uirenderer */ 104 } /* namespace android */ 105 106 #endif /* PAINT_UTILS_H */ 107