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 #ifndef ANDROID_UI_GRALLOC2_H 17 #define ANDROID_UI_GRALLOC2_H 18 #include <string> 19 #include <cutils/native_handle.h> 20 #include <utils/StrongPointer.h> 21 #include <stdint.h> 22 #include <ui/PixelFormat.h> 23 24 namespace android { 25 struct YCbCrLayout { 26 void* y; 27 void* cb; 28 void* cr; 29 uint32_t yStride; 30 uint32_t cStride; 31 uint32_t chromaStep; 32 }; 33 34 namespace hardware { 35 typedef buffer_handle_t hidl_handle; 36 } // namespace hardware 37 38 namespace Gralloc2 { 39 40 using android::PixelFormat; 41 using android::YCbCrLayout; 42 43 namespace IMapper { 44 45 struct BufferDescriptorInfo { 46 uint32_t width; 47 uint32_t height; 48 uint32_t layerCount; 49 uint32_t format; 50 uint32_t usage; 51 }; 52 53 struct Rect { 54 int32_t left; 55 int32_t top; 56 int32_t width; 57 int32_t height; 58 }; 59 60 } // namespace IMapper 61 62 typedef IMapper::BufferDescriptorInfo* BufferDescriptor; 63 64 enum Error { 65 NONE = 0, 66 BAD_VALUE = -22, 67 NO_RESOURCES = -12, 68 }; 69 70 // A wrapper to IMapper 71 class Mapper { 72 public: 73 static void preload(); 74 Mapper(); 75 Error createDescriptor( 76 const IMapper::BufferDescriptorInfo& descriptorInfo, 77 BufferDescriptor* outDescriptor) const; 78 // Import a buffer that is from another HAL, another process, or is 79 // cloned. 80 // 81 // The returned handle must be freed with freeBuffer. 82 Error importBuffer(const hardware::hidl_handle& rawHandle, 83 buffer_handle_t* outBufferHandle) const; 84 void freeBuffer(buffer_handle_t bufferHandle) const; 85 Error validateBufferSize(buffer_handle_t bufferHandle, 86 const IMapper::BufferDescriptorInfo& descriptorInfo, 87 uint32_t stride) const; 88 void getTransportSize(buffer_handle_t bufferHandle, 89 uint32_t* outNumFds, uint32_t* outNumInts) const; 90 // The ownership of acquireFence is always transferred to the callee, even 91 // on errors. 92 Error lock(buffer_handle_t bufferHandle, uint64_t usage, 93 const IMapper::Rect& accessRegion, 94 int acquireFence, void** outData) const; 95 // The ownership of acquireFence is always transferred to the callee, even 96 // on errors. 97 Error lock(buffer_handle_t bufferHandle, uint64_t usage, 98 const IMapper::Rect& accessRegion, 99 int acquireFence, YCbCrLayout* outLayout) const; 100 // unlock returns a fence sync object (or -1) and the fence sync object is 101 // owned by the caller 102 int unlock(buffer_handle_t bufferHandle) const; 103 private: 104 // Determines whether the passed info is compatible with the mapper. 105 Error validateBufferDescriptorInfo( 106 const IMapper::BufferDescriptorInfo& descriptorInfo) const; 107 }; 108 109 // A wrapper to IAllocator 110 class Allocator { 111 public: 112 // An allocator relies on a mapper, and that mapper must be alive at all 113 // time. 114 Allocator(const Mapper& mapper); 115 std::string dumpDebugInfo() const; 116 /* 117 * The returned buffers are already imported and must not be imported 118 * again. outBufferHandles must point to a space that can contain at 119 * least "count" buffer_handle_t. 120 */ 121 Error allocate(BufferDescriptor descriptor, uint32_t count, 122 uint32_t* outStride, buffer_handle_t* outBufferHandles) const; allocate(BufferDescriptor descriptor,uint32_t * outStride,buffer_handle_t * outBufferHandle)123 Error allocate(BufferDescriptor descriptor, 124 uint32_t* outStride, buffer_handle_t* outBufferHandle) const 125 { 126 return allocate(descriptor, 1, outStride, outBufferHandle); 127 } allocate(const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t count,uint32_t * outStride,buffer_handle_t * outBufferHandles)128 Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t count, 129 uint32_t* outStride, buffer_handle_t* outBufferHandles) const 130 { 131 BufferDescriptor descriptor; 132 Error error = mMapper.createDescriptor(descriptorInfo, &descriptor); 133 if (error == Error::NONE) { 134 error = allocate(descriptor, count, outStride, outBufferHandles); 135 } 136 return error; 137 } allocate(const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t * outStride,buffer_handle_t * outBufferHandle)138 Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, 139 uint32_t* outStride, buffer_handle_t* outBufferHandle) const 140 { 141 return allocate(descriptorInfo, 1, outStride, outBufferHandle); 142 } 143 private: 144 const Mapper& mMapper; 145 }; 146 } // namespace Gralloc2 147 } // namespace android 148 #endif // ANDROID_UI_GRALLOC2_H 149