1/*
2 * Copyright 2016 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#pragma once
18
19#include <android/hardware/graphics/mapper/2.0/IMapper.h>
20
21namespace android {
22namespace hardware {
23namespace graphics {
24namespace mapper {
25namespace V2_0 {
26namespace passthrough {
27
28using android::hardware::graphics::common::V1_0::PixelFormat;
29
30/**
31 * BufferDescriptor is created by IMapper and consumed by IAllocator. It is
32 * versioned so that IMapper and IAllocator can be updated independently.
33 */
34constexpr uint32_t grallocBufferDescriptorSize = 7;
35constexpr uint32_t grallocBufferDescriptorMagicVersion = ((0x9487 << 16) | 0);
36
37inline BufferDescriptor grallocEncodeBufferDescriptor(
38    const IMapper::BufferDescriptorInfo& descriptorInfo) {
39    BufferDescriptor descriptor;
40    descriptor.resize(grallocBufferDescriptorSize);
41    descriptor[0] = grallocBufferDescriptorMagicVersion;
42    descriptor[1] = descriptorInfo.width;
43    descriptor[2] = descriptorInfo.height;
44    descriptor[3] = descriptorInfo.layerCount;
45    descriptor[4] = static_cast<uint32_t>(descriptorInfo.format);
46    descriptor[5] = static_cast<uint32_t>(descriptorInfo.usage);
47    descriptor[6] = static_cast<uint32_t>(descriptorInfo.usage >> 32);
48
49    return descriptor;
50}
51
52inline bool grallocDecodeBufferDescriptor(const BufferDescriptor& descriptor,
53                                          IMapper::BufferDescriptorInfo* outDescriptorInfo) {
54    if (descriptor.size() != grallocBufferDescriptorSize ||
55        descriptor[0] != grallocBufferDescriptorMagicVersion) {
56        return false;
57    }
58
59    *outDescriptorInfo = IMapper::BufferDescriptorInfo{
60        descriptor[1],
61        descriptor[2],
62        descriptor[3],
63        static_cast<PixelFormat>(descriptor[4]),
64        (static_cast<uint64_t>(descriptor[6]) << 32) | descriptor[5],
65    };
66
67    return true;
68}
69
70}  // namespace passthrough
71}  // namespace V2_0
72}  // namespace mapper
73}  // namespace graphics
74}  // namespace hardware
75}  // namespace android
76