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 
38 class GrContext;
39 
40 namespace android {
41 namespace uirenderer {
42 
43 class Caches;
44 class Layer;
45 class DeferredLayerUpdater;
46 
47 namespace renderthread {
48 class CanvasContext;
49 class 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.
54 class RenderState {
55     PREVENT_COPY_AND_ASSIGN(RenderState);
56     friend class renderthread::RenderThread;
57     friend class Caches;
58 public:
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);
getFramebuffer()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 
registerLayer(Layer * layer)80     void registerLayer(Layer* layer) {
81         mActiveLayers.insert(layer);
82     }
unregisterLayer(Layer * layer)83     void unregisterLayer(Layer* layer) {
84         mActiveLayers.erase(layer);
85     }
86 
registerCanvasContext(renderthread::CanvasContext * context)87     void registerCanvasContext(renderthread::CanvasContext* context) {
88         mRegisteredContexts.insert(context);
89     }
90 
unregisterCanvasContext(renderthread::CanvasContext * context)91     void unregisterCanvasContext(renderthread::CanvasContext* context) {
92         mRegisteredContexts.erase(context);
93     }
94 
registerDeferredLayerUpdater(DeferredLayerUpdater * layerUpdater)95     void registerDeferredLayerUpdater(DeferredLayerUpdater* layerUpdater) {
96         mActiveLayerUpdaters.insert(layerUpdater);
97     }
98 
unregisterDeferredLayerUpdater(DeferredLayerUpdater * layerUpdater)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 
blend()109     Blend& blend() { return *mBlend; }
meshState()110     MeshState& meshState() { return *mMeshState; }
scissor()111     Scissor& scissor() { return *mScissor; }
stencil()112     Stencil& stencil() { return *mStencil; }
113 
layerPool()114     OffscreenBufferPool& layerPool() { return mLayerPool; }
115 
116     GrContext* getGrContext() const;
117 
118     void dump();
119 
120 private:
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