| 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> | 
|  | 23 |  | 
|  | 24 | #include <mutex> | 
|  | 25 | #include <unordered_map> | 
|  | 26 | #include <vector> | 
|  | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | class BufferStateLayerCache { | 
|  | 31 | public: | 
|  | 32 | int32_t add(const sp<IBinder>& processToken, const sp<GraphicBuffer>& buffer); | 
|  | 33 | void release(const sp<IBinder>& processToken, int32_t id); | 
|  | 34 |  | 
|  | 35 | sp<GraphicBuffer> get(const sp<IBinder>& processToken, int32_t id); | 
|  | 36 |  | 
|  | 37 | private: | 
|  | 38 | std::mutex mMutex; | 
|  | 39 |  | 
|  | 40 | std::vector<sp<GraphicBuffer>>& getProccessCache(const sp<IBinder>& processToken) | 
|  | 41 | REQUIRES(mMutex); | 
|  | 42 |  | 
|  | 43 | int32_t findSlot(std::vector<sp<GraphicBuffer>>& proccessCache) REQUIRES(mMutex); | 
|  | 44 |  | 
|  | 45 | struct IBinderHash { | 
|  | 46 | std::size_t operator()(const sp<IBinder>& strongPointer) const { | 
|  | 47 | return std::hash<IBinder*>{}(strongPointer.get()); | 
|  | 48 | } | 
|  | 49 | }; | 
|  | 50 |  | 
|  | 51 | std::unordered_map<sp<IBinder> /*caching process*/, std::vector<sp<GraphicBuffer>>, IBinderHash> | 
|  | 52 | mBuffers GUARDED_BY(mMutex); | 
|  | 53 | }; | 
|  | 54 |  | 
|  | 55 | }; // namespace android |