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 if (helper->isBound()) return; 52 if (!helper->setupContext()) { 53 mHelper = nullptr; 54 return; 55 } 56 mNeedUnbind = true; 57 } 58 isOk()59 bool isOk() const { return mHelper != nullptr; } 60 ~RecursiveScopedContextBind()61 ~RecursiveScopedContextBind() { release(); } 62 release()63 void release() { 64 if (mNeedUnbind) { 65 mHelper->teardownContext(); 66 mNeedUnbind = false; 67 } 68 mHelper = nullptr; 69 } 70 71 private: 72 ContextHelper* mHelper; 73 bool mNeedUnbind = false; 74 }; 75 76 } // namespace gfxstream 77