blob: 3c0cdaae8253f767fe5f520cd527477235bcc1af [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#pragma once
18
19#include "SkImageFilter.h"
20#include "SkShader.h"
21#include "SkPaint.h"
22#include "SkRefCnt.h"
23
24class SkMatrix;
25
26namespace android::uirenderer {
27
28/**
29 * Shader class that can optionally wrap an SkShader or SkImageFilter depending
30 * on the implementation
31 */
32class Shader: public SkRefCnt {
33public:
34 /**
35 * Creates a Shader instance with an optional transformation matrix
36 * @param matrix Optional matrix to transform the underlying SkShader or SkImageFilter
37 */
38 Shader(const SkMatrix* matrix);
39 virtual ~Shader();
40
41 /**
42 * Create an SkShader from the current Shader instance or return a previously
43 * created instance. This can be null if no SkShader could be created from this
44 * Shader instance.
45 */
46 sk_sp<SkShader> asSkShader();
47
48 /**
49 * Create an SkImageFilter from the current Shader instance or return a previously
50 * created instance. Unlike asSkShader, this method cannot return null.
51 */
52 sk_sp<SkImageFilter> asSkImageFilter();
53
54protected:
55 /**
56 * Create a new SkShader instance based on this Shader instance
57 */
58 virtual sk_sp<SkShader> makeSkShader();
59
60 /**
61 * Create a new SkImageFilter instance based on this Shader instance. If no SkImageFilter
62 * can be created then return nullptr
63 */
64 virtual sk_sp<SkImageFilter> makeSkImageFilter();
65
66private:
67 /**
68 * Optional matrix transform
69 */
70 const SkMatrix localMatrix;
71
72 /**
73 * Cached SkShader instance to be returned on subsequent queries
74 */
75 sk_sp<SkShader> skShader;
76
77 /**
78 * Cached SkImageFilter instance to be returned on subsequent queries
79 */
80 sk_sp<SkImageFilter> skImageFilter;
81};
82} // namespace android::uirenderer