Layer.h revision 56ad6ec42f814e9e61030ff819cac4e5d31def8b
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 <cutils/compiler.h>
20#include <sys/types.h>
21#include <utils/StrongPointer.h>
22#include <utils/RefBase.h>
23#include <memory>
24
25#include <GLES2/gl2.h>
26#include <GpuMemoryTracker.h>
27
28#include <ui/Region.h>
29
30#include <SkPaint.h>
31#include <SkXfermode.h>
32
33#include "Matrix.h"
34#include "Rect.h"
35#include "RenderBuffer.h"
36#include "Texture.h"
37#include "Vertex.h"
38
39namespace android {
40namespace uirenderer {
41
42///////////////////////////////////////////////////////////////////////////////
43// Layers
44///////////////////////////////////////////////////////////////////////////////
45
46// Forward declarations
47class Caches;
48class RenderState;
49
50/**
51 * A layer has dimensions and is backed by an OpenGL texture or FBO.
52 */
53class Layer : public VirtualLightRefBase, GpuMemoryTracker {
54public:
55    // layer lifecycle, controlled from outside
56    enum class State {
57        Uncached = 0,
58        InCache = 1,
59        FailedToCache = 2,
60        RemovedFromCache = 3,
61        DeletedFromCache = 4,
62        InGarbageList = 5,
63    };
64    State state; // public for logging/debugging purposes
65
66    Layer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight);
67    ~Layer();
68
69    inline uint32_t getWidth() const {
70        return texture.mWidth;
71    }
72
73    inline uint32_t getHeight() const {
74        return texture.mHeight;
75    }
76
77    void setSize(uint32_t width, uint32_t height) {
78        texture.updateSize(width, height, texture.format());
79    }
80
81    inline void setBlend(bool blend) {
82        texture.blend = blend;
83    }
84
85    inline bool isBlend() const {
86        return texture.blend;
87    }
88
89    inline void setForceFilter(bool forceFilter) {
90        this->forceFilter = forceFilter;
91    }
92
93    inline bool getForceFilter() const {
94        return forceFilter;
95    }
96
97    inline void setAlpha(int alpha) {
98        this->alpha = alpha;
99    }
100
101    inline void setAlpha(int alpha, SkXfermode::Mode mode) {
102        this->alpha = alpha;
103        this->mode = mode;
104    }
105
106    inline int getAlpha() const {
107        return alpha;
108    }
109
110    inline SkXfermode::Mode getMode() const {
111        return mode;
112    }
113
114    inline GLuint getTextureId() const {
115        return texture.id();
116    }
117
118    inline Texture& getTexture() {
119        return texture;
120    }
121
122    inline GLenum getRenderTarget() const {
123        return renderTarget;
124    }
125
126    inline void setRenderTarget(GLenum renderTarget) {
127        this->renderTarget = renderTarget;
128    }
129
130    inline bool isRenderable() const {
131        return renderTarget != GL_NONE;
132    }
133
134    void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
135        texture.setWrap(wrap, bindTexture, force, renderTarget);
136    }
137
138    void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
139        texture.setFilter(filter, bindTexture, force, renderTarget);
140    }
141
142    inline SkColorFilter* getColorFilter() const {
143        return colorFilter;
144    }
145
146    void setColorFilter(SkColorFilter* filter);
147
148    void bindTexture() const;
149    void generateTexture();
150
151    /**
152     * When the caller frees the texture itself, the caller
153     * must call this method to tell this layer that it lost
154     * the texture.
155     */
156    void clearTexture();
157
158    inline mat4& getTexTransform() {
159        return texTransform;
160    }
161
162    inline mat4& getTransform() {
163        return transform;
164    }
165
166    /**
167     * Posts a decStrong call to the appropriate thread.
168     * Thread-safe.
169     */
170    void postDecStrong();
171
172    /**
173     * Lost the GL context but the layer is still around, mark it invalid internally
174     * so the dtor knows not to do any GL work
175     */
176    void onGlContextLost();
177
178private:
179    Caches& caches;
180
181    RenderState& renderState;
182
183    /**
184     * The texture backing this layer.
185     */
186    Texture texture;
187
188    /**
189     * Indicates the render target.
190     */
191    GLenum renderTarget = GL_TEXTURE_2D;
192
193    /**
194     * Color filter used to draw this layer. Optional.
195     */
196    SkColorFilter* colorFilter = nullptr;
197
198    /**
199     * Indicates raster data backing the layer is scaled, requiring filtration.
200     */
201    bool forceFilter = false;
202
203    /**
204     * Opacity of the layer.
205     */
206    int alpha = 255;
207
208    /**
209     * Blending mode of the layer.
210     */
211    SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
212
213    /**
214     * Optional texture coordinates transform.
215     */
216    mat4 texTransform;
217
218    /**
219     * Optional transform.
220     */
221    mat4 transform;
222
223}; // struct Layer
224
225}; // namespace uirenderer
226}; // namespace android
227