a5de01a3a844718d308fe6a92a1963c9b0c44a0f
[mesa.git] / src / mesa / drivers / dri / intel / intel_regions.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Provide additional functionality on top of bufmgr buffers:
29 * - 2d semantics and blit operations
30 * - refcounting of buffers for multiple images in a buffer.
31 * - refcounting of buffer mappings.
32 * - some logic for moving the buffers to the best memory pools for
33 * given operations.
34 *
35 * Most of this is to make it easier to implement the fixed-layout
36 * mipmap tree required by intel hardware in the face of GL's
37 * programming interface where each image can be specifed in random
38 * order and it isn't clear what layout the tree should have until the
39 * last moment.
40 */
41
42 #include "intel_context.h"
43 #include "intel_regions.h"
44 #include "intel_blit.h"
45 #include "intel_buffer_objects.h"
46 #include "dri_bufmgr.h"
47 #include "intel_bufmgr_ttm.h"
48 #include "intel_batchbuffer.h"
49
50 #define FILE_DEBUG_FLAG DEBUG_REGION
51
52 /* XXX: Thread safety?
53 */
54 GLubyte *
55 intel_region_map(struct intel_context *intel, struct intel_region *region)
56 {
57 DBG("%s\n", __FUNCTION__);
58 if (!region->map_refcount++) {
59 if (region->pbo)
60 intel_region_cow(intel, region);
61
62 dri_bo_map(region->buffer, GL_TRUE);
63 region->map = region->buffer->virtual;
64 }
65
66 return region->map;
67 }
68
69 void
70 intel_region_unmap(struct intel_context *intel, struct intel_region *region)
71 {
72 DBG("%s\n", __FUNCTION__);
73 if (!--region->map_refcount) {
74 dri_bo_unmap(region->buffer);
75 region->map = NULL;
76 }
77 }
78
79 struct intel_region *
80 intel_region_alloc(struct intel_context *intel,
81 GLuint cpp, GLuint pitch, GLuint height)
82 {
83 struct intel_region *region = calloc(sizeof(*region), 1);
84
85 DBG("%s\n", __FUNCTION__);
86
87 region->cpp = cpp;
88 region->pitch = pitch;
89 region->height = height; /* needed? */
90 region->refcount = 1;
91
92 region->buffer = dri_bo_alloc(intel->bufmgr, "region",
93 pitch * cpp * height, 64, DRM_BO_FLAG_MEM_TT);
94 return region;
95 }
96
97 void
98 intel_region_reference(struct intel_region **dst, struct intel_region *src)
99 {
100 assert(*dst == NULL);
101 if (src) {
102 src->refcount++;
103 *dst = src;
104 }
105 }
106
107 void
108 intel_region_release(struct intel_region **region)
109 {
110 if (!*region)
111 return;
112
113 DBG("%s %d\n", __FUNCTION__, (*region)->refcount - 1);
114
115 ASSERT((*region)->refcount > 0);
116 (*region)->refcount--;
117
118 if ((*region)->refcount == 0) {
119 assert((*region)->map_refcount == 0);
120
121 if ((*region)->pbo)
122 (*region)->pbo->region = NULL;
123 (*region)->pbo = NULL;
124 dri_bo_unreference((*region)->buffer);
125 free(*region);
126 }
127 *region = NULL;
128 }
129
130 /*
131 * XXX Move this into core Mesa?
132 */
133 static void
134 _mesa_copy_rect(GLubyte * dst,
135 GLuint cpp,
136 GLuint dst_pitch,
137 GLuint dst_x,
138 GLuint dst_y,
139 GLuint width,
140 GLuint height,
141 const GLubyte * src,
142 GLuint src_pitch, GLuint src_x, GLuint src_y)
143 {
144 GLuint i;
145
146 dst_pitch *= cpp;
147 src_pitch *= cpp;
148 dst += dst_x * cpp;
149 src += src_x * cpp;
150 dst += dst_y * dst_pitch;
151 src += src_y * dst_pitch;
152 width *= cpp;
153
154 if (width == dst_pitch && width == src_pitch)
155 memcpy(dst, src, height * width);
156 else {
157 for (i = 0; i < height; i++) {
158 memcpy(dst, src, width);
159 dst += dst_pitch;
160 src += src_pitch;
161 }
162 }
163 }
164
165
166 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
167 *
168 * - memcpy by span to current destination
169 * - upload data as new buffer and blit
170 *
171 * Currently always memcpy.
172 */
173 void
174 intel_region_data(struct intel_context *intel,
175 struct intel_region *dst,
176 GLuint dst_offset,
177 GLuint dstx, GLuint dsty,
178 const void *src, GLuint src_pitch,
179 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
180 {
181 GLboolean locked = GL_FALSE;
182
183 DBG("%s\n", __FUNCTION__);
184
185 if (intel == NULL)
186 return;
187
188 if (dst->pbo) {
189 if (dstx == 0 &&
190 dsty == 0 && width == dst->pitch && height == dst->height)
191 intel_region_release_pbo(intel, dst);
192 else
193 intel_region_cow(intel, dst);
194 }
195
196 if (!intel->locked) {
197 LOCK_HARDWARE(intel);
198 locked = GL_TRUE;
199 }
200
201 _mesa_copy_rect(intel_region_map(intel, dst) + dst_offset,
202 dst->cpp,
203 dst->pitch,
204 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
205
206 intel_region_unmap(intel, dst);
207
208 if (locked)
209 UNLOCK_HARDWARE(intel);
210
211 }
212
213 /* Copy rectangular sub-regions. Need better logic about when to
214 * push buffers into AGP - will currently do so whenever possible.
215 */
216 void
217 intel_region_copy(struct intel_context *intel,
218 struct intel_region *dst,
219 GLuint dst_offset,
220 GLuint dstx, GLuint dsty,
221 struct intel_region *src,
222 GLuint src_offset,
223 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
224 {
225 DBG("%s\n", __FUNCTION__);
226
227 if (intel == NULL)
228 return;
229
230 if (dst->pbo) {
231 if (dstx == 0 &&
232 dsty == 0 && width == dst->pitch && height == dst->height)
233 intel_region_release_pbo(intel, dst);
234 else
235 intel_region_cow(intel, dst);
236 }
237
238 assert(src->cpp == dst->cpp);
239
240 intelEmitCopyBlit(intel,
241 dst->cpp,
242 src->pitch, src->buffer, src_offset, src->tiled,
243 dst->pitch, dst->buffer, dst_offset, dst->tiled,
244 srcx, srcy, dstx, dsty, width, height,
245 GL_COPY);
246 }
247
248 /* Fill a rectangular sub-region. Need better logic about when to
249 * push buffers into AGP - will currently do so whenever possible.
250 */
251 void
252 intel_region_fill(struct intel_context *intel,
253 struct intel_region *dst,
254 GLuint dst_offset,
255 GLuint dstx, GLuint dsty,
256 GLuint width, GLuint height, GLuint color)
257 {
258 DBG("%s\n", __FUNCTION__);
259
260 if (intel == NULL)
261 return;
262
263 if (dst->pbo) {
264 if (dstx == 0 &&
265 dsty == 0 && width == dst->pitch && height == dst->height)
266 intel_region_release_pbo(intel, dst);
267 else
268 intel_region_cow(intel, dst);
269 }
270
271 intelEmitFillBlit(intel,
272 dst->cpp,
273 dst->pitch, dst->buffer, dst_offset, dst->tiled,
274 dstx, dsty, width, height, color);
275 }
276
277 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
278 * the pbo's data.
279 */
280 void
281 intel_region_attach_pbo(struct intel_context *intel,
282 struct intel_region *region,
283 struct intel_buffer_object *pbo)
284 {
285 if (region->pbo == pbo)
286 return;
287
288 /* If there is already a pbo attached, break the cow tie now.
289 * Don't call intel_region_release_pbo() as that would
290 * unnecessarily allocate a new buffer we would have to immediately
291 * discard.
292 */
293 if (region->pbo) {
294 region->pbo->region = NULL;
295 region->pbo = NULL;
296 }
297
298 if (region->buffer) {
299 dri_bo_unreference(region->buffer);
300 region->buffer = NULL;
301 }
302
303 region->pbo = pbo;
304 region->pbo->region = region;
305 dri_bo_reference(pbo->buffer);
306 region->buffer = pbo->buffer;
307 }
308
309
310 /* Break the COW tie to the pbo and allocate a new buffer.
311 * The pbo gets to keep the data.
312 */
313 void
314 intel_region_release_pbo(struct intel_context *intel,
315 struct intel_region *region)
316 {
317 assert(region->buffer == region->pbo->buffer);
318 region->pbo->region = NULL;
319 region->pbo = NULL;
320 dri_bo_unreference(region->buffer);
321 region->buffer = NULL;
322
323 region->buffer = dri_bo_alloc(intel->bufmgr, "region",
324 region->pitch * region->cpp * region->height,
325 64, DRM_BO_FLAG_MEM_TT);
326 }
327
328 /* Break the COW tie to the pbo. Both the pbo and the region end up
329 * with a copy of the data.
330 */
331 void
332 intel_region_cow(struct intel_context *intel, struct intel_region *region)
333 {
334 struct intel_buffer_object *pbo = region->pbo;
335 GLboolean was_locked = intel->locked;
336
337 if (intel == NULL)
338 return;
339
340 intel_region_release_pbo(intel, region);
341
342 assert(region->cpp * region->pitch * region->height == pbo->Base.Size);
343
344 DBG("%s (%d bytes)\n", __FUNCTION__, pbo->Base.Size);
345
346 /* Now blit from the texture buffer to the new buffer:
347 */
348
349 intel_batchbuffer_flush(intel->batch);
350
351 was_locked = intel->locked;
352 if (intel->locked)
353 LOCK_HARDWARE(intel);
354
355 intelEmitCopyBlit(intel,
356 region->cpp,
357 region->pitch, region->buffer, 0, region->tiled,
358 region->pitch, pbo->buffer, 0, region->tiled,
359 0, 0, 0, 0,
360 region->pitch, region->height,
361 GL_COPY);
362
363 intel_batchbuffer_flush(intel->batch);
364
365 if (was_locked)
366 UNLOCK_HARDWARE(intel);
367 }
368
369 dri_bo *
370 intel_region_buffer(struct intel_context *intel,
371 struct intel_region *region, GLuint flag)
372 {
373 if (region->pbo) {
374 if (flag == INTEL_WRITE_PART)
375 intel_region_cow(intel, region);
376 else if (flag == INTEL_WRITE_FULL)
377 intel_region_release_pbo(intel, region);
378 }
379
380 return region->buffer;
381 }
382
383 static struct intel_region *
384 intel_recreate_static(struct intel_context *intel,
385 const char *name,
386 struct intel_region *region,
387 intelRegion *region_desc,
388 GLuint mem_type)
389 {
390 intelScreenPrivate *intelScreen = intel->intelScreen;
391
392 if (region == NULL) {
393 region = calloc(sizeof(*region), 1);
394 region->refcount = 1;
395 }
396
397 region->cpp = intelScreen->cpp;
398 region->pitch = region_desc->pitch / intelScreen->cpp;
399 region->height = intelScreen->height; /* needed? */
400 region->tiled = region_desc->tiled;
401
402 if (intel->ttm) {
403 assert(region_desc->bo_handle != -1);
404 region->buffer = intel_ttm_bo_create_from_handle(intel->bufmgr,
405 name,
406 region_desc->bo_handle);
407 } else {
408 region->buffer = dri_bo_alloc_static(intel->bufmgr,
409 name,
410 region_desc->offset,
411 region_desc->pitch *
412 intelScreen->height,
413 region_desc->map,
414 DRM_BO_FLAG_MEM_TT);
415 }
416
417 assert(region->buffer != NULL);
418
419 return region;
420 }
421
422 /**
423 * Create intel_region structs to describe the static front, back, and depth
424 * buffers created by the xserver.
425 *
426 * Although FBO's mean we now no longer use these as render targets in
427 * all circumstances, they won't go away until the back and depth
428 * buffers become private, and the front buffer will remain even then.
429 *
430 * Note that these don't allocate video memory, just describe
431 * allocations alread made by the X server.
432 */
433 void
434 intel_recreate_static_regions(struct intel_context *intel)
435 {
436 intelScreenPrivate *intelScreen = intel->intelScreen;
437
438 intel->front_region =
439 intel_recreate_static(intel, "front",
440 intel->front_region,
441 &intelScreen->front,
442 DRM_BO_FLAG_MEM_TT);
443
444 intel->back_region =
445 intel_recreate_static(intel, "back",
446 intel->back_region,
447 &intelScreen->back,
448 DRM_BO_FLAG_MEM_TT);
449
450 #ifdef I915
451 if (intelScreen->third.handle) {
452 intel->third_region =
453 intel_recreate_static(intel, "third",
454 intel->third_region,
455 &intelScreen->third,
456 DRM_BO_FLAG_MEM_TT);
457 }
458 #endif /* I915 */
459
460 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
461 * private buffers.
462 */
463 intel->depth_region =
464 intel_recreate_static(intel, "depth",
465 intel->depth_region,
466 &intelScreen->depth,
467 DRM_BO_FLAG_MEM_TT);
468 }