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