1#include "drm-uapi/drm_fourcc.h"
2
3#include "pipe/p_context.h"
4#include "nvc0/nvc0_resource.h"
5#include "nouveau_screen.h"
6
7
8static struct pipe_resource *
9nvc0_resource_create(struct pipe_screen *screen,
10                     const struct pipe_resource *templ)
11{
12   const uint64_t modifier = DRM_FORMAT_MOD_INVALID;
13
14   switch (templ->target) {
15   case PIPE_BUFFER:
16      return nouveau_buffer_create(screen, templ);
17   default:
18      return nvc0_miptree_create(screen, templ, &modifier, 1);
19   }
20}
21
22static struct pipe_resource *
23nvc0_resource_create_with_modifiers(struct pipe_screen *screen,
24                                    const struct pipe_resource *templ,
25                                    const uint64_t *modifiers, int count)
26{
27   switch (templ->target) {
28   case PIPE_BUFFER:
29      return nouveau_buffer_create(screen, templ);
30   default:
31      return nvc0_miptree_create(screen, templ, modifiers, count);
32   }
33}
34
35static void
36nvc0_query_dmabuf_modifiers(struct pipe_screen *screen,
37                            enum pipe_format format, int max,
38                            uint64_t *modifiers, unsigned int *external_only,
39                            int *count)
40{
41   static const uint64_t supported_modifiers[] = {
42      DRM_FORMAT_MOD_LINEAR,
43      DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_ONE_GOB,
44      DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_TWO_GOB,
45      DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_FOUR_GOB,
46      DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_EIGHT_GOB,
47      DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_SIXTEEN_GOB,
48      DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_THIRTYTWO_GOB,
49   };
50   int i, num = 0;
51
52   if (max > ARRAY_SIZE(supported_modifiers))
53      max = ARRAY_SIZE(supported_modifiers);
54
55   if (!max) {
56      max = ARRAY_SIZE(supported_modifiers);
57      external_only = NULL;
58      modifiers = NULL;
59   }
60
61   for (i = 0; i < max; i++) {
62      if (modifiers)
63         modifiers[num] = supported_modifiers[i];
64
65      if (external_only)
66         external_only[num] = 0;
67
68      num++;
69   }
70
71   *count = num;
72}
73
74static struct pipe_resource *
75nvc0_resource_from_handle(struct pipe_screen * screen,
76                          const struct pipe_resource *templ,
77                          struct winsys_handle *whandle,
78                          unsigned usage)
79{
80   if (templ->target == PIPE_BUFFER) {
81      return NULL;
82   } else {
83      struct pipe_resource *res = nv50_miptree_from_handle(screen,
84                                                           templ, whandle);
85      if (res)
86         nv04_resource(res)->vtbl = &nvc0_miptree_vtbl;
87      return res;
88   }
89}
90
91static struct pipe_surface *
92nvc0_surface_create(struct pipe_context *pipe,
93                    struct pipe_resource *pres,
94                    const struct pipe_surface *templ)
95{
96   if (unlikely(pres->target == PIPE_BUFFER))
97      return nv50_surface_from_buffer(pipe, pres, templ);
98   return nvc0_miptree_surface_new(pipe, pres, templ);
99}
100
101void
102nvc0_init_resource_functions(struct pipe_context *pcontext)
103{
104   pcontext->transfer_map = u_transfer_map_vtbl;
105   pcontext->transfer_flush_region = u_transfer_flush_region_vtbl;
106   pcontext->transfer_unmap = u_transfer_unmap_vtbl;
107   pcontext->buffer_subdata = u_default_buffer_subdata;
108   pcontext->texture_subdata = u_default_texture_subdata;
109   pcontext->create_surface = nvc0_surface_create;
110   pcontext->surface_destroy = nv50_surface_destroy;
111   pcontext->invalidate_resource = nv50_invalidate_resource;
112}
113
114void
115nvc0_screen_init_resource_functions(struct pipe_screen *pscreen)
116{
117   pscreen->resource_create = nvc0_resource_create;
118   pscreen->resource_create_with_modifiers = nvc0_resource_create_with_modifiers;
119   pscreen->query_dmabuf_modifiers = nvc0_query_dmabuf_modifiers;
120   pscreen->resource_from_handle = nvc0_resource_from_handle;
121   pscreen->resource_get_handle = u_resource_get_handle_vtbl;
122   pscreen->resource_destroy = u_resource_destroy_vtbl;
123}
124