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