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 <SkColorFilter.h>
     20 #include <SkXfermode.h>
     21 
     22 namespace android {
     23 namespace uirenderer {
     24 
     25 class PaintUtils {
     26 public:
     27 
     28    /**
     29      * Safely retrieves the mode from the specified xfermode. If the specified
     30      * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
     31      */
     32     static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
     33         SkXfermode::Mode resultMode;
     34         if (!SkXfermode::AsMode(mode, &resultMode)) {
     35             resultMode = SkXfermode::kSrcOver_Mode;
     36         }
     37         return resultMode;
     38     }
     39 
     40     static inline GLenum getFilter(const SkPaint* paint) {
     41         if (!paint || paint->getFilterQuality() != kNone_SkFilterQuality) {
     42             return GL_LINEAR;
     43         }
     44         return GL_NEAREST;
     45     }
     46 
     47     // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()?
     48     static inline bool paintWillNotDraw(const SkPaint& paint) {
     49         return paint.getAlpha() == 0
     50                 && !paint.getColorFilter()
     51                 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
     52     }
     53 
     54     // TODO: move to a method on android:Paint? replace with SkPaint::nothingToDraw()?
     55     static inline bool paintWillNotDrawText(const SkPaint& paint) {
     56         return paint.getAlpha() == 0
     57                 && paint.getLooper() == nullptr
     58                 && !paint.getColorFilter()
     59                 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
     60     }
     61 
     62     static bool isBlendedShader(const SkShader* shader) {
     63         if (shader == nullptr) {
     64             return false;
     65         }
     66         return !shader->isOpaque();
     67     }
     68 
     69     static bool isBlendedColorFilter(const SkColorFilter* filter) {
     70         if (filter == nullptr) {
     71             return false;
     72         }
     73         return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
     74     }
     75 
     76 }; // class PaintUtils
     77 
     78 } /* namespace uirenderer */
     79 } /* namespace android */
     80 
     81 #endif /* PAINT_UTILS_H */
     82