vulkan: move fullscreenquad object to library
[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
30 static GstVulkanInstance *instance;
31 static GstVulkanDevice *device;
32
33 static void
34 setup (void)
35 {
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));
40 }
41
42 static void
43 teardown (void)
44 {
45   gst_object_unref (instance);
46   gst_object_unref (device);
47 }
48
49 static void
50 check_size (GstMemory * mem, gsize at_least)
51 {
52   gsize size, maxsize, offset;
53
54   size = gst_memory_get_sizes (mem, &offset, &maxsize);
55   fail_unless (size <= maxsize);
56   fail_unless (size >= at_least);
57 }
58
59 static GstVulkanImageMemory *
60 create_image_mem (GstVideoInfo * v_info)
61 {
62   GstVulkanImageMemory *vk_mem;
63   VkImageUsageFlags usage;
64   VkFormat vk_format;
65   GstMemory *mem;
66
67   vk_format = gst_vulkan_format_from_video_info (v_info, 0);
68
69   usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
70   mem =
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);
78   return vk_mem;
79 }
80
81 GST_START_TEST (test_image_new)
82 {
83   GstVulkanImageMemory *vk_mem;
84   GstVideoInfo v_info;
85   gsize offset, size;
86
87   gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
88   vk_mem = create_image_mem (&v_info);
89
90   fail_unless (vk_mem->device == device);
91   fail_unless (vk_mem->vk_mem != NULL);
92
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);
97
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);
101
102   gst_memory_unref ((GstMemory *) vk_mem);
103 }
104
105 GST_END_TEST;
106
107 GST_START_TEST (test_image_view_new)
108 {
109   GstVulkanImageMemory *vk_mem;
110   GstVulkanImageView *view;
111   GstVideoInfo v_info;
112
113   gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
114   vk_mem = create_image_mem (&v_info);
115
116   view = gst_vulkan_get_or_create_image_view (vk_mem);
117
118   gst_vulkan_image_view_unref (view);
119   gst_memory_unref ((GstMemory *) vk_mem);
120 }
121
122 GST_END_TEST;
123
124 GST_START_TEST (test_image_view_get)
125 {
126   GstVulkanImageMemory *vk_mem;
127   GstVulkanImageView *view;
128   GstVideoInfo v_info;
129
130   gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 16, 16);
131   vk_mem = create_image_mem (&v_info);
132
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);
137
138   gst_memory_unref ((GstMemory *) vk_mem);
139 }
140
141 GST_END_TEST;
142
143 #define N_THREADS 2
144 #define N_MEMORY 4
145 #define N_OPS 512
146
147 struct view_stress
148 {
149   GMutex lock;
150   GCond cond;
151   gboolean ready;
152   volatile int n_ops;
153   GQueue *memories;
154   GstHarnessThread *threads[N_THREADS];
155 };
156
157 static void
158 wait_for_ready (GstHarnessThread * thread, struct view_stress *stress)
159 {
160   g_mutex_lock (&stress->lock);
161   while (!stress->ready)
162     g_cond_wait (&stress->cond, &stress->lock);
163   g_mutex_unlock (&stress->lock);
164 }
165
166 static void
167 get_unref_image_view (GstHarnessThread * thread, struct view_stress *stress)
168 {
169   int rand = g_random_int_range (0, N_MEMORY);
170   GstVulkanImageMemory *mem;
171   GstVulkanImageView *view;
172
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);
176
177   g_atomic_int_inc (&stress->n_ops);
178   if (g_atomic_int_get (&stress->n_ops) > N_OPS)
179     g_usleep (100);
180 }
181
182 GST_START_TEST (test_image_view_stress)
183 {
184   GstHarness *h = gst_harness_new_empty ();
185   struct view_stress stress;
186   GstVideoInfo v_info;
187   int i;
188
189   g_mutex_init (&stress.lock);
190   g_cond_init (&stress.cond);
191   stress.ready = FALSE;
192   stress.n_ops = 0;
193   stress.memories = g_queue_new ();
194
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));
198   }
199
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);
204   }
205   stress.ready = TRUE;
206   g_cond_broadcast (&stress.cond);
207   g_mutex_unlock (&stress.lock);
208
209   while (g_atomic_int_get (&stress.n_ops) < N_OPS)
210     g_usleep (10000);
211
212   for (i = 0; i < N_THREADS; i++) {
213     gst_harness_stress_thread_stop (stress.threads[i]);
214   }
215
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);
220 }
221
222 GST_END_TEST;
223
224 static Suite *
225 vkimage_suite (void)
226 {
227   Suite *s = suite_create ("vkimage");
228   TCase *tc_basic = tcase_create ("general");
229   gboolean have_instance;
230
231   suite_add_tcase (s, tc_basic);
232   tcase_add_checked_fixture (tc_basic, setup, teardown);
233
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);
238   if (have_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);
243   }
244
245   return s;
246 }
247
248
249 GST_CHECK_MAIN (vkimage);