1 // Copyright 2022 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expresso or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 namespace gfxstream { 18 namespace gl { 19 class TextureDraw; 20 } // namespace gl 21 } // namespace gfxstream 22 23 namespace gfxstream { 24 25 // ContextHelper interface class used during Buffer and ColorBuffer 26 // operations. This is introduced to remove coupling from the FrameBuffer 27 // class implementation. 28 class ContextHelper { 29 public: 30 ContextHelper() = default; 31 virtual ~ContextHelper() = default; 32 virtual bool setupContext() = 0; 33 virtual void teardownContext() = 0; 34 virtual bool isBound() const = 0; 35 }; 36 37 // Helper class to use a ContextHelper for some set of operations. 38 // Usage is pretty simple: 39 // 40 // { 41 // RecursiveScopedContextBind context(m_helper); 42 // if (!context.isOk()) { 43 // return false; // something bad happened. 44 // } 45 // .... do something .... 46 // } // automatically calls m_helper->teardownContext(); 47 // 48 class RecursiveScopedContextBind { 49 public: RecursiveScopedContextBind(ContextHelper * helper)50 RecursiveScopedContextBind(ContextHelper* helper) : mHelper(helper) { 51 mIsBound = helper->setupContext(); 52 } 53 isOk()54 bool isOk() const { return mIsBound; } 55 ~RecursiveScopedContextBind()56 ~RecursiveScopedContextBind() { release(); } 57 release()58 void release() { 59 if (mIsBound) { 60 mHelper->teardownContext(); 61 mIsBound = false; 62 } 63 } 64 65 private: 66 ContextHelper* mHelper; 67 bool mIsBound = false; 68 }; 69 70 } // namespace gfxstream 71