Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017 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 | #include <grallocusage/GrallocUsageConversion.h> |
| 18 | |
| 19 | #include <android/hardware/graphics/allocator/2.0/types.h> |
| 20 | #include <hardware/gralloc.h> |
| 21 | |
| 22 | using android::hardware::graphics::allocator::V2_0::ProducerUsage; |
| 23 | using android::hardware::graphics::allocator::V2_0::ConsumerUsage; |
| 24 | |
| 25 | void android_convertGralloc0To1Usage(int32_t usage, uint64_t* producerUsage, |
| 26 | uint64_t* consumerUsage) { |
| 27 | constexpr uint64_t PRODUCER_MASK = ProducerUsage::CPU_READ | |
| 28 | /* ProducerUsage::CPU_READ_OFTEN | */ |
| 29 | ProducerUsage::CPU_WRITE | |
| 30 | /* ProducerUsage::CPU_WRITE_OFTEN | */ |
| 31 | ProducerUsage::GPU_RENDER_TARGET | ProducerUsage::PROTECTED | |
| 32 | ProducerUsage::CAMERA | ProducerUsage::VIDEO_DECODER | |
| 33 | ProducerUsage::SENSOR_DIRECT_DATA; |
| 34 | constexpr uint64_t CONSUMER_MASK = ConsumerUsage::CPU_READ | |
| 35 | /* ConsumerUsage::CPU_READ_OFTEN | */ |
| 36 | ConsumerUsage::GPU_TEXTURE | ConsumerUsage::HWCOMPOSER | |
| 37 | ConsumerUsage::CLIENT_TARGET | ConsumerUsage::CURSOR | |
| 38 | ConsumerUsage::VIDEO_ENCODER | ConsumerUsage::CAMERA | |
| 39 | ConsumerUsage::RENDERSCRIPT | ConsumerUsage::GPU_DATA_BUFFER; |
| 40 | *producerUsage = static_cast<uint64_t>(usage) & PRODUCER_MASK; |
| 41 | *consumerUsage = static_cast<uint64_t>(usage) & CONSUMER_MASK; |
| 42 | if ((static_cast<uint32_t>(usage) & GRALLOC_USAGE_SW_READ_OFTEN) == GRALLOC_USAGE_SW_READ_OFTEN) { |
| 43 | *producerUsage |= ProducerUsage::CPU_READ_OFTEN; |
| 44 | *consumerUsage |= ConsumerUsage::CPU_READ_OFTEN; |
| 45 | } |
| 46 | if ((static_cast<uint32_t>(usage) & GRALLOC_USAGE_SW_WRITE_OFTEN) == |
| 47 | GRALLOC_USAGE_SW_WRITE_OFTEN) { |
| 48 | *producerUsage |= ProducerUsage::CPU_WRITE_OFTEN; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int32_t android_convertGralloc1To0Usage(uint64_t producerUsage, uint64_t consumerUsage) { |
| 53 | static_assert(uint64_t(ConsumerUsage::CPU_READ_OFTEN) == uint64_t(ProducerUsage::CPU_READ_OFTEN), |
| 54 | "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN bits to match"); |
| 55 | uint64_t merged = producerUsage | consumerUsage; |
| 56 | if ((merged & (ConsumerUsage::CPU_READ_OFTEN)) != 0) { |
| 57 | merged &= ~uint64_t(ConsumerUsage::CPU_READ_OFTEN); |
| 58 | merged |= GRALLOC_USAGE_SW_READ_OFTEN; |
| 59 | } |
| 60 | if ((merged & (ProducerUsage::CPU_WRITE_OFTEN)) != 0) { |
| 61 | merged &= ~uint64_t(ProducerUsage::CPU_WRITE_OFTEN); |
| 62 | merged |= GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 63 | } |
| 64 | return static_cast<int32_t>(merged); |
| 65 | } |