• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #ifndef ANDROID_HARDWARD_HIDLMEMORY_CACHE_H
17 #define ANDROID_HARDWARD_HIDLMEMORY_CACHE_H
18 
19 #include <android/hidl/memory/block/1.0/types.h>
20 #include <android/hidl/memory/token/1.0/IMemoryToken.h>
21 #include <hidl/HidlBinderSupport.h>
22 #include <hwbinder/IBinder.h>
23 #include <utils/RefBase.h>
24 #include "HidlCache.h"
25 
26 namespace android {
27 namespace hardware {
28 
29 struct IMemoryTokenCompare {
30     using IMemoryToken = ::android::hidl::memory::token::V1_0::IMemoryToken;
operatorIMemoryTokenCompare31     bool operator()(const sp<IMemoryToken>& lhs, const sp<IMemoryToken>& rhs) const {
32         sp<IBinder> lb = toBinder<IMemoryToken>(lhs);
33         sp<IBinder> rb = toBinder<IMemoryToken>(rhs);
34         return lb < rb;
35     }
36 };
37 
38 // The HidlMemoryCache is a singleton class to provides cache for
39 // IMemoryToken => ::android::hidl::memory::V1_0::IMemory
40 // It's an abstraction layer on top of the IMapper and supports, but is
41 // not limited to, the Ashmem type HidlMemory.
42 class HidlMemoryCache
43     : public virtual HidlCache<sp<::android::hidl::memory::token::V1_0::IMemoryToken>,
44                                ::android::hidl::memory::V1_0::IMemory, IMemoryTokenCompare> {
45     using IMemoryToken = ::android::hidl::memory::token::V1_0::IMemoryToken;
46     using IMemory = ::android::hidl::memory::V1_0::IMemory;
47     using MemoryBlock = ::android::hidl::memory::block::V1_0::MemoryBlock;
48 
49    public:
50     virtual sp<IMemory> map(const MemoryBlock& block);
51     // get the singleton
52     static sp<HidlMemoryCache> getInstance();
53 
54    protected:
HidlMemoryCache()55     HidlMemoryCache() {}
56     virtual sp<IMemory> fillLocked(const sp<IMemoryToken>& key) override;
57 };
58 
59 }  // namespace hardware
60 }  // namespace android
61 
62 #endif
63