blob: b56b252d9fa8bdded189e34c08dc1d6e1834c27a [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
Brian Lindahl439afad2022-11-14 11:16:55 -070036// This class manages a cache of buffer handles between SurfaceFlinger clients
37// and the SurfaceFlinger process which optimizes away some of the cost of
38// sending buffer handles across processes.
39//
40// Buffers are explicitly cached and uncached by the SurfaceFlinger client. When
41// a buffer is uncached, it is not only purged from this cache, but the buffer
42// ID is also passed down to CompositionEngine to purge it from a similar cache
43// used between SurfaceFlinger and Composer HAL. The buffer ID used to purge
44// both the SurfaceFlinger side of this other cache, as well as Composer HAL's
45// side of the cache.
46//
Marissa Wall947d34e2019-03-29 14:03:53 -070047class ClientCache : public Singleton<ClientCache> {
48public:
49 ClientCache();
50
Patrick Williamsf1e5df12022-10-17 21:37:42 +000051 enum class AddError { CacheFull, Unspecified };
52
53 base::expected<std::shared_ptr<renderengine::ExternalTexture>, AddError> add(
54 const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer);
Brian Lindahl439afad2022-11-14 11:16:55 -070055
56 sp<GraphicBuffer> erase(const client_cache_t& cacheId);
Marissa Wall947d34e2019-03-29 14:03:53 -070057
Alec Mouria90a5702021-04-16 16:36:21 +000058 std::shared_ptr<renderengine::ExternalTexture> get(const client_cache_t& cacheId);
59
60 // Always called immediately after setup. Will be set to non-null, and then should never be
61 // called again.
62 void setRenderEngine(renderengine::RenderEngine* renderEngine) { mRenderEngine = renderEngine; }
Marissa Wall947d34e2019-03-29 14:03:53 -070063
64 void removeProcess(const wp<IBinder>& processToken);
65
66 class ErasedRecipient : public virtual RefBase {
67 public:
68 virtual void bufferErased(const client_cache_t& clientCacheId) = 0;
69 };
70
Alec Mouri4545a8a2019-08-08 20:05:32 -070071 bool registerErasedRecipient(const client_cache_t& cacheId,
Marissa Wall947d34e2019-03-29 14:03:53 -070072 const wp<ErasedRecipient>& recipient);
73 void unregisterErasedRecipient(const client_cache_t& cacheId,
74 const wp<ErasedRecipient>& recipient);
75
Robert Carrbeba6f02021-02-10 21:06:27 -080076 void dump(std::string& result);
77
Marissa Wall947d34e2019-03-29 14:03:53 -070078private:
79 std::mutex mMutex;
80
81 struct ClientCacheBuffer {
Alec Mouria90a5702021-04-16 16:36:21 +000082 std::shared_ptr<renderengine::ExternalTexture> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -070083 std::set<wp<ErasedRecipient>> recipients;
84 };
85 std::map<wp<IBinder> /*caching process*/,
Valerie Hau90fcc942019-11-18 11:18:57 -080086 std::pair<sp<IBinder> /*strong ref to caching process*/,
87 std::unordered_map<uint64_t /*cache id*/, ClientCacheBuffer>>>
Marissa Wall947d34e2019-03-29 14:03:53 -070088 mBuffers GUARDED_BY(mMutex);
89
90 class CacheDeathRecipient : public IBinder::DeathRecipient {
91 public:
92 void binderDied(const wp<IBinder>& who) override;
93 };
94
95 sp<CacheDeathRecipient> mDeathRecipient;
Alec Mouria90a5702021-04-16 16:36:21 +000096 renderengine::RenderEngine* mRenderEngine = nullptr;
Marissa Wall947d34e2019-03-29 14:03:53 -070097
98 bool getBuffer(const client_cache_t& cacheId, ClientCacheBuffer** outClientCacheBuffer)
99 REQUIRES(mMutex);
100};
101
102}; // namespace android