2015-05-09 06:32:37 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2015 Intel Corporation
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2015-07-17 23:04:27 +01:00
|
|
|
#include "anv_private.h"
|
2015-05-09 06:32:37 +01:00
|
|
|
|
2015-05-18 00:33:48 +01:00
|
|
|
VkResult anv_CreateDmaBufImageINTEL(
|
2015-05-09 06:32:37 +01:00
|
|
|
VkDevice _device,
|
|
|
|
const VkDmaBufImageCreateInfo* pCreateInfo,
|
2015-12-02 11:28:27 +00:00
|
|
|
const VkAllocationCallbacks* pAllocator,
|
2015-05-09 06:32:37 +01:00
|
|
|
VkDeviceMemory* pMem,
|
|
|
|
VkImage* pImage)
|
|
|
|
{
|
2015-07-17 21:59:48 +01:00
|
|
|
ANV_FROM_HANDLE(anv_device, device, _device);
|
2015-05-09 06:32:37 +01:00
|
|
|
struct anv_device_memory *mem;
|
|
|
|
struct anv_image *image;
|
|
|
|
VkResult result;
|
2015-08-14 18:02:19 +01:00
|
|
|
VkImage image_h;
|
2015-05-09 06:32:37 +01:00
|
|
|
|
|
|
|
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DMA_BUF_IMAGE_CREATE_INFO_INTEL);
|
|
|
|
|
2016-10-14 04:31:35 +01:00
|
|
|
mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
|
2015-12-02 11:28:27 +00:00
|
|
|
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
|
2015-05-09 06:32:37 +01:00
|
|
|
if (mem == NULL)
|
|
|
|
return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
|
|
|
|
|
2017-10-11 17:24:37 +01:00
|
|
|
result = anv_image_create(_device,
|
2015-08-14 18:02:19 +01:00
|
|
|
&(struct anv_image_create_info) {
|
2015-12-07 16:50:28 +00:00
|
|
|
.isl_tiling_flags = ISL_TILING_X_BIT,
|
2015-08-14 18:02:19 +01:00
|
|
|
.stride = pCreateInfo->strideInBytes,
|
|
|
|
.vk_info =
|
|
|
|
&(VkImageCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
|
|
|
.imageType = VK_IMAGE_TYPE_2D,
|
|
|
|
.format = pCreateInfo->format,
|
|
|
|
.extent = pCreateInfo->extent,
|
|
|
|
.mipLevels = 1,
|
2015-12-01 20:52:56 +00:00
|
|
|
.arrayLayers = 1,
|
2015-08-14 18:02:19 +01:00
|
|
|
.samples = 1,
|
|
|
|
/* FIXME: Need a way to use X tiling to allow scanout */
|
|
|
|
.tiling = VK_IMAGE_TILING_OPTIMAL,
|
|
|
|
.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
|
|
.flags = 0,
|
|
|
|
}},
|
2015-12-02 11:28:27 +00:00
|
|
|
pAllocator, &image_h);
|
2017-10-11 17:24:37 +01:00
|
|
|
if (result != VK_SUCCESS)
|
|
|
|
goto fail;
|
2015-08-14 18:02:19 +01:00
|
|
|
|
|
|
|
image = anv_image_from_handle(image_h);
|
2017-10-11 17:24:37 +01:00
|
|
|
|
2018-07-09 22:21:33 +01:00
|
|
|
uint64_t bo_flags = ANV_BO_EXTERNAL;
|
2018-05-30 23:34:25 +01:00
|
|
|
if (device->instance->physicalDevice.supports_48bit_addresses)
|
|
|
|
bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
|
2018-06-04 09:54:29 +01:00
|
|
|
if (device->instance->physicalDevice.use_softpin)
|
|
|
|
bo_flags |= EXEC_OBJECT_PINNED;
|
2018-05-30 23:34:25 +01:00
|
|
|
|
2017-10-11 17:24:37 +01:00
|
|
|
result = anv_bo_cache_import(device, &device->bo_cache,
|
2018-05-30 23:34:25 +01:00
|
|
|
pCreateInfo->fd, bo_flags, &mem->bo);
|
2017-10-11 17:24:37 +01:00
|
|
|
if (result != VK_SUCCESS)
|
|
|
|
goto fail_import;
|
|
|
|
|
anv: Move size check from anv_bo_cache_import() to caller (v2)
This change prepares for VK_ANDROID_native_buffer. When the user imports
a gralloc hande into a VkImage using VK_ANDROID_native_buffer, the user
provides no size. The driver must infer the size from the internals of
the gralloc buffer.
The patch is essentially a refactor patch, but it does change behavior
in some edge cases, described below. In what follows, the "nominal size"
of the bo refers to anv_bo::size, which may not match the bo's "actual
size" according to the kernel.
Post-patch, the nominal size of the bo returned from
anv_bo_cache_import() is always the size of imported dma-buf according
to lseek(). Pre-patch, the bo's nominal size was difficult to predict.
If the imported dma-buf's gem handle was not resident in the cache, then
the bo's nominal size was align(VkMemoryAllocateInfo::allocationSize,
4096). If it *was* resident, then the bo's nominal size was whatever
the cache returned. As a consequence, the first cache insert decided the
bo's nominal size, which could be significantly smaller compared to the
dma-buf's actual size, as the nominal size was determined by
VkMemoryAllocationInfo::allocationSize and not lseek().
I believe this patch cleans up that messy behavior. For an imported or
exported VkDeviceMemory, anv_bo::size should now be the true size of the
bo, if I correctly understand the problem (which I possibly don't).
v2:
- Preserve behavior of aligning size to 4096 before checking. [for
jekstrand]
- Check size with < instead of <=, to match behavior of commit c0a4f56
"anv: bo_cache: allow importing a BO larger than needed". [for
chadv]
2017-09-12 22:05:08 +01:00
|
|
|
VkDeviceSize aligned_image_size = align_u64(image->size, 4096);
|
|
|
|
|
|
|
|
if (mem->bo->size < aligned_image_size) {
|
2017-10-18 09:12:27 +01:00
|
|
|
result = vk_errorf(device->instance, device,
|
anv: Move size check from anv_bo_cache_import() to caller (v2)
This change prepares for VK_ANDROID_native_buffer. When the user imports
a gralloc hande into a VkImage using VK_ANDROID_native_buffer, the user
provides no size. The driver must infer the size from the internals of
the gralloc buffer.
The patch is essentially a refactor patch, but it does change behavior
in some edge cases, described below. In what follows, the "nominal size"
of the bo refers to anv_bo::size, which may not match the bo's "actual
size" according to the kernel.
Post-patch, the nominal size of the bo returned from
anv_bo_cache_import() is always the size of imported dma-buf according
to lseek(). Pre-patch, the bo's nominal size was difficult to predict.
If the imported dma-buf's gem handle was not resident in the cache, then
the bo's nominal size was align(VkMemoryAllocateInfo::allocationSize,
4096). If it *was* resident, then the bo's nominal size was whatever
the cache returned. As a consequence, the first cache insert decided the
bo's nominal size, which could be significantly smaller compared to the
dma-buf's actual size, as the nominal size was determined by
VkMemoryAllocationInfo::allocationSize and not lseek().
I believe this patch cleans up that messy behavior. For an imported or
exported VkDeviceMemory, anv_bo::size should now be the true size of the
bo, if I correctly understand the problem (which I possibly don't).
v2:
- Preserve behavior of aligning size to 4096 before checking. [for
jekstrand]
- Check size with < instead of <=, to match behavior of commit c0a4f56
"anv: bo_cache: allow importing a BO larger than needed". [for
chadv]
2017-09-12 22:05:08 +01:00
|
|
|
VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
|
|
|
|
"dma-buf too small for image in "
|
|
|
|
"vkCreateDmaBufImageINTEL: %"PRIu64"B < "PRIu64"B",
|
|
|
|
mem->bo->size, aligned_image_size);
|
|
|
|
anv_bo_cache_release(device, &device->bo_cache, mem->bo);
|
|
|
|
goto fail_import;
|
|
|
|
}
|
|
|
|
|
2018-05-31 02:55:00 +01:00
|
|
|
image->planes[0].address = (struct anv_address) {
|
|
|
|
.bo = mem->bo,
|
|
|
|
.offset = 0,
|
|
|
|
};
|
2015-05-09 06:32:37 +01:00
|
|
|
|
|
|
|
assert(image->extent.width > 0);
|
|
|
|
assert(image->extent.height > 0);
|
|
|
|
assert(image->extent.depth == 1);
|
|
|
|
|
2015-07-15 22:00:21 +01:00
|
|
|
*pMem = anv_device_memory_to_handle(mem);
|
|
|
|
*pImage = anv_image_to_handle(image);
|
2015-05-09 06:32:37 +01:00
|
|
|
|
2018-04-03 22:21:34 +01:00
|
|
|
close(pCreateInfo->fd);
|
|
|
|
|
2015-05-09 06:32:37 +01:00
|
|
|
return VK_SUCCESS;
|
|
|
|
|
2017-10-11 17:24:37 +01:00
|
|
|
fail_import:
|
|
|
|
vk_free2(&device->alloc, pAllocator, image);
|
|
|
|
|
2015-05-09 06:32:37 +01:00
|
|
|
fail:
|
2016-10-14 04:31:35 +01:00
|
|
|
vk_free2(&device->alloc, pAllocator, mem);
|
2015-05-09 06:32:37 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|