| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [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_UI_GRALLOC2_H | 
|  | 18 | #define ANDROID_UI_GRALLOC2_H | 
|  | 19 |  | 
|  | 20 | #include <string> | 
|  | 21 |  | 
|  | 22 | #include <android/hardware/graphics/allocator/2.0/IAllocator.h> | 
|  | 23 | #include <android/hardware/graphics/mapper/2.0/IMapper.h> | 
|  | 24 | #include <system/window.h> | 
|  | 25 | #include <utils/StrongPointer.h> | 
|  | 26 |  | 
|  | 27 | namespace android { | 
|  | 28 |  | 
|  | 29 | namespace Gralloc2 { | 
|  | 30 |  | 
|  | 31 | using hardware::graphics::allocator::V2_0::IAllocator; | 
|  | 32 | using hardware::graphics::common::V1_0::BufferUsage; | 
|  | 33 | using hardware::graphics::common::V1_0::PixelFormat; | 
|  | 34 | using hardware::graphics::mapper::V2_0::BufferDescriptor; | 
|  | 35 | using hardware::graphics::mapper::V2_0::Error; | 
|  | 36 | using hardware::graphics::mapper::V2_0::IMapper; | 
|  | 37 | using hardware::graphics::mapper::V2_0::YCbCrLayout; | 
|  | 38 |  | 
|  | 39 | // A wrapper to IMapper | 
|  | 40 | class Mapper { | 
|  | 41 | public: | 
|  | 42 | Mapper(); | 
|  | 43 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 44 | Error createDescriptor( | 
|  | 45 | const IMapper::BufferDescriptorInfo& descriptorInfo, | 
|  | 46 | BufferDescriptor* outDescriptor) const; | 
|  | 47 |  | 
|  | 48 | // Import a buffer that is from another HAL, another process, or is | 
|  | 49 | // cloned. | 
|  | 50 | // | 
|  | 51 | // The returned handle must be freed with freeBuffer. | 
|  | 52 | Error importBuffer(const hardware::hidl_handle& rawHandle, | 
|  | 53 | buffer_handle_t* outBufferHandle) const; | 
|  | 54 |  | 
|  | 55 | void freeBuffer(buffer_handle_t bufferHandle) const; | 
|  | 56 |  | 
|  | 57 | // The ownership of acquireFence is always transferred to the callee, even | 
|  | 58 | // on errors. | 
|  | 59 | Error lock(buffer_handle_t bufferHandle, uint64_t usage, | 
|  | 60 | const IMapper::Rect& accessRegion, | 
|  | 61 | int acquireFence, void** outData) const; | 
|  | 62 |  | 
|  | 63 | // The ownership of acquireFence is always transferred to the callee, even | 
|  | 64 | // on errors. | 
|  | 65 | Error lock(buffer_handle_t bufferHandle, uint64_t usage, | 
|  | 66 | const IMapper::Rect& accessRegion, | 
|  | 67 | int acquireFence, YCbCrLayout* outLayout) const; | 
|  | 68 |  | 
|  | 69 | // unlock returns a fence sync object (or -1) and the fence sync object is | 
|  | 70 | // owned by the caller | 
|  | 71 | int unlock(buffer_handle_t bufferHandle) const; | 
|  | 72 |  | 
|  | 73 | private: | 
|  | 74 | sp<IMapper> mMapper; | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 | // A wrapper to IAllocator | 
|  | 78 | class Allocator { | 
|  | 79 | public: | 
|  | 80 | // An allocator relies on a mapper, and that mapper must be alive at all | 
|  | 81 | // time. | 
|  | 82 | Allocator(const Mapper& mapper); | 
|  | 83 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 84 | std::string dumpDebugInfo() const; | 
|  | 85 |  | 
|  | 86 | /* | 
|  | 87 | * The returned buffers are already imported and must not be imported | 
|  | 88 | * again.  outBufferHandles must point to a space that can contain at | 
|  | 89 | * least "count" buffer_handle_t. | 
|  | 90 | */ | 
|  | 91 | Error allocate(BufferDescriptor descriptor, uint32_t count, | 
|  | 92 | uint32_t* outStride, buffer_handle_t* outBufferHandles) const; | 
|  | 93 |  | 
|  | 94 | Error allocate(BufferDescriptor descriptor, | 
|  | 95 | uint32_t* outStride, buffer_handle_t* outBufferHandle) const | 
|  | 96 | { | 
|  | 97 | return allocate(descriptor, 1, outStride, outBufferHandle); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t count, | 
|  | 101 | uint32_t* outStride, buffer_handle_t* outBufferHandles) const | 
|  | 102 | { | 
|  | 103 | BufferDescriptor descriptor; | 
|  | 104 | Error error = mMapper.createDescriptor(descriptorInfo, &descriptor); | 
|  | 105 | if (error == Error::NONE) { | 
|  | 106 | error = allocate(descriptor, count, outStride, outBufferHandles); | 
|  | 107 | } | 
|  | 108 | return error; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, | 
|  | 112 | uint32_t* outStride, buffer_handle_t* outBufferHandle) const | 
|  | 113 | { | 
|  | 114 | return allocate(descriptorInfo, 1, outStride, outBufferHandle); | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | private: | 
|  | 118 | const Mapper& mMapper; | 
|  | 119 | sp<IAllocator> mAllocator; | 
|  | 120 | }; | 
|  | 121 |  | 
|  | 122 | } // namespace Gralloc2 | 
|  | 123 |  | 
|  | 124 | } // namespace android | 
|  | 125 |  | 
|  | 126 | #endif // ANDROID_UI_GRALLOC2_H |