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