blob: f44f9d0fe2d458878ccf4f3c20a60f531d706590 [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
Kevin Lubick4e8ce462022-12-01 20:29:16 +000022#include <SkBlendMode.h>
Chris Craik03188872015-02-02 18:39:33 -080023#include <SkColorFilter.h>
Mike Reed8cafcc62018-05-03 11:32:46 -040024#include <SkPaint.h>
Chris Craikb565df12015-10-05 13:00:52 -070025#include <SkShader.h>
Chris Craik03188872015-02-02 18:39:33 -080026
Tom Hudson8dfaa492014-12-09 15:03:44 -050027namespace android {
28namespace uirenderer {
29
Chris Craikbf6f0f22015-10-01 12:36:07 -070030/**
31 * Utility methods for accessing data within SkPaint, and providing defaults
32 * with optional SkPaint pointers.
33 */
Tom Hudson8dfaa492014-12-09 15:03:44 -050034class PaintUtils {
35public:
Chris Craik80d2ade2016-03-28 12:54:07 -070036 static bool isOpaquePaint(const SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -070037 if (!paint) return true; // default (paintless) behavior is SrcOver, black
Chris Craik80d2ade2016-03-28 12:54:07 -070038
John Reck1bcacfd2017-11-03 10:12:19 -070039 if (paint->getAlpha() != 0xFF || PaintUtils::isBlendedShader(paint->getShader()) ||
40 PaintUtils::isBlendedColorFilter(paint->getColorFilter())) {
Chris Craik80d2ade2016-03-28 12:54:07 -070041 return false;
42 }
43
44 // Only let simple srcOver / src blending modes declare opaque, since behavior is clear.
Mike Reed5743fa12021-07-10 10:01:20 -040045 const auto mode = paint->asBlendMode();
John Reck1bcacfd2017-11-03 10:12:19 -070046 return mode == SkBlendMode::kSrcOver || mode == SkBlendMode::kSrc;
Chris Craik80d2ade2016-03-28 12:54:07 -070047 }
48
Mike Reedca556362021-03-10 14:31:45 -050049 static bool isBlendedShader(const SkShader* shader) { return shader && !shader->isOpaque(); }
Chris Craik117bdbc2015-02-05 10:12:38 -080050
Tom Hudson8dfaa492014-12-09 15:03:44 -050051 static bool isBlendedColorFilter(const SkColorFilter* filter) {
Mike Reedca556362021-03-10 14:31:45 -050052 return filter && !filter->isAlphaUnchanged();
Tom Hudson8dfaa492014-12-09 15:03:44 -050053 }
54
Mike Reed260ab722016-10-07 15:59:20 -040055 static inline SkBlendMode getBlendModeDirect(const SkPaint* paint) {
Mike Reed5743fa12021-07-10 10:01:20 -040056 return paint ? paint->getBlendMode_or(SkBlendMode::kSrcOver) : SkBlendMode::kSrcOver;
Chris Craikbf6f0f22015-10-01 12:36:07 -070057 }
58
59 static inline int getAlphaDirect(const SkPaint* paint) {
60 return paint ? paint->getAlpha() : 255;
61 }
62
John Reck1bcacfd2017-11-03 10:12:19 -070063}; // class PaintUtils
Tom Hudson8dfaa492014-12-09 15:03:44 -050064
65} /* namespace uirenderer */
66} /* namespace android */
67
68#endif /* PAINT_UTILS_H */