1 /*
2 * Copyright (C) 2018 ARM Limited. All rights reserved.
3 *
4 * Copyright 2016 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #ifndef ANDROID_HARDWARE_GRAPHICS_V2_X_GRALLOCBUFFERDESCRIPTOR_H
20 #define ANDROID_HARDWARE_GRAPHICS_V2_X_GRALLOCBUFFERDESCRIPTOR_H
21
22 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
23 #if HIDL_MAPPER_VERSION_SCALED == 210
24 #include <android/hardware/graphics/mapper/2.1/IMapper.h>
25 #endif
26
27 #include "mali_gralloc_bufferdescriptor.h"
28
29 namespace android {
30 namespace hardware {
31 namespace graphics {
32 namespace mapper {
33 namespace HIDL_IMAPPER_NAMESPACE {
34 namespace implementation {
35
36 /**
37 * BufferDescriptor is created by IMapper and consumed by IAllocator. It is
38 * versioned so that IMapper and IAllocator can be updated independently.
39 * Gralloc creates the same BufferDescriptor configuration across all versions of
40 * IMapper (BufferDescriptorInfo)
41 */
42 constexpr uint32_t grallocBufferDescriptorMagicVersion = HIDL_MAPPER_VERSION_SCALED;
43 constexpr uint32_t grallocBufferDescriptorSize = 7;
44
45 /*
46 * Creates a buffer descriptor from incoming descriptor attributes
47 *
48 * @param descriptorInfo [in] Specifies the (2.1 IMapper) attributes of
49 * the descriptor.
50 *
51 * @return Newly created buffer descriptor;
52 */
53 #if HIDL_MAPPER_VERSION_SCALED == 210
grallocEncodeBufferDescriptor(const V2_1::IMapper::BufferDescriptorInfo & descriptorInfo)54 inline V2_0::BufferDescriptor grallocEncodeBufferDescriptor(
55 const V2_1::IMapper::BufferDescriptorInfo& descriptorInfo)
56 {
57 V2_0::BufferDescriptor descriptor;
58 descriptor.resize(grallocBufferDescriptorSize);
59 descriptor[0] = grallocBufferDescriptorMagicVersion;
60 descriptor[1] = descriptorInfo.width;
61 descriptor[2] = descriptorInfo.height;
62 descriptor[3] = descriptorInfo.layerCount;
63 descriptor[4] = static_cast<uint32_t>(descriptorInfo.format);
64 descriptor[5] = static_cast<uint32_t>(descriptorInfo.usage);
65 descriptor[6] = static_cast<uint32_t>(descriptorInfo.usage >> 32);
66
67 return descriptor;
68 }
69 #endif
70
71 /*
72 * Creates a buffer descriptor from incoming descriptor attributes
73 *
74 * @param descriptorInfo [in] Specifies the (2.0 IMapper) attributes of
75 * the descriptor.
76 *
77 * @return Newly created buffer descriptor;
78 */
grallocEncodeBufferDescriptor(const V2_0::IMapper::BufferDescriptorInfo & descriptorInfo)79 inline V2_0::BufferDescriptor grallocEncodeBufferDescriptor(
80 const V2_0::IMapper::BufferDescriptorInfo& descriptorInfo)
81 {
82 V2_0::BufferDescriptor descriptor;
83 descriptor.resize(grallocBufferDescriptorSize);
84 descriptor[0] = grallocBufferDescriptorMagicVersion;
85 descriptor[1] = descriptorInfo.width;
86 descriptor[2] = descriptorInfo.height;
87 descriptor[3] = descriptorInfo.layerCount;
88 descriptor[4] = static_cast<uint32_t>(descriptorInfo.format);
89 descriptor[5] = static_cast<uint32_t>(descriptorInfo.usage);
90 descriptor[6] = static_cast<uint32_t>(descriptorInfo.usage >> 32);
91
92 return descriptor;
93 }
94
95 /*
96 * Decodes incoming buffer descriptor (previously encoded by gralloc for Android)
97 * and populates Gralloc's internal buffer descriptor (which is used across
98 * various versions of Gralloc) for allocation purpose
99 *
100 * @param androidDescriptor [in] Specifies the (encoded) properties of the buffers
101 * @param grallocDescriptor [out] Gralloc's internal buffer descriptor
102 *
103 * @return true, upon successful decode
104 * false, corrupted descriptor encountered
105 */
grallocDecodeBufferDescriptor(const V2_0::BufferDescriptor & androidDescriptor,buffer_descriptor_t & grallocDescriptor)106 inline bool grallocDecodeBufferDescriptor(const V2_0::BufferDescriptor& androidDescriptor,
107 buffer_descriptor_t& grallocDescriptor)
108 {
109 if (androidDescriptor.size() != grallocBufferDescriptorSize)
110 {
111 AERR("Corrupted buffer size in descriptor = %p, pid = %d ", &androidDescriptor, getpid());
112 return false;
113 }
114
115 if (androidDescriptor[0] != grallocBufferDescriptorMagicVersion)
116 {
117 AERR("Corrupted buffer version in descriptor = %p, pid = %d ", &androidDescriptor, getpid());
118 return false;
119 }
120
121 grallocDescriptor.width = androidDescriptor[1];
122 grallocDescriptor.height = androidDescriptor[2];
123 grallocDescriptor.layer_count = androidDescriptor[3];
124 grallocDescriptor.hal_format = static_cast<uint64_t>(androidDescriptor[4]);
125 grallocDescriptor.producer_usage = (static_cast<uint64_t>(androidDescriptor[6]) << 32) | androidDescriptor[5];
126 grallocDescriptor.consumer_usage = grallocDescriptor.producer_usage;
127 grallocDescriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
128 grallocDescriptor.signature = sizeof(buffer_descriptor_t);
129
130 return true;
131 }
132
133 } // namespace implementation
134 } // namespace HIDL_IMAPPER_NAMESPACE
135 } // namespace mapper
136 } // namespace graphics
137 } // namespace hardware
138 } // namespace android
139
140 #endif // ANDROID_HARDWARE_GRAPHICS_V2_X_GRALLOCBUFFERDESCRIPTOR_H
141