Layer.h revision 5e00c7ce063116c11315639f0035aca8ad73e8cc
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 178 /** 179 * Bounds of the layer. 180 */ 181 Rect layer; 182 /** 183 * Texture coordinates of the layer. 184 */ 185 Rect texCoords; 186 /** 187 * Clipping rectangle. 188 */ 189 Rect clipRect; 190 191 /** 192 * If the layer can be rendered as a mesh, this is non-null. 193 */ 194 TextureVertex* mesh = nullptr; 195 GLsizei meshElementCount = 0; 196 197private: 198 Caches& caches; 199 200 RenderState& renderState; 201 202 /** 203 * The texture backing this layer. 204 */ 205 Texture texture; 206 207 /** 208 * Indicates the render target. 209 */ 210 GLenum renderTarget = GL_TEXTURE_2D; 211 212 /** 213 * Color filter used to draw this layer. Optional. 214 */ 215 SkColorFilter* colorFilter = nullptr; 216 217 /** 218 * Indicates raster data backing the layer is scaled, requiring filtration. 219 */ 220 bool forceFilter = false; 221 222 /** 223 * Opacity of the layer. 224 */ 225 int alpha = 255; 226 227 /** 228 * Blending mode of the layer. 229 */ 230 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode; 231 232 /** 233 * Optional texture coordinates transform. 234 */ 235 mat4 texTransform; 236 237 /** 238 * Optional transform. 239 */ 240 mat4 transform; 241 242}; // struct Layer 243 244}; // namespace uirenderer 245}; // namespace android 246