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