3 * Copyright (C) 2019 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
26 #include <gst/check/gstcheck.h>
27 #include <gst/check/gstharness.h>
28 #include <gst/vulkan/vulkan.h>
29 #include "../../ext/vulkan/vkelementutils.h"
30 #include "../../ext/vulkan/vkelementutils.c"
32 static GstVulkanInstance *instance;
33 static GstVulkanDevice *device;
38 instance = gst_vulkan_instance_new ();
39 fail_unless (gst_vulkan_instance_open (instance, NULL));
40 device = gst_vulkan_device_new_with_index (instance, 0);
41 fail_unless (gst_vulkan_device_open (device, NULL));
47 gst_object_unref (instance);
48 gst_object_unref (device);
52 check_size (GstMemory * mem, gsize at_least)
54 gsize size, maxsize, offset;
56 size = gst_memory_get_sizes (mem, &offset, &maxsize);
57 fail_unless (size <= maxsize);
58 fail_unless (size >= at_least);
61 static GstVulkanImageMemory *
62 create_image_mem (GstVideoInfo * v_info)
64 GstVulkanImageMemory *vk_mem;
65 VkImageUsageFlags usage;
69 vk_format = gst_vulkan_format_from_video_info (v_info, 0);
71 usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
73 gst_vulkan_image_memory_alloc (device, vk_format,
74 GST_VIDEO_INFO_COMP_WIDTH (v_info, 0),
75 GST_VIDEO_INFO_COMP_HEIGHT (v_info, 0), VK_IMAGE_TILING_LINEAR,
76 usage, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
77 fail_unless (gst_is_vulkan_image_memory (mem));
78 vk_mem = (GstVulkanImageMemory *) mem;
79 fail_unless (vk_mem->usage == usage);
83 GST_START_TEST (test_image_new)
85 GstVulkanImageMemory *vk_mem;
89 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
90 vk_mem = create_image_mem (&v_info);
92 fail_unless (vk_mem->device == device);
93 fail_unless (vk_mem->vk_mem != NULL);
95 size = gst_memory_get_sizes ((GstMemory *) vk_mem, &offset, NULL);
96 fail_unless (offset == 0);
97 check_size ((GstMemory *) vk_mem, v_info.size);
98 fail_unless (vk_mem->requirements.size >= size);
100 size = gst_memory_get_sizes ((GstMemory *) vk_mem->vk_mem, &offset, NULL);
101 fail_unless (offset == 0);
102 check_size ((GstMemory *) vk_mem->vk_mem, v_info.size);
104 gst_memory_unref ((GstMemory *) vk_mem);
109 GST_START_TEST (test_image_view_new)
111 GstVulkanImageMemory *vk_mem;
112 GstVulkanImageView *view;
115 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
116 vk_mem = create_image_mem (&v_info);
118 view = get_or_create_image_view (vk_mem);
120 gst_vulkan_image_view_unref (view);
121 gst_memory_unref ((GstMemory *) vk_mem);
126 GST_START_TEST (test_image_view_get)
128 GstVulkanImageMemory *vk_mem;
129 GstVulkanImageView *view;
132 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
133 vk_mem = create_image_mem (&v_info);
135 view = get_or_create_image_view (vk_mem);
136 gst_vulkan_image_view_unref (view);
137 view = get_or_create_image_view (vk_mem);
138 gst_vulkan_image_view_unref (view);
140 gst_memory_unref ((GstMemory *) vk_mem);
156 GstHarnessThread *threads[N_THREADS];
160 wait_for_ready (GstHarnessThread * thread, struct view_stress *stress)
162 g_mutex_lock (&stress->lock);
163 while (!stress->ready)
164 g_cond_wait (&stress->cond, &stress->lock);
165 g_mutex_unlock (&stress->lock);
169 get_unref_image_view (GstHarnessThread * thread, struct view_stress *stress)
171 int rand = g_random_int_range (0, N_MEMORY);
172 GstVulkanImageMemory *mem;
173 GstVulkanImageView *view;
175 mem = g_queue_peek_nth (stress->memories, rand);
176 view = get_or_create_image_view (mem);
177 gst_vulkan_image_view_unref (view);
179 g_atomic_int_inc (&stress->n_ops);
180 if (g_atomic_int_get (&stress->n_ops) > N_OPS)
184 GST_START_TEST (test_image_view_stress)
186 GstHarness *h = gst_harness_new_empty ();
187 struct view_stress stress;
191 g_mutex_init (&stress.lock);
192 g_cond_init (&stress.cond);
193 stress.ready = FALSE;
195 stress.memories = g_queue_new ();
197 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
198 for (i = 0; i < N_MEMORY; i++) {
199 g_queue_push_head (stress.memories, create_image_mem (&v_info));
202 g_mutex_lock (&stress.lock);
203 for (i = 0; i < N_THREADS; i++) {
204 stress.threads[i] = gst_harness_stress_custom_start (h,
205 (GFunc) wait_for_ready, (GFunc) get_unref_image_view, &stress, 10);
208 g_cond_broadcast (&stress.cond);
209 g_mutex_unlock (&stress.lock);
211 while (g_atomic_int_get (&stress.n_ops) < N_OPS)
214 for (i = 0; i < N_THREADS; i++) {
215 gst_harness_stress_thread_stop (stress.threads[i]);
218 g_mutex_clear (&stress.lock);
219 g_cond_clear (&stress.cond);
220 g_queue_free_full (stress.memories, (GDestroyNotify) gst_memory_unref);
221 gst_harness_teardown (h);
229 Suite *s = suite_create ("vkimage");
230 TCase *tc_basic = tcase_create ("general");
231 gboolean have_instance;
233 suite_add_tcase (s, tc_basic);
234 tcase_add_checked_fixture (tc_basic, setup, teardown);
236 /* FIXME: CI doesn't have a software vulkan renderer (and none exists currently) */
237 instance = gst_vulkan_instance_new ();
238 have_instance = gst_vulkan_instance_open (instance, NULL);
239 gst_object_unref (instance);
241 tcase_add_test (tc_basic, test_image_new);
242 tcase_add_test (tc_basic, test_image_view_new);
243 tcase_add_test (tc_basic, test_image_view_get);
244 tcase_add_test (tc_basic, test_image_view_stress);
251 GST_CHECK_MAIN (vkimage);