PaintUtils.h revision c2f31df8b3b9a237e9abffc59c61804ad8495073
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 <utils/Blur.h> 20 21#include <SkColorFilter.h> 22#include <SkDrawLooper.h> 23#include <SkShader.h> 24 25namespace android { 26namespace uirenderer { 27 28/** 29 * Utility methods for accessing data within SkPaint, and providing defaults 30 * with optional SkPaint pointers. 31 */ 32class PaintUtils { 33public: 34 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 42 // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()? 43 static inline bool paintWillNotDraw(const SkPaint& paint) { 44 return paint.getAlpha() == 0 45 && !paint.getColorFilter() 46 && paint.getBlendMode() == SkBlendMode::kSrcOver; 47 } 48 49 // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()? 50 static inline bool paintWillNotDrawText(const SkPaint& paint) { 51 return paint.getAlpha() == 0 52 && paint.getLooper() == nullptr 53 && !paint.getColorFilter() 54 && paint.getBlendMode() == SkBlendMode::kSrcOver; 55 } 56 57 static bool isOpaquePaint(const SkPaint* paint) { 58 if (!paint) return true; // default (paintless) behavior is SrcOver, black 59 60 if (paint->getAlpha() != 0xFF 61 || PaintUtils::isBlendedShader(paint->getShader()) 62 || PaintUtils::isBlendedColorFilter(paint->getColorFilter())) { 63 return false; 64 } 65 66 // Only let simple srcOver / src blending modes declare opaque, since behavior is clear. 67 SkBlendMode mode = paint->getBlendMode(); 68 return mode == SkBlendMode::kSrcOver 69 || mode == SkBlendMode::kSrc; 70 } 71 72 static bool isBlendedShader(const SkShader* shader) { 73 if (shader == nullptr) { 74 return false; 75 } 76 return !shader->isOpaque(); 77 } 78 79 static bool isBlendedColorFilter(const SkColorFilter* filter) { 80 if (filter == nullptr) { 81 return false; 82 } 83 return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0; 84 } 85 86 struct TextShadow { 87 SkScalar radius; 88 float dx; 89 float dy; 90 SkColor color; 91 }; 92 93 static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) { 94 SkDrawLooper::BlurShadowRec blur; 95 if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) { 96 if (textShadow) { 97 textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma); 98 textShadow->dx = blur.fOffset.fX; 99 textShadow->dy = blur.fOffset.fY; 100 textShadow->color = blur.fColor; 101 } 102 return true; 103 } 104 return false; 105 } 106 107 static inline bool hasTextShadow(const SkPaint* paint) { 108 return getTextShadow(paint, nullptr); 109 } 110 111 static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) { 112 return paint ? paint->getBlendMode() : SkBlendMode::kSrcOver; 113 } 114 115 static inline int getAlphaDirect(const SkPaint* paint) { 116 return paint ? paint->getAlpha() : 255; 117 } 118 119}; // class PaintUtils 120 121} /* namespace uirenderer */ 122} /* namespace android */ 123 124#endif /* PAINT_UTILS_H */ 125