Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / vulkan / anv_intel.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 VkResult anv_CreateDmaBufImageINTEL(
33 VkDevice _device,
34 const VkDmaBufImageCreateInfo* pCreateInfo,
35 VkDeviceMemory* pMem,
36 VkImage* pImage)
37 {
38 ANV_FROM_HANDLE(anv_device, device, _device);
39 struct anv_device_memory *mem;
40 struct anv_image *image;
41 VkResult result;
42
43 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DMA_BUF_IMAGE_CREATE_INFO_INTEL);
44
45 mem = anv_device_alloc(device, sizeof(*mem), 8,
46 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
47 if (mem == NULL)
48 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
49
50 mem->bo.gem_handle = anv_gem_fd_to_handle(device, pCreateInfo->fd);
51 if (!mem->bo.gem_handle) {
52 result = vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
53 goto fail;
54 }
55
56 mem->bo.map = NULL;
57 mem->bo.index = 0;
58 mem->bo.offset = 0;
59 mem->bo.size = pCreateInfo->strideInBytes * pCreateInfo->extent.height;
60
61 image = anv_device_alloc(device, sizeof(*image), 8,
62 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
63 if (image == NULL) {
64 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
65 goto fail_mem;
66 }
67
68 *image = (struct anv_image) {
69 .bo = &mem->bo,
70 .offset = 0,
71 .type = VK_IMAGE_TYPE_2D,
72 .extent = pCreateInfo->extent,
73 .size = mem->bo.size,
74
75 .primary_surface = {
76 .offset = 0,
77 .stride = pCreateInfo->strideInBytes,
78 .tile_mode = XMAJOR,
79 },
80 };
81
82 assert(image->extent.width > 0);
83 assert(image->extent.height > 0);
84 assert(image->extent.depth == 1);
85
86 *pMem = anv_device_memory_to_handle(mem);
87 *pImage = anv_image_to_handle(image);
88
89 return VK_SUCCESS;
90
91 fail_mem:
92 anv_gem_close(device, mem->bo.gem_handle);
93 fail:
94 anv_device_free(device, mem);
95
96 return result;
97 }