blob: 94bcb1110e0524b5eb71c27c2512cc497a439c5b [file] [log] [blame]
Tom Hudson8dfaa492014-12-09 15:03:44 -05001/*
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
Stan Iliev564ca3e2018-09-04 22:00:00 +000019#include <GLES2/gl2.h>
Chris Craikbf6f0f22015-10-01 12:36:07 -070020#include <utils/Blur.h>
21
Chris Craik03188872015-02-02 18:39:33 -080022#include <SkColorFilter.h>
Mike Reed8cafcc62018-05-03 11:32:46 -040023#include <SkPaint.h>
Chris Craikb565df12015-10-05 13:00:52 -070024#include <SkShader.h>
Chris Craik03188872015-02-02 18:39:33 -080025
Tom Hudson8dfaa492014-12-09 15:03:44 -050026namespace android {
27namespace uirenderer {
28
Chris Craikbf6f0f22015-10-01 12:36:07 -070029/**
30 * Utility methods for accessing data within SkPaint, and providing defaults
31 * with optional SkPaint pointers.
32 */
Tom Hudson8dfaa492014-12-09 15:03:44 -050033class PaintUtils {
34public:
Chris Craik80d2ade2016-03-28 12:54:07 -070035 static bool isOpaquePaint(const SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -070036 if (!paint) return true; // default (paintless) behavior is SrcOver, black
Chris Craik80d2ade2016-03-28 12:54:07 -070037
John Reck1bcacfd2017-11-03 10:12:19 -070038 if (paint->getAlpha() != 0xFF || PaintUtils::isBlendedShader(paint->getShader()) ||
39 PaintUtils::isBlendedColorFilter(paint->getColorFilter())) {
Chris Craik80d2ade2016-03-28 12:54:07 -070040 return false;
41 }
42
43 // Only let simple srcOver / src blending modes declare opaque, since behavior is clear.
Mike Reed5743fa12021-07-10 10:01:20 -040044 const auto mode = paint->asBlendMode();
John Reck1bcacfd2017-11-03 10:12:19 -070045 return mode == SkBlendMode::kSrcOver || mode == SkBlendMode::kSrc;
Chris Craik80d2ade2016-03-28 12:54:07 -070046 }
47
Mike Reedca556362021-03-10 14:31:45 -050048 static bool isBlendedShader(const SkShader* shader) { return shader && !shader->isOpaque(); }
Chris Craik117bdbc2015-02-05 10:12:38 -080049
Tom Hudson8dfaa492014-12-09 15:03:44 -050050 static bool isBlendedColorFilter(const SkColorFilter* filter) {
Mike Reedca556362021-03-10 14:31:45 -050051 return filter && !filter->isAlphaUnchanged();
Tom Hudson8dfaa492014-12-09 15:03:44 -050052 }
53
Mike Reed260ab722016-10-07 15:59:20 -040054 static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) {
Mike Reed5743fa12021-07-10 10:01:20 -040055 return paint ? paint->getBlendMode_or(SkBlendMode::kSrcOver) : SkBlendMode::kSrcOver;
Chris Craikbf6f0f22015-10-01 12:36:07 -070056 }
57
58 static inline int getAlphaDirect(const SkPaint* paint) {
59 return paint ? paint->getAlpha() : 255;
60 }
61
John Reck1bcacfd2017-11-03 10:12:19 -070062}; // class PaintUtils
Tom Hudson8dfaa492014-12-09 15:03:44 -050063
64} /* namespace uirenderer */
65} /* namespace android */
66
67#endif /* PAINT_UTILS_H */