Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019 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/thread_annotations.h> |
| 20 | #include <binder/IBinder.h> |
| 21 | #include <gui/LayerState.h> |
| 22 | #include <ui/GraphicBuffer.h> |
| 23 | #include <utils/RefBase.h> |
| 24 | #include <utils/Singleton.h> |
| 25 | |
| 26 | #include <map> |
| 27 | #include <mutex> |
| 28 | #include <set> |
| 29 | #include <unordered_map> |
| 30 | |
| 31 | #define BUFFER_CACHE_MAX_SIZE 64 |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | class ClientCache : public Singleton<ClientCache> { |
| 36 | public: |
| 37 | ClientCache(); |
| 38 | |
| 39 | void add(const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer); |
| 40 | void erase(const client_cache_t& cacheId); |
| 41 | |
| 42 | sp<GraphicBuffer> get(const client_cache_t& cacheId); |
| 43 | |
| 44 | void removeProcess(const wp<IBinder>& processToken); |
| 45 | |
| 46 | class ErasedRecipient : public virtual RefBase { |
| 47 | public: |
| 48 | virtual void bufferErased(const client_cache_t& clientCacheId) = 0; |
| 49 | }; |
| 50 | |
| 51 | void registerErasedRecipient(const client_cache_t& cacheId, |
| 52 | const wp<ErasedRecipient>& recipient); |
| 53 | void unregisterErasedRecipient(const client_cache_t& cacheId, |
| 54 | const wp<ErasedRecipient>& recipient); |
| 55 | |
| 56 | private: |
| 57 | std::mutex mMutex; |
| 58 | |
| 59 | struct ClientCacheBuffer { |
| 60 | sp<GraphicBuffer> buffer; |
| 61 | std::set<wp<ErasedRecipient>> recipients; |
| 62 | }; |
| 63 | std::map<wp<IBinder> /*caching process*/, |
| 64 | std::unordered_map<uint64_t /*cache id*/, ClientCacheBuffer>> |
| 65 | mBuffers GUARDED_BY(mMutex); |
| 66 | |
| 67 | class CacheDeathRecipient : public IBinder::DeathRecipient { |
| 68 | public: |
| 69 | void binderDied(const wp<IBinder>& who) override; |
| 70 | }; |
| 71 | |
| 72 | sp<CacheDeathRecipient> mDeathRecipient; |
| 73 | |
| 74 | bool getBuffer(const client_cache_t& cacheId, ClientCacheBuffer** outClientCacheBuffer) |
| 75 | REQUIRES(mMutex); |
| 76 | }; |
| 77 | |
| 78 | }; // namespace android |