Home | History | Annotate | Download | only in utils
      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 
     25 namespace android {
     26 namespace uirenderer {
     27 
     28 /**
     29  * Utility methods for accessing data within SkPaint, and providing defaults
     30  * with optional SkPaint pointers.
     31  */
     32 class PaintUtils {
     33 public:
     34     static inline GLenum getFilter(const SkPaint* paint) {
     35         if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) {
     36             return GL_LINEAR;
     37         }
     38         return GL_NEAREST;
     39     }
     40 
     41     static bool isOpaquePaint(const SkPaint* paint) {
     42         if (!paint) return true;  // default (paintless) behavior is SrcOver, black
     43 
     44         if (paint->getAlpha() != 0xFF || PaintUtils::isBlendedShader(paint->getShader()) ||
     45             PaintUtils::isBlendedColorFilter(paint->getColorFilter())) {
     46             return false;
     47         }
     48 
     49         // Only let simple srcOver / src blending modes declare opaque, since behavior is clear.
     50         SkBlendMode mode = paint->getBlendMode();
     51         return mode == SkBlendMode::kSrcOver || mode == SkBlendMode::kSrc;
     52     }
     53 
     54     static bool isBlendedShader(const SkShader* shader) {
     55         if (shader == nullptr) {
     56             return false;
     57         }
     58         return !shader->isOpaque();
     59     }
     60 
     61     static bool isBlendedColorFilter(const SkColorFilter* filter) {
     62         if (filter == nullptr) {
     63             return false;
     64         }
     65         return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
     66     }
     67 
     68     struct TextShadow {
     69         SkScalar radius;
     70         float dx;
     71         float dy;
     72         SkColor color;
     73     };
     74 
     75     static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) {
     76         SkDrawLooper::BlurShadowRec blur;
     77         if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) {
     78             if (textShadow) {
     79                 textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma);
     80                 textShadow->dx = blur.fOffset.fX;
     81                 textShadow->dy = blur.fOffset.fY;
     82                 textShadow->color = blur.fColor;
     83             }
     84             return true;
     85         }
     86         return false;
     87     }
     88 
     89     static inline bool hasTextShadow(const SkPaint* paint) { return getTextShadow(paint, nullptr); }
     90 
     91     static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) {
     92         return paint ? paint->getBlendMode() : SkBlendMode::kSrcOver;
     93     }
     94 
     95     static inline int getAlphaDirect(const SkPaint* paint) {
     96         return paint ? paint->getAlpha() : 255;
     97     }
     98 
     99 };  // class PaintUtils
    100 
    101 } /* namespace uirenderer */
    102 } /* namespace android */
    103 
    104 #endif /* PAINT_UTILS_H */
    105