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>
30 static GstVulkanInstance *instance;
31 static GstVulkanDevice *device;
36 instance = gst_vulkan_instance_new ();
37 fail_unless (gst_vulkan_instance_open (instance, NULL));
38 device = gst_vulkan_device_new_with_index (instance, 0);
39 fail_unless (gst_vulkan_device_open (device, NULL));
45 gst_object_unref (instance);
46 gst_object_unref (device);
50 check_size (GstMemory * mem, gsize at_least)
52 gsize size, maxsize, offset;
54 size = gst_memory_get_sizes (mem, &offset, &maxsize);
55 fail_unless (size <= maxsize);
56 fail_unless (size >= at_least);
59 static GstVulkanImageMemory *
60 create_image_mem (GstVideoInfo * v_info)
62 GstVulkanImageMemory *vk_mem;
63 VkImageUsageFlags usage;
67 vk_format = gst_vulkan_format_from_video_info (v_info, 0);
69 usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
71 gst_vulkan_image_memory_alloc (device, vk_format,
72 GST_VIDEO_INFO_COMP_WIDTH (v_info, 0),
73 GST_VIDEO_INFO_COMP_HEIGHT (v_info, 0), VK_IMAGE_TILING_LINEAR,
74 usage, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
75 fail_unless (gst_is_vulkan_image_memory (mem));
76 vk_mem = (GstVulkanImageMemory *) mem;
77 fail_unless (vk_mem->usage == usage);
81 GST_START_TEST (test_image_new)
83 GstVulkanImageMemory *vk_mem;
87 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
88 vk_mem = create_image_mem (&v_info);
90 fail_unless (vk_mem->device == device);
91 fail_unless (vk_mem->vk_mem != NULL);
93 size = gst_memory_get_sizes ((GstMemory *) vk_mem, &offset, NULL);
94 fail_unless (offset == 0);
95 check_size ((GstMemory *) vk_mem, v_info.size);
96 fail_unless (vk_mem->requirements.size >= size);
98 size = gst_memory_get_sizes ((GstMemory *) vk_mem->vk_mem, &offset, NULL);
99 fail_unless (offset == 0);
100 check_size ((GstMemory *) vk_mem->vk_mem, v_info.size);
102 gst_memory_unref ((GstMemory *) vk_mem);
107 GST_START_TEST (test_image_view_new)
109 GstVulkanImageMemory *vk_mem;
110 GstVulkanImageView *view;
113 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
114 vk_mem = create_image_mem (&v_info);
116 view = gst_vulkan_get_or_create_image_view (vk_mem);
118 gst_vulkan_image_view_unref (view);
119 gst_memory_unref ((GstMemory *) vk_mem);
124 GST_START_TEST (test_image_view_get)
126 GstVulkanImageMemory *vk_mem;
127 GstVulkanImageView *view;
130 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
131 vk_mem = create_image_mem (&v_info);
133 view = gst_vulkan_get_or_create_image_view (vk_mem);
134 gst_vulkan_image_view_unref (view);
135 view = gst_vulkan_get_or_create_image_view (vk_mem);
136 gst_vulkan_image_view_unref (view);
138 gst_memory_unref ((GstMemory *) vk_mem);
154 GstHarnessThread *threads[N_THREADS];
158 wait_for_ready (GstHarnessThread * thread, struct view_stress *stress)
160 g_mutex_lock (&stress->lock);
161 while (!stress->ready)
162 g_cond_wait (&stress->cond, &stress->lock);
163 g_mutex_unlock (&stress->lock);
167 get_unref_image_view (GstHarnessThread * thread, struct view_stress *stress)
169 int rand = g_random_int_range (0, N_MEMORY);
170 GstVulkanImageMemory *mem;
171 GstVulkanImageView *view;
173 mem = g_queue_peek_nth (stress->memories, rand);
174 view = gst_vulkan_get_or_create_image_view (mem);
175 gst_vulkan_image_view_unref (view);
177 g_atomic_int_inc (&stress->n_ops);
178 if (g_atomic_int_get (&stress->n_ops) > N_OPS)
182 GST_START_TEST (test_image_view_stress)
184 GstHarness *h = gst_harness_new_empty ();
185 struct view_stress stress;
189 g_mutex_init (&stress.lock);
190 g_cond_init (&stress.cond);
191 stress.ready = FALSE;
193 stress.memories = g_queue_new ();
195 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
196 for (i = 0; i < N_MEMORY; i++) {
197 g_queue_push_head (stress.memories, create_image_mem (&v_info));
200 g_mutex_lock (&stress.lock);
201 for (i = 0; i < N_THREADS; i++) {
202 stress.threads[i] = gst_harness_stress_custom_start (h,
203 (GFunc) wait_for_ready, (GFunc) get_unref_image_view, &stress, 10);
206 g_cond_broadcast (&stress.cond);
207 g_mutex_unlock (&stress.lock);
209 while (g_atomic_int_get (&stress.n_ops) < N_OPS)
212 for (i = 0; i < N_THREADS; i++) {
213 gst_harness_stress_thread_stop (stress.threads[i]);
216 g_mutex_clear (&stress.lock);
217 g_cond_clear (&stress.cond);
218 g_queue_free_full (stress.memories, (GDestroyNotify) gst_memory_unref);
219 gst_harness_teardown (h);
227 Suite *s = suite_create ("vkimage");
228 TCase *tc_basic = tcase_create ("general");
229 gboolean have_instance;
231 suite_add_tcase (s, tc_basic);
232 tcase_add_checked_fixture (tc_basic, setup, teardown);
234 /* FIXME: CI doesn't have a software vulkan renderer (and none exists currently) */
235 instance = gst_vulkan_instance_new ();
236 have_instance = gst_vulkan_instance_open (instance, NULL);
237 gst_object_unref (instance);
239 tcase_add_test (tc_basic, test_image_new);
240 tcase_add_test (tc_basic, test_image_view_new);
241 tcase_add_test (tc_basic, test_image_view_get);
242 tcase_add_test (tc_basic, test_image_view_stress);
249 GST_CHECK_MAIN (vkimage);