BakedOpRenderer.h revision 8d2cf943d9c7292e54726399faefdec4a01c084b
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;
30
31/**
32 * Lightweight alternative to Layer. Owns the persistent state of an offscreen render target, and
33 * encompasses enough information to draw it back on screen (minus paint properties, which are held
34 * by LayerOp).
35 */
36class OffscreenBuffer {
37public:
38    OffscreenBuffer(RenderState& renderState, Caches& caches,
39            uint32_t textureWidth, uint32_t textureHeight,
40            uint32_t viewportWidth, uint32_t viewportHeight);
41    ~OffscreenBuffer();
42
43    // must be called prior to rendering, to construct/update vertex buffer
44    void updateMeshFromRegion();
45
46    RenderState& renderState;
47    uint32_t viewportWidth;
48    uint32_t viewportHeight;
49    Texture texture;
50
51    // Portion of offscreen buffer that has been drawn to. Used to minimize drawing area when
52    // drawing back to screen / parent FBO.
53    Region region;
54    GLsizei elementCount = 0;
55    GLuint vbo = 0;
56};
57
58/**
59 * Main rendering manager for a collection of work - one frame + any contained FBOs.
60 *
61 * Manages frame and FBO lifecycle, binding the GL framebuffer as appropriate. This is the only
62 * place where FBOs are bound, created, and destroyed.
63 *
64 * All rendering operations will be sent by the Dispatcher, a collection of static methods,
65 * which has intentionally limited access to the renderer functionality.
66 */
67class BakedOpRenderer {
68public:
69    BakedOpRenderer(Caches& caches, RenderState& renderState, bool opaque)
70            : mRenderState(renderState)
71            , mCaches(caches)
72            , mOpaque(opaque) {
73    }
74
75    static OffscreenBuffer* createOffscreenBuffer(RenderState& renderState,
76            uint32_t width, uint32_t height);
77    static void destroyOffscreenBuffer(OffscreenBuffer*);
78
79    RenderState& renderState() { return mRenderState; }
80    Caches& caches() { return mCaches; }
81
82    void startFrame(uint32_t width, uint32_t height);
83    void endFrame();
84    OffscreenBuffer* createLayer(uint32_t width, uint32_t height);
85    void startLayer(OffscreenBuffer* offscreenBuffer);
86    void endLayer();
87
88    Texture* getTexture(const SkBitmap* bitmap);
89
90    void renderGlop(const BakedOpState& state, const Glop& glop);
91    bool didDraw() { return mHasDrawn; }
92private:
93    void setViewport(uint32_t width, uint32_t height);
94
95    RenderState& mRenderState;
96    Caches& mCaches;
97    bool mOpaque;
98    bool mHasDrawn = false;
99
100    // render target state - setup by start/end layer/frame
101    // only valid to use in between start/end pairs.
102    struct {
103        GLuint frameBufferId = 0;
104        OffscreenBuffer* offscreenBuffer = nullptr;
105        uint32_t viewportWidth = 0;
106        uint32_t viewportHeight = 0;
107        Matrix4 orthoMatrix;
108    } mRenderTarget;
109};
110
111/**
112 * Provides all "onBitmapOp(...)" style static methods for every op type, which convert the
113 * RecordedOps and their state to Glops, and renders them with the provided BakedOpRenderer.
114 *
115 * This dispatcher is separate from the renderer so that the dispatcher / renderer interaction is
116 * minimal through public BakedOpRenderer APIs.
117 */
118class BakedOpDispatcher {
119public:
120    // Declares all "onBitmapOp(...)" style methods for every op type
121#define DISPATCH_METHOD(Type) \
122        static void on##Type(BakedOpRenderer& renderer, const Type& op, const BakedOpState& state);
123    MAP_OPS(DISPATCH_METHOD);
124};
125
126}; // namespace uirenderer
127}; // namespace android
128
129#endif // ANDROID_HWUI_BAKED_OP_RENDERER_H
130