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 <hardware/gralloc.h>
20 #include <hardware/gralloc1.h>
21 
android_convertGralloc0To1Usage(int32_t usage,uint64_t * producerUsage,uint64_t * consumerUsage)22 void android_convertGralloc0To1Usage(int32_t usage, uint64_t* producerUsage,
23                                      uint64_t* consumerUsage) {
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;
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) {
42         *producerUsage |= GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN;
43         *consumerUsage |= GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN;
44     }
45     if ((static_cast<uint32_t>(usage) & GRALLOC_USAGE_SW_WRITE_OFTEN) ==
46         GRALLOC_USAGE_SW_WRITE_OFTEN) {
47         *producerUsage |= GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN;
48     }
49 }
50 
android_convertGralloc1To0Usage(uint64_t producerUsage,uint64_t consumerUsage)51 int32_t android_convertGralloc1To0Usage(uint64_t producerUsage, uint64_t consumerUsage) {
52     static_assert(uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
53                       uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN),
54                   "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN bits to match");
55     uint64_t merged = producerUsage | consumerUsage;
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);
59         merged |= GRALLOC_USAGE_SW_READ_OFTEN;
60     }
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);
64         merged |= GRALLOC_USAGE_SW_WRITE_OFTEN;
65     }
66     return static_cast<int32_t>(merged);
67 }
68