blob: ede3feb1c8ca4171db0d0372f7b45ffa0b6c49d2 [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 Wall73411622019-01-25 10:45:41 -080037 void add(sp<IBinder> processToken, int32_t id, const sp<GraphicBuffer>& buffer);
38 sp<GraphicBuffer> get(sp<IBinder> processToken, int32_t id);
39
40 void removeProcess(const wp<IBinder>& processToken);
Marissa Wallebc2c052019-01-16 19:16:55 -080041
42private:
43 std::mutex mMutex;
Marissa Wall73411622019-01-25 10:45:41 -080044 std::map<wp<IBinder> /*caching process*/, std::array<sp<GraphicBuffer>, BUFFER_CACHE_MAX_SIZE>>
45 mBuffers GUARDED_BY(mMutex);
Marissa Wallebc2c052019-01-16 19:16:55 -080046
Marissa Wall73411622019-01-25 10:45:41 -080047 class CacheDeathRecipient : public IBinder::DeathRecipient {
48 public:
49 void binderDied(const wp<IBinder>& who) override;
Marissa Wallebc2c052019-01-16 19:16:55 -080050 };
51
Marissa Wall73411622019-01-25 10:45:41 -080052 sp<CacheDeathRecipient> mDeathRecipient;
Marissa Wallebc2c052019-01-16 19:16:55 -080053};
54
55}; // namespace android