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