RenderState.h revision 44eb2c00861098dd3e2950d923646814b4cc57c2
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#include <renderstate/Blend.h>
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(Layer* layer) {
66        mActiveLayers.insert(layer);
67    }
68    void unregisterLayer(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    Blend& blend() { return *mBlend; }
88    MeshState& meshState() { return *mMeshState; }
89    Scissor& scissor() { return *mScissor; }
90    Stencil& stencil() { return *mStencil; }
91private:
92    friend class renderthread::RenderThread;
93    friend class Caches;
94
95    void interruptForFunctorInvoke();
96    void resumeFromFunctorInvoke();
97    void assertOnGLThread();
98
99    RenderState(renderthread::RenderThread& thread);
100    ~RenderState();
101
102
103    renderthread::RenderThread& mRenderThread;
104    Caches* mCaches = nullptr;
105
106    Blend* mBlend = nullptr;
107    MeshState* mMeshState = nullptr;
108    Scissor* mScissor = nullptr;
109    Stencil* mStencil = nullptr;
110
111    AssetAtlas mAssetAtlas;
112    std::set<Layer*> mActiveLayers;
113    std::set<renderthread::CanvasContext*> mRegisteredContexts;
114
115    GLsizei mViewportWidth;
116    GLsizei mViewportHeight;
117    GLuint mFramebuffer;
118
119    pthread_t mThreadId;
120};
121
122} /* namespace uirenderer */
123} /* namespace android */
124
125#endif /* RENDERSTATE_H */
126