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