blob: 415c09cf96bfbe55863264db72d529d7150e67c1 [file] [log] [blame]
Marissa Wallebc2c052019-01-16 19:16:55 -08001/*
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 <ui/GraphicBuffer.h>
22#include <utils/RefBase.h>
Marissa Wall73411622019-01-25 10:45:41 -080023#include <utils/Singleton.h>
Marissa Wallebc2c052019-01-16 19:16:55 -080024
Marissa Wall73411622019-01-25 10:45:41 -080025#include <array>
26#include <map>
Marissa Wallebc2c052019-01-16 19:16:55 -080027#include <mutex>
Marissa Wall73411622019-01-25 10:45:41 -080028
29#define BUFFER_CACHE_MAX_SIZE 64
Marissa Wallebc2c052019-01-16 19:16:55 -080030
31namespace android {
32
Marissa Wall73411622019-01-25 10:45:41 -080033class BufferStateLayerCache : public Singleton<BufferStateLayerCache> {
Marissa Wallebc2c052019-01-16 19:16:55 -080034public:
Marissa Wall73411622019-01-25 10:45:41 -080035 BufferStateLayerCache();
Marissa Wallebc2c052019-01-16 19:16:55 -080036
Marissa Wall78b72202019-03-15 14:58:34 -070037 void add(const sp<IBinder>& processToken, uint64_t id, const sp<GraphicBuffer>& buffer);
38 void erase(const sp<IBinder>& processToken, uint64_t id);
39
40 sp<GraphicBuffer> get(const sp<IBinder>& processToken, uint64_t id);
Marissa Wall73411622019-01-25 10:45:41 -080041
42 void removeProcess(const wp<IBinder>& processToken);
Marissa Wallebc2c052019-01-16 19:16:55 -080043
44private:
45 std::mutex mMutex;
Marissa Wall78b72202019-03-15 14:58:34 -070046 std::map<wp<IBinder> /*caching process*/, std::map<uint64_t /*Cache id*/, sp<GraphicBuffer>>>
Marissa Wall73411622019-01-25 10:45:41 -080047 mBuffers GUARDED_BY(mMutex);
Marissa Wallebc2c052019-01-16 19:16:55 -080048
Marissa Wall73411622019-01-25 10:45:41 -080049 class CacheDeathRecipient : public IBinder::DeathRecipient {
50 public:
51 void binderDied(const wp<IBinder>& who) override;
Marissa Wallebc2c052019-01-16 19:16:55 -080052 };
53
Marissa Wall73411622019-01-25 10:45:41 -080054 sp<CacheDeathRecipient> mDeathRecipient;
Marissa Wallebc2c052019-01-16 19:16:55 -080055};
56
57}; // namespace android