| 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 |  | 
 | 44 |     // this will be removed and Mapper will be always valid | 
 | 45 |     bool valid() const { return (mMapper != nullptr); } | 
 | 46 |  | 
 | 47 |     Error createDescriptor( | 
 | 48 |             const IMapper::BufferDescriptorInfo& descriptorInfo, | 
 | 49 |             BufferDescriptor* outDescriptor) const; | 
 | 50 |  | 
 | 51 |     // Import a buffer that is from another HAL, another process, or is | 
 | 52 |     // cloned. | 
 | 53 |     // | 
 | 54 |     // The returned handle must be freed with freeBuffer. | 
 | 55 |     Error importBuffer(const hardware::hidl_handle& rawHandle, | 
 | 56 |             buffer_handle_t* outBufferHandle) const; | 
 | 57 |  | 
 | 58 |     void freeBuffer(buffer_handle_t bufferHandle) const; | 
 | 59 |  | 
 | 60 |     // The ownership of acquireFence is always transferred to the callee, even | 
 | 61 |     // on errors. | 
 | 62 |     Error lock(buffer_handle_t bufferHandle, uint64_t usage, | 
 | 63 |             const IMapper::Rect& accessRegion, | 
 | 64 |             int acquireFence, void** outData) const; | 
 | 65 |  | 
 | 66 |     // The ownership of acquireFence is always transferred to the callee, even | 
 | 67 |     // on errors. | 
 | 68 |     Error lock(buffer_handle_t bufferHandle, uint64_t usage, | 
 | 69 |             const IMapper::Rect& accessRegion, | 
 | 70 |             int acquireFence, YCbCrLayout* outLayout) const; | 
 | 71 |  | 
 | 72 |     // unlock returns a fence sync object (or -1) and the fence sync object is | 
 | 73 |     // owned by the caller | 
 | 74 |     int unlock(buffer_handle_t bufferHandle) const; | 
 | 75 |  | 
 | 76 | private: | 
 | 77 |     sp<IMapper> mMapper; | 
 | 78 | }; | 
 | 79 |  | 
 | 80 | // A wrapper to IAllocator | 
 | 81 | class Allocator { | 
 | 82 | public: | 
 | 83 |     // An allocator relies on a mapper, and that mapper must be alive at all | 
 | 84 |     // time. | 
 | 85 |     Allocator(const Mapper& mapper); | 
 | 86 |  | 
 | 87 |     // this will be removed and Allocator will be always valid | 
 | 88 |     bool valid() const { return (mAllocator != nullptr); } | 
 | 89 |  | 
 | 90 |     std::string dumpDebugInfo() const; | 
 | 91 |  | 
 | 92 |     /* | 
 | 93 |      * The returned buffers are already imported and must not be imported | 
 | 94 |      * again.  outBufferHandles must point to a space that can contain at | 
 | 95 |      * least "count" buffer_handle_t. | 
 | 96 |      */ | 
 | 97 |     Error allocate(BufferDescriptor descriptor, uint32_t count, | 
 | 98 |             uint32_t* outStride, buffer_handle_t* outBufferHandles) const; | 
 | 99 |  | 
 | 100 |     Error allocate(BufferDescriptor descriptor, | 
 | 101 |             uint32_t* outStride, buffer_handle_t* outBufferHandle) const | 
 | 102 |     { | 
 | 103 |         return allocate(descriptor, 1, outStride, outBufferHandle); | 
 | 104 |     } | 
 | 105 |  | 
 | 106 |     Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t count, | 
 | 107 |             uint32_t* outStride, buffer_handle_t* outBufferHandles) const | 
 | 108 |     { | 
 | 109 |         BufferDescriptor descriptor; | 
 | 110 |         Error error = mMapper.createDescriptor(descriptorInfo, &descriptor); | 
 | 111 |         if (error == Error::NONE) { | 
 | 112 |             error = allocate(descriptor, count, outStride, outBufferHandles); | 
 | 113 |         } | 
 | 114 |         return error; | 
 | 115 |     } | 
 | 116 |  | 
 | 117 |     Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, | 
 | 118 |             uint32_t* outStride, buffer_handle_t* outBufferHandle) const | 
 | 119 |     { | 
 | 120 |         return allocate(descriptorInfo, 1, outStride, outBufferHandle); | 
 | 121 |     } | 
 | 122 |  | 
 | 123 | private: | 
 | 124 |     const Mapper& mMapper; | 
 | 125 |     sp<IAllocator> mAllocator; | 
 | 126 | }; | 
 | 127 |  | 
 | 128 | } // namespace Gralloc2 | 
 | 129 |  | 
 | 130 | } // namespace android | 
 | 131 |  | 
 | 132 | #endif // ANDROID_UI_GRALLOC2_H |