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