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