Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / winsys / egl_drm / intel / intel_winsys_i915.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 "ws_dri_bufpool.h"
35 #include "ws_dri_bufmgr.h"
36
37 #include "intel_context.h"
38 #include "intel_batchbuffer.h"
39 #include "intel_winsys.h"
40
41 #include "pipe/p_util.h"
42 #include "pipe/p_winsys.h"
43 #include "i915simple/i915_winsys.h"
44 #include "i915simple/i915_screen.h"
45
46
47 struct intel_i915_winsys {
48 struct i915_winsys winsys; /**< batch buffer funcs */
49 struct pipe_winsys *pws;
50 struct intel_context *intel;
51 };
52
53
54 /* Turn a i915simple winsys into an intel/i915simple winsys:
55 */
56 static inline struct intel_i915_winsys *
57 intel_i915_winsys( struct i915_winsys *sws )
58 {
59 return (struct intel_i915_winsys *)sws;
60 }
61
62
63 /* Simple batchbuffer interface:
64 */
65
66 static unsigned *intel_i915_batch_start( struct i915_winsys *sws,
67 unsigned dwords,
68 unsigned relocs )
69 {
70 struct intel_context *intel = intel_i915_winsys(sws)->intel;
71
72 /* XXX: check relocs.
73 */
74 if (intel_batchbuffer_space( intel->batch ) >= dwords * 4) {
75 /* XXX: Hmm, the driver can't really do much with this pointer:
76 */
77 return (unsigned *)intel->batch->ptr;
78 }
79 else
80 return NULL;
81 }
82
83 static void intel_i915_batch_dword( struct i915_winsys *sws,
84 unsigned dword )
85 {
86 struct intel_context *intel = intel_i915_winsys(sws)->intel;
87 intel_batchbuffer_emit_dword( intel->batch, dword );
88 }
89
90 static void intel_i915_batch_reloc( struct i915_winsys *sws,
91 struct pipe_buffer *buf,
92 unsigned access_flags,
93 unsigned delta )
94 {
95 struct intel_context *intel = intel_i915_winsys(sws)->intel;
96 unsigned flags = DRM_BO_FLAG_MEM_TT;
97 unsigned mask = DRM_BO_MASK_MEM;
98
99 if (access_flags & I915_BUFFER_ACCESS_WRITE) {
100 flags |= DRM_BO_FLAG_WRITE;
101 mask |= DRM_BO_FLAG_WRITE;
102 }
103
104 if (access_flags & I915_BUFFER_ACCESS_READ) {
105 flags |= DRM_BO_FLAG_READ;
106 mask |= DRM_BO_FLAG_READ;
107 }
108
109 #if 0 /* JB old */
110 intel_batchbuffer_emit_reloc( intel->batch,
111 dri_bo( buf ),
112 flags, mask,
113 delta );
114 #else /* new */
115 intel_offset_relocation( intel->batch,
116 delta,
117 dri_bo( buf ),
118 flags,
119 mask );
120 #endif
121 }
122
123
124
125 static void intel_i915_batch_flush( struct i915_winsys *sws,
126 struct pipe_fence_handle **fence )
127 {
128 struct intel_i915_winsys *iws = intel_i915_winsys(sws);
129 struct intel_context *intel = iws->intel;
130 union {
131 struct _DriFenceObject *dri;
132 struct pipe_fence_handle *pipe;
133 } fu;
134
135 if (fence)
136 assert(!*fence);
137
138 fu.dri = intel_batchbuffer_flush( intel->batch );
139
140 if (!fu.dri) {
141 assert(0);
142 *fence = NULL;
143 return;
144 }
145
146 if (fu.dri) {
147 if (fence)
148 *fence = fu.pipe;
149 else
150 driFenceUnReference(&fu.dri);
151 }
152
153 }
154
155
156 /**
157 * Create i915 hardware rendering context.
158 */
159 struct pipe_context *
160 intel_create_i915simple( struct intel_context *intel,
161 struct pipe_winsys *winsys )
162 {
163 struct intel_i915_winsys *iws = CALLOC_STRUCT( intel_i915_winsys );
164 struct pipe_screen *screen;
165
166 /* Fill in this struct with callbacks that i915simple will need to
167 * communicate with the window system, buffer manager, etc.
168 */
169 iws->winsys.batch_start = intel_i915_batch_start;
170 iws->winsys.batch_dword = intel_i915_batch_dword;
171 iws->winsys.batch_reloc = intel_i915_batch_reloc;
172 iws->winsys.batch_flush = intel_i915_batch_flush;
173 iws->pws = winsys;
174 iws->intel = intel;
175
176 screen = i915_create_screen(winsys, PCI_CHIP_I945_GM);
177 assert(screen);
178
179 /* Create the i915simple context:
180 */
181 return i915_create_context( screen,
182 winsys,
183 &iws->winsys );
184 }