1/*
2 * Copyright (C) 2010 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 <GpuMemoryTracker.h>
20#include <utils/RefBase.h>
21
22#include <SkColorFilter.h>
23#include <SkColorSpace.h>
24#include <SkBlendMode.h>
25#include <SkPaint.h>
26
27#include "Matrix.h"
28
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Layers
34///////////////////////////////////////////////////////////////////////////////
35
36class RenderState;
37
38/**
39 * A layer has dimensions and is backed by a backend specific texture or framebuffer.
40 */
41class Layer : public VirtualLightRefBase, GpuMemoryTracker {
42public:
43    enum class Api {
44        OpenGL = 0,
45        Vulkan = 1,
46    };
47
48    Api getApi() const { return mApi; }
49
50    ~Layer();
51
52    virtual uint32_t getWidth() const = 0;
53
54    virtual uint32_t getHeight() const = 0;
55
56    virtual void setSize(uint32_t width, uint32_t height) = 0;
57
58    virtual void setBlend(bool blend) = 0;
59
60    virtual bool isBlend() const = 0;
61
62    inline void setForceFilter(bool forceFilter) { this->forceFilter = forceFilter; }
63
64    inline bool getForceFilter() const { return forceFilter; }
65
66    inline void setAlpha(int alpha) { this->alpha = alpha; }
67
68    inline void setAlpha(int alpha, SkBlendMode mode) {
69        this->alpha = alpha;
70        this->mode = mode;
71    }
72
73    inline int getAlpha() const { return alpha; }
74
75    inline SkBlendMode getMode() const { return mode; }
76
77    inline SkColorFilter* getColorFilter() const { return mColorFilter.get(); }
78
79    void setColorFilter(sk_sp<SkColorFilter> filter);
80
81    void setDataSpace(android_dataspace dataspace);
82
83    void setColorSpace(sk_sp<SkColorSpace> colorSpace);
84
85    inline sk_sp<SkColorFilter> getColorSpaceWithFilter() const { return mColorSpaceWithFilter; }
86
87    inline mat4& getTexTransform() { return texTransform; }
88
89    inline mat4& getTransform() { return transform; }
90
91    /**
92     * Posts a decStrong call to the appropriate thread.
93     * Thread-safe.
94     */
95    void postDecStrong();
96
97protected:
98    Layer(RenderState& renderState, Api api, sk_sp<SkColorFilter>, int alpha,
99          SkBlendMode mode);
100
101    RenderState& mRenderState;
102
103private:
104    void buildColorSpaceWithFilter();
105
106    Api mApi;
107
108    /**
109     * Color filter used to draw this layer. Optional.
110     */
111    sk_sp<SkColorFilter> mColorFilter;
112
113    /**
114     * Colorspace of the contents of the layer. Optional.
115     */
116    android_dataspace mCurrentDataspace = HAL_DATASPACE_UNKNOWN;
117
118    /**
119     * A color filter that is the combination of the mColorFilter and mColorSpace. Optional.
120     */
121    sk_sp<SkColorFilter> mColorSpaceWithFilter;
122
123    /**
124     * Indicates raster data backing the layer is scaled, requiring filtration.
125     */
126    bool forceFilter = false;
127
128    /**
129     * Opacity of the layer.
130     */
131    int alpha;
132
133    /**
134     * Blending mode of the layer.
135     */
136    SkBlendMode mode;
137
138    /**
139     * Optional texture coordinates transform.
140     */
141    mat4 texTransform;
142
143    /**
144     * Optional transform.
145     */
146    mat4 transform;
147
148};  // struct Layer
149
150};  // namespace uirenderer
151};  // namespace android
152