blob: 8e71cd11599d3da96efb30c0a4353f7b88da8ea3 [file] [log] [blame]
Romain Guydda570202010-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 Guydda570202010-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 Guydda570202010-07-06 11:39:32 -070025#include <GLES2/gl2.h>
John Reck38e0c322015-11-10 12:19:17 -080026#include <GpuMemoryTracker.h>
Romain Guydda570202010-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 Guydda570202010-07-06 11:39:32 -070032
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050033#include "Matrix.h"
Romain Guydda570202010-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 Guydda570202010-07-06 11:39:32 -070038
39namespace android {
40namespace uirenderer {
41
Romain Guy8550c4c2010-10-08 15:49:53 -070042///////////////////////////////////////////////////////////////////////////////
43// Layers
44///////////////////////////////////////////////////////////////////////////////
Romain Guydda570202010-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 Guydda570202010-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 Guydda570202010-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) {
sergeyv2a38c422016-10-25 15:21:50 -070078 texture.updateSize(width, height, texture.internalFormat(), texture.format(),
79 texture.target());
Romain Guy9ace8f52011-07-07 20:50:11 -070080 }
81
82 inline void setBlend(bool blend) {
83 texture.blend = blend;
84 }
85
Romain Guy3bbacf22013-02-06 16:51:04 -080086 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070087 return texture.blend;
88 }
89
Chris Craik9757ac02014-02-25 18:50:17 -080090 inline void setForceFilter(bool forceFilter) {
91 this->forceFilter = forceFilter;
92 }
93
94 inline bool getForceFilter() const {
95 return forceFilter;
96 }
97
Romain Guy9ace8f52011-07-07 20:50:11 -070098 inline void setAlpha(int alpha) {
99 this->alpha = alpha;
100 }
101
Mike Reed260ab722016-10-07 15:59:20 -0400102 inline void setAlpha(int alpha, SkBlendMode mode) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700103 this->alpha = alpha;
104 this->mode = mode;
105 }
106
Romain Guy3bbacf22013-02-06 16:51:04 -0800107 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700108 return alpha;
109 }
110
Mike Reed260ab722016-10-07 15:59:20 -0400111 inline SkBlendMode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700112 return mode;
113 }
114
Chris Craikf27133d2015-02-19 09:51:53 -0800115 inline GLuint getTextureId() const {
John Reck38e0c322015-11-10 12:19:17 -0800116 return texture.id();
Romain Guy9ace8f52011-07-07 20:50:11 -0700117 }
118
Chris Craikf27133d2015-02-19 09:51:53 -0800119 inline Texture& getTexture() {
120 return texture;
121 }
122
Romain Guy3bbacf22013-02-06 16:51:04 -0800123 inline GLenum getRenderTarget() const {
sergeyv2a38c422016-10-25 15:21:50 -0700124 return texture.target();
Romain Guy9ace8f52011-07-07 20:50:11 -0700125 }
126
127 inline void setRenderTarget(GLenum renderTarget) {
sergeyv2a38c422016-10-25 15:21:50 -0700128 texture.mTarget = renderTarget;
Romain Guy9ace8f52011-07-07 20:50:11 -0700129 }
130
John Reck417ed6d2016-03-22 16:01:08 -0700131 inline bool isRenderable() const {
sergeyv2a38c422016-10-25 15:21:50 -0700132 return texture.target() != GL_NONE;
John Reck417ed6d2016-03-22 16:01:08 -0700133 }
134
Romain Guyd21b6e12011-11-30 20:21:23 -0800135 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
sergeyv2a38c422016-10-25 15:21:50 -0700136 texture.setWrap(wrap, bindTexture, force);
Romain Guy9ace8f52011-07-07 20:50:11 -0700137 }
138
Romain Guyd21b6e12011-11-30 20:21:23 -0800139 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
sergeyv2a38c422016-10-25 15:21:50 -0700140 texture.setFilter(filter, bindTexture, force);
Romain Guy9ace8f52011-07-07 20:50:11 -0700141 }
142
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500143 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700144 return colorFilter;
145 }
146
Chris Craik5e00c7c2016-07-06 16:10:09 -0700147 void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700148
Romain Guy8aa195d2013-06-04 18:00:09 -0700149 void bindTexture() const;
150 void generateTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700151
152 /**
153 * When the caller frees the texture itself, the caller
154 * must call this method to tell this layer that it lost
155 * the texture.
156 */
Chris Craik5e00c7c2016-07-06 16:10:09 -0700157 void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800158
Romain Guy9ace8f52011-07-07 20:50:11 -0700159 inline mat4& getTexTransform() {
160 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700161 }
162
Romain Guy302a9df2011-08-16 13:55:02 -0700163 inline mat4& getTransform() {
164 return transform;
165 }
166
Romain Guy9fc27812011-04-27 14:21:41 -0700167 /**
John Reck0e89e2b2014-10-31 14:49:06 -0700168 * Posts a decStrong call to the appropriate thread.
169 * Thread-safe.
170 */
171 void postDecStrong();
172
173 /**
John Reck57998012015-01-29 10:17:57 -0800174 * Lost the GL context but the layer is still around, mark it invalid internally
175 * so the dtor knows not to do any GL work
176 */
177 void onGlContextLost();
178
Romain Guy9ace8f52011-07-07 20:50:11 -0700179private:
Romain Guy8aa195d2013-06-04 18:00:09 -0700180 Caches& caches;
181
John Reck3b202512014-06-23 13:13:08 -0700182 RenderState& renderState;
183
Romain Guy9ace8f52011-07-07 20:50:11 -0700184 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700185 * The texture backing this layer.
186 */
187 Texture texture;
188
Romain Guyaa6c24c2011-04-28 18:40:04 -0700189 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700190 * Color filter used to draw this layer. Optional.
191 */
Chris Craike5c65842015-03-02 17:50:26 -0800192 SkColorFilter* colorFilter = nullptr;
Romain Guy9ace8f52011-07-07 20:50:11 -0700193
194 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800195 * Indicates raster data backing the layer is scaled, requiring filtration.
196 */
Chris Craike5c65842015-03-02 17:50:26 -0800197 bool forceFilter = false;
Chris Craik9757ac02014-02-25 18:50:17 -0800198
199 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700200 * Opacity of the layer.
201 */
Chris Craike5c65842015-03-02 17:50:26 -0800202 int alpha = 255;
Chris Craik9757ac02014-02-25 18:50:17 -0800203
Romain Guy9ace8f52011-07-07 20:50:11 -0700204 /**
205 * Blending mode of the layer.
206 */
Mike Reed260ab722016-10-07 15:59:20 -0400207 SkBlendMode mode = SkBlendMode::kSrcOver;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700208
209 /**
210 * Optional texture coordinates transform.
211 */
212 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700213
Romain Guy302a9df2011-08-16 13:55:02 -0700214 /**
215 * Optional transform.
216 */
217 mat4 transform;
218
Romain Guydda570202010-07-06 11:39:32 -0700219}; // struct Layer
220
221}; // namespace uirenderer
222}; // namespace android