summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/virgl/tests/virgl_staging_mgr_test.cpp
blob: a475ff408b467fbe4252d932e7112b245e0a0276 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright 2019 Collabora Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <gtest/gtest.h>

#include "virgl_context.h"
#include "virgl_resource.h"
#include "virgl_screen.h"
#include "virgl_staging_mgr.h"
#include "virgl_winsys.h"

#include "util/u_inlines.h"
#include "util/u_memory.h"

struct virgl_hw_res {
    struct pipe_reference reference;
    uint32_t target;
    uint32_t bind;
    uint32_t size;
    void *data;
};

static struct virgl_hw_res *
fake_resource_create(struct virgl_winsys *vws,
                     enum pipe_texture_target target,
                     uint32_t format, uint32_t bind,
                     uint32_t width, uint32_t height,
                     uint32_t depth, uint32_t array_size,
                     uint32_t last_level, uint32_t nr_samples,
                     uint32_t flags, uint32_t size)
{
   struct virgl_hw_res *hw_res = CALLOC_STRUCT(virgl_hw_res);

   pipe_reference_init(&hw_res->reference, 1);

   hw_res->target = target;
   hw_res->bind = bind;
   hw_res->size = size;
   hw_res->data = CALLOC(size, 1);

   return hw_res;
}

static void
fake_resource_reference(struct virgl_winsys *vws,
                        struct virgl_hw_res **dres,
                        struct virgl_hw_res *sres)
{
   struct virgl_hw_res *old = *dres;

   if (pipe_reference(&(*dres)->reference, &sres->reference)) {
      FREE(old->data);
      FREE(old);
   }

   *dres = sres;
}

static void *
fake_resource_map(struct virgl_winsys *vws, struct virgl_hw_res *hw_res)
{
   return hw_res->data;
}

static struct pipe_context *
fake_virgl_context_create()
{
   struct virgl_context *vctx = CALLOC_STRUCT(virgl_context);
   struct virgl_screen *vs = CALLOC_STRUCT(virgl_screen);
   struct virgl_winsys *vws = CALLOC_STRUCT(virgl_winsys);

   vctx->base.screen = &vs->base;
   vs->vws = vws;

   vs->vws->resource_create = fake_resource_create;
   vs->vws->resource_reference = fake_resource_reference;
   vs->vws->resource_map = fake_resource_map;

   return &vctx->base;
}

static void
fake_virgl_context_destroy(struct pipe_context *ctx)
{
   struct virgl_context *vctx = virgl_context(ctx);
   struct virgl_screen *vs = virgl_screen(ctx->screen);

   FREE(vs->vws);
   FREE(vs);
   FREE(vctx);
}

static void *
resource_map(struct virgl_hw_res *hw_res)
{
   return hw_res->data;
}

static void
release_resources(struct virgl_hw_res *resources[], unsigned len)
{
   for (unsigned i = 0; i < len; ++i)
      fake_resource_reference(NULL, &resources[i], NULL);
}

class VirglStagingMgr : public ::testing::Test
{
protected:
   VirglStagingMgr() : ctx(fake_virgl_context_create())
   {
      virgl_staging_init(&staging, ctx, staging_size);
   }

   ~VirglStagingMgr()
   {
      virgl_staging_destroy(&staging);
      fake_virgl_context_destroy(ctx);
   }

   static const unsigned staging_size;
   struct pipe_context * const ctx;
   struct virgl_staging_mgr staging;
};

const unsigned VirglStagingMgr::staging_size = 4096;

class VirglStagingMgrWithAlignment : public VirglStagingMgr,
                                     public ::testing::WithParamInterface<unsigned>
{
protected:
   VirglStagingMgrWithAlignment() : alignment(GetParam()) {}
   const unsigned alignment;
};

TEST_P(VirglStagingMgrWithAlignment,
       suballocations_are_non_overlapping_in_same_resource)
{
   const unsigned alloc_sizes[] = {16, 450, 79, 240, 128, 1001};
   const unsigned num_resources = sizeof(alloc_sizes) / sizeof(alloc_sizes[0]);
   struct virgl_hw_res *out_resource[num_resources] = {0};
   unsigned expected_offset = 0;
   unsigned out_offset;
   void *map_ptr;
   bool alloc_succeeded;

   for (unsigned i = 0; i < num_resources; ++i) {
      alloc_succeeded =
         virgl_staging_alloc(&staging, alloc_sizes[i], alignment, &out_offset,
                           &out_resource[i], &map_ptr);

      EXPECT_TRUE(alloc_succeeded);
      EXPECT_EQ(out_offset, expected_offset);
      ASSERT_NE(out_resource[i], nullptr);
      if (i > 0) {
         EXPECT_EQ(out_resource[i], out_resource[i - 1]);
      }
      EXPECT_EQ(map_ptr,
            (uint8_t*)resource_map(out_resource[i]) + expected_offset);

      expected_offset += alloc_sizes[i];
      expected_offset = align(expected_offset, alignment);
   }

   release_resources(out_resource, num_resources);
}

INSTANTIATE_TEST_CASE_P(WithAlignment,
                        VirglStagingMgrWithAlignment,
                        ::testing::Values(1, 16),
                        testing::PrintToStringParamName());

TEST_F(VirglStagingMgr,
       non_fitting_allocation_reallocates_resource)
{
   struct virgl_hw_res *out_resource[2] = {0};
   unsigned out_offset;
   void *map_ptr;
   bool alloc_succeeded;

   alloc_succeeded =
      virgl_staging_alloc(&staging, staging_size - 1, 1, &out_offset,
                          &out_resource[0], &map_ptr);

   EXPECT_TRUE(alloc_succeeded);
   EXPECT_EQ(out_offset, 0);
   ASSERT_NE(out_resource[0], nullptr);
   EXPECT_EQ(map_ptr, resource_map(out_resource[0]));

   alloc_succeeded =
      virgl_staging_alloc(&staging, 2, 1, &out_offset,
                          &out_resource[1], &map_ptr);

   EXPECT_TRUE(alloc_succeeded);
   EXPECT_EQ(out_offset, 0);
   ASSERT_NE(out_resource[1], nullptr);
   EXPECT_EQ(map_ptr, resource_map(out_resource[1]));
   /* New resource with same size as old resource. */
   EXPECT_NE(out_resource[1], out_resource[0]);
   EXPECT_EQ(out_resource[1]->size, out_resource[0]->size);

   release_resources(out_resource, 2);
}

TEST_F(VirglStagingMgr,
       non_fitting_aligned_allocation_reallocates_resource)
{
   struct virgl_hw_res *out_resource[2] = {0};
   unsigned out_offset;
   void *map_ptr;
   bool alloc_succeeded;

   alloc_succeeded =
      virgl_staging_alloc(&staging, staging_size - 1, 1, &out_offset,
                          &out_resource[0], &map_ptr);

   EXPECT_TRUE(alloc_succeeded);
   EXPECT_EQ(out_offset, 0);
   ASSERT_NE(out_resource[0], nullptr);
   EXPECT_EQ(map_ptr, resource_map(out_resource[0]));

   alloc_succeeded =
      virgl_staging_alloc(&staging, 1, 16, &out_offset,
                          &out_resource[1], &map_ptr);

   EXPECT_TRUE(alloc_succeeded);
   EXPECT_EQ(out_offset, 0);
   ASSERT_NE(out_resource[1], nullptr);
   EXPECT_EQ(map_ptr, resource_map(out_resource[1]));
   /* New resource with same size as old resource. */
   EXPECT_NE(out_resource[1], out_resource[0]);
   EXPECT_EQ(out_resource[1]->size, out_resource[0]->size);

   release_resources(out_resource, 2);
}

TEST_F(VirglStagingMgr,
       large_non_fitting_allocation_reallocates_large_resource)
{
   struct virgl_hw_res *out_resource[2] = {0};
   unsigned out_offset;
   void *map_ptr;
   bool alloc_succeeded;

   ASSERT_LT(staging_size, 5123);

   alloc_succeeded =
      virgl_staging_alloc(&staging, 5123, 1, &out_offset,
                          &out_resource[0], &map_ptr);

   EXPECT_TRUE(alloc_succeeded);
   EXPECT_EQ(out_offset, 0);
   ASSERT_NE(out_resource[0], nullptr);
   EXPECT_EQ(map_ptr, resource_map(out_resource[0]));
   EXPECT_GE(out_resource[0]->size, 5123);

   alloc_succeeded =
      virgl_staging_alloc(&staging, 19345, 1, &out_offset,
                          &out_resource[1], &map_ptr);

   EXPECT_TRUE(alloc_succeeded);
   EXPECT_EQ(out_offset, 0);
   ASSERT_NE(out_resource[1], nullptr);
   EXPECT_EQ(map_ptr, resource_map(out_resource[1]));
   /* New resource */
   EXPECT_NE(out_resource[1], out_resource[0]);
   EXPECT_GE(out_resource[1]->size, 19345);

   release_resources(out_resource, 2);
}

TEST_F(VirglStagingMgr, releases_resource_on_destruction)
{
   struct virgl_hw_res *out_resource = NULL;
   unsigned out_offset;
   void *map_ptr;
   bool alloc_succeeded;

   alloc_succeeded =
      virgl_staging_alloc(&staging, 128, 1, &out_offset,
                          &out_resource, &map_ptr);

   EXPECT_TRUE(alloc_succeeded);
   ASSERT_NE(out_resource, nullptr);
   /* The resource is referenced both by staging internally,
    * and out_resource.
    */
   EXPECT_EQ(out_resource->reference.count, 2);

   /* Destroying staging releases the internal reference. */
   virgl_staging_destroy(&staging);
   EXPECT_EQ(out_resource->reference.count, 1);

   release_resources(&out_resource, 1);
}

static struct virgl_hw_res *
failing_resource_create(struct virgl_winsys *vws,
                        enum pipe_texture_target target,
                        uint32_t format, uint32_t bind,
                        uint32_t width, uint32_t height,
                        uint32_t depth, uint32_t array_size,
                        uint32_t last_level, uint32_t nr_samples,
                        uint32_t flags, uint32_t size)
{
   return NULL;
}

TEST_F(VirglStagingMgr, fails_gracefully_if_resource_create_fails)
{
   struct virgl_screen *vs = virgl_screen(ctx->screen);
   struct virgl_hw_res *out_resource = NULL;
   unsigned out_offset;
   void *map_ptr;
   bool alloc_succeeded;

   vs->vws->resource_create = failing_resource_create;

   alloc_succeeded =
      virgl_staging_alloc(&staging, 128, 1, &out_offset,
                          &out_resource, &map_ptr);

   EXPECT_FALSE(alloc_succeeded);
   EXPECT_EQ(out_resource, nullptr);
   EXPECT_EQ(map_ptr, nullptr);
}

static void *
failing_resource_map(struct virgl_winsys *vws, struct virgl_hw_res *hw_res)
{
   return NULL;
}

TEST_F(VirglStagingMgr, fails_gracefully_if_map_fails)
{
   struct virgl_screen *vs = virgl_screen(ctx->screen);
   struct virgl_hw_res *out_resource = NULL;
   unsigned out_offset;
   void *map_ptr;
   bool alloc_succeeded;

   vs->vws->resource_map = failing_resource_map;

   alloc_succeeded =
      virgl_staging_alloc(&staging, 128, 1, &out_offset,
                          &out_resource, &map_ptr);

   EXPECT_FALSE(alloc_succeeded);
   EXPECT_EQ(out_resource, nullptr);
   EXPECT_EQ(map_ptr, nullptr);
}

TEST_F(VirglStagingMgr, uses_staging_buffer_resource)
{
   struct virgl_hw_res *out_resource = NULL;
   unsigned out_offset;
   void *map_ptr;
   bool alloc_succeeded;

   alloc_succeeded =
      virgl_staging_alloc(&staging, 128, 1, &out_offset,
                          &out_resource, &map_ptr);

   EXPECT_TRUE(alloc_succeeded);
   ASSERT_NE(out_resource, nullptr);
   EXPECT_EQ(out_resource->target, PIPE_BUFFER);
   EXPECT_EQ(out_resource->bind, VIRGL_BIND_STAGING);

   release_resources(&out_resource, 1);
}