| 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> | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 22 | #include <renderengine/RenderEngine.h> | 
| Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 23 | #include <ui/GraphicBuffer.h> | 
|  | 24 | #include <utils/RefBase.h> | 
|  | 25 | #include <utils/Singleton.h> | 
|  | 26 |  | 
|  | 27 | #include <map> | 
|  | 28 | #include <mutex> | 
|  | 29 | #include <set> | 
|  | 30 | #include <unordered_map> | 
|  | 31 |  | 
|  | 32 | #define BUFFER_CACHE_MAX_SIZE 64 | 
|  | 33 |  | 
|  | 34 | namespace android { | 
|  | 35 |  | 
|  | 36 | class ClientCache : public Singleton<ClientCache> { | 
|  | 37 | public: | 
|  | 38 | ClientCache(); | 
|  | 39 |  | 
| Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 40 | bool add(const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer); | 
| Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 41 | void erase(const client_cache_t& cacheId); | 
|  | 42 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 43 | std::shared_ptr<renderengine::ExternalTexture> get(const client_cache_t& cacheId); | 
|  | 44 |  | 
|  | 45 | // Always called immediately after setup. Will be set to non-null, and then should never be | 
|  | 46 | // called again. | 
|  | 47 | void setRenderEngine(renderengine::RenderEngine* renderEngine) { mRenderEngine = renderEngine; } | 
| Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | void removeProcess(const wp<IBinder>& processToken); | 
|  | 50 |  | 
|  | 51 | class ErasedRecipient : public virtual RefBase { | 
|  | 52 | public: | 
|  | 53 | virtual void bufferErased(const client_cache_t& clientCacheId) = 0; | 
|  | 54 | }; | 
|  | 55 |  | 
| Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 56 | bool registerErasedRecipient(const client_cache_t& cacheId, | 
| Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 57 | const wp<ErasedRecipient>& recipient); | 
|  | 58 | void unregisterErasedRecipient(const client_cache_t& cacheId, | 
|  | 59 | const wp<ErasedRecipient>& recipient); | 
|  | 60 |  | 
| Robert Carr | beba6f0 | 2021-02-10 21:06:27 -0800 | [diff] [blame] | 61 | void dump(std::string& result); | 
|  | 62 |  | 
| Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 63 | private: | 
|  | 64 | std::mutex mMutex; | 
|  | 65 |  | 
|  | 66 | struct ClientCacheBuffer { | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 67 | std::shared_ptr<renderengine::ExternalTexture> buffer; | 
| Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 68 | std::set<wp<ErasedRecipient>> recipients; | 
|  | 69 | }; | 
|  | 70 | std::map<wp<IBinder> /*caching process*/, | 
| Valerie Hau | 90fcc94 | 2019-11-18 11:18:57 -0800 | [diff] [blame] | 71 | std::pair<sp<IBinder> /*strong ref to caching process*/, | 
|  | 72 | std::unordered_map<uint64_t /*cache id*/, ClientCacheBuffer>>> | 
| Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 73 | mBuffers GUARDED_BY(mMutex); | 
|  | 74 |  | 
|  | 75 | class CacheDeathRecipient : public IBinder::DeathRecipient { | 
|  | 76 | public: | 
|  | 77 | void binderDied(const wp<IBinder>& who) override; | 
|  | 78 | }; | 
|  | 79 |  | 
|  | 80 | sp<CacheDeathRecipient> mDeathRecipient; | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 81 | renderengine::RenderEngine* mRenderEngine = nullptr; | 
| Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 82 |  | 
|  | 83 | bool getBuffer(const client_cache_t& cacheId, ClientCacheBuffer** outClientCacheBuffer) | 
|  | 84 | REQUIRES(mMutex); | 
|  | 85 | }; | 
|  | 86 |  | 
|  | 87 | }; // namespace android |