RenderState.h revision c3f131696111a066d9efd9c7c3e37566a2a9fb89
1/*
2 * Copyright (C) 2014 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#ifndef RENDERSTATE_H
17#define RENDERSTATE_H
18
19#include "Caches.h"
20#include "Glop.h"
21#include "renderstate/Blend.h"
22#include "renderstate/MeshState.h"
23#include "renderstate/OffscreenBufferPool.h"
24#include "renderstate/PixelBufferState.h"
25#include "renderstate/Scissor.h"
26#include "renderstate/Stencil.h"
27#include "utils/Macros.h"
28
29#include <set>
30#include <GLES2/gl2.h>
31#include <GLES2/gl2ext.h>
32#include <ui/Region.h>
33#include <utils/Mutex.h>
34#include <utils/Functor.h>
35#include <utils/RefBase.h>
36#include <private/hwui/DrawGlInfo.h>
37
38class GrContext;
39
40namespace android {
41namespace uirenderer {
42
43class Caches;
44class Layer;
45class DeferredLayerUpdater;
46
47namespace renderthread {
48class CanvasContext;
49class RenderThread;
50}
51
52// TODO: Replace Cache's GL state tracking with this. For now it's more a thin
53// wrapper of Caches for users to migrate to.
54class RenderState {
55    PREVENT_COPY_AND_ASSIGN(RenderState);
56    friend class renderthread::RenderThread;
57    friend class Caches;
58public:
59    void onGLContextCreated();
60    void onGLContextDestroyed();
61
62    void onVkContextCreated();
63    void onVkContextDestroyed();
64
65    void flush(Caches::FlushMode flushMode);
66
67    void setViewport(GLsizei width, GLsizei height);
68    void getViewport(GLsizei* outWidth, GLsizei* outHeight);
69
70    void bindFramebuffer(GLuint fbo);
71    GLuint getFramebuffer() { return mFramebuffer; }
72    GLuint createFramebuffer();
73    void deleteFramebuffer(GLuint fbo);
74
75    void invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info);
76
77    void debugOverdraw(bool enable, bool clear);
78
79    void registerLayer(Layer* layer) {
80        mActiveLayers.insert(layer);
81    }
82    void unregisterLayer(Layer* layer) {
83        mActiveLayers.erase(layer);
84    }
85
86    void registerCanvasContext(renderthread::CanvasContext* context) {
87        mRegisteredContexts.insert(context);
88    }
89
90    void unregisterCanvasContext(renderthread::CanvasContext* context) {
91        mRegisteredContexts.erase(context);
92    }
93
94    void registerDeferredLayerUpdater(DeferredLayerUpdater* layerUpdater) {
95        mActiveLayerUpdaters.insert(layerUpdater);
96    }
97
98    void unregisterDeferredLayerUpdater(DeferredLayerUpdater* layerUpdater) {
99        mActiveLayerUpdaters.erase(layerUpdater);
100    }
101
102    // TODO: This system is a little clunky feeling, this could use some
103    // more thinking...
104    void postDecStrong(VirtualLightRefBase* object);
105
106    void render(const Glop& glop, const Matrix4& orthoMatrix);
107
108    Blend& blend() { return *mBlend; }
109    MeshState& meshState() { return *mMeshState; }
110    Scissor& scissor() { return *mScissor; }
111    Stencil& stencil() { return *mStencil; }
112
113    OffscreenBufferPool& layerPool() { return mLayerPool; }
114
115    GrContext* getGrContext() const;
116
117    void dump();
118
119private:
120    void interruptForFunctorInvoke();
121    void resumeFromFunctorInvoke();
122    void destroyLayersInUpdater();
123
124    explicit RenderState(renderthread::RenderThread& thread);
125    ~RenderState();
126
127
128    renderthread::RenderThread& mRenderThread;
129    Caches* mCaches = nullptr;
130
131    Blend* mBlend = nullptr;
132    MeshState* mMeshState = nullptr;
133    Scissor* mScissor = nullptr;
134    Stencil* mStencil = nullptr;
135
136    OffscreenBufferPool mLayerPool;
137
138    std::set<Layer*> mActiveLayers;
139    std::set<DeferredLayerUpdater*> mActiveLayerUpdaters;
140    std::set<renderthread::CanvasContext*> mRegisteredContexts;
141
142    GLsizei mViewportWidth;
143    GLsizei mViewportHeight;
144    GLuint mFramebuffer;
145
146    pthread_t mThreadId;
147};
148
149} /* namespace uirenderer */
150} /* namespace android */
151
152#endif /* RENDERSTATE_H */
153