1 /* 2 * Copyright 2016 The Android Open Source Project 3 * * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #pragma once 17 18 #include <hardware/gralloc1.h> 19 #include <mapper-hal/2.1/MapperHal.h> 20 #include <mapper-passthrough/2.0/Gralloc1Hal.h> 21 22 namespace android { 23 namespace hardware { 24 namespace graphics { 25 namespace mapper { 26 namespace V2_1 { 27 namespace passthrough { 28 29 using V2_0::BufferDescriptor; 30 using V2_0::Error; 31 using android::hardware::graphics::common::V1_1::BufferUsage; 32 33 namespace detail { 34 35 // Gralloc1HalImpl implements V2_*::hal::MapperHal on top of gralloc1 36 template <typename Hal> 37 class Gralloc1HalImpl : public V2_0::passthrough::detail::Gralloc1HalImpl<Hal> { 38 public: validateBufferSize(const native_handle_t * bufferHandle,const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t stride)39 Error validateBufferSize(const native_handle_t* bufferHandle, 40 const IMapper::BufferDescriptorInfo& descriptorInfo, 41 uint32_t stride) override { 42 gralloc1_buffer_descriptor_info_t bufferDescriptorInfo; 43 44 bufferDescriptorInfo.width = descriptorInfo.width; 45 bufferDescriptorInfo.height = descriptorInfo.height; 46 bufferDescriptorInfo.layerCount = descriptorInfo.layerCount; 47 bufferDescriptorInfo.format = static_cast<android_pixel_format_t>(descriptorInfo.format); 48 bufferDescriptorInfo.consumerUsage = toConsumerUsage(descriptorInfo.usage); 49 bufferDescriptorInfo.producerUsage = toProducerUsage(descriptorInfo.usage); 50 51 int32_t error = 52 mDispatch.validateBufferSize(mDevice, bufferHandle, &bufferDescriptorInfo, stride); 53 if (error != GRALLOC1_ERROR_NONE) { 54 return toError(error); 55 } 56 57 return Error::NONE; 58 } 59 getTransportSize(const native_handle_t * bufferHandle,uint32_t * outNumFds,uint32_t * outNumInts)60 Error getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds, 61 uint32_t* outNumInts) override { 62 int32_t error = mDispatch.getTransportSize(mDevice, bufferHandle, outNumFds, outNumInts); 63 return toError(error); 64 } 65 importBuffer(const native_handle_t * rawHandle,native_handle_t ** outBufferHandle)66 Error importBuffer(const native_handle_t* rawHandle, 67 native_handle_t** outBufferHandle) override { 68 int32_t error = mDispatch.importBuffer( 69 mDevice, rawHandle, const_cast<const native_handle_t**>(outBufferHandle)); 70 return toError(error); 71 } 72 createDescriptor_2_1(const IMapper::BufferDescriptorInfo & descriptorInfo,BufferDescriptor * outDescriptor)73 Error createDescriptor_2_1(const IMapper::BufferDescriptorInfo& descriptorInfo, 74 BufferDescriptor* outDescriptor) override { 75 if (gralloc1UsageUnsupported(descriptorInfo.usage)) 76 return Error::BAD_DESCRIPTOR; 77 return createDescriptor( 78 V2_0::IMapper::BufferDescriptorInfo{ 79 descriptorInfo.width, descriptorInfo.height, descriptorInfo.layerCount, 80 static_cast<common::V1_0::PixelFormat>(descriptorInfo.format), descriptorInfo.usage, 81 }, 82 outDescriptor); 83 } 84 85 protected: initDispatch()86 bool initDispatch() override { 87 if (!BaseType2_0::initDispatch()) { 88 return false; 89 } 90 91 if (!initDispatch(GRALLOC1_FUNCTION_VALIDATE_BUFFER_SIZE, &mDispatch.validateBufferSize) || 92 !initDispatch(GRALLOC1_FUNCTION_GET_TRANSPORT_SIZE, &mDispatch.getTransportSize) || 93 !initDispatch(GRALLOC1_FUNCTION_IMPORT_BUFFER, &mDispatch.importBuffer)) { 94 return false; 95 } 96 97 return true; 98 } 99 100 struct { 101 GRALLOC1_PFN_VALIDATE_BUFFER_SIZE validateBufferSize; 102 GRALLOC1_PFN_GET_TRANSPORT_SIZE getTransportSize; 103 GRALLOC1_PFN_IMPORT_BUFFER importBuffer; 104 } mDispatch = {}; 105 toProducerUsage(uint64_t usage)106 static uint64_t toProducerUsage(uint64_t usage) { 107 // this is potentially broken as we have no idea which private flags 108 // should be filtered out 109 uint64_t producerUsage = usage & ~static_cast<uint64_t>(BufferUsage::CPU_READ_MASK | 110 BufferUsage::CPU_WRITE_MASK | 111 BufferUsage::GPU_DATA_BUFFER); 112 113 switch (usage & BufferUsage::CPU_WRITE_MASK) { 114 case static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY): 115 producerUsage |= GRALLOC1_PRODUCER_USAGE_CPU_WRITE; 116 break; 117 case static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN): 118 producerUsage |= GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN; 119 break; 120 default: 121 break; 122 } 123 124 switch (usage & BufferUsage::CPU_READ_MASK) { 125 case static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY): 126 producerUsage |= GRALLOC1_PRODUCER_USAGE_CPU_READ; 127 break; 128 case static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN): 129 producerUsage |= GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN; 130 break; 131 default: 132 break; 133 } 134 135 // BufferUsage::GPU_DATA_BUFFER is always filtered out 136 137 return producerUsage; 138 } 139 toConsumerUsage(uint64_t usage)140 static uint64_t toConsumerUsage(uint64_t usage) { 141 // this is potentially broken as we have no idea which private flags 142 // should be filtered out 143 uint64_t consumerUsage = 144 usage & 145 ~static_cast<uint64_t>(BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK | 146 BufferUsage::SENSOR_DIRECT_DATA | BufferUsage::GPU_DATA_BUFFER); 147 148 switch (usage & BufferUsage::CPU_READ_MASK) { 149 case static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY): 150 consumerUsage |= GRALLOC1_CONSUMER_USAGE_CPU_READ; 151 break; 152 case static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN): 153 consumerUsage |= GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN; 154 break; 155 default: 156 break; 157 } 158 159 // BufferUsage::SENSOR_DIRECT_DATA is always filtered out 160 161 if (usage & BufferUsage::GPU_DATA_BUFFER) { 162 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_DATA_BUFFER; 163 } 164 165 return consumerUsage; 166 } 167 gralloc1UsageUnsupported(uint64_t usage)168 static bool gralloc1UsageUnsupported(uint64_t usage) { 169 // Certain newer public usage bits should not be used with gralloc1. 170 // We use a blacklist instead of a whitelist here in order to avoid 171 // breaking private usage flags. 172 constexpr uint64_t unsupportedMask = BufferUsage::GPU_CUBE_MAP | 173 BufferUsage::GPU_MIPMAP_COMPLETE; 174 175 return usage & unsupportedMask; 176 } 177 178 private: 179 using BaseType2_0 = V2_0::passthrough::detail::Gralloc1HalImpl<Hal>; 180 using BaseType2_0::createDescriptor; 181 using BaseType2_0::initDispatch; 182 using BaseType2_0::mDevice; 183 using BaseType2_0::toError; 184 }; 185 186 } // namespace detail 187 188 using Gralloc1Hal = detail::Gralloc1HalImpl<hal::MapperHal>; 189 190 } // namespace passthrough 191 } // namespace V2_1 192 } // namespace mapper 193 } // namespace graphics 194 } // namespace hardware 195 } // namespace android 196