1 /* 2 * Copyright (C) 2024 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 17 #pragma once 18 19 #include <android-base/no_destructor.h> 20 #include <media/stagefright/foundation/ABase.h> 21 22 #include <condition_variable> 23 #include <deque> 24 #include <list> 25 #include <memory> 26 #include <thread> 27 28 class C2BufferQueueBlockPool; 29 30 namespace android { 31 32 /** 33 * Container class in order to invalidate C2BufferQueueBlockPool(s) and their resources 34 * when the client process is dead abruptly. 35 */ 36 class C2BqPoolInvalidateItem { 37 public: 38 39 /** 40 * invalidate contained C2BufferQueueBlockPool(s) and their resources 41 */ 42 void invalidate(); 43 44 /** 45 * skip invalidate(), if it is scheduled and not yet invalidated. 46 */ 47 void skip(); 48 49 /** 50 * returns whether invalidate() is reuqired or not. 51 */ 52 bool needsInvalidate(); 53 54 C2BqPoolInvalidateItem(std::list<std::shared_ptr<C2BufferQueueBlockPool>> &&pools); 55 56 ~C2BqPoolInvalidateItem() = default; 57 private: 58 59 std::list<std::shared_ptr<C2BufferQueueBlockPool>> mPools; 60 bool mNeedsInvalidate; 61 std::mutex mLock; 62 63 DISALLOW_EVIL_CONSTRUCTORS(C2BqPoolInvalidateItem); 64 }; 65 66 /** 67 * Asynchronous C2BufferQueueBlockPool invalidator. 68 * 69 * this has C2BqPoolInvalidateItem inside. and call invalidate() from a separate 70 * thread asynchronously. 71 */ 72 class C2BqPoolInvalidator { 73 public: 74 /** 75 * This gets the singleton instance of the class. 76 */ 77 static C2BqPoolInvalidator &getInstance(); 78 79 /** 80 * queue invalidation items. the item will be invalidated after certain 81 * amount of delay from a separate thread. 82 */ 83 void queue(std::shared_ptr<C2BqPoolInvalidateItem> &item); 84 85 ~C2BqPoolInvalidator(); 86 private: 87 88 C2BqPoolInvalidator(); 89 90 void run(); 91 92 std::thread mThread; 93 bool mDone; 94 95 std::mutex mMutex; 96 std::condition_variable mCv; 97 98 std::deque<std::pair<int64_t, std::shared_ptr<C2BqPoolInvalidateItem>>> mItems; 99 100 friend class ::android::base::NoDestructor<C2BqPoolInvalidator>; 101 102 DISALLOW_EVIL_CONSTRUCTORS(C2BqPoolInvalidator); 103 }; 104 105 } // namespace android 106