aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/winsys/drm/intel/gem/intel_drm_buffer.c
blob: e017cd2e982a8def787cf01d281ad1b0832543bd (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

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

#include "i915_drm.h"

static struct intel_buffer *
intel_drm_buffer_create(struct intel_winsys *iws,
                        unsigned size, unsigned alignment,
                        enum intel_buffer_type type)
{
   struct intel_drm_buffer *buf = CALLOC_STRUCT(intel_drm_buffer);
   struct intel_drm_winsys *idws = intel_drm_winsys(iws);
   drm_intel_bufmgr *pool;
   char *name;

   if (!buf)
      return NULL;

   buf->magic = 0xDEAD1337;
   buf->flinked = FALSE;
   buf->flink = 0;
   buf->map_gtt = FALSE;

   if (type == INTEL_NEW_TEXTURE) {
      name = "gallium3d_texture";
      pool = idws->pools.gem;
   } else if (type == INTEL_NEW_VERTEX) {
      name = "gallium3d_vertex";
      pool = idws->pools.gem;
   } else if (type == INTEL_NEW_SCANOUT) {
      name = "gallium3d_scanout";
      pool = idws->pools.gem;
      buf->map_gtt = TRUE;
   } else {
      assert(0);
      name = "gallium3d_unknown";
      pool = idws->pools.gem;
   }

   buf->bo = drm_intel_bo_alloc(pool, name, size, alignment);

   if (!buf->bo)
      goto err;

   return (struct intel_buffer *)buf;

err:
   assert(0);
   FREE(buf);
   return NULL;
}

static int
intel_drm_buffer_set_fence_reg(struct intel_winsys *iws,
                               struct intel_buffer *buffer,
                               unsigned stride,
                               enum intel_buffer_tile tile)
{
   assert(I915_TILING_NONE == INTEL_TILE_NONE);
   assert(I915_TILING_X == INTEL_TILE_X);
   assert(I915_TILING_Y == INTEL_TILE_Y);

   return drm_intel_bo_set_tiling(intel_bo(buffer), &tile, stride);
}

static void *
intel_drm_buffer_map(struct intel_winsys *iws,
                     struct intel_buffer *buffer,
                     boolean write)
{
   struct intel_drm_buffer *buf = intel_drm_buffer(buffer);
   drm_intel_bo *bo = intel_bo(buffer);
   int ret = 0;

   assert(bo);

   if (buf->map_count)
      goto out;

   if (buf->map_gtt)
      ret = drm_intel_gem_bo_map_gtt(bo);
   else
      ret = drm_intel_bo_map(bo, write);

   buf->ptr = bo->virtual;

   assert(ret == 0);
out:
   if (ret)
      return NULL;

   buf->map_count++;
   return buf->ptr;
}

static void
intel_drm_buffer_unmap(struct intel_winsys *iws,
                       struct intel_buffer *buffer)
{
   struct intel_drm_buffer *buf = intel_drm_buffer(buffer);

   if (--buf->map_count)
      return;

   if (buf->map_gtt)
      drm_intel_gem_bo_unmap_gtt(intel_bo(buffer));
   else
      drm_intel_bo_unmap(intel_bo(buffer));
}

static void
intel_drm_buffer_destroy(struct intel_winsys *iws,
                         struct intel_buffer *buffer)
{
   drm_intel_bo_unreference(intel_bo(buffer));

#ifdef DEBUG
   intel_drm_buffer(buffer)->magic = 0;
   intel_drm_buffer(buffer)->bo = NULL;
#endif

   FREE(buffer);
}

void
intel_drm_winsys_init_buffer_functions(struct intel_drm_winsys *idws)
{
   idws->base.buffer_create = intel_drm_buffer_create;
   idws->base.buffer_set_fence_reg = intel_drm_buffer_set_fence_reg;
   idws->base.buffer_map = intel_drm_buffer_map;
   idws->base.buffer_unmap = intel_drm_buffer_unmap;
   idws->base.buffer_destroy = intel_drm_buffer_destroy;
}