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 <GLES2/gl2.h>
30 #include <GLES2/gl2ext.h>
31 #include <private/hwui/DrawGlInfo.h>
32 #include <ui/Region.h>
33 #include <utils/Functor.h>
34 #include <utils/Mutex.h>
35 #include <utils/RefBase.h>
36 #include <set>
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 CacheManager;
49 class CanvasContext;
50 class 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.
55 class RenderState {
56     PREVENT_COPY_AND_ASSIGN(RenderState);
57     friend class renderthread::RenderThread;
58     friend class Caches;
59     friend class renderthread::CacheManager;
60 
61 public:
62     void onGLContextCreated();
63     void onGLContextDestroyed();
64 
65     void onVkContextCreated();
66     void onVkContextDestroyed();
67 
68     void flush(Caches::FlushMode flushMode);
69     void onBitmapDestroyed(uint32_t pixelRefId);
70 
71     void setViewport(GLsizei width, GLsizei height);
72     void getViewport(GLsizei* outWidth, GLsizei* outHeight);
73 
74     void bindFramebuffer(GLuint fbo);
getFramebuffer()75     GLuint getFramebuffer() { return mFramebuffer; }
76     GLuint createFramebuffer();
77     void deleteFramebuffer(GLuint fbo);
78 
79     void invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info);
80 
81     void debugOverdraw(bool enable, bool clear);
82 
registerLayer(Layer * layer)83     void registerLayer(Layer* layer) { mActiveLayers.insert(layer); }
unregisterLayer(Layer * layer)84     void unregisterLayer(Layer* layer) { mActiveLayers.erase(layer); }
85 
registerCanvasContext(renderthread::CanvasContext * context)86     void registerCanvasContext(renderthread::CanvasContext* context) {
87         mRegisteredContexts.insert(context);
88     }
89 
unregisterCanvasContext(renderthread::CanvasContext * context)90     void unregisterCanvasContext(renderthread::CanvasContext* context) {
91         mRegisteredContexts.erase(context);
92     }
93 
registerDeferredLayerUpdater(DeferredLayerUpdater * layerUpdater)94     void registerDeferredLayerUpdater(DeferredLayerUpdater* layerUpdater) {
95         mActiveLayerUpdaters.insert(layerUpdater);
96     }
97 
unregisterDeferredLayerUpdater(DeferredLayerUpdater * layerUpdater)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, bool overrideDisableBlending);
107 
blend()108     Blend& blend() { return *mBlend; }
meshState()109     MeshState& meshState() { return *mMeshState; }
scissor()110     Scissor& scissor() { return *mScissor; }
stencil()111     Stencil& stencil() { return *mStencil; }
112 
layerPool()113     OffscreenBufferPool& layerPool() { return *mLayerPool; }
114 
115     GrContext* getGrContext() const;
116 
117     void dump();
118 
119 private:
120     void interruptForFunctorInvoke();
121     void resumeFromFunctorInvoke();
122     void destroyLayersInUpdater();
123 
124     explicit RenderState(renderthread::RenderThread& thread);
125     ~RenderState();
126 
127     renderthread::RenderThread& mRenderThread;
128     Caches* mCaches = nullptr;
129 
130     Blend* mBlend = nullptr;
131     MeshState* mMeshState = nullptr;
132     Scissor* mScissor = nullptr;
133     Stencil* mStencil = nullptr;
134 
135     OffscreenBufferPool* mLayerPool = nullptr;
136 
137     std::set<Layer*> mActiveLayers;
138     std::set<DeferredLayerUpdater*> mActiveLayerUpdaters;
139     std::set<renderthread::CanvasContext*> mRegisteredContexts;
140 
141     GLsizei mViewportWidth;
142     GLsizei mViewportHeight;
143     GLuint mFramebuffer;
144 
145     pthread_t mThreadId;
146 };
147 
148 } /* namespace uirenderer */
149 } /* namespace android */
150 
151 #endif /* RENDERSTATE_H */
152