BakedOpRenderer.h revision 1dfa0704964c17e45775b9e01f1fa0b1a10774f7
1/* 2 * Copyright (C) 2015 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#ifndef ANDROID_HWUI_BAKED_OP_RENDERER_H 18#define ANDROID_HWUI_BAKED_OP_RENDERER_H 19 20#include "BakedOpState.h" 21#include "Matrix.h" 22#include "utils/Macros.h" 23 24namespace android { 25namespace uirenderer { 26 27class Caches; 28struct Glop; 29class Layer; 30class RenderState; 31struct ClipBase; 32 33/** 34 * Main rendering manager for a collection of work - one frame + any contained FBOs. 35 * 36 * Manages frame and FBO lifecycle, binding the GL framebuffer as appropriate. This is the only 37 * place where FBOs are bound, created, and destroyed. 38 * 39 * All rendering operations will be sent by the Dispatcher, a collection of static methods, 40 * which has intentionally limited access to the renderer functionality. 41 */ 42class BakedOpRenderer { 43public: 44 /** 45 * Position agnostic shadow lighting info. Used with all shadow ops in scene. 46 */ 47 struct LightInfo { 48 LightInfo() : LightInfo(0, 0) {} 49 LightInfo(uint8_t ambientShadowAlpha, 50 uint8_t spotShadowAlpha) 51 : ambientShadowAlpha(ambientShadowAlpha) 52 , spotShadowAlpha(spotShadowAlpha) {} 53 uint8_t ambientShadowAlpha; 54 uint8_t spotShadowAlpha; 55 }; 56 57 BakedOpRenderer(Caches& caches, RenderState& renderState, bool opaque, const LightInfo& lightInfo) 58 : mRenderState(renderState) 59 , mCaches(caches) 60 , mOpaque(opaque) 61 , mLightInfo(lightInfo) { 62 } 63 64 RenderState& renderState() { return mRenderState; } 65 Caches& caches() { return mCaches; } 66 67 void startFrame(uint32_t width, uint32_t height, const Rect& repaintRect); 68 void endFrame(const Rect& repaintRect); 69 WARN_UNUSED_RESULT OffscreenBuffer* startTemporaryLayer(uint32_t width, uint32_t height); 70 void startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect); 71 void endLayer(); 72 WARN_UNUSED_RESULT OffscreenBuffer* copyToLayer(const Rect& area); 73 74 Texture* getTexture(const SkBitmap* bitmap); 75 const LightInfo& getLightInfo() const { return mLightInfo; } 76 77 void renderGlop(const BakedOpState& state, const Glop& glop) { 78 renderGlop(&state.computedState.clippedBounds, 79 state.computedState.getClipIfNeeded(), 80 glop); 81 } 82 void renderFunctor(const FunctorOp& op, const BakedOpState& state); 83 84 void renderGlop(const Rect* dirtyBounds, const ClipBase* clip, const Glop& glop); 85 bool offscreenRenderTarget() { return mRenderTarget.offscreenBuffer != nullptr; } 86 void dirtyRenderTarget(const Rect& dirtyRect); 87 bool didDraw() const { return mHasDrawn; } 88 89 uint32_t getViewportWidth() const { return mRenderTarget.viewportWidth; } 90 uint32_t getViewportHeight() const { return mRenderTarget.viewportHeight; } 91 92 // simple draw methods, to be used for end frame decoration 93 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint) { 94 float ltrb[4] = { left, top, right, bottom }; 95 drawRects(ltrb, 4, paint); 96 } 97 void drawRects(const float* rects, int count, const SkPaint* paint); 98private: 99 void setViewport(uint32_t width, uint32_t height); 100 void clearColorBuffer(const Rect& clearRect); 101 void prepareRender(const Rect* dirtyBounds, const ClipBase* clip); 102 void setupStencilRectList(const ClipBase* clip); 103 void setupStencilRegion(const ClipBase* clip); 104 void setupStencilQuads(std::vector<Vertex>& quadVertices, int incrementThreshold); 105 106 RenderState& mRenderState; 107 Caches& mCaches; 108 bool mOpaque; 109 bool mHasDrawn = false; 110 111 // render target state - setup by start/end layer/frame 112 // only valid to use in between start/end pairs. 113 struct { 114 // If not drawing to a layer: fbo = 0, offscreenBuffer = null, 115 // Otherwise these refer to currently painting layer's state 116 GLuint frameBufferId = 0; 117 OffscreenBuffer* offscreenBuffer = nullptr; 118 119 // Used when drawing to a layer and using stencil clipping. otherwise null. 120 RenderBuffer* stencil = nullptr; 121 122 // value representing the ClipRectList* or ClipRegion* currently stored in 123 // the stencil of the current render target 124 const ClipBase* lastStencilClip = nullptr; 125 126 // Size of renderable region in current render target - for layers, may not match actual 127 // bounds of FBO texture. offscreenBuffer->texture has this information. 128 uint32_t viewportWidth = 0; 129 uint32_t viewportHeight = 0; 130 131 Matrix4 orthoMatrix; 132 } mRenderTarget; 133 134 const LightInfo mLightInfo; 135}; 136 137}; // namespace uirenderer 138}; // namespace android 139 140#endif // ANDROID_HWUI_BAKED_OP_RENDERER_H 141