aboutsummaryrefslogtreecommitdiff
path: root/guest/vulkan_enc/AndroidHardwareBuffer.cpp
blob: c5984b356da6c85c39faad2fcec4af5172e2585a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/// Copyright (C) 2019 The Android Open Source Project
// Copyright (C) 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "AndroidHardwareBuffer.h"

#if defined(__ANDROID__) || defined(__linux__)
#include <drm_fourcc.h>
#define DRM_FORMAT_YVU420_ANDROID fourcc_code('9', '9', '9', '7')
#endif

#include <assert.h>
#include <log/log.h>

#include "gfxstream/guest/Gralloc.h"
#include "vk_format_info.h"
#include "vk_util.h"

namespace gfxstream {
namespace vk {

// From Intel ANV implementation.
/* Construct ahw usage mask from image usage bits, see
 * 'AHardwareBuffer Usage Equivalence' in Vulkan spec.
 */
uint64_t getAndroidHardwareBufferUsageFromVkUsage(const VkImageCreateFlags vk_create,
                                                  const VkImageUsageFlags vk_usage) {
    uint64_t ahw_usage = 0;

    if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT) ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;

    if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
        ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;

    if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
        ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;

    if (vk_create & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
        ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP;

    if (vk_create & VK_IMAGE_CREATE_PROTECTED_BIT)
        ahw_usage |= AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT;

    /* No usage bits set - set at least one GPU usage. */
    if (ahw_usage == 0) ahw_usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;

    return ahw_usage;
}

VkResult getAndroidHardwareBufferPropertiesANDROID(
    gfxstream::Gralloc* grallocHelper, const AHardwareBuffer* buffer,
    VkAndroidHardwareBufferPropertiesANDROID* pProperties) {
    VkAndroidHardwareBufferFormatPropertiesANDROID* ahbFormatProps =
        vk_find_struct<VkAndroidHardwareBufferFormatPropertiesANDROID>(pProperties);

    const auto format = grallocHelper->getFormat(buffer);
    if (ahbFormatProps) {
        switch (format) {
            case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
                ahbFormatProps->format = VK_FORMAT_R8G8B8A8_UNORM;
                break;
            case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
                ahbFormatProps->format = VK_FORMAT_R8G8B8A8_UNORM;
                break;
            case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
                ahbFormatProps->format = VK_FORMAT_R8G8B8_UNORM;
                break;
            case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
                ahbFormatProps->format = VK_FORMAT_R5G6B5_UNORM_PACK16;
                break;
            case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
                ahbFormatProps->format = VK_FORMAT_R16G16B16A16_SFLOAT;
                break;
            case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
                ahbFormatProps->format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
                break;
            case AHARDWAREBUFFER_FORMAT_D16_UNORM:
                ahbFormatProps->format = VK_FORMAT_D16_UNORM;
                break;
            case AHARDWAREBUFFER_FORMAT_D24_UNORM:
                ahbFormatProps->format = VK_FORMAT_X8_D24_UNORM_PACK32;
                break;
            case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT:
                ahbFormatProps->format = VK_FORMAT_D24_UNORM_S8_UINT;
                break;
            case AHARDWAREBUFFER_FORMAT_D32_FLOAT:
                ahbFormatProps->format = VK_FORMAT_D32_SFLOAT;
                break;
            case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT:
                ahbFormatProps->format = VK_FORMAT_D32_SFLOAT_S8_UINT;
                break;
            case AHARDWAREBUFFER_FORMAT_S8_UINT:
                ahbFormatProps->format = VK_FORMAT_S8_UINT;
                break;
            default:
                ahbFormatProps->format = VK_FORMAT_UNDEFINED;
        }
        ahbFormatProps->externalFormat = format;

        // The formatFeatures member must include
        // VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and at least one of
        // VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT or
        // VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT, and should include
        // VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT and
        // VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT.

        // org.skia.skqp.SkQPRunner#UnitTest_VulkanHardwareBuffer* requires the following:
        // VK_FORMAT_FEATURE_TRANSFER_SRC_BIT
        // VK_FORMAT_FEATURE_TRANSFER_DST_BIT
        // VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
        ahbFormatProps->formatFeatures =
            VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT |
            VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT |
            VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;

        // "Implementations may not always be able to determine the color model,
        // numerical range, or chroma offsets of the image contents, so the values in
        // VkAndroidHardwareBufferFormatPropertiesANDROID are only suggestions.
        // Applications should treat these values as sensible defaults to use in the
        // absence of more reliable information obtained through some other means."

        ahbFormatProps->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY;
        ahbFormatProps->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY;
        ahbFormatProps->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY;
        ahbFormatProps->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY;

#if defined(__ANDROID__) || defined(__linux__)
        if (android_format_is_yuv(format)) {
            uint32_t drmFormat = grallocHelper->getFormatDrmFourcc(buffer);
            if (drmFormat) {
                // The host renderer is not aware of the plane ordering for YUV formats used
                // in the guest and simply knows that the format "layout" is one of:
                //
                //  * VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16
                //  * VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
                //  * VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM
                //
                // With this, the guest needs to adjust the component swizzle based on plane
                // ordering to ensure that the channels are interpreted correctly.
                //
                // From the Vulkan spec's "Sampler Y'CBCR Conversion" section:
                //
                //  * Y comes from the G-channel (after swizzle)
                //  * U (CB) comes from the B-channel (after swizzle)
                //  * V (CR) comes from the R-channel (after swizzle)
                //
                // See
                // https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/vkspec.html#textures-sampler-YCbCr-conversion
                //
                // To match the above, the guest needs to swizzle such that:
                //
                //  * Y ends up in the G-channel
                //  * U (CB) ends up in the B-channel
                //  * V (CB) ends up in the R-channel
                switch (drmFormat) {
                    case DRM_FORMAT_NV12:
                        // NV12 is a Y-plane followed by a interleaved UV-plane and is
                        // VK_FORMAT_G8_B8R8_2PLANE_420_UNORM on the host.
                    case DRM_FORMAT_P010:
                        // P010 is a Y-plane followed by a interleaved UV-plane and is
                        // VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16 on the host.
                        break;

                    case DRM_FORMAT_NV21:
                        // NV21 is a Y-plane followed by a interleaved VU-plane and is
                        // VK_FORMAT_G8_B8R8_2PLANE_420_UNORM on the host.
                    case DRM_FORMAT_YVU420:
                        // YV12 is a Y-plane, then a V-plane, and then a U-plane and is
                        // VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM on the host.
                    case DRM_FORMAT_YVU420_ANDROID:
                        // DRM_FORMAT_YVU420_ANDROID is the same as DRM_FORMAT_YVU420 with
                        // Android's extra alignement requirements.
                        ahbFormatProps->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_B;
                        ahbFormatProps->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_R;
                        break;

                    default:
                        ALOGE("%s: Unhandled YUV drm format:%" PRIu32, __FUNCTION__, drmFormat);
                        break;
                }
            }
        }
#endif
        ahbFormatProps->suggestedYcbcrModel = android_format_is_yuv(format)
                                                  ? VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601
                                                  : VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
        ahbFormatProps->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;

        ahbFormatProps->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
        ahbFormatProps->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
    }

    uint32_t colorBufferHandle = grallocHelper->getHostHandle(buffer);
    if (!colorBufferHandle) {
        return VK_ERROR_INVALID_EXTERNAL_HANDLE;
    }

    pProperties->allocationSize = grallocHelper->getAllocatedSize(buffer);

    return VK_SUCCESS;
}

// Based on Intel ANV implementation.
VkResult getMemoryAndroidHardwareBufferANDROID(gfxstream::Gralloc* gralloc,
                                               struct AHardwareBuffer** pBuffer) {
    /* Some quotes from Vulkan spec:
     *
     * "If the device memory was created by importing an Android hardware
     * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
     * Android hardware buffer object."
     *
     * "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
     * have been included in VkExportMemoryAllocateInfo::handleTypes when
     * memory was created."
     */

    if (!pBuffer) return VK_ERROR_OUT_OF_HOST_MEMORY;
    if (!(*pBuffer)) return VK_ERROR_OUT_OF_HOST_MEMORY;

    gralloc->acquire(*pBuffer);
    return VK_SUCCESS;
}

VkResult importAndroidHardwareBuffer(gfxstream::Gralloc* grallocHelper,
                                     const VkImportAndroidHardwareBufferInfoANDROID* info,
                                     struct AHardwareBuffer** importOut) {
    if (!info || !info->buffer) {
        return VK_ERROR_INVALID_EXTERNAL_HANDLE;
    }

    auto ahb = info->buffer;

    uint32_t colorBufferHandle = grallocHelper->getHostHandle(ahb);
    if (!colorBufferHandle) {
        return VK_ERROR_INVALID_EXTERNAL_HANDLE;
    }

    grallocHelper->acquire(ahb);

    if (importOut) *importOut = ahb;

    return VK_SUCCESS;
}

VkResult createAndroidHardwareBuffer(gfxstream::Gralloc* gralloc, bool hasDedicatedImage,
                                     bool hasDedicatedBuffer, const VkExtent3D& imageExtent,
                                     uint32_t imageLayers, VkFormat imageFormat,
                                     VkImageUsageFlags imageUsage,
                                     VkImageCreateFlags imageCreateFlags, VkDeviceSize bufferSize,
                                     VkDeviceSize allocationInfoAllocSize,
                                     struct AHardwareBuffer** out) {
    uint32_t w = 0;
    uint32_t h = 1;
    uint32_t layers = 1;
    uint32_t format = 0;
    uint64_t usage = 0;

    /* If caller passed dedicated information. */
    if (hasDedicatedImage) {
        w = imageExtent.width;
        h = imageExtent.height;
        layers = imageLayers;
        format = android_format_from_vk(imageFormat);
        usage = getAndroidHardwareBufferUsageFromVkUsage(imageCreateFlags, imageUsage);
    } else if (hasDedicatedBuffer) {
        w = bufferSize;
        format = AHARDWAREBUFFER_FORMAT_BLOB;
        usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
                AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER;
    } else {
        w = allocationInfoAllocSize;
        format = AHARDWAREBUFFER_FORMAT_BLOB;
        usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
                AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER;
    }

    struct AHardwareBuffer* ahb = NULL;

    if (gralloc->allocate(w, h, format, usage, &ahb) != 0) {
        return VK_ERROR_OUT_OF_HOST_MEMORY;
    }

    *out = ahb;

    return VK_SUCCESS;
}

}  // namespace vk
}  // namespace gfxstream