summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/intel_winsys/intel_winsys_pipe.c
blob: 9c3fd56d8d2ca55ab6ca515573a4ae608c2b216c (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**************************************************************************
 * 
 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * 
 **************************************************************************/
/*
 * Authors: Keith Whitwell <keithw-at-tungstengraphics-dot-com>
 */

#include <stdlib.h>
#include <xf86drm.h>
#include "dri_bufpool.h"
#include "dri_bufmgr.h"

#include "intel_context.h"
#include "intel_winsys.h"
#include "intel_swapbuffers.h"
#include "intel_batchbuffer.h"

#include "pipe/p_winsys.h"
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "pipe/p_util.h"



struct intel_pipe_winsys {
   struct pipe_winsys winsys;
   struct intel_context *intel;
};



/* Turn a pipe winsys into an intel/pipe winsys:
 */
static inline struct intel_pipe_winsys *
intel_pipe_winsys( struct pipe_winsys *sws )
{
   return (struct intel_pipe_winsys *)sws;
}


/* Most callbacks map direcly onto dri_bufmgr operations:
 */
static void *intel_buffer_map(struct pipe_winsys *sws, 
			      struct pipe_buffer_handle *buf,
			      unsigned flags )
{
   unsigned drm_flags = 0;
   
   if (flags & PIPE_BUFFER_FLAG_WRITE)
      drm_flags |= DRM_BO_FLAG_WRITE;

   if (flags & PIPE_BUFFER_FLAG_READ)
      drm_flags |= DRM_BO_FLAG_READ;

   return driBOMap( dri_bo(buf), drm_flags, 0 );
}

static void intel_buffer_unmap(struct pipe_winsys *sws, 
			       struct pipe_buffer_handle *buf)
{
   driBOUnmap( dri_bo(buf) );
}


static void
intel_buffer_reference(struct pipe_winsys *sws,
		       struct pipe_buffer_handle **ptr,
		       struct pipe_buffer_handle *buf)
{
   if (*ptr) {
      driBOUnReference( dri_bo(*ptr) );
      *ptr = NULL;
   }

   if (buf) {
      driBOReference( dri_bo(buf) );
      *ptr = buf;
   }
}


/* Grabs the hardware lock!
 */
static void intel_buffer_data(struct pipe_winsys *sws, 
			      struct pipe_buffer_handle *buf,
			      unsigned size, const void *data )
{
   struct intel_context *intel = intel_pipe_winsys(sws)->intel;

   LOCK_HARDWARE( intel );
   driBOData( dri_bo(buf), size, data, 0 );
   UNLOCK_HARDWARE( intel );
}

static void intel_buffer_subdata(struct pipe_winsys *sws, 
				 struct pipe_buffer_handle *buf,
				 unsigned long offset, 
				 unsigned long size, 
				 const void *data)
{
   driBOSubData( dri_bo(buf), offset, size, data );
}

static void intel_buffer_get_subdata(struct pipe_winsys *sws, 
				     struct pipe_buffer_handle *buf,
				     unsigned long offset, 
				     unsigned long size, 
				     void *data)
{
   driBOGetSubData( dri_bo(buf), offset, size, data );
}

/* Pipe has no concept of pools.  We choose the tex/region pool
 * for all buffers.
 */
static struct pipe_buffer_handle *
intel_buffer_create(struct pipe_winsys *sws, 
		    unsigned alignment)
{
   struct intel_context *intel = intel_pipe_winsys(sws)->intel;
   struct _DriBufferObject *buffer;

   LOCK_HARDWARE( intel );
   driGenBuffers( intel->intelScreen->regionPool, 
		  "pipe buffer", 1, &buffer, alignment, 0, 0 );
   UNLOCK_HARDWARE( intel );

   return pipe_bo(buffer);
}


static struct pipe_buffer_handle *
intel_user_buffer_create(struct pipe_winsys *sws, void *ptr, unsigned bytes)
{
   struct intel_context *intel = intel_pipe_winsys(sws)->intel;
   struct _DriBufferObject *buffer;

   LOCK_HARDWARE( intel );
   driGenUserBuffer( intel->intelScreen->regionPool, 
                     "pipe user buffer", &buffer, ptr, bytes);
   UNLOCK_HARDWARE( intel );

   return pipe_bo(buffer);
}


static void intel_wait_idle( struct pipe_winsys *sws )
{
   struct intel_context *intel = intel_pipe_winsys(sws)->intel;

   if (intel->batch->last_fence) {
      driFenceFinish(intel->batch->last_fence, 
		     DRM_FENCE_TYPE_EXE | DRM_I915_FENCE_TYPE_RW, GL_FALSE);
      driFenceUnReference(intel->batch->last_fence);
      intel->batch->last_fence = NULL;
   }
}


/* The state tracker (should!) keep track of whether the fake
 * frontbuffer has been touched by any rendering since the last time
 * we copied its contents to the real frontbuffer.  Our task is easy:
 */
static void
intel_flush_frontbuffer( struct pipe_winsys *sws,
                         struct pipe_surface *surf )
{
   struct intel_context *intel = intel_pipe_winsys(sws)->intel;
   __DRIdrawablePrivate *dPriv = intel->driDrawable;

   intelDisplayBuffer(dPriv, surf, NULL);
}


static struct pipe_region *
intel_i915_region_alloc(struct pipe_winsys *winsys,
                        unsigned cpp, unsigned width,
                        unsigned height, unsigned flags)
{
   struct pipe_region *region = calloc(sizeof(*region), 1);
   const unsigned alignment = 64;

   /* Choose a pitch to match hardware requirements - requires 64 byte
    * alignment of render targets.  
    *
    * XXX: is this ok for textures??
    * clearly want to be able to render to textures under some
    * circumstances, but maybe not always a requirement.
    */
   unsigned pitch;

   /* XXX is the pitch different for textures vs. drawables? */
   if (flags & PIPE_SURFACE_FLAG_TEXTURE)  /* or PIPE_SURFACE_FLAG_RENDER? */
      pitch = ((cpp * width + 63) & ~63) / cpp;
   else
      pitch = ((cpp * width + 63) & ~63) / cpp;

   region->cpp = cpp;
   region->pitch = pitch;
   region->height = height;     /* needed? */
   region->refcount = 1;

   region->buffer = winsys->buffer_create( winsys, alignment );

   winsys->buffer_data( winsys,
                        region->buffer, 
                        pitch * cpp * height, 
                        NULL );

   return region;
}

static void
intel_i915_region_release(struct pipe_winsys *winsys,
                          struct pipe_region **region)
{
   if (!*region)
      return;

   assert((*region)->refcount > 0);
   (*region)->refcount--;

   if ((*region)->refcount == 0) {
      assert((*region)->map_refcount == 0);

      winsys->buffer_reference( winsys,
                                &((*region)->buffer), NULL );
      free(*region);
   }
   *region = NULL;
}


static struct pipe_surface *
intel_i915_surface_alloc(struct pipe_winsys *winsys, unsigned format)
{
   struct pipe_surface *surf = CALLOC_STRUCT(pipe_surface);
   if (surf) {
      surf->format = format;
      surf->refcount = 1;
   }
   return surf;
}


static void
intel_printf( struct pipe_winsys *sws, const char *fmtString, ... )
{
   va_list args;
   va_start( args, fmtString );  
   vfprintf(stderr, fmtString, args);
   va_end( args );
}

static const char *
intel_get_name( struct pipe_winsys *sws )
{
   return "Intel/DRI/ttm";
}


struct pipe_winsys *
intel_create_pipe_winsys( struct intel_context *intel )
{
   struct intel_pipe_winsys *iws = CALLOC_STRUCT( intel_pipe_winsys );
   
   /* Fill in this struct with callbacks that pipe will need to
    * communicate with the window system, buffer manager, etc. 
    *
    * Pipe would be happy with a malloc based memory manager, but
    * the SwapBuffers implementation in this winsys driver requires
    * that rendering be done to an appropriate _DriBufferObject.  
    */
   iws->winsys.buffer_create = intel_buffer_create;
   iws->winsys.user_buffer_create = intel_user_buffer_create;
   iws->winsys.buffer_map = intel_buffer_map;
   iws->winsys.buffer_unmap = intel_buffer_unmap;
   iws->winsys.buffer_reference = intel_buffer_reference;
   iws->winsys.buffer_data = intel_buffer_data;
   iws->winsys.buffer_subdata = intel_buffer_subdata;
   iws->winsys.buffer_get_subdata = intel_buffer_get_subdata;
   iws->winsys.flush_frontbuffer = intel_flush_frontbuffer;
   iws->winsys.wait_idle = intel_wait_idle;
   iws->winsys.printf = intel_printf;
   iws->winsys.get_name = intel_get_name;
   iws->intel = intel;

   iws->winsys.region_alloc = intel_i915_region_alloc;
   iws->winsys.region_release = intel_i915_region_release;

   iws->winsys.surface_alloc = intel_i915_surface_alloc;

   return &iws->winsys;
}