blob: fefc04054dd6b5db23da836dc0b76a2de2e08021 [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
Patrick Williams271afa92023-08-09 18:30:22 -050032// 4096 is based on 64 buffers * 64 layers. Once this limit is reached, the least recently used
33// buffer is uncached before the new buffer is cached.
34#define BUFFER_CACHE_MAX_SIZE 4096
Marissa Wall947d34e2019-03-29 14:03:53 -070035
36namespace android {
37
Brian Lindahl439afad2022-11-14 11:16:55 -070038// This class manages a cache of buffer handles between SurfaceFlinger clients
39// and the SurfaceFlinger process which optimizes away some of the cost of
40// sending buffer handles across processes.
41//
42// Buffers are explicitly cached and uncached by the SurfaceFlinger client. When
43// a buffer is uncached, it is not only purged from this cache, but the buffer
44// ID is also passed down to CompositionEngine to purge it from a similar cache
45// used between SurfaceFlinger and Composer HAL. The buffer ID used to purge
46// both the SurfaceFlinger side of this other cache, as well as Composer HAL's
47// side of the cache.
48//
Marissa Wall947d34e2019-03-29 14:03:53 -070049class ClientCache : public Singleton<ClientCache> {
50public:
51 ClientCache();
52
Patrick Williamsf1e5df12022-10-17 21:37:42 +000053 enum class AddError { CacheFull, Unspecified };
54
55 base::expected<std::shared_ptr<renderengine::ExternalTexture>, AddError> add(
56 const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer);
Brian Lindahl439afad2022-11-14 11:16:55 -070057
58 sp<GraphicBuffer> erase(const client_cache_t& cacheId);
Marissa Wall947d34e2019-03-29 14:03:53 -070059
Alec Mouria90a5702021-04-16 16:36:21 +000060 std::shared_ptr<renderengine::ExternalTexture> get(const client_cache_t& cacheId);
61
62 // Always called immediately after setup. Will be set to non-null, and then should never be
63 // called again.
64 void setRenderEngine(renderengine::RenderEngine* renderEngine) { mRenderEngine = renderEngine; }
Marissa Wall947d34e2019-03-29 14:03:53 -070065
66 void removeProcess(const wp<IBinder>& processToken);
67
68 class ErasedRecipient : public virtual RefBase {
69 public:
70 virtual void bufferErased(const client_cache_t& clientCacheId) = 0;
71 };
72
Alec Mouri4545a8a2019-08-08 20:05:32 -070073 bool registerErasedRecipient(const client_cache_t& cacheId,
Marissa Wall947d34e2019-03-29 14:03:53 -070074 const wp<ErasedRecipient>& recipient);
75 void unregisterErasedRecipient(const client_cache_t& cacheId,
76 const wp<ErasedRecipient>& recipient);
77
Robert Carrbeba6f02021-02-10 21:06:27 -080078 void dump(std::string& result);
79
Marissa Wall947d34e2019-03-29 14:03:53 -070080private:
81 std::mutex mMutex;
82
83 struct ClientCacheBuffer {
Alec Mouria90a5702021-04-16 16:36:21 +000084 std::shared_ptr<renderengine::ExternalTexture> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -070085 std::set<wp<ErasedRecipient>> recipients;
86 };
87 std::map<wp<IBinder> /*caching process*/,
Valerie Hau90fcc942019-11-18 11:18:57 -080088 std::pair<sp<IBinder> /*strong ref to caching process*/,
89 std::unordered_map<uint64_t /*cache id*/, ClientCacheBuffer>>>
Marissa Wall947d34e2019-03-29 14:03:53 -070090 mBuffers GUARDED_BY(mMutex);
91
92 class CacheDeathRecipient : public IBinder::DeathRecipient {
93 public:
94 void binderDied(const wp<IBinder>& who) override;
95 };
96
97 sp<CacheDeathRecipient> mDeathRecipient;
Alec Mouria90a5702021-04-16 16:36:21 +000098 renderengine::RenderEngine* mRenderEngine = nullptr;
Marissa Wall947d34e2019-03-29 14:03:53 -070099
100 bool getBuffer(const client_cache_t& cacheId, ClientCacheBuffer** outClientCacheBuffer)
101 REQUIRES(mMutex);
102};
103
104}; // namespace android