1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017, Google Inc.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "v3dv_private.h"
25bf215546Sopenharmony_ci#include <hardware/gralloc.h>
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#if ANDROID_API_LEVEL >= 26
28bf215546Sopenharmony_ci#include <hardware/gralloc1.h>
29bf215546Sopenharmony_ci#endif
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "drm-uapi/drm_fourcc.h"
32bf215546Sopenharmony_ci#include <hardware/hardware.h>
33bf215546Sopenharmony_ci#include <hardware/hwvulkan.h>
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include <vulkan/vk_android_native_buffer.h>
36bf215546Sopenharmony_ci#include <vulkan/vk_icd.h>
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include "util/libsync.h"
39bf215546Sopenharmony_ci#include "util/log.h"
40bf215546Sopenharmony_ci#include "util/os_file.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistatic int
43bf215546Sopenharmony_civ3dv_hal_open(const struct hw_module_t *mod,
44bf215546Sopenharmony_ci              const char *id,
45bf215546Sopenharmony_ci              struct hw_device_t **dev);
46bf215546Sopenharmony_cistatic int
47bf215546Sopenharmony_civ3dv_hal_close(struct hw_device_t *dev);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cistatic void UNUSED
50bf215546Sopenharmony_cistatic_asserts(void)
51bf215546Sopenharmony_ci{
52bf215546Sopenharmony_ci   STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC);
53bf215546Sopenharmony_ci}
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ciPUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = {
56bf215546Sopenharmony_ci   .common =
57bf215546Sopenharmony_ci     {
58bf215546Sopenharmony_ci       .tag = HARDWARE_MODULE_TAG,
59bf215546Sopenharmony_ci       .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
60bf215546Sopenharmony_ci       .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0),
61bf215546Sopenharmony_ci       .id = HWVULKAN_HARDWARE_MODULE_ID,
62bf215546Sopenharmony_ci       .name = "Broadcom Vulkan HAL",
63bf215546Sopenharmony_ci       .author = "Mesa3D",
64bf215546Sopenharmony_ci       .methods =
65bf215546Sopenharmony_ci         &(hw_module_methods_t) {
66bf215546Sopenharmony_ci           .open = v3dv_hal_open,
67bf215546Sopenharmony_ci         },
68bf215546Sopenharmony_ci     },
69bf215546Sopenharmony_ci};
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci/* If any bits in test_mask are set, then unset them and return true. */
72bf215546Sopenharmony_cistatic inline bool
73bf215546Sopenharmony_ciunmask32(uint32_t *inout_mask, uint32_t test_mask)
74bf215546Sopenharmony_ci{
75bf215546Sopenharmony_ci   uint32_t orig_mask = *inout_mask;
76bf215546Sopenharmony_ci   *inout_mask &= ~test_mask;
77bf215546Sopenharmony_ci   return *inout_mask != orig_mask;
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_cistatic int
81bf215546Sopenharmony_civ3dv_hal_open(const struct hw_module_t *mod,
82bf215546Sopenharmony_ci              const char *id,
83bf215546Sopenharmony_ci              struct hw_device_t **dev)
84bf215546Sopenharmony_ci{
85bf215546Sopenharmony_ci   assert(mod == &HAL_MODULE_INFO_SYM.common);
86bf215546Sopenharmony_ci   assert(strcmp(id, HWVULKAN_DEVICE_0) == 0);
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   hwvulkan_device_t *hal_dev = malloc(sizeof(*hal_dev));
89bf215546Sopenharmony_ci   if (!hal_dev)
90bf215546Sopenharmony_ci      return -1;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   *hal_dev = (hwvulkan_device_t){
93bf215546Sopenharmony_ci      .common =
94bf215546Sopenharmony_ci        {
95bf215546Sopenharmony_ci          .tag = HARDWARE_DEVICE_TAG,
96bf215546Sopenharmony_ci          .version = HWVULKAN_DEVICE_API_VERSION_0_1,
97bf215546Sopenharmony_ci          .module = &HAL_MODULE_INFO_SYM.common,
98bf215546Sopenharmony_ci          .close = v3dv_hal_close,
99bf215546Sopenharmony_ci        },
100bf215546Sopenharmony_ci     .EnumerateInstanceExtensionProperties =
101bf215546Sopenharmony_ci        v3dv_EnumerateInstanceExtensionProperties,
102bf215546Sopenharmony_ci     .CreateInstance = v3dv_CreateInstance,
103bf215546Sopenharmony_ci     .GetInstanceProcAddr = v3dv_GetInstanceProcAddr,
104bf215546Sopenharmony_ci   };
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   mesa_logi("v3dv: Warning: Android Vulkan implementation is experimental");
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   *dev = &hal_dev->common;
109bf215546Sopenharmony_ci   return 0;
110bf215546Sopenharmony_ci}
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_cistatic int
113bf215546Sopenharmony_civ3dv_hal_close(struct hw_device_t *dev)
114bf215546Sopenharmony_ci{
115bf215546Sopenharmony_ci   /* hwvulkan.h claims that hw_device_t::close() is never called. */
116bf215546Sopenharmony_ci   return -1;
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_cistatic int
120bf215546Sopenharmony_ciget_format_bpp(int native)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci   int bpp;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   switch (native) {
125bf215546Sopenharmony_ci   case HAL_PIXEL_FORMAT_RGBA_FP16:
126bf215546Sopenharmony_ci      bpp = 8;
127bf215546Sopenharmony_ci      break;
128bf215546Sopenharmony_ci   case HAL_PIXEL_FORMAT_RGBA_8888:
129bf215546Sopenharmony_ci   case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
130bf215546Sopenharmony_ci   case HAL_PIXEL_FORMAT_RGBX_8888:
131bf215546Sopenharmony_ci   case HAL_PIXEL_FORMAT_BGRA_8888:
132bf215546Sopenharmony_ci   case HAL_PIXEL_FORMAT_RGBA_1010102:
133bf215546Sopenharmony_ci      bpp = 4;
134bf215546Sopenharmony_ci      break;
135bf215546Sopenharmony_ci   case HAL_PIXEL_FORMAT_RGB_565:
136bf215546Sopenharmony_ci      bpp = 2;
137bf215546Sopenharmony_ci      break;
138bf215546Sopenharmony_ci   default:
139bf215546Sopenharmony_ci      bpp = 0;
140bf215546Sopenharmony_ci      break;
141bf215546Sopenharmony_ci   }
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   return bpp;
144bf215546Sopenharmony_ci}
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci/* get buffer info from VkNativeBufferANDROID */
147bf215546Sopenharmony_cistatic VkResult
148bf215546Sopenharmony_civ3dv_gralloc_info_other(struct v3dv_device *device,
149bf215546Sopenharmony_ci                        const VkNativeBufferANDROID *native_buffer,
150bf215546Sopenharmony_ci                        int *out_stride,
151bf215546Sopenharmony_ci                        uint64_t *out_modifier)
152bf215546Sopenharmony_ci{
153bf215546Sopenharmony_ci   *out_stride = native_buffer->stride /*in pixels*/ *
154bf215546Sopenharmony_ci                 get_format_bpp(native_buffer->format);
155bf215546Sopenharmony_ci   *out_modifier = DRM_FORMAT_MOD_LINEAR;
156bf215546Sopenharmony_ci   return VK_SUCCESS;
157bf215546Sopenharmony_ci}
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_cistatic const char cros_gralloc_module_name[] = "CrOS Gralloc";
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci#define CROS_GRALLOC_DRM_GET_BUFFER_INFO 4
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_cistruct cros_gralloc0_buffer_info
164bf215546Sopenharmony_ci{
165bf215546Sopenharmony_ci   uint32_t drm_fourcc;
166bf215546Sopenharmony_ci   int num_fds;
167bf215546Sopenharmony_ci   int fds[4];
168bf215546Sopenharmony_ci   uint64_t modifier;
169bf215546Sopenharmony_ci   int offset[4];
170bf215546Sopenharmony_ci   int stride[4];
171bf215546Sopenharmony_ci};
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_cistatic VkResult
174bf215546Sopenharmony_civ3dv_gralloc_info_cros(struct v3dv_device *device,
175bf215546Sopenharmony_ci                       const VkNativeBufferANDROID *native_buffer,
176bf215546Sopenharmony_ci                       int *out_stride,
177bf215546Sopenharmony_ci                       uint64_t *out_modifier)
178bf215546Sopenharmony_ci{
179bf215546Sopenharmony_ci   const gralloc_module_t *gralloc = device->gralloc;
180bf215546Sopenharmony_ci   struct cros_gralloc0_buffer_info info;
181bf215546Sopenharmony_ci   int ret;
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   ret = gralloc->perform(gralloc, CROS_GRALLOC_DRM_GET_BUFFER_INFO,
184bf215546Sopenharmony_ci                          native_buffer->handle, &info);
185bf215546Sopenharmony_ci   if (ret)
186bf215546Sopenharmony_ci      return VK_ERROR_INVALID_EXTERNAL_HANDLE;
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci   *out_stride = info.stride[0];
189bf215546Sopenharmony_ci   *out_modifier = info.modifier;
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci   return VK_SUCCESS;
192bf215546Sopenharmony_ci}
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ciVkResult
195bf215546Sopenharmony_civ3dv_gralloc_info(struct v3dv_device *device,
196bf215546Sopenharmony_ci                  const VkNativeBufferANDROID *native_buffer,
197bf215546Sopenharmony_ci                  int *out_dmabuf,
198bf215546Sopenharmony_ci                  int *out_stride,
199bf215546Sopenharmony_ci                  int *out_size,
200bf215546Sopenharmony_ci                  uint64_t *out_modifier)
201bf215546Sopenharmony_ci{
202bf215546Sopenharmony_ci   if (device->gralloc_type == V3DV_GRALLOC_UNKNOWN) {
203bf215546Sopenharmony_ci      /* get gralloc module for gralloc buffer info query */
204bf215546Sopenharmony_ci      int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
205bf215546Sopenharmony_ci                              (const hw_module_t **) &device->gralloc);
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci      device->gralloc_type = V3DV_GRALLOC_OTHER;
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci      if (err == 0) {
210bf215546Sopenharmony_ci         const gralloc_module_t *gralloc = device->gralloc;
211bf215546Sopenharmony_ci         mesa_logi("opened gralloc module name: %s", gralloc->common.name);
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci         if (strcmp(gralloc->common.name, cros_gralloc_module_name) == 0 &&
214bf215546Sopenharmony_ci             gralloc->perform) {
215bf215546Sopenharmony_ci            device->gralloc_type = V3DV_GRALLOC_CROS;
216bf215546Sopenharmony_ci         }
217bf215546Sopenharmony_ci      }
218bf215546Sopenharmony_ci   }
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci   *out_dmabuf = native_buffer->handle->data[0];
221bf215546Sopenharmony_ci   *out_size = lseek(*out_dmabuf, 0, SEEK_END);
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci   if (device->gralloc_type == V3DV_GRALLOC_CROS) {
224bf215546Sopenharmony_ci      return v3dv_gralloc_info_cros(device, native_buffer, out_stride,
225bf215546Sopenharmony_ci                                    out_modifier);
226bf215546Sopenharmony_ci   } else {
227bf215546Sopenharmony_ci      return v3dv_gralloc_info_other(device, native_buffer, out_stride,
228bf215546Sopenharmony_ci                                     out_modifier);
229bf215546Sopenharmony_ci   }
230bf215546Sopenharmony_ci}
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ciVkResult
233bf215546Sopenharmony_civ3dv_import_native_buffer_fd(VkDevice device_h,
234bf215546Sopenharmony_ci                             int native_buffer_fd,
235bf215546Sopenharmony_ci                             const VkAllocationCallbacks *alloc,
236bf215546Sopenharmony_ci                             VkImage image_h)
237bf215546Sopenharmony_ci{
238bf215546Sopenharmony_ci   struct v3dv_image *image = NULL;
239bf215546Sopenharmony_ci   VkResult result;
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci   image = v3dv_image_from_handle(image_h);
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   VkDeviceMemory memory_h;
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci   const VkMemoryDedicatedAllocateInfo ded_alloc = {
246bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
247bf215546Sopenharmony_ci      .pNext = NULL,
248bf215546Sopenharmony_ci      .buffer = VK_NULL_HANDLE,
249bf215546Sopenharmony_ci      .image = image_h
250bf215546Sopenharmony_ci   };
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci   const VkImportMemoryFdInfoKHR import_info = {
253bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
254bf215546Sopenharmony_ci      .pNext = &ded_alloc,
255bf215546Sopenharmony_ci      .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
256bf215546Sopenharmony_ci      .fd = os_dupfd_cloexec(native_buffer_fd),
257bf215546Sopenharmony_ci   };
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci   result =
260bf215546Sopenharmony_ci      v3dv_AllocateMemory(device_h,
261bf215546Sopenharmony_ci                          &(VkMemoryAllocateInfo) {
262bf215546Sopenharmony_ci                             .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
263bf215546Sopenharmony_ci                             .pNext = &import_info,
264bf215546Sopenharmony_ci                             .allocationSize = image->size,
265bf215546Sopenharmony_ci                             .memoryTypeIndex = 0,
266bf215546Sopenharmony_ci                          },
267bf215546Sopenharmony_ci                          alloc, &memory_h);
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
270bf215546Sopenharmony_ci      goto fail_create_image;
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci   VkBindImageMemoryInfo bind_info = {
273bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO,
274bf215546Sopenharmony_ci      .image = image_h,
275bf215546Sopenharmony_ci      .memory = memory_h,
276bf215546Sopenharmony_ci      .memoryOffset = 0,
277bf215546Sopenharmony_ci   };
278bf215546Sopenharmony_ci   v3dv_BindImageMemory2(device_h, 1, &bind_info);
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_ci   image->is_native_buffer_memory = true;
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_ci   return VK_SUCCESS;
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_cifail_create_image:
285bf215546Sopenharmony_ci   close(import_info.fd);
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci   return result;
288bf215546Sopenharmony_ci}
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_cistatic VkResult
291bf215546Sopenharmony_ciformat_supported_with_usage(VkDevice device_h,
292bf215546Sopenharmony_ci                            VkFormat format,
293bf215546Sopenharmony_ci                            VkImageUsageFlags imageUsage)
294bf215546Sopenharmony_ci{
295bf215546Sopenharmony_ci   V3DV_FROM_HANDLE(v3dv_device, device, device_h);
296bf215546Sopenharmony_ci   struct v3dv_physical_device *phys_dev = &device->instance->physicalDevice;
297bf215546Sopenharmony_ci   VkPhysicalDevice phys_dev_h = v3dv_physical_device_to_handle(phys_dev);
298bf215546Sopenharmony_ci   VkResult result;
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   const VkPhysicalDeviceImageFormatInfo2 image_format_info = {
301bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
302bf215546Sopenharmony_ci      .format = format,
303bf215546Sopenharmony_ci      .type = VK_IMAGE_TYPE_2D,
304bf215546Sopenharmony_ci      .tiling = VK_IMAGE_TILING_OPTIMAL,
305bf215546Sopenharmony_ci      .usage = imageUsage,
306bf215546Sopenharmony_ci   };
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci   VkImageFormatProperties2 image_format_props = {
309bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
310bf215546Sopenharmony_ci   };
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci   /* Check that requested format and usage are supported. */
313bf215546Sopenharmony_ci   result = v3dv_GetPhysicalDeviceImageFormatProperties2(
314bf215546Sopenharmony_ci      phys_dev_h, &image_format_info, &image_format_props);
315bf215546Sopenharmony_ci   if (result != VK_SUCCESS) {
316bf215546Sopenharmony_ci      return vk_errorf(device, result,
317bf215546Sopenharmony_ci                       "v3dv_GetPhysicalDeviceImageFormatProperties2 failed "
318bf215546Sopenharmony_ci                       "inside %s",
319bf215546Sopenharmony_ci                       __func__);
320bf215546Sopenharmony_ci   }
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci   return VK_SUCCESS;
323bf215546Sopenharmony_ci}
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_cistatic VkResult
326bf215546Sopenharmony_cisetup_gralloc0_usage(struct v3dv_device *device,
327bf215546Sopenharmony_ci                     VkFormat format,
328bf215546Sopenharmony_ci                     VkImageUsageFlags imageUsage,
329bf215546Sopenharmony_ci                     int *grallocUsage)
330bf215546Sopenharmony_ci{
331bf215546Sopenharmony_ci   if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
332bf215546Sopenharmony_ci                             VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
333bf215546Sopenharmony_ci      *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci   if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
336bf215546Sopenharmony_ci                             VK_IMAGE_USAGE_SAMPLED_BIT |
337bf215546Sopenharmony_ci                             VK_IMAGE_USAGE_STORAGE_BIT |
338bf215546Sopenharmony_ci                             VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
339bf215546Sopenharmony_ci      *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci   /* All VkImageUsageFlags not explicitly checked here are unsupported for
342bf215546Sopenharmony_ci    * gralloc swapchains.
343bf215546Sopenharmony_ci    */
344bf215546Sopenharmony_ci   if (imageUsage != 0) {
345bf215546Sopenharmony_ci      return vk_errorf(device, VK_ERROR_FORMAT_NOT_SUPPORTED,
346bf215546Sopenharmony_ci                       "unsupported VkImageUsageFlags(0x%x) for gralloc "
347bf215546Sopenharmony_ci                       "swapchain",
348bf215546Sopenharmony_ci                       imageUsage);
349bf215546Sopenharmony_ci   }
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci   /* Swapchain assumes direct displaying, therefore enable COMPOSER flag,
352bf215546Sopenharmony_ci    * In case format is not supported by display controller, gralloc will
353bf215546Sopenharmony_ci    * drop this flag and still allocate the buffer in VRAM
354bf215546Sopenharmony_ci    */
355bf215546Sopenharmony_ci   *grallocUsage |= GRALLOC_USAGE_HW_COMPOSER;
356bf215546Sopenharmony_ci
357bf215546Sopenharmony_ci   if (*grallocUsage == 0)
358bf215546Sopenharmony_ci      return VK_ERROR_FORMAT_NOT_SUPPORTED;
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci   return VK_SUCCESS;
361bf215546Sopenharmony_ci}
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ciVKAPI_ATTR VkResult VKAPI_CALL
364bf215546Sopenharmony_civ3dv_GetSwapchainGrallocUsageANDROID(VkDevice device_h,
365bf215546Sopenharmony_ci                                     VkFormat format,
366bf215546Sopenharmony_ci                                     VkImageUsageFlags imageUsage,
367bf215546Sopenharmony_ci                                     int *grallocUsage)
368bf215546Sopenharmony_ci{
369bf215546Sopenharmony_ci   V3DV_FROM_HANDLE(v3dv_device, device, device_h);
370bf215546Sopenharmony_ci   VkResult result;
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_ci   result = format_supported_with_usage(device_h, format, imageUsage);
373bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
374bf215546Sopenharmony_ci      return result;
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci   *grallocUsage = 0;
377bf215546Sopenharmony_ci   return setup_gralloc0_usage(device, format, imageUsage, grallocUsage);
378bf215546Sopenharmony_ci}
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci#if ANDROID_API_LEVEL >= 26
381bf215546Sopenharmony_ciVKAPI_ATTR VkResult VKAPI_CALL
382bf215546Sopenharmony_civ3dv_GetSwapchainGrallocUsage2ANDROID(
383bf215546Sopenharmony_ci   VkDevice device_h,
384bf215546Sopenharmony_ci   VkFormat format,
385bf215546Sopenharmony_ci   VkImageUsageFlags imageUsage,
386bf215546Sopenharmony_ci   VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
387bf215546Sopenharmony_ci   uint64_t *grallocConsumerUsage,
388bf215546Sopenharmony_ci   uint64_t *grallocProducerUsage)
389bf215546Sopenharmony_ci{
390bf215546Sopenharmony_ci   V3DV_FROM_HANDLE(v3dv_device, device, device_h);
391bf215546Sopenharmony_ci   VkResult result;
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci   *grallocConsumerUsage = 0;
394bf215546Sopenharmony_ci   *grallocProducerUsage = 0;
395bf215546Sopenharmony_ci   mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci   result = format_supported_with_usage(device_h, format, imageUsage);
398bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
399bf215546Sopenharmony_ci      return result;
400bf215546Sopenharmony_ci
401bf215546Sopenharmony_ci   int32_t grallocUsage = 0;
402bf215546Sopenharmony_ci   result = setup_gralloc0_usage(device, format, imageUsage, &grallocUsage);
403bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
404bf215546Sopenharmony_ci      return result;
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci   /* Setup gralloc1 usage flags from gralloc0 flags. */
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci   if (grallocUsage & GRALLOC_USAGE_HW_RENDER) {
409bf215546Sopenharmony_ci      *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
410bf215546Sopenharmony_ci   }
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci   if (grallocUsage & GRALLOC_USAGE_HW_TEXTURE) {
413bf215546Sopenharmony_ci      *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
414bf215546Sopenharmony_ci   }
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci   if (grallocUsage & GRALLOC_USAGE_HW_COMPOSER) {
417bf215546Sopenharmony_ci      /* GPU composing case */
418bf215546Sopenharmony_ci      *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
419bf215546Sopenharmony_ci      /* Hardware composing case */
420bf215546Sopenharmony_ci      *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER;
421bf215546Sopenharmony_ci   }
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci   return VK_SUCCESS;
424bf215546Sopenharmony_ci}
425bf215546Sopenharmony_ci#endif
426