2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_LAYER_H
18 #define ANDROID_HWUI_LAYER_H
20 #include <cutils/compiler.h>
21 #include <sys/types.h>
22 #include <utils/StrongPointer.h>
23 #include <utils/RefBase.h>
26 #include <GLES2/gl2.h>
27 #include <GpuMemoryTracker.h>
29 #include <ui/Region.h>
32 #include <SkXfermode.h>
36 #include "RenderBuffer.h"
41 namespace uirenderer {
43 ///////////////////////////////////////////////////////////////////////////////
45 ///////////////////////////////////////////////////////////////////////////////
47 // Forward declarations
52 class DeferredDisplayList;
53 struct DeferStateStruct;
56 * A layer has dimensions and is backed by an OpenGL texture or FBO.
58 class Layer : public VirtualLightRefBase, GpuMemoryTracker {
65 // layer lifecycle, controlled from outside
74 State state; // public for logging/debugging purposes
76 Layer(Type type, RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight);
79 static uint32_t computeIdealWidth(uint32_t layerWidth);
80 static uint32_t computeIdealHeight(uint32_t layerHeight);
83 * Calling this method will remove (either by recycling or
84 * destroying) the associated FBO, if present, and any render
85 * buffer (stencil for instance.)
87 void removeFbo(bool flush = true);
90 * Sets this layer's region to a rectangle. Computes the appropriate
91 * texture coordinates.
93 void setRegionAsRect() {
94 const android::Rect& bounds = region.getBounds();
95 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
96 bounds.rightBottom().x, bounds.rightBottom().y);
98 const float texX = 1.0f / float(texture.mWidth);
99 const float texY = 1.0f / float(texture.mHeight);
100 const float height = layer.getHeight();
102 regionRect.left * texX, (height - regionRect.top) * texY,
103 regionRect.right * texX, (height - regionRect.bottom) * texY);
105 regionRect.translate(layer.left, layer.top);
108 void setWindowTransform(Matrix4& windowTransform) {
109 cachedInvTransformInWindow.loadInverse(windowTransform);
110 rendererLightPosDirty = true;
113 void updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom);
115 inline uint32_t getWidth() const {
116 return texture.mWidth;
119 inline uint32_t getHeight() const {
120 return texture.mHeight;
124 * Resize the layer and its texture if needed.
126 * @param width The new width of the layer
127 * @param height The new height of the layer
129 * @return True if the layer was resized or nothing happened, false if
130 * a failure occurred during the resizing operation
132 bool resize(const uint32_t width, const uint32_t height);
134 void setSize(uint32_t width, uint32_t height) {
135 texture.updateSize(width, height, texture.format());
138 ANDROID_API void setPaint(const SkPaint* paint);
140 inline void setBlend(bool blend) {
141 texture.blend = blend;
144 inline bool isBlend() const {
145 return texture.blend;
148 inline void setForceFilter(bool forceFilter) {
149 this->forceFilter = forceFilter;
152 inline bool getForceFilter() const {
156 inline void setAlpha(int alpha) {
160 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
165 inline int getAlpha() const {
169 inline SkXfermode::Mode getMode() const {
173 inline void setEmpty(bool empty) {
177 inline bool isEmpty() const {
181 inline void setFbo(GLuint fbo) {
185 inline GLuint getFbo() const {
189 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
190 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
191 this->stencil = renderBuffer;
192 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
193 GL_RENDERBUFFER, stencil->getName());
195 ALOGE("The specified render buffer is not a stencil buffer");
199 inline RenderBuffer* getStencilRenderBuffer() const {
203 inline GLuint getTextureId() const {
207 inline Texture& getTexture() {
211 inline GLenum getRenderTarget() const {
215 inline void setRenderTarget(GLenum renderTarget) {
216 this->renderTarget = renderTarget;
219 inline bool isRenderable() const {
220 return renderTarget != GL_NONE;
223 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
224 texture.setWrap(wrap, bindTexture, force, renderTarget);
227 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
228 texture.setFilter(filter, bindTexture, force, renderTarget);
231 inline bool isCacheable() const {
235 inline void setCacheable(bool cacheable) {
236 this->cacheable = cacheable;
239 inline bool isDirty() const {
243 inline void setDirty(bool dirty) {
247 inline bool isTextureLayer() const {
248 return type == Type::Texture;
251 inline SkColorFilter* getColorFilter() const {
255 ANDROID_API void setColorFilter(SkColorFilter* filter);
257 inline void setConvexMask(const SkPath* convexMask) {
258 this->convexMask = convexMask;
261 inline const SkPath* getConvexMask() {
265 void bindStencilRenderBuffer() const;
267 void bindTexture() const;
268 void generateTexture();
269 void allocateTexture();
272 * When the caller frees the texture itself, the caller
273 * must call this method to tell this layer that it lost
276 ANDROID_API void clearTexture();
278 inline mat4& getTexTransform() {
282 inline mat4& getTransform() {
286 void defer(const OpenGLRenderer& rootRenderer);
289 void render(const OpenGLRenderer& rootRenderer);
292 * Posts a decStrong call to the appropriate thread.
295 void postDecStrong();
298 * Lost the GL context but the layer is still around, mark it invalid internally
299 * so the dtor knows not to do any GL work
301 void onGlContextLost();
304 * Bounds of the layer.
308 * Texture coordinates of the layer.
312 * Clipping rectangle.
317 * Dirty region indicating what parts of the layer
322 * If the region is a rectangle, coordinates of the
323 * region are stored here.
328 * If the layer can be rendered as a mesh, this is non-null.
330 TextureVertex* mesh = nullptr;
331 GLsizei meshElementCount = 0;
334 * Used for deferred updates.
336 bool deferredUpdateScheduled = false;
337 std::unique_ptr<OpenGLRenderer> renderer;
338 sp<RenderNode> renderNode;
340 bool debugDrawUpdate = false;
341 bool hasDrawnSinceUpdate = false;
342 bool wasBuildLayered = false;
345 void requireRenderer();
346 void updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer);
350 RenderState& renderState;
353 * Name of the FBO used to render the layer. If the name is 0
354 * this layer is not backed by an FBO, but a simple texture.
359 * The render buffer used as the stencil buffer.
361 RenderBuffer* stencil = nullptr;
364 * Indicates whether this layer has been used already.
369 * The texture backing this layer.
374 * If set to true (by default), the layer can be reused.
376 bool cacheable = true;
379 * Denotes whether the layer is a DisplayList, or Texture layer.
384 * When set to true, this layer is dirty and should be cleared
385 * before any rendering occurs.
390 * Indicates the render target.
392 GLenum renderTarget = GL_TEXTURE_2D;
395 * Color filter used to draw this layer. Optional.
397 SkColorFilter* colorFilter = nullptr;
400 * Indicates raster data backing the layer is scaled, requiring filtration.
402 bool forceFilter = false;
405 * Opacity of the layer.
410 * Blending mode of the layer.
412 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
415 * Optional texture coordinates transform.
420 * Optional transform.
425 * Cached transform of layer in window, updated only on creation / resize
427 mat4 cachedInvTransformInWindow;
428 bool rendererLightPosDirty = true;
431 * Used to defer display lists when the layer is updated with a
434 std::unique_ptr<DeferredDisplayList> deferredList;
437 * This convex path should be used to mask the layer's draw to the screen.
439 * Data not owned/managed by layer object.
441 const SkPath* convexMask = nullptr;
445 }; // namespace uirenderer
446 }; // namespace android
448 #endif // ANDROID_HWUI_LAYER_H