blob: cdeac2bbc186eb6318ac2e2f9abea6e309cf8f37 [file] [log] [blame]
Marissa Wall947d34e2019-03-29 14:03:53 -07001/*
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 Mouria90a5702021-04-16 16:36:21 +000022#include <renderengine/RenderEngine.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070023#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
34namespace android {
35
36class ClientCache : public Singleton<ClientCache> {
37public:
38 ClientCache();
39
Patrick Williamsf1e5df12022-10-17 21:37:42 +000040 enum class AddError { CacheFull, Unspecified };
41
42 base::expected<std::shared_ptr<renderengine::ExternalTexture>, AddError> add(
43 const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer);
Marissa Wall947d34e2019-03-29 14:03:53 -070044 void erase(const client_cache_t& cacheId);
45
Alec Mouria90a5702021-04-16 16:36:21 +000046 std::shared_ptr<renderengine::ExternalTexture> get(const client_cache_t& cacheId);
47
48 // Always called immediately after setup. Will be set to non-null, and then should never be
49 // called again.
50 void setRenderEngine(renderengine::RenderEngine* renderEngine) { mRenderEngine = renderEngine; }
Marissa Wall947d34e2019-03-29 14:03:53 -070051
52 void removeProcess(const wp<IBinder>& processToken);
53
54 class ErasedRecipient : public virtual RefBase {
55 public:
56 virtual void bufferErased(const client_cache_t& clientCacheId) = 0;
57 };
58
Alec Mouri4545a8a2019-08-08 20:05:32 -070059 bool registerErasedRecipient(const client_cache_t& cacheId,
Marissa Wall947d34e2019-03-29 14:03:53 -070060 const wp<ErasedRecipient>& recipient);
61 void unregisterErasedRecipient(const client_cache_t& cacheId,
62 const wp<ErasedRecipient>& recipient);
63
Robert Carrbeba6f02021-02-10 21:06:27 -080064 void dump(std::string& result);
65
Marissa Wall947d34e2019-03-29 14:03:53 -070066private:
67 std::mutex mMutex;
68
69 struct ClientCacheBuffer {
Alec Mouria90a5702021-04-16 16:36:21 +000070 std::shared_ptr<renderengine::ExternalTexture> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -070071 std::set<wp<ErasedRecipient>> recipients;
72 };
73 std::map<wp<IBinder> /*caching process*/,
Valerie Hau90fcc942019-11-18 11:18:57 -080074 std::pair<sp<IBinder> /*strong ref to caching process*/,
75 std::unordered_map<uint64_t /*cache id*/, ClientCacheBuffer>>>
Marissa Wall947d34e2019-03-29 14:03:53 -070076 mBuffers GUARDED_BY(mMutex);
77
78 class CacheDeathRecipient : public IBinder::DeathRecipient {
79 public:
80 void binderDied(const wp<IBinder>& who) override;
81 };
82
83 sp<CacheDeathRecipient> mDeathRecipient;
Alec Mouria90a5702021-04-16 16:36:21 +000084 renderengine::RenderEngine* mRenderEngine = nullptr;
Marissa Wall947d34e2019-03-29 14:03:53 -070085
86 bool getBuffer(const client_cache_t& cacheId, ClientCacheBuffer** outClientCacheBuffer)
87 REQUIRES(mMutex);
88};
89
90}; // namespace android