Home | History | Annotate | Download | only in vulkan
      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     const VkAllocationCallbacks*                pAllocator,
     36     VkDeviceMemory*                             pMem,
     37     VkImage*                                    pImage)
     38 {
     39    ANV_FROM_HANDLE(anv_device, device, _device);
     40    struct anv_device_memory *mem;
     41    struct anv_image *image;
     42    VkResult result;
     43    VkImage image_h;
     44 
     45    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DMA_BUF_IMAGE_CREATE_INFO_INTEL);
     46 
     47    mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
     48                     VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
     49    if (mem == NULL)
     50       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
     51 
     52    uint32_t gem_handle = anv_gem_fd_to_handle(device, pCreateInfo->fd);
     53    if (!gem_handle) {
     54       result = vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
     55       goto fail;
     56    }
     57 
     58    uint64_t size = (uint64_t)pCreateInfo->strideInBytes * pCreateInfo->extent.height;
     59 
     60    anv_bo_init(&mem->bo, gem_handle, size);
     61 
     62    anv_image_create(_device,
     63       &(struct anv_image_create_info) {
     64          .isl_tiling_flags = ISL_TILING_X_BIT,
     65          .stride = pCreateInfo->strideInBytes,
     66          .vk_info =
     67       &(VkImageCreateInfo) {
     68          .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
     69          .imageType = VK_IMAGE_TYPE_2D,
     70          .format = pCreateInfo->format,
     71          .extent = pCreateInfo->extent,
     72          .mipLevels = 1,
     73          .arrayLayers = 1,
     74          .samples = 1,
     75          /* FIXME: Need a way to use X tiling to allow scanout */
     76          .tiling = VK_IMAGE_TILING_OPTIMAL,
     77          .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
     78          .flags = 0,
     79       }},
     80       pAllocator, &image_h);
     81 
     82    image = anv_image_from_handle(image_h);
     83    image->bo = &mem->bo;
     84    image->offset = 0;
     85 
     86    assert(image->extent.width > 0);
     87    assert(image->extent.height > 0);
     88    assert(image->extent.depth == 1);
     89 
     90    *pMem = anv_device_memory_to_handle(mem);
     91    *pImage = anv_image_to_handle(image);
     92 
     93    return VK_SUCCESS;
     94 
     95  fail:
     96    vk_free2(&device->alloc, pAllocator, mem);
     97 
     98    return result;
     99 }
    100