blob: 9874ce29cb3c228b18f3a8b373abf4f8d8f8c8cd [file] [log] [blame]
Romain Guydda57022010-07-06 11:39:32 -07001/*
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
Chris Craik5e00c7c2016-07-06 16:10:09 -070017#pragma once
Romain Guydda57022010-07-06 11:39:32 -070018
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050019#include <cutils/compiler.h>
Romain Guyf7f93552010-07-08 19:17:03 -070020#include <sys/types.h>
John Reck087bc0c2014-04-04 16:20:08 -070021#include <utils/StrongPointer.h>
Nick Kralevichbfed8272014-11-01 18:37:39 -070022#include <utils/RefBase.h>
Chris Craik51d6a3d2014-12-22 17:16:56 -080023#include <memory>
Romain Guyf7f93552010-07-08 19:17:03 -070024
Romain Guydda57022010-07-06 11:39:32 -070025#include <GLES2/gl2.h>
John Reck38e0c322015-11-10 12:19:17 -080026#include <GpuMemoryTracker.h>
Romain Guydda57022010-07-06 11:39:32 -070027
Romain Guy5b3b3522010-10-27 18:57:51 -070028#include <ui/Region.h>
29
Derek Sollenbergerca79cf62012-08-14 16:44:52 -040030#include <SkPaint.h>
Mike Reed260ab722016-10-07 15:59:20 -040031#include <SkBlendMode.h>
Romain Guydda57022010-07-06 11:39:32 -070032
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050033#include "Matrix.h"
Romain Guydda57022010-07-06 11:39:32 -070034#include "Rect.h"
Romain Guy3bbacf22013-02-06 16:51:04 -080035#include "RenderBuffer.h"
Romain Guy9ace8f52011-07-07 20:50:11 -070036#include "Texture.h"
Romain Guyf219da52011-01-16 12:54:25 -080037#include "Vertex.h"
Romain Guydda57022010-07-06 11:39:32 -070038
39namespace android {
40namespace uirenderer {
41
Romain Guy8550c4c2010-10-08 15:49:53 -070042///////////////////////////////////////////////////////////////////////////////
43// Layers
44///////////////////////////////////////////////////////////////////////////////
Romain Guydda57022010-07-06 11:39:32 -070045
Romain Guy2bf68f02012-03-02 13:37:47 -080046// Forward declarations
Romain Guy8aa195d2013-06-04 18:00:09 -070047class Caches;
John Reck3b202512014-06-23 13:13:08 -070048class RenderState;
Romain Guy2bf68f02012-03-02 13:37:47 -080049
Romain Guydda57022010-07-06 11:39:32 -070050/**
Romain Guyeb993562010-10-05 18:14:38 -070051 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda57022010-07-06 11:39:32 -070052 */
John Reck38e0c322015-11-10 12:19:17 -080053class Layer : public VirtualLightRefBase, GpuMemoryTracker {
Chris Craik564acf72014-01-02 16:46:18 -080054public:
Chris Craikbfd1cd62014-09-10 13:04:31 -070055 // layer lifecycle, controlled from outside
Chris Craikb9ce116d2015-08-20 15:14:06 -070056 enum class State {
57 Uncached = 0,
58 InCache = 1,
59 FailedToCache = 2,
60 RemovedFromCache = 3,
61 DeletedFromCache = 4,
62 InGarbageList = 5,
Chris Craikbfd1cd62014-09-10 13:04:31 -070063 };
64 State state; // public for logging/debugging purposes
65
Chris Craik5e00c7c2016-07-06 16:10:09 -070066 Layer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070067 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070068
Romain Guy3bbacf22013-02-06 16:51:04 -080069 inline uint32_t getWidth() const {
John Reck38e0c322015-11-10 12:19:17 -080070 return texture.mWidth;
Romain Guy9ace8f52011-07-07 20:50:11 -070071 }
72
Romain Guy3bbacf22013-02-06 16:51:04 -080073 inline uint32_t getHeight() const {
John Reck38e0c322015-11-10 12:19:17 -080074 return texture.mHeight;
Romain Guy9ace8f52011-07-07 20:50:11 -070075 }
76
77 void setSize(uint32_t width, uint32_t height) {
Romain Guy253f2c22016-09-28 17:34:42 -070078 texture.updateSize(width, height, texture.internalFormat(), texture.format());
Romain Guy9ace8f52011-07-07 20:50:11 -070079 }
80
81 inline void setBlend(bool blend) {
82 texture.blend = blend;
83 }
84
Romain Guy3bbacf22013-02-06 16:51:04 -080085 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070086 return texture.blend;
87 }
88
Chris Craik9757ac02014-02-25 18:50:17 -080089 inline void setForceFilter(bool forceFilter) {
90 this->forceFilter = forceFilter;
91 }
92
93 inline bool getForceFilter() const {
94 return forceFilter;
95 }
96
Romain Guy9ace8f52011-07-07 20:50:11 -070097 inline void setAlpha(int alpha) {
98 this->alpha = alpha;
99 }
100
Mike Reed260ab722016-10-07 15:59:20 -0400101 inline void setAlpha(int alpha, SkBlendMode mode) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700102 this->alpha = alpha;
103 this->mode = mode;
104 }
105
Romain Guy3bbacf22013-02-06 16:51:04 -0800106 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700107 return alpha;
108 }
109
Mike Reed260ab722016-10-07 15:59:20 -0400110 inline SkBlendMode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700111 return mode;
112 }
113
Chris Craikf27133d2015-02-19 09:51:53 -0800114 inline GLuint getTextureId() const {
John Reck38e0c322015-11-10 12:19:17 -0800115 return texture.id();
Romain Guy9ace8f52011-07-07 20:50:11 -0700116 }
117
Chris Craikf27133d2015-02-19 09:51:53 -0800118 inline Texture& getTexture() {
119 return texture;
120 }
121
Romain Guy3bbacf22013-02-06 16:51:04 -0800122 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700123 return renderTarget;
124 }
125
126 inline void setRenderTarget(GLenum renderTarget) {
127 this->renderTarget = renderTarget;
128 }
129
John Reck417ed6d2016-03-22 16:01:08 -0700130 inline bool isRenderable() const {
131 return renderTarget != GL_NONE;
132 }
133
Romain Guyd21b6e12011-11-30 20:21:23 -0800134 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
135 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700136 }
137
Romain Guyd21b6e12011-11-30 20:21:23 -0800138 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
139 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700140 }
141
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500142 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700143 return colorFilter;
144 }
145
Chris Craik5e00c7c2016-07-06 16:10:09 -0700146 void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700147
Romain Guy8aa195d2013-06-04 18:00:09 -0700148 void bindTexture() const;
149 void generateTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700150
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 */
Chris Craik5e00c7c2016-07-06 16:10:09 -0700156 void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800157
Romain Guy9ace8f52011-07-07 20:50:11 -0700158 inline mat4& getTexTransform() {
159 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700160 }
161
Romain Guy302a9df2011-08-16 13:55:02 -0700162 inline mat4& getTransform() {
163 return transform;
164 }
165
Romain Guy9fc27812011-04-27 14:21:41 -0700166 /**
John Reck0e89e2b2014-10-31 14:49:06 -0700167 * Posts a decStrong call to the appropriate thread.
168 * Thread-safe.
169 */
170 void postDecStrong();
171
172 /**
John Reck57998012015-01-29 10:17:57 -0800173 * 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
Romain Guy9ace8f52011-07-07 20:50:11 -0700178private:
Romain Guy8aa195d2013-06-04 18:00:09 -0700179 Caches& caches;
180
John Reck3b202512014-06-23 13:13:08 -0700181 RenderState& renderState;
182
Romain Guy9ace8f52011-07-07 20:50:11 -0700183 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700184 * The texture backing this layer.
185 */
186 Texture texture;
187
Romain Guyaa6c24c2011-04-28 18:40:04 -0700188 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700189 * Indicates the render target.
190 */
Chris Craike5c65842015-03-02 17:50:26 -0800191 GLenum renderTarget = GL_TEXTURE_2D;
Romain Guy9ace8f52011-07-07 20:50:11 -0700192
193 /**
194 * Color filter used to draw this layer. Optional.
195 */
Chris Craike5c65842015-03-02 17:50:26 -0800196 SkColorFilter* colorFilter = nullptr;
Romain Guy9ace8f52011-07-07 20:50:11 -0700197
198 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800199 * Indicates raster data backing the layer is scaled, requiring filtration.
200 */
Chris Craike5c65842015-03-02 17:50:26 -0800201 bool forceFilter = false;
Chris Craik9757ac02014-02-25 18:50:17 -0800202
203 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700204 * Opacity of the layer.
205 */
Chris Craike5c65842015-03-02 17:50:26 -0800206 int alpha = 255;
Chris Craik9757ac02014-02-25 18:50:17 -0800207
Romain Guy9ace8f52011-07-07 20:50:11 -0700208 /**
209 * Blending mode of the layer.
210 */
Mike Reed260ab722016-10-07 15:59:20 -0400211 SkBlendMode mode = SkBlendMode::kSrcOver;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700212
213 /**
214 * Optional texture coordinates transform.
215 */
216 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700217
Romain Guy302a9df2011-08-16 13:55:02 -0700218 /**
219 * Optional transform.
220 */
221 mat4 transform;
222
Romain Guydda57022010-07-06 11:39:32 -0700223}; // struct Layer
224
225}; // namespace uirenderer
226}; // namespace android