PaintUtils.h revision ea508588e9c4a9d87f36b0e74de4d9420c2af06d
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 <SkXfermode.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 /** 36 * Safely retrieves the mode from the specified xfermode. If the specified 37 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode. 38 */ 39 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) { 40 SkXfermode::Mode resultMode; 41 if (!SkXfermode::AsMode(mode, &resultMode)) { 42 resultMode = SkXfermode::kSrcOver_Mode; 43 } 44 return resultMode; 45 } 46 47 static inline GLenum getFilter(const SkPaint* paint) { 48 if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) { 49 return GL_LINEAR; 50 } 51 return GL_NEAREST; 52 } 53 54 // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()? 55 static inline bool paintWillNotDraw(const SkPaint& paint) { 56 return paint.getAlpha() == 0 57 && !paint.getColorFilter() 58 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode; 59 } 60 61 // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()? 62 static inline bool paintWillNotDrawText(const SkPaint& paint) { 63 return paint.getAlpha() == 0 64 && paint.getLooper() == nullptr 65 && !paint.getColorFilter() 66 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode; 67 } 68 69 static bool isBlendedShader(const SkShader* shader) { 70 if (shader == nullptr) { 71 return false; 72 } 73 return !shader->isOpaque(); 74 } 75 76 static bool isBlendedColorFilter(const SkColorFilter* filter) { 77 if (filter == nullptr) { 78 return false; 79 } 80 return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0; 81 } 82 83 struct TextShadow { 84 SkScalar radius; 85 float dx; 86 float dy; 87 SkColor color; 88 }; 89 90 static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) { 91 SkDrawLooper::BlurShadowRec blur; 92 if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) { 93 if (textShadow) { 94 textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma); 95 textShadow->dx = blur.fOffset.fX; 96 textShadow->dy = blur.fOffset.fY; 97 textShadow->color = blur.fColor; 98 } 99 return true; 100 } 101 return false; 102 } 103 104 static inline bool hasTextShadow(const SkPaint* paint) { 105 return getTextShadow(paint, nullptr); 106 } 107 108 static inline SkXfermode::Mode getXfermodeDirect(const SkPaint* paint) { 109 return paint ? getXfermode(paint->getXfermode()) : SkXfermode::kSrcOver_Mode; 110 } 111 112 static inline int getAlphaDirect(const SkPaint* paint) { 113 return paint ? paint->getAlpha() : 255; 114 } 115 116}; // class PaintUtils 117 118} /* namespace uirenderer */ 119} /* namespace android */ 120 121#endif /* PAINT_UTILS_H */ 122