blob: 45123dd55002d277f2303ef81bb475088331fd07 [file] [log] [blame]
Nader Jawadfc42a992020-07-29 22:48:59 -07001/*
2 * Copyright (C) 2020 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
17#include "Shader.h"
18
19#include "SkImageFilters.h"
20#include "SkPaint.h"
21#include "SkRefCnt.h"
22
23namespace android::uirenderer {
24
25Shader::Shader(const SkMatrix* matrix)
26 : localMatrix(matrix ? *matrix : SkMatrix::I())
27 , skShader(nullptr)
28 , skImageFilter(nullptr) {}
29
30Shader::~Shader() {}
31
32sk_sp<SkShader> Shader::asSkShader() {
33 // If we already have created a shader with these parameters just return the existing
34 // shader we have already created
35 if (!this->skShader.get()) {
36 this->skShader = makeSkShader();
37 if (this->skShader.get()) {
38 if (!localMatrix.isIdentity()) {
39 this->skShader = this->skShader->makeWithLocalMatrix(localMatrix);
40 }
41 }
42 }
43 return this->skShader;
44}
45
46/**
47 * By default return null as we cannot convert all visual effects to SkShader instances
48 */
49sk_sp<SkShader> Shader::makeSkShader() {
50 return nullptr;
51}
52
53sk_sp<SkImageFilter> Shader::asSkImageFilter() {
54 // If we already have created an ImageFilter with these parameters just return the existing
55 // ImageFilter we have already created
56 if (!this->skImageFilter.get()) {
57 // Attempt to create an SkImageFilter from the current Shader implementation
58 this->skImageFilter = makeSkImageFilter();
59 if (this->skImageFilter) {
60 if (!localMatrix.isIdentity()) {
61 // If we have created an SkImageFilter and we have a transformation, wrap
62 // the created SkImageFilter to apply the given matrix
63 this->skImageFilter = SkImageFilters::MatrixTransform(
64 localMatrix, kMedium_SkFilterQuality, this->skImageFilter);
65 }
66 } else {
67 // Otherwise if no SkImageFilter implementation is provided, create one from
68 // the result of asSkShader. Note the matrix is already applied to the shader in
69 // this case so just convert it to an SkImageFilter using SkImageFilters::Paint
70 SkPaint paint;
71 paint.setShader(asSkShader());
72 sk_sp<SkImageFilter> paintFilter = SkImageFilters::Paint(paint);
73 this->skImageFilter = SkImageFilters::Xfermode(SkBlendMode::kDstIn,
74 std::move(paintFilter));
75 }
76 }
77 return this->skImageFilter;
78}
79
80/**
81 * By default return null for subclasses to implement. If there is not a direct SkImageFilter
82 * conversion
83 */
84sk_sp<SkImageFilter> Shader::makeSkImageFilter() {
85 return nullptr;
86}
87} // namespace android::uirenderer