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