Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 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 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 17 | #pragma once |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 18 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 19 | #include <cutils/compiler.h> |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 21 | #include <utils/StrongPointer.h> |
Nick Kralevich | bfed827 | 2014-11-01 18:37:39 -0700 | [diff] [blame] | 22 | #include <utils/RefBase.h> |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 23 | #include <memory> |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 24 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 25 | #include <GLES2/gl2.h> |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 26 | #include <GpuMemoryTracker.h> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 27 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 28 | #include <ui/Region.h> |
| 29 | |
Derek Sollenberger | ca79cf6 | 2012-08-14 16:44:52 -0400 | [diff] [blame] | 30 | #include <SkPaint.h> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 31 | #include <SkXfermode.h> |
| 32 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 33 | #include "Matrix.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 34 | #include "Rect.h" |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 35 | #include "RenderBuffer.h" |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 36 | #include "Texture.h" |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 37 | #include "Vertex.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | // Layers |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 45 | |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 46 | // Forward declarations |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 47 | class Caches; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 48 | class RenderState; |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 49 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 50 | /** |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 51 | * A layer has dimensions and is backed by an OpenGL texture or FBO. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 52 | */ |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 53 | class Layer : public VirtualLightRefBase, GpuMemoryTracker { |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 54 | public: |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 55 | // layer lifecycle, controlled from outside |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 56 | enum class State { |
| 57 | Uncached = 0, |
| 58 | InCache = 1, |
| 59 | FailedToCache = 2, |
| 60 | RemovedFromCache = 3, |
| 61 | DeletedFromCache = 4, |
| 62 | InGarbageList = 5, |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 63 | }; |
| 64 | State state; // public for logging/debugging purposes |
| 65 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 66 | Layer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight); |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 67 | ~Layer(); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 68 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 69 | inline uint32_t getWidth() const { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 70 | return texture.mWidth; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 73 | inline uint32_t getHeight() const { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 74 | return texture.mHeight; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void setSize(uint32_t width, uint32_t height) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 78 | texture.updateSize(width, height, texture.format()); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | inline void setBlend(bool blend) { |
| 82 | texture.blend = blend; |
| 83 | } |
| 84 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 85 | inline bool isBlend() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 86 | return texture.blend; |
| 87 | } |
| 88 | |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 89 | inline void setForceFilter(bool forceFilter) { |
| 90 | this->forceFilter = forceFilter; |
| 91 | } |
| 92 | |
| 93 | inline bool getForceFilter() const { |
| 94 | return forceFilter; |
| 95 | } |
| 96 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 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 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 106 | inline int getAlpha() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 107 | return alpha; |
| 108 | } |
| 109 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 110 | inline SkXfermode::Mode getMode() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 111 | return mode; |
| 112 | } |
| 113 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 114 | inline GLuint getTextureId() const { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 115 | return texture.id(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 118 | inline Texture& getTexture() { |
| 119 | return texture; |
| 120 | } |
| 121 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 122 | inline GLenum getRenderTarget() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 123 | return renderTarget; |
| 124 | } |
| 125 | |
| 126 | inline void setRenderTarget(GLenum renderTarget) { |
| 127 | this->renderTarget = renderTarget; |
| 128 | } |
| 129 | |
John Reck | 417ed6d | 2016-03-22 16:01:08 -0700 | [diff] [blame] | 130 | inline bool isRenderable() const { |
| 131 | return renderTarget != GL_NONE; |
| 132 | } |
| 133 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 134 | void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { |
| 135 | texture.setWrap(wrap, bindTexture, force, renderTarget); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 138 | void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { |
| 139 | texture.setFilter(filter, bindTexture, force, renderTarget); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 142 | inline SkColorFilter* getColorFilter() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 143 | return colorFilter; |
| 144 | } |
| 145 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 146 | void setColorFilter(SkColorFilter* filter); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 147 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 148 | void bindTexture() const; |
| 149 | void generateTexture(); |
Romain Guy | ef09a21 | 2012-09-25 12:17:14 -0700 | [diff] [blame] | 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 | */ |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 156 | void clearTexture(); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 157 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 158 | inline mat4& getTexTransform() { |
| 159 | return texTransform; |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 162 | inline mat4& getTransform() { |
| 163 | return transform; |
| 164 | } |
| 165 | |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 166 | /** |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 167 | * Posts a decStrong call to the appropriate thread. |
| 168 | * Thread-safe. |
| 169 | */ |
| 170 | void postDecStrong(); |
| 171 | |
| 172 | /** |
John Reck | 5799801 | 2015-01-29 10:17:57 -0800 | [diff] [blame] | 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 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 178 | private: |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 179 | Caches& caches; |
| 180 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 181 | RenderState& renderState; |
| 182 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 183 | /** |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 184 | * The texture backing this layer. |
| 185 | */ |
| 186 | Texture texture; |
| 187 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 188 | /** |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 189 | * Indicates the render target. |
| 190 | */ |
Chris Craik | e5c6584 | 2015-03-02 17:50:26 -0800 | [diff] [blame] | 191 | GLenum renderTarget = GL_TEXTURE_2D; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * Color filter used to draw this layer. Optional. |
| 195 | */ |
Chris Craik | e5c6584 | 2015-03-02 17:50:26 -0800 | [diff] [blame] | 196 | SkColorFilter* colorFilter = nullptr; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 199 | * Indicates raster data backing the layer is scaled, requiring filtration. |
| 200 | */ |
Chris Craik | e5c6584 | 2015-03-02 17:50:26 -0800 | [diff] [blame] | 201 | bool forceFilter = false; |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 202 | |
| 203 | /** |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 204 | * Opacity of the layer. |
| 205 | */ |
Chris Craik | e5c6584 | 2015-03-02 17:50:26 -0800 | [diff] [blame] | 206 | int alpha = 255; |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 207 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 208 | /** |
| 209 | * Blending mode of the layer. |
| 210 | */ |
Chris Craik | e5c6584 | 2015-03-02 17:50:26 -0800 | [diff] [blame] | 211 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * Optional texture coordinates transform. |
| 215 | */ |
| 216 | mat4 texTransform; |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 217 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Optional transform. |
| 220 | */ |
| 221 | mat4 transform; |
| 222 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 223 | }; // struct Layer |
| 224 | |
| 225 | }; // namespace uirenderer |
| 226 | }; // namespace android |