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    void onBitmapDestroyed(uint32_t pixelRefId);
67
68    void setViewport(GLsizei width, GLsizei height);
69    void getViewport(GLsizei* outWidth, GLsizei* outHeight);
70
71    void bindFramebuffer(GLuint fbo);
72    GLuint getFramebuffer() { return mFramebuffer; }
73    GLuint createFramebuffer();
74    void deleteFramebuffer(GLuint fbo);
75
76    void invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info);
77
78    void debugOverdraw(bool enable, bool clear);
79
80    void registerLayer(Layer* layer) {
81        mActiveLayers.insert(layer);
82    }
83    void unregisterLayer(Layer* layer) {
84        mActiveLayers.erase(layer);
85    }
86
87    void registerCanvasContext(renderthread::CanvasContext* context) {
88        mRegisteredContexts.insert(context);
89    }
90
91    void unregisterCanvasContext(renderthread::CanvasContext* context) {
92        mRegisteredContexts.erase(context);
93    }
94
95    void registerDeferredLayerUpdater(DeferredLayerUpdater* layerUpdater) {
96        mActiveLayerUpdaters.insert(layerUpdater);
97    }
98
99    void unregisterDeferredLayerUpdater(DeferredLayerUpdater* layerUpdater) {
100        mActiveLayerUpdaters.erase(layerUpdater);
101    }
102
103    // TODO: This system is a little clunky feeling, this could use some
104    // more thinking...
105    void postDecStrong(VirtualLightRefBase* object);
106
107    void render(const Glop& glop, const Matrix4& orthoMatrix);
108
109    Blend& blend() { return *mBlend; }
110    MeshState& meshState() { return *mMeshState; }
111    Scissor& scissor() { return *mScissor; }
112    Stencil& stencil() { return *mStencil; }
113
114    OffscreenBufferPool& layerPool() { return mLayerPool; }
115
116    GrContext* getGrContext() const;
117
118    void dump();
119
120private:
121    void interruptForFunctorInvoke();
122    void resumeFromFunctorInvoke();
123    void destroyLayersInUpdater();
124
125    explicit RenderState(renderthread::RenderThread& thread);
126    ~RenderState();
127
128
129    renderthread::RenderThread& mRenderThread;
130    Caches* mCaches = nullptr;
131
132    Blend* mBlend = nullptr;
133    MeshState* mMeshState = nullptr;
134    Scissor* mScissor = nullptr;
135    Stencil* mStencil = nullptr;
136
137    OffscreenBufferPool mLayerPool;
138
139    std::set<Layer*> mActiveLayers;
140    std::set<DeferredLayerUpdater*> mActiveLayerUpdaters;
141    std::set<renderthread::CanvasContext*> mRegisteredContexts;
142
143    GLsizei mViewportWidth;
144    GLsizei mViewportHeight;
145    GLuint mFramebuffer;
146
147    pthread_t mThreadId;
148};
149
150} /* namespace uirenderer */
151} /* namespace android */
152
153#endif /* RENDERSTATE_H */
154