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 | |
Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame] | 19 | #include <hardware/gralloc.h> |
Chia-I Wu | 1e29643 | 2017-04-07 21:32:18 -0700 | [diff] [blame] | 20 | #include <hardware/gralloc1.h> |
Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame] | 21 | |
| 22 | void android_convertGralloc0To1Usage(int32_t usage, uint64_t* producerUsage, |
| 23 | uint64_t* consumerUsage) { |
Chia-I Wu | 1e29643 | 2017-04-07 21:32:18 -0700 | [diff] [blame] | 24 | constexpr uint64_t PRODUCER_MASK = |
| 25 | GRALLOC1_PRODUCER_USAGE_CPU_READ | |
| 26 | /* GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN | */ |
| 27 | GRALLOC1_PRODUCER_USAGE_CPU_WRITE | |
| 28 | /* GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN | */ |
| 29 | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET | GRALLOC1_PRODUCER_USAGE_PROTECTED | |
| 30 | GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER | |
| 31 | GRALLOC1_PRODUCER_USAGE_SENSOR_DIRECT_DATA; |
| 32 | constexpr uint64_t CONSUMER_MASK = |
| 33 | GRALLOC1_CONSUMER_USAGE_CPU_READ | |
| 34 | /* GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN | */ |
| 35 | GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE | GRALLOC1_CONSUMER_USAGE_HWCOMPOSER | |
| 36 | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET | GRALLOC1_CONSUMER_USAGE_CURSOR | |
| 37 | GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CAMERA | |
| 38 | GRALLOC1_CONSUMER_USAGE_RENDERSCRIPT | GRALLOC1_CONSUMER_USAGE_GPU_DATA_BUFFER; |
Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame] | 39 | *producerUsage = static_cast<uint64_t>(usage) & PRODUCER_MASK; |
| 40 | *consumerUsage = static_cast<uint64_t>(usage) & CONSUMER_MASK; |
| 41 | if ((static_cast<uint32_t>(usage) & GRALLOC_USAGE_SW_READ_OFTEN) == GRALLOC_USAGE_SW_READ_OFTEN) { |
Chia-I Wu | 1e29643 | 2017-04-07 21:32:18 -0700 | [diff] [blame] | 42 | *producerUsage |= GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN; |
| 43 | *consumerUsage |= GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN; |
Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame] | 44 | } |
| 45 | if ((static_cast<uint32_t>(usage) & GRALLOC_USAGE_SW_WRITE_OFTEN) == |
| 46 | GRALLOC_USAGE_SW_WRITE_OFTEN) { |
Chia-I Wu | 1e29643 | 2017-04-07 21:32:18 -0700 | [diff] [blame] | 47 | *producerUsage |= GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN; |
Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | int32_t android_convertGralloc1To0Usage(uint64_t producerUsage, uint64_t consumerUsage) { |
Chia-I Wu | 1e29643 | 2017-04-07 21:32:18 -0700 | [diff] [blame] | 52 | static_assert(uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) == |
| 53 | uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN), |
Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame] | 54 | "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN bits to match"); |
| 55 | uint64_t merged = producerUsage | consumerUsage; |
Chia-I Wu | 1e29643 | 2017-04-07 21:32:18 -0700 | [diff] [blame] | 56 | if ((merged & (GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN)) == |
| 57 | GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) { |
| 58 | merged &= ~uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN); |
Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame] | 59 | merged |= GRALLOC_USAGE_SW_READ_OFTEN; |
| 60 | } |
Chia-I Wu | 1e29643 | 2017-04-07 21:32:18 -0700 | [diff] [blame] | 61 | if ((merged & (GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) == |
| 62 | GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) { |
| 63 | merged &= ~uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN); |
Jesse Hall | 33eab3a | 2017-03-24 13:16:27 -0700 | [diff] [blame] | 64 | merged |= GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 65 | } |
| 66 | return static_cast<int32_t>(merged); |
| 67 | } |