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