• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include "i915_sw_winsys.h"
3 #include "util/u_memory.h"
4 
5 static struct i915_winsys_buffer *
i915_sw_buffer_create(struct i915_winsys * iws,unsigned size,enum i915_winsys_buffer_type type)6 i915_sw_buffer_create(struct i915_winsys *iws,
7                       unsigned size,
8                       enum i915_winsys_buffer_type type)
9 {
10    struct i915_sw_buffer *buf = CALLOC_STRUCT(i915_sw_buffer);
11 
12    if (!buf)
13       return NULL;
14 
15    buf->magic = 0xDEAD1337;
16    buf->type = type;
17    buf->ptr = CALLOC(size, 1);
18 
19    if (!buf->ptr)
20       goto err;
21 
22    return (struct i915_winsys_buffer *)buf;
23 
24 err:
25    assert(0);
26    FREE(buf);
27    return NULL;
28 }
29 
30 static struct i915_winsys_buffer *
i915_sw_buffer_create_tiled(struct i915_winsys * iws,unsigned * stride,unsigned height,enum i915_winsys_buffer_tile * tiling,enum i915_winsys_buffer_type type)31 i915_sw_buffer_create_tiled(struct i915_winsys *iws,
32                       unsigned *stride, unsigned height,
33                       enum i915_winsys_buffer_tile *tiling,
34                       enum i915_winsys_buffer_type type)
35 {
36    struct i915_sw_buffer *buf = CALLOC_STRUCT(i915_sw_buffer);
37 
38    if (!buf)
39       return NULL;
40 
41    buf->magic = 0xDEAD1337;
42    buf->type = type;
43    buf->ptr = CALLOC(*stride * height, 1);
44    buf->tiling = *tiling;
45    buf->stride = *stride;
46 
47    if (!buf->ptr)
48       goto err;
49 
50    return (struct i915_winsys_buffer *)buf;
51 
52 err:
53    assert(0);
54    FREE(buf);
55    return NULL;
56 }
57 
58 static void *
i915_sw_buffer_map(struct i915_winsys * iws,struct i915_winsys_buffer * buffer,boolean write)59 i915_sw_buffer_map(struct i915_winsys *iws,
60                    struct i915_winsys_buffer *buffer,
61                    boolean write)
62 {
63    struct i915_sw_buffer *buf = i915_sw_buffer(buffer);
64 
65    buf->map_count += 1;
66    return buf->ptr;
67 }
68 
69 static void
i915_sw_buffer_unmap(struct i915_winsys * iws,struct i915_winsys_buffer * buffer)70 i915_sw_buffer_unmap(struct i915_winsys *iws,
71                      struct i915_winsys_buffer *buffer)
72 {
73    struct i915_sw_buffer *buf = i915_sw_buffer(buffer);
74 
75    buf->map_count -= 1;
76 }
77 
78 static int
i915_sw_buffer_write(struct i915_winsys * iws,struct i915_winsys_buffer * buffer,size_t offset,size_t size,const void * data)79 i915_sw_buffer_write(struct i915_winsys *iws,
80                      struct i915_winsys_buffer *buffer,
81                      size_t offset,
82                      size_t size,
83                      const void *data)
84 {
85    struct i915_sw_buffer *buf = i915_sw_buffer(buffer);
86 
87    memcpy((char*)buf->ptr + offset, data, size);
88    return 0;
89 }
90 
91 static void
i915_sw_buffer_destroy(struct i915_winsys * iws,struct i915_winsys_buffer * buffer)92 i915_sw_buffer_destroy(struct i915_winsys *iws,
93                        struct i915_winsys_buffer *buffer)
94 {
95    struct i915_sw_buffer *buf = i915_sw_buffer(buffer);
96 
97 #ifdef DEBUG
98    buf->magic = 0;
99 #endif
100 
101    FREE(buf->ptr);
102    FREE(buf);
103 }
104 
105 void
i915_sw_winsys_init_buffer_functions(struct i915_sw_winsys * isws)106 i915_sw_winsys_init_buffer_functions(struct i915_sw_winsys *isws)
107 {
108    isws->base.buffer_create = i915_sw_buffer_create;
109    isws->base.buffer_create_tiled = i915_sw_buffer_create_tiled;
110    isws->base.buffer_map = i915_sw_buffer_map;
111    isws->base.buffer_unmap = i915_sw_buffer_unmap;
112    isws->base.buffer_write = i915_sw_buffer_write;
113    isws->base.buffer_destroy = i915_sw_buffer_destroy;
114 }
115