intel_winsys: remove leftover code
[mesa.git] / src / mesa / drivers / dri / intel_winsys / intel_winsys_pipe.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28 /*
29 * Authors: Keith Whitwell <keithw-at-tungstengraphics-dot-com>
30 */
31
32 #include <stdlib.h>
33 #include <xf86drm.h>
34 #include "dri_bufpool.h"
35 #include "dri_bufmgr.h"
36
37 #include "intel_context.h"
38 #include "intel_winsys.h"
39 #include "intel_swapbuffers.h"
40 #include "intel_batchbuffer.h"
41
42 #include "pipe/p_winsys.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_state.h"
45 #include "pipe/p_util.h"
46 #include "pipe/p_inlines.h"
47
48
49
50 struct intel_pipe_winsys {
51 struct pipe_winsys winsys;
52 struct _DriBufferPool *regionPool;
53 };
54
55
56
57 /* Turn a pipe winsys into an intel/pipe winsys:
58 */
59 static inline struct intel_pipe_winsys *
60 intel_pipe_winsys( struct pipe_winsys *winsys )
61 {
62 return (struct intel_pipe_winsys *)winsys;
63 }
64
65
66 /* Most callbacks map direcly onto dri_bufmgr operations:
67 */
68 static void *intel_buffer_map(struct pipe_winsys *winsys,
69 struct pipe_buffer *buf,
70 unsigned flags )
71 {
72 unsigned drm_flags = 0;
73
74 if (flags & PIPE_BUFFER_USAGE_CPU_WRITE)
75 drm_flags |= DRM_BO_FLAG_WRITE;
76
77 if (flags & PIPE_BUFFER_USAGE_CPU_READ)
78 drm_flags |= DRM_BO_FLAG_READ;
79
80 return driBOMap( dri_bo(buf), drm_flags, 0 );
81 }
82
83 static void intel_buffer_unmap(struct pipe_winsys *winsys,
84 struct pipe_buffer *buf)
85 {
86 driBOUnmap( dri_bo(buf) );
87 }
88
89
90 static void
91 intel_buffer_destroy(struct pipe_winsys *winsys,
92 struct pipe_buffer *buf)
93 {
94 driBOUnReference( dri_bo(buf) );
95 }
96
97
98 /* Pipe has no concept of pools. We choose the tex/region pool
99 * for all buffers.
100 * Grabs the hardware lock!
101 */
102 static struct pipe_buffer *
103 intel_buffer_create(struct pipe_winsys *winsys,
104 unsigned alignment,
105 unsigned usage,
106 unsigned size )
107 {
108 struct intel_buffer *buffer = CALLOC_STRUCT( intel_buffer );
109 struct intel_pipe_winsys *iws = intel_pipe_winsys(winsys);
110 unsigned flags = 0;
111
112 buffer->base.refcount = 1;
113 buffer->base.alignment = alignment;
114 buffer->base.usage = usage;
115 buffer->base.size = size;
116
117 if (usage & (PIPE_BUFFER_USAGE_VERTEX /*| IWS_BUFFER_USAGE_LOCAL*/)) {
118 flags |= DRM_BO_FLAG_MEM_LOCAL | DRM_BO_FLAG_CACHED;
119 } else {
120 flags |= DRM_BO_FLAG_MEM_VRAM | DRM_BO_FLAG_MEM_TT;
121 }
122
123 if (usage & PIPE_BUFFER_USAGE_GPU_READ)
124 flags |= DRM_BO_FLAG_READ;
125
126 if (usage & PIPE_BUFFER_USAGE_GPU_WRITE)
127 flags |= DRM_BO_FLAG_WRITE;
128
129 /* drm complains if we don't set any read/write flags.
130 */
131 if ((flags & (DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE)) == 0)
132 flags |= DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE;
133
134 #if 0
135 if (flags & IWS_BUFFER_USAGE_EXE)
136 flags |= DRM_BO_FLAG_EXE;
137
138 if (usage & IWS_BUFFER_USAGE_CACHED)
139 flags |= DRM_BO_FLAG_CACHED;
140 #endif
141
142 driGenBuffers( iws->regionPool,
143 "pipe buffer", 1, &buffer->driBO, alignment, flags, 0 );
144
145 driBOData( buffer->driBO, size, NULL, 0 );
146
147 return &buffer->base;
148 }
149
150
151 static struct pipe_buffer *
152 intel_user_buffer_create(struct pipe_winsys *winsys, void *ptr, unsigned bytes)
153 {
154 struct intel_buffer *buffer = CALLOC_STRUCT( intel_buffer );
155 struct intel_pipe_winsys *iws = intel_pipe_winsys(winsys);
156
157 driGenUserBuffer( iws->regionPool,
158 "pipe user buffer", &buffer->driBO, ptr, bytes);
159
160 return &buffer->base;
161 }
162
163
164 /* The state tracker (should!) keep track of whether the fake
165 * frontbuffer has been touched by any rendering since the last time
166 * we copied its contents to the real frontbuffer. Our task is easy:
167 */
168 static void
169 intel_flush_frontbuffer( struct pipe_winsys *winsys,
170 struct pipe_surface *surf,
171 void *context_private)
172 {
173 struct intel_context *intel = (struct intel_context *) context_private;
174 __DRIdrawablePrivate *dPriv = intel->driDrawable;
175
176 intelDisplaySurface(dPriv, surf, NULL);
177 }
178
179
180 static struct pipe_surface *
181 intel_i915_surface_alloc(struct pipe_winsys *winsys)
182 {
183 struct pipe_surface *surf = CALLOC_STRUCT(pipe_surface);
184 if (surf) {
185 surf->refcount = 1;
186 surf->winsys = winsys;
187 }
188 return surf;
189 }
190
191
192 /**
193 * Round n up to next multiple.
194 */
195 static INLINE unsigned
196 round_up(unsigned n, unsigned multiple)
197 {
198 return (n + multiple - 1) & ~(multiple - 1);
199 }
200
201 /**
202 * Copied from xm_winsys.c
203 */
204 static int
205 intel_i915_surface_alloc_storage(struct pipe_winsys *winsys,
206 struct pipe_surface *surf,
207 unsigned width, unsigned height,
208 enum pipe_format format,
209 unsigned flags)
210 {
211 const unsigned alignment = 64;
212 int ret;
213
214 surf->width = width;
215 surf->height = height;
216 surf->format = format;
217 surf->cpp = pf_get_size(format);
218 surf->pitch = round_up(width, alignment / surf->cpp);
219
220 assert(!surf->buffer);
221 surf->buffer = winsys->buffer_create(winsys, alignment,
222 PIPE_BUFFER_USAGE_PIXEL,
223 surf->pitch * surf->cpp * height);
224 if(!surf->buffer)
225 return -1;
226
227 return 0;
228 }
229
230
231 static void
232 intel_i915_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
233 {
234 struct pipe_surface *surf = *s;
235 surf->refcount--;
236 if (surf->refcount == 0) {
237 if (surf->buffer)
238 pipe_buffer_reference(winsys, &surf->buffer, NULL);
239 free(surf);
240 }
241 *s = NULL;
242 }
243
244
245
246 static void
247 intel_printf( struct pipe_winsys *winsys, const char *fmtString, ... )
248 {
249 va_list args;
250 va_start( args, fmtString );
251 vfprintf(stderr, fmtString, args);
252 va_end( args );
253 }
254
255 static const char *
256 intel_get_name( struct pipe_winsys *winsys )
257 {
258 return "Intel/DRI/ttm";
259 }
260
261
262 struct pipe_winsys *
263 intel_create_pipe_winsys( int fd )
264 {
265 struct intel_pipe_winsys *iws = CALLOC_STRUCT( intel_pipe_winsys );
266
267 /* Fill in this struct with callbacks that pipe will need to
268 * communicate with the window system, buffer manager, etc.
269 *
270 * Pipe would be happy with a malloc based memory manager, but
271 * the SwapBuffers implementation in this winsys driver requires
272 * that rendering be done to an appropriate _DriBufferObject.
273 */
274 iws->winsys.buffer_create = intel_buffer_create;
275 iws->winsys.user_buffer_create = intel_user_buffer_create;
276 iws->winsys.buffer_map = intel_buffer_map;
277 iws->winsys.buffer_unmap = intel_buffer_unmap;
278 iws->winsys.buffer_destroy = intel_buffer_destroy;
279 iws->winsys.flush_frontbuffer = intel_flush_frontbuffer;
280 iws->winsys.printf = intel_printf;
281 iws->winsys.get_name = intel_get_name;
282 iws->winsys.surface_alloc = intel_i915_surface_alloc;
283 iws->winsys.surface_alloc_storage = intel_i915_surface_alloc_storage;
284 iws->winsys.surface_release = intel_i915_surface_release;
285
286 if (fd)
287 iws->regionPool = driDRMPoolInit(fd);
288
289 return &iws->winsys;
290 }
291
292
293 void
294 intel_destroy_pipe_winsys( struct pipe_winsys *winsys )
295 {
296 struct intel_pipe_winsys *iws = intel_pipe_winsys(winsys);
297 if (iws->regionPool) {
298 driPoolTakeDown(iws->regionPool);
299 }
300 free(iws);
301 }
302