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> |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 31 | #include <SkBlendMode.h> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 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) { |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame^] | 78 | texture.updateSize(width, height, texture.internalFormat(), texture.format(), |
| 79 | texture.target()); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | inline void setBlend(bool blend) { |
| 83 | texture.blend = blend; |
| 84 | } |
| 85 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 86 | inline bool isBlend() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 87 | return texture.blend; |
| 88 | } |
| 89 | |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 90 | inline void setForceFilter(bool forceFilter) { |
| 91 | this->forceFilter = forceFilter; |
| 92 | } |
| 93 | |
| 94 | inline bool getForceFilter() const { |
| 95 | return forceFilter; |
| 96 | } |
| 97 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 98 | inline void setAlpha(int alpha) { |
| 99 | this->alpha = alpha; |
| 100 | } |
| 101 | |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 102 | inline void setAlpha(int alpha, SkBlendMode mode) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 103 | this->alpha = alpha; |
| 104 | this->mode = mode; |
| 105 | } |
| 106 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 107 | inline int getAlpha() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 108 | return alpha; |
| 109 | } |
| 110 | |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 111 | inline SkBlendMode getMode() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 112 | return mode; |
| 113 | } |
| 114 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 115 | inline GLuint getTextureId() const { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 116 | return texture.id(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 119 | inline Texture& getTexture() { |
| 120 | return texture; |
| 121 | } |
| 122 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 123 | inline GLenum getRenderTarget() const { |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame^] | 124 | return texture.target(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | inline void setRenderTarget(GLenum renderTarget) { |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame^] | 128 | texture.mTarget = renderTarget; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 129 | } |
| 130 | |
John Reck | 417ed6d | 2016-03-22 16:01:08 -0700 | [diff] [blame] | 131 | inline bool isRenderable() const { |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame^] | 132 | return texture.target() != GL_NONE; |
John Reck | 417ed6d | 2016-03-22 16:01:08 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 135 | void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame^] | 136 | texture.setWrap(wrap, bindTexture, force); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 139 | void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame^] | 140 | texture.setFilter(filter, bindTexture, force); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 143 | inline SkColorFilter* getColorFilter() const { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 144 | return colorFilter; |
| 145 | } |
| 146 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 147 | void setColorFilter(SkColorFilter* filter); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 148 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 149 | void bindTexture() const; |
| 150 | void generateTexture(); |
Romain Guy | ef09a21 | 2012-09-25 12:17:14 -0700 | [diff] [blame] | 151 | |
| 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 Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 157 | void clearTexture(); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 158 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 159 | inline mat4& getTexTransform() { |
| 160 | return texTransform; |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 163 | inline mat4& getTransform() { |
| 164 | return transform; |
| 165 | } |
| 166 | |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 167 | /** |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 168 | * Posts a decStrong call to the appropriate thread. |
| 169 | * Thread-safe. |
| 170 | */ |
| 171 | void postDecStrong(); |
| 172 | |
| 173 | /** |
John Reck | 5799801 | 2015-01-29 10:17:57 -0800 | [diff] [blame] | 174 | * 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 Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 179 | private: |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 180 | Caches& caches; |
| 181 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 182 | RenderState& renderState; |
| 183 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 184 | /** |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 185 | * The texture backing this layer. |
| 186 | */ |
| 187 | Texture texture; |
| 188 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 189 | /** |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 190 | * Color filter used to draw this layer. Optional. |
| 191 | */ |
Chris Craik | e5c6584 | 2015-03-02 17:50:26 -0800 | [diff] [blame] | 192 | SkColorFilter* colorFilter = nullptr; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 193 | |
| 194 | /** |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 195 | * Indicates raster data backing the layer is scaled, requiring filtration. |
| 196 | */ |
Chris Craik | e5c6584 | 2015-03-02 17:50:26 -0800 | [diff] [blame] | 197 | bool forceFilter = false; |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 198 | |
| 199 | /** |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 200 | * Opacity of the layer. |
| 201 | */ |
Chris Craik | e5c6584 | 2015-03-02 17:50:26 -0800 | [diff] [blame] | 202 | int alpha = 255; |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 203 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Blending mode of the layer. |
| 206 | */ |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 207 | SkBlendMode mode = SkBlendMode::kSrcOver; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Optional texture coordinates transform. |
| 211 | */ |
| 212 | mat4 texTransform; |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 213 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Optional transform. |
| 216 | */ |
| 217 | mat4 transform; |
| 218 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 219 | }; // struct Layer |
| 220 | |
| 221 | }; // namespace uirenderer |
| 222 | }; // namespace android |