~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/gallium/auxiliary/vl/vl_video_buffer.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**************************************************************************
2
 
 *
3
 
 * Copyright 2011 Christian König.
4
 
 * All Rights Reserved.
5
 
 *
6
 
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 
 * copy of this software and associated documentation files (the
8
 
 * "Software"), to deal in the Software without restriction, including
9
 
 * without limitation the rights to use, copy, modify, merge, publish,
10
 
 * distribute, sub license, and/or sell copies of the Software, and to
11
 
 * permit persons to whom the Software is furnished to do so, subject to
12
 
 * the following conditions:
13
 
 *
14
 
 * The above copyright notice and this permission notice (including the
15
 
 * next paragraph) shall be included in all copies or substantial portions
16
 
 * of the Software.
17
 
 *
18
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
 
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21
 
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22
 
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 
 *
26
 
 **************************************************************************/
27
 
 
28
 
#include <assert.h>
29
 
 
30
 
#include "pipe/p_screen.h"
31
 
#include "pipe/p_context.h"
32
 
#include "pipe/p_state.h"
33
 
 
34
 
#include "util/format/u_format.h"
35
 
#include "util/u_inlines.h"
36
 
#include "util/u_sampler.h"
37
 
#include "util/u_memory.h"
38
 
 
39
 
#include "vl_video_buffer.h"
40
 
 
41
 
const unsigned const_resource_plane_order_YUV[3] = {
42
 
   0,
43
 
   1,
44
 
   2
45
 
};
46
 
 
47
 
const unsigned const_resource_plane_order_YVU[3] = {
48
 
   0,
49
 
   2,
50
 
   1
51
 
};
52
 
 
53
 
void
54
 
vl_get_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format,
55
 
                            enum pipe_format out_format[VL_NUM_COMPONENTS])
56
 
{
57
 
   unsigned num_planes = util_format_get_num_planes(format);
58
 
   unsigned i;
59
 
 
60
 
   for (i = 0; i < num_planes; i++)
61
 
      out_format[i] = util_format_get_plane_format(format, i);
62
 
   for (; i < VL_NUM_COMPONENTS; i++)
63
 
      out_format[i] = PIPE_FORMAT_NONE;
64
 
 
65
 
   if (format == PIPE_FORMAT_YUYV)
66
 
      out_format[0] = PIPE_FORMAT_R8G8_R8B8_UNORM;
67
 
   else if (format == PIPE_FORMAT_UYVY)
68
 
      out_format[0] = PIPE_FORMAT_G8R8_B8R8_UNORM;
69
 
}
70
 
 
71
 
const unsigned *
72
 
vl_video_buffer_plane_order(enum pipe_format format)
73
 
{
74
 
   switch(format) {
75
 
   case PIPE_FORMAT_YV12:
76
 
      return const_resource_plane_order_YVU;
77
 
 
78
 
   case PIPE_FORMAT_NV12:
79
 
   case PIPE_FORMAT_R8G8B8A8_UNORM:
80
 
   case PIPE_FORMAT_B8G8R8A8_UNORM:
81
 
   case PIPE_FORMAT_YUYV:
82
 
   case PIPE_FORMAT_UYVY:
83
 
   case PIPE_FORMAT_P010:
84
 
   case PIPE_FORMAT_P016:
85
 
      return const_resource_plane_order_YUV;
86
 
 
87
 
   default:
88
 
      return NULL;
89
 
   }
90
 
}
91
 
 
92
 
static enum pipe_format
93
 
vl_video_buffer_surface_format(enum pipe_format format)
94
 
{
95
 
   const struct util_format_description *desc = util_format_description(format);
96
 
 
97
 
   /* a subsampled formats can't work as surface use RGBA instead */
98
 
   if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
99
 
      return PIPE_FORMAT_R8G8B8A8_UNORM;
100
 
 
101
 
   return format;
102
 
}
103
 
 
104
 
bool
105
 
vl_video_buffer_is_format_supported(struct pipe_screen *screen,
106
 
                                    enum pipe_format format,
107
 
                                    enum pipe_video_profile profile,
108
 
                                    enum pipe_video_entrypoint entrypoint)
109
 
{
110
 
   enum pipe_format resource_formats[VL_NUM_COMPONENTS];
111
 
   unsigned i;
112
 
 
113
 
   vl_get_video_buffer_formats(screen, format, resource_formats);
114
 
 
115
 
   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
116
 
      enum pipe_format format = resource_formats[i];
117
 
 
118
 
      if (format == PIPE_FORMAT_NONE)
119
 
         continue;
120
 
 
121
 
      /* we at least need to sample from it */
122
 
      if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW))
123
 
         return false;
124
 
 
125
 
      format = vl_video_buffer_surface_format(format);
126
 
      if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_RENDER_TARGET))
127
 
         return false;
128
 
   }
129
 
 
130
 
   return true;
131
 
}
132
 
 
133
 
unsigned
134
 
vl_video_buffer_max_size(struct pipe_screen *screen)
135
 
{
136
 
   return screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
137
 
}
138
 
 
139
 
void
140
 
vl_video_buffer_set_associated_data(struct pipe_video_buffer *vbuf,
141
 
                                    struct pipe_video_codec *vcodec,
142
 
                                    void *associated_data,
143
 
                                    void (*destroy_associated_data)(void *))
144
 
{
145
 
   vbuf->codec = vcodec;
146
 
 
147
 
   if (vbuf->associated_data == associated_data)
148
 
      return;
149
 
 
150
 
   if (vbuf->associated_data)
151
 
      vbuf->destroy_associated_data(vbuf->associated_data);
152
 
 
153
 
   vbuf->associated_data = associated_data;
154
 
   vbuf->destroy_associated_data = destroy_associated_data;
155
 
}
156
 
 
157
 
void *
158
 
vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf,
159
 
                                    struct pipe_video_codec *vcodec)
160
 
{
161
 
   if (vbuf->codec == vcodec)
162
 
      return vbuf->associated_data;
163
 
   else
164
 
      return NULL;
165
 
}
166
 
 
167
 
void
168
 
vl_video_buffer_template(struct pipe_resource *templ,
169
 
                         const struct pipe_video_buffer *tmpl,
170
 
                         enum pipe_format resource_format,
171
 
                         unsigned depth, unsigned array_size,
172
 
                         unsigned usage, unsigned plane,
173
 
                         enum pipe_video_chroma_format chroma_format)
174
 
{
175
 
   unsigned height = tmpl->height;
176
 
 
177
 
   memset(templ, 0, sizeof(*templ));
178
 
   if (depth > 1)
179
 
      templ->target = PIPE_TEXTURE_3D;
180
 
   else if (array_size > 1)
181
 
      templ->target = PIPE_TEXTURE_2D_ARRAY;
182
 
   else
183
 
      templ->target = PIPE_TEXTURE_2D;
184
 
   templ->format = resource_format;
185
 
   templ->width0 = tmpl->width;
186
 
   templ->depth0 = depth;
187
 
   templ->array_size = array_size;
188
 
   templ->bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
189
 
   templ->usage = usage;
190
 
 
191
 
   vl_video_buffer_adjust_size(&templ->width0, &height, plane,
192
 
                               chroma_format, false);
193
 
   templ->height0 = height;
194
 
}
195
 
 
196
 
static void
197
 
vl_video_buffer_destroy(struct pipe_video_buffer *buffer)
198
 
{
199
 
   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
200
 
   unsigned i;
201
 
 
202
 
   assert(buf);
203
 
 
204
 
   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
205
 
      pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
206
 
      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
207
 
      pipe_resource_reference(&buf->resources[i], NULL);
208
 
   }
209
 
 
210
 
   for (i = 0; i < VL_MAX_SURFACES; ++i)
211
 
      pipe_surface_reference(&buf->surfaces[i], NULL);
212
 
 
213
 
   vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL);
214
 
 
215
 
   FREE(buffer);
216
 
}
217
 
 
218
 
static struct pipe_sampler_view **
219
 
vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
220
 
{
221
 
   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
222
 
   unsigned num_planes = util_format_get_num_planes(buffer->buffer_format);
223
 
   struct pipe_sampler_view sv_templ;
224
 
   struct pipe_context *pipe;
225
 
   unsigned i;
226
 
 
227
 
   assert(buf);
228
 
 
229
 
   pipe = buf->base.context;
230
 
 
231
 
   for (i = 0; i < num_planes; ++i ) {
232
 
      if (!buf->sampler_view_planes[i]) {
233
 
         memset(&sv_templ, 0, sizeof(sv_templ));
234
 
         u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
235
 
 
236
 
         if (util_format_get_nr_components(buf->resources[i]->format) == 1)
237
 
            sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_X;
238
 
 
239
 
         buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
240
 
         if (!buf->sampler_view_planes[i])
241
 
            goto error;
242
 
      }
243
 
   }
244
 
 
245
 
   return buf->sampler_view_planes;
246
 
 
247
 
error:
248
 
   for (i = 0; i < num_planes; ++i )
249
 
      pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
250
 
 
251
 
   return NULL;
252
 
}
253
 
 
254
 
static struct pipe_sampler_view **
255
 
vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
256
 
{
257
 
   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
258
 
   struct pipe_sampler_view sv_templ;
259
 
   struct pipe_context *pipe;
260
 
   enum pipe_format sampler_format[VL_NUM_COMPONENTS];
261
 
   const unsigned *plane_order;
262
 
   unsigned i, j, component;
263
 
 
264
 
   assert(buf);
265
 
 
266
 
   pipe = buf->base.context;
267
 
 
268
 
   vl_get_video_buffer_formats(pipe->screen, buf->base.buffer_format, sampler_format);
269
 
   plane_order = vl_video_buffer_plane_order(buf->base.buffer_format);
270
 
 
271
 
   for (component = 0, i = 0; i < buf->num_planes; ++i ) {
272
 
      struct pipe_resource *res = buf->resources[plane_order[i]];
273
 
      const struct util_format_description *desc = util_format_description(res->format);
274
 
      unsigned nr_components = util_format_get_nr_components(res->format);
275
 
      if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
276
 
         nr_components = 3;
277
 
 
278
 
      for (j = 0; j < nr_components && component < VL_NUM_COMPONENTS; ++j, ++component) {
279
 
         if (buf->sampler_view_components[component])
280
 
            continue;
281
 
 
282
 
         memset(&sv_templ, 0, sizeof(sv_templ));
283
 
         u_sampler_view_default_template(&sv_templ, res, sampler_format[plane_order[i]]);
284
 
         sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_X + j;
285
 
         sv_templ.swizzle_a = PIPE_SWIZZLE_1;
286
 
         buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
287
 
         if (!buf->sampler_view_components[component])
288
 
            goto error;
289
 
      }
290
 
   }
291
 
   assert(component == VL_NUM_COMPONENTS);
292
 
 
293
 
   return buf->sampler_view_components;
294
 
 
295
 
error:
296
 
   for (i = 0; i < VL_NUM_COMPONENTS; ++i )
297
 
      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
298
 
 
299
 
   return NULL;
300
 
}
301
 
 
302
 
static struct pipe_surface **
303
 
vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)
304
 
{
305
 
   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
306
 
   struct pipe_surface surf_templ;
307
 
   struct pipe_context *pipe;
308
 
   unsigned i, j, array_size, surf;
309
 
 
310
 
   assert(buf);
311
 
 
312
 
   pipe = buf->base.context;
313
 
 
314
 
   array_size = buffer->interlaced ? 2 : 1;
315
 
   for (i = 0, surf = 0; i < VL_NUM_COMPONENTS; ++i) {
316
 
      for (j = 0; j < array_size; ++j, ++surf) {
317
 
         assert(surf < VL_MAX_SURFACES);
318
 
 
319
 
         if (!buf->resources[i]) {
320
 
            pipe_surface_reference(&buf->surfaces[surf], NULL);
321
 
            continue;
322
 
         }
323
 
 
324
 
         if (!buf->surfaces[surf]) {
325
 
            memset(&surf_templ, 0, sizeof(surf_templ));
326
 
            surf_templ.format = vl_video_buffer_surface_format(buf->resources[i]->format);
327
 
            surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = j;
328
 
            buf->surfaces[surf] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
329
 
            if (!buf->surfaces[surf])
330
 
               goto error;
331
 
         }
332
 
      }
333
 
   }
334
 
 
335
 
   return buf->surfaces;
336
 
 
337
 
error:
338
 
   for (i = 0; i < VL_MAX_SURFACES; ++i )
339
 
      pipe_surface_reference(&buf->surfaces[i], NULL);
340
 
 
341
 
   return NULL;
342
 
}
343
 
 
344
 
struct pipe_video_buffer *
345
 
vl_video_buffer_create(struct pipe_context *pipe,
346
 
                       const struct pipe_video_buffer *tmpl)
347
 
{
348
 
   enum pipe_format resource_formats[VL_NUM_COMPONENTS];
349
 
   struct pipe_video_buffer templat, *result;
350
 
   bool pot_buffers;
351
 
 
352
 
   assert(pipe);
353
 
   assert(tmpl->width > 0 && tmpl->height > 0);
354
 
 
355
 
   pot_buffers = !pipe->screen->get_video_param
356
 
   (
357
 
      pipe->screen,
358
 
      PIPE_VIDEO_PROFILE_UNKNOWN,
359
 
      PIPE_VIDEO_ENTRYPOINT_UNKNOWN,
360
 
      PIPE_VIDEO_CAP_NPOT_TEXTURES
361
 
   );
362
 
 
363
 
   vl_get_video_buffer_formats(pipe->screen, tmpl->buffer_format, resource_formats);
364
 
 
365
 
   templat = *tmpl;
366
 
   templat.width = pot_buffers ? util_next_power_of_two(tmpl->width)
367
 
                 : align(tmpl->width, VL_MACROBLOCK_WIDTH);
368
 
   templat.height = pot_buffers ? util_next_power_of_two(tmpl->height)
369
 
                  : align(tmpl->height, VL_MACROBLOCK_HEIGHT);
370
 
 
371
 
   if (tmpl->interlaced)
372
 
      templat.height /= 2;
373
 
 
374
 
   result = vl_video_buffer_create_ex
375
 
   (
376
 
      pipe, &templat, resource_formats,
377
 
      1, tmpl->interlaced ? 2 : 1, PIPE_USAGE_DEFAULT,
378
 
      pipe_format_to_chroma_format(templat.buffer_format)
379
 
   );
380
 
 
381
 
 
382
 
   if (result && tmpl->interlaced)
383
 
      result->height *= 2;
384
 
 
385
 
   return result;
386
 
}
387
 
 
388
 
struct pipe_video_buffer *
389
 
vl_video_buffer_create_ex(struct pipe_context *pipe,
390
 
                          const struct pipe_video_buffer *tmpl,
391
 
                          const enum pipe_format resource_formats[VL_NUM_COMPONENTS],
392
 
                          unsigned depth, unsigned array_size, unsigned usage,
393
 
                          enum pipe_video_chroma_format chroma_format)
394
 
{
395
 
   struct pipe_resource res_tmpl;
396
 
   struct pipe_resource *resources[VL_NUM_COMPONENTS];
397
 
   unsigned i;
398
 
 
399
 
   assert(pipe);
400
 
 
401
 
   memset(resources, 0, sizeof resources);
402
 
 
403
 
   vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[0], depth, array_size,
404
 
                            usage, 0, chroma_format);
405
 
   resources[0] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
406
 
   if (!resources[0])
407
 
      goto error;
408
 
 
409
 
   if (resource_formats[1] == PIPE_FORMAT_NONE) {
410
 
      assert(resource_formats[2] == PIPE_FORMAT_NONE);
411
 
      return vl_video_buffer_create_ex2(pipe, tmpl, resources);
412
 
   }
413
 
 
414
 
   vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[1], depth, array_size,
415
 
                            usage, 1, chroma_format);
416
 
   resources[1] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
417
 
   if (!resources[1])
418
 
      goto error;
419
 
 
420
 
   if (resource_formats[2] == PIPE_FORMAT_NONE)
421
 
      return vl_video_buffer_create_ex2(pipe, tmpl, resources);
422
 
 
423
 
   vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[2], depth, array_size,
424
 
                            usage, 2, chroma_format);
425
 
   resources[2] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
426
 
   if (!resources[2])
427
 
      goto error;
428
 
 
429
 
   return vl_video_buffer_create_ex2(pipe, tmpl, resources);
430
 
 
431
 
error:
432
 
   for (i = 0; i < VL_NUM_COMPONENTS; ++i)
433
 
      pipe_resource_reference(&resources[i], NULL);
434
 
 
435
 
   return NULL;
436
 
}
437
 
 
438
 
struct pipe_video_buffer *
439
 
vl_video_buffer_create_ex2(struct pipe_context *pipe,
440
 
                           const struct pipe_video_buffer *tmpl,
441
 
                           struct pipe_resource *resources[VL_NUM_COMPONENTS])
442
 
{
443
 
   struct vl_video_buffer *buffer;
444
 
   unsigned i;
445
 
 
446
 
   buffer = CALLOC_STRUCT(vl_video_buffer);
447
 
   if (!buffer)
448
 
      return NULL;
449
 
 
450
 
   buffer->base = *tmpl;
451
 
   buffer->base.context = pipe;
452
 
   buffer->base.destroy = vl_video_buffer_destroy;
453
 
   buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;
454
 
   buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;
455
 
   buffer->base.get_surfaces = vl_video_buffer_surfaces;
456
 
   buffer->num_planes = 0;
457
 
 
458
 
   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
459
 
      buffer->resources[i] = resources[i];
460
 
      if (resources[i])
461
 
         buffer->num_planes++;
462
 
   }
463
 
 
464
 
   return &buffer->base;
465
 
}
466
 
 
467
 
/* Create pipe_video_buffer by using resource_create with planar formats. */
468
 
struct pipe_video_buffer *
469
 
vl_video_buffer_create_as_resource(struct pipe_context *pipe,
470
 
                                   const struct pipe_video_buffer *tmpl,
471
 
                                   const uint64_t *modifiers,
472
 
                                   int modifiers_count)
473
 
{
474
 
   struct pipe_resource templ, *resources[VL_NUM_COMPONENTS] = {0};
475
 
   unsigned array_size =  tmpl->interlaced ? 2 : 1;
476
 
 
477
 
   memset(&templ, 0, sizeof(templ));
478
 
   templ.target = array_size > 1 ? PIPE_TEXTURE_2D_ARRAY : PIPE_TEXTURE_2D;
479
 
   templ.width0 = align(tmpl->width, VL_MACROBLOCK_WIDTH);
480
 
   templ.height0 = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
481
 
   templ.depth0 = 1;
482
 
   templ.array_size = array_size;
483
 
   templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
484
 
   templ.usage = PIPE_USAGE_DEFAULT;
485
 
 
486
 
   if (tmpl->buffer_format == PIPE_FORMAT_YUYV)
487
 
      templ.format = PIPE_FORMAT_R8G8_R8B8_UNORM;
488
 
   else if (tmpl->buffer_format == PIPE_FORMAT_UYVY)
489
 
      templ.format = PIPE_FORMAT_G8R8_B8R8_UNORM;
490
 
   else
491
 
      templ.format = tmpl->buffer_format;
492
 
 
493
 
   if (modifiers)
494
 
      resources[0] = pipe->screen->resource_create_with_modifiers(pipe->screen,
495
 
                                                                  &templ, modifiers,
496
 
                                                                  modifiers_count);
497
 
   else
498
 
      resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
499
 
   if (!resources[0])
500
 
      return NULL;
501
 
 
502
 
   if (resources[0]->next) {
503
 
      pipe_resource_reference(&resources[1], resources[0]->next);
504
 
      if (resources[1]->next)
505
 
         pipe_resource_reference(&resources[2], resources[1]->next);
506
 
   }
507
 
 
508
 
   struct pipe_video_buffer vidtemplate = *tmpl;
509
 
   vidtemplate.width = templ.width0;
510
 
   vidtemplate.height = templ.height0 * array_size;
511
 
   return vl_video_buffer_create_ex2(pipe, &vidtemplate, resources);
512
 
}