Chia-I Wu | 158d530 | 2016-10-04 06:00:12 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | #ifndef ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_GRALLOC_MAPPER_H |
| 18 | #define ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_GRALLOC_MAPPER_H |
| 19 | |
| 20 | #include <android/hardware/graphics/mapper/2.0/IMapper.h> |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 21 | #include <system/window.h> |
| 22 | |
| 23 | #include <mutex> |
| 24 | #include <unordered_set> |
Chia-I Wu | 158d530 | 2016-10-04 06:00:12 +0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace graphics { |
| 29 | namespace mapper { |
| 30 | namespace V2_0 { |
| 31 | namespace implementation { |
| 32 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 33 | class GrallocMapper : public IMapper { |
| 34 | public: |
| 35 | // IMapper interface |
| 36 | Return<void> createDescriptor(const BufferDescriptorInfo& descriptorInfo, |
| 37 | createDescriptor_cb hidl_cb) override; |
| 38 | Return<void> importBuffer(const hidl_handle& rawHandle, |
| 39 | importBuffer_cb hidl_cb) override; |
| 40 | Return<Error> freeBuffer(void* buffer) override; |
| 41 | Return<void> lock(void* buffer, uint64_t cpuUsage, |
| 42 | const IMapper::Rect& accessRegion, |
| 43 | const hidl_handle& acquireFence, |
| 44 | lock_cb hidl_cb) override; |
| 45 | Return<void> lockYCbCr(void* buffer, uint64_t cpuUsage, |
| 46 | const IMapper::Rect& accessRegion, |
| 47 | const hidl_handle& acquireFence, |
| 48 | lockYCbCr_cb hidl_cb) override; |
| 49 | Return<void> unlock(void* buffer, unlock_cb hidl_cb) override; |
| 50 | |
| 51 | protected: |
| 52 | static void waitFenceFd(int fenceFd, const char* logname); |
| 53 | |
| 54 | struct { |
| 55 | bool highUsageBits; |
| 56 | bool layeredBuffers; |
| 57 | bool unregisterImplyDelete; |
| 58 | } mCapabilities = {}; |
| 59 | |
| 60 | private: |
| 61 | virtual bool validateDescriptorInfo( |
| 62 | const BufferDescriptorInfo& descriptorInfo) const; |
| 63 | |
| 64 | // Register a buffer. The handle is already cloned by the caller. |
| 65 | virtual Error registerBuffer(buffer_handle_t bufferHandle) = 0; |
| 66 | |
| 67 | // Unregister a buffer. The handle is closed and deleted by the |
| 68 | // callee if and only if mCapabilities.unregisterImplyDelete is set. |
| 69 | virtual void unregisterBuffer(buffer_handle_t bufferHandle) = 0; |
| 70 | |
| 71 | // Lock a buffer. The fence is owned by the caller. |
| 72 | virtual Error lockBuffer(buffer_handle_t bufferHandle, uint64_t cpuUsage, |
| 73 | const IMapper::Rect& accessRegion, int fenceFd, |
| 74 | void** outData) = 0; |
| 75 | virtual Error lockBuffer(buffer_handle_t bufferHandle, uint64_t cpuUsage, |
| 76 | const IMapper::Rect& accessRegion, int fenceFd, |
| 77 | YCbCrLayout* outLayout) = 0; |
| 78 | |
| 79 | // Unlock a buffer. The returned fence is owned by the caller. |
| 80 | virtual Error unlockBuffer(buffer_handle_t bufferHandle, |
| 81 | int* outFenceFd) = 0; |
| 82 | |
| 83 | static bool addRegisteredHandle(buffer_handle_t bufferHandle); |
| 84 | static buffer_handle_t getRegisteredHandle(const void* buffer); |
| 85 | static native_handle_t* popRegisteredHandle(void* buffer); |
| 86 | |
| 87 | static bool getFenceFd(const hidl_handle& fenceHandle, int* outFenceFd); |
| 88 | static hidl_handle getFenceHandle(int fenceFd, char* handleStorage); |
| 89 | |
| 90 | // these are static and shared by all mappers |
| 91 | static std::mutex mMutex; |
| 92 | static std::unordered_set<buffer_handle_t> mRegisteredHandles; |
| 93 | }; |
| 94 | |
Chia-I Wu | 158d530 | 2016-10-04 06:00:12 +0800 | [diff] [blame] | 95 | extern "C" IMapper* HIDL_FETCH_IMapper(const char* name); |
| 96 | |
| 97 | } // namespace implementation |
| 98 | } // namespace V2_0 |
| 99 | } // namespace mapper |
| 100 | } // namespace graphics |
| 101 | } // namespace hardware |
| 102 | } // namespace android |
| 103 | |
| 104 | #endif // ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_GRALLOC_MAPPER_H |