Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 23 | #include <utils/Singleton.h> |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 24 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 25 | #include <array> |
| 26 | #include <map> |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 27 | #include <mutex> |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 28 | |
| 29 | #define BUFFER_CACHE_MAX_SIZE 64 |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 33 | class BufferStateLayerCache : public Singleton<BufferStateLayerCache> { |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 34 | public: |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 35 | BufferStateLayerCache(); |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 36 | |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 37 | 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 Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 41 | |
| 42 | void removeProcess(const wp<IBinder>& processToken); |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 43 | |
| 44 | private: |
| 45 | std::mutex mMutex; |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 46 | std::map<wp<IBinder> /*caching process*/, std::map<uint64_t /*Cache id*/, sp<GraphicBuffer>>> |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 47 | mBuffers GUARDED_BY(mMutex); |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 48 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 49 | class CacheDeathRecipient : public IBinder::DeathRecipient { |
| 50 | public: |
| 51 | void binderDied(const wp<IBinder>& who) override; |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 54 | sp<CacheDeathRecipient> mDeathRecipient; |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | }; // namespace android |