ffa13f4a48dd12a1c25f221154be09e53abf771b
[platform/upstream/gstreamer.git] / tests / check / libs / vkimage.c
1 /* GStreamer
2  *
3  * Copyright (C) 2019 Matthew Waters <matthew@centricular.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
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"
31
32 static GstVulkanInstance *instance;
33 static GstVulkanDevice *device;
34
35 static void
36 setup (void)
37 {
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));
42 }
43
44 static void
45 teardown (void)
46 {
47   gst_object_unref (instance);
48   gst_object_unref (device);
49 }
50
51 static void
52 check_size (GstMemory * mem, gsize at_least)
53 {
54   gsize size, maxsize, offset;
55
56   size = gst_memory_get_sizes (mem, &offset, &maxsize);
57   fail_unless (size <= maxsize);
58   fail_unless (size >= at_least);
59 }
60
61 static GstVulkanImageMemory *
62 create_image_mem (GstVideoInfo * v_info)
63 {
64   GstVulkanImageMemory *vk_mem;
65   VkImageUsageFlags usage;
66   VkFormat vk_format;
67   GstMemory *mem;
68
69   vk_format = gst_vulkan_format_from_video_info (v_info, 0);
70
71   usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
72   mem =
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);
80   return vk_mem;
81 }
82
83 GST_START_TEST (test_image_new)
84 {
85   GstVulkanImageMemory *vk_mem;
86   GstVideoInfo v_info;
87   gsize offset, size;
88
89   gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
90   vk_mem = create_image_mem (&v_info);
91
92   fail_unless (vk_mem->device == device);
93   fail_unless (vk_mem->vk_mem != NULL);
94
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);
99
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);
103
104   gst_memory_unref ((GstMemory *) vk_mem);
105 }
106
107 GST_END_TEST;
108
109 GST_START_TEST (test_image_view_new)
110 {
111   GstVulkanImageMemory *vk_mem;
112   GstVulkanImageView *view;
113   GstVideoInfo v_info;
114
115   gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
116   vk_mem = create_image_mem (&v_info);
117
118   view = get_or_create_image_view (vk_mem);
119
120   gst_vulkan_image_view_unref (view);
121   gst_memory_unref ((GstMemory *) vk_mem);
122 }
123
124 GST_END_TEST;
125
126 GST_START_TEST (test_image_view_get)
127 {
128   GstVulkanImageMemory *vk_mem;
129   GstVulkanImageView *view;
130   GstVideoInfo v_info;
131
132   gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
133   vk_mem = create_image_mem (&v_info);
134
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);
139
140   gst_memory_unref ((GstMemory *) vk_mem);
141 }
142
143 GST_END_TEST;
144
145 #define N_THREADS 2
146 #define N_MEMORY 4
147 #define N_OPS 512
148
149 struct view_stress
150 {
151   GMutex lock;
152   GCond cond;
153   gboolean ready;
154   volatile int n_ops;
155   GQueue *memories;
156   GstHarnessThread *threads[N_THREADS];
157 };
158
159 static void
160 wait_for_ready (GstHarnessThread * thread, struct view_stress *stress)
161 {
162   g_mutex_lock (&stress->lock);
163   while (!stress->ready)
164     g_cond_wait (&stress->cond, &stress->lock);
165   g_mutex_unlock (&stress->lock);
166 }
167
168 static void
169 get_unref_image_view (GstHarnessThread * thread, struct view_stress *stress)
170 {
171   int rand = g_random_int_range (0, N_MEMORY);
172   GstVulkanImageMemory *mem;
173   GstVulkanImageView *view;
174
175   mem = g_queue_peek_nth (stress->memories, rand);
176   view = get_or_create_image_view (mem);
177   gst_vulkan_image_view_unref (view);
178
179   g_atomic_int_inc (&stress->n_ops);
180   if (g_atomic_int_get (&stress->n_ops) > N_OPS)
181     g_usleep (100);
182 }
183
184 GST_START_TEST (test_image_view_stress)
185 {
186   GstHarness *h = gst_harness_new_empty ();
187   struct view_stress stress;
188   GstVideoInfo v_info;
189   int i;
190
191   g_mutex_init (&stress.lock);
192   g_cond_init (&stress.cond);
193   stress.ready = FALSE;
194   stress.n_ops = 0;
195   stress.memories = g_queue_new ();
196
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));
200   }
201
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);
206   }
207   stress.ready = TRUE;
208   g_cond_broadcast (&stress.cond);
209   g_mutex_unlock (&stress.lock);
210
211   while (g_atomic_int_get (&stress.n_ops) < N_OPS)
212     g_usleep (10000);
213
214   for (i = 0; i < N_THREADS; i++) {
215     gst_harness_stress_thread_stop (stress.threads[i]);
216   }
217
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);
222 }
223
224 GST_END_TEST;
225
226 static Suite *
227 vkimage_suite (void)
228 {
229   Suite *s = suite_create ("vkimage");
230   TCase *tc_basic = tcase_create ("general");
231   gboolean have_instance;
232
233   suite_add_tcase (s, tc_basic);
234   tcase_add_checked_fixture (tc_basic, setup, teardown);
235
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);
240   if (have_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);
245   }
246
247   return s;
248 }
249
250
251 GST_CHECK_MAIN (vkimage);