[965] Use shared intel_regions.c.
[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 DBG("%s\n", __FUNCTION__);
182
183 if (intel == NULL)
184 return;
185
186 if (dst->pbo) {
187 if (dstx == 0 &&
188 dsty == 0 && width == dst->pitch && height == dst->height)
189 intel_region_release_pbo(intel, dst);
190 else
191 intel_region_cow(intel, dst);
192 }
193
194
195 LOCK_HARDWARE(intel);
196
197 _mesa_copy_rect(intel_region_map(intel, dst) + dst_offset,
198 dst->cpp,
199 dst->pitch,
200 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
201
202 intel_region_unmap(intel, dst);
203
204 UNLOCK_HARDWARE(intel);
205
206 }
207
208 /* Copy rectangular sub-regions. Need better logic about when to
209 * push buffers into AGP - will currently do so whenever possible.
210 */
211 void
212 intel_region_copy(struct intel_context *intel,
213 struct intel_region *dst,
214 GLuint dst_offset,
215 GLuint dstx, GLuint dsty,
216 struct intel_region *src,
217 GLuint src_offset,
218 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
219 {
220 DBG("%s\n", __FUNCTION__);
221
222 if (intel == NULL)
223 return;
224
225 if (dst->pbo) {
226 if (dstx == 0 &&
227 dsty == 0 && width == dst->pitch && height == dst->height)
228 intel_region_release_pbo(intel, dst);
229 else
230 intel_region_cow(intel, dst);
231 }
232
233 assert(src->cpp == dst->cpp);
234
235 intelEmitCopyBlit(intel,
236 dst->cpp,
237 src->pitch, src->buffer, src_offset, src->tiled,
238 dst->pitch, dst->buffer, dst_offset, dst->tiled,
239 srcx, srcy, dstx, dsty, width, height,
240 GL_COPY);
241 }
242
243 /* Fill a rectangular sub-region. Need better logic about when to
244 * push buffers into AGP - will currently do so whenever possible.
245 */
246 void
247 intel_region_fill(struct intel_context *intel,
248 struct intel_region *dst,
249 GLuint dst_offset,
250 GLuint dstx, GLuint dsty,
251 GLuint width, GLuint height, GLuint color)
252 {
253 DBG("%s\n", __FUNCTION__);
254
255 if (intel == NULL)
256 return;
257
258 if (dst->pbo) {
259 if (dstx == 0 &&
260 dsty == 0 && width == dst->pitch && height == dst->height)
261 intel_region_release_pbo(intel, dst);
262 else
263 intel_region_cow(intel, dst);
264 }
265
266 intelEmitFillBlit(intel,
267 dst->cpp,
268 dst->pitch, dst->buffer, dst_offset, dst->tiled,
269 dstx, dsty, width, height, color);
270 }
271
272 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
273 * the pbo's data.
274 */
275 void
276 intel_region_attach_pbo(struct intel_context *intel,
277 struct intel_region *region,
278 struct intel_buffer_object *pbo)
279 {
280 if (region->pbo == pbo)
281 return;
282
283 /* If there is already a pbo attached, break the cow tie now.
284 * Don't call intel_region_release_pbo() as that would
285 * unnecessarily allocate a new buffer we would have to immediately
286 * discard.
287 */
288 if (region->pbo) {
289 region->pbo->region = NULL;
290 region->pbo = NULL;
291 }
292
293 if (region->buffer) {
294 dri_bo_unreference(region->buffer);
295 region->buffer = NULL;
296 }
297
298 region->pbo = pbo;
299 region->pbo->region = region;
300 dri_bo_reference(pbo->buffer);
301 region->buffer = pbo->buffer;
302 }
303
304
305 /* Break the COW tie to the pbo and allocate a new buffer.
306 * The pbo gets to keep the data.
307 */
308 void
309 intel_region_release_pbo(struct intel_context *intel,
310 struct intel_region *region)
311 {
312 assert(region->buffer == region->pbo->buffer);
313 region->pbo->region = NULL;
314 region->pbo = NULL;
315 dri_bo_unreference(region->buffer);
316 region->buffer = NULL;
317
318 region->buffer = dri_bo_alloc(intel->bufmgr, "region",
319 region->pitch * region->cpp * region->height,
320 64, DRM_BO_FLAG_MEM_TT);
321 }
322
323 /* Break the COW tie to the pbo. Both the pbo and the region end up
324 * with a copy of the data.
325 */
326 void
327 intel_region_cow(struct intel_context *intel, struct intel_region *region)
328 {
329 struct intel_buffer_object *pbo = region->pbo;
330 GLboolean was_locked = intel->locked;
331
332 if (intel == NULL)
333 return;
334
335 intel_region_release_pbo(intel, region);
336
337 assert(region->cpp * region->pitch * region->height == pbo->Base.Size);
338
339 DBG("%s (%d bytes)\n", __FUNCTION__, pbo->Base.Size);
340
341 /* Now blit from the texture buffer to the new buffer:
342 */
343
344 intel_batchbuffer_flush(intel->batch);
345
346 was_locked = intel->locked;
347 if (intel->locked)
348 LOCK_HARDWARE(intel);
349
350 intelEmitCopyBlit(intel,
351 region->cpp,
352 region->pitch, region->buffer, 0, region->tiled,
353 region->pitch, pbo->buffer, 0, region->tiled,
354 0, 0, 0, 0,
355 region->pitch, region->height,
356 GL_COPY);
357
358 intel_batchbuffer_flush(intel->batch);
359
360 if (was_locked)
361 UNLOCK_HARDWARE(intel);
362 }
363
364 dri_bo *
365 intel_region_buffer(struct intel_context *intel,
366 struct intel_region *region, GLuint flag)
367 {
368 if (region->pbo) {
369 if (flag == INTEL_WRITE_PART)
370 intel_region_cow(intel, region);
371 else if (flag == INTEL_WRITE_FULL)
372 intel_region_release_pbo(intel, region);
373 }
374
375 return region->buffer;
376 }
377
378 static struct intel_region *
379 intel_recreate_static(struct intel_context *intel,
380 const char *name,
381 struct intel_region *region,
382 intelRegion *region_desc,
383 GLuint mem_type)
384 {
385 intelScreenPrivate *intelScreen = intel->intelScreen;
386
387 if (region == NULL) {
388 region = calloc(sizeof(*region), 1);
389 region->refcount = 1;
390 }
391
392 region->cpp = intelScreen->cpp;
393 region->pitch = region_desc->pitch / intelScreen->cpp;
394 region->height = intelScreen->height; /* needed? */
395 region->tiled = region_desc->tiled;
396
397 if (intel->ttm) {
398 assert(region_desc->bo_handle != -1);
399 region->buffer = intel_ttm_bo_create_from_handle(intel->bufmgr,
400 name,
401 region_desc->bo_handle);
402 } else {
403 region->buffer = dri_bo_alloc_static(intel->bufmgr,
404 name,
405 region_desc->offset,
406 region_desc->pitch *
407 intelScreen->height,
408 region_desc->map,
409 DRM_BO_FLAG_MEM_TT);
410 }
411
412 assert(region->buffer != NULL);
413
414 return region;
415 }
416
417 /**
418 * Create intel_region structs to describe the static front, back, and depth
419 * buffers created by the xserver.
420 *
421 * Although FBO's mean we now no longer use these as render targets in
422 * all circumstances, they won't go away until the back and depth
423 * buffers become private, and the front buffer will remain even then.
424 *
425 * Note that these don't allocate video memory, just describe
426 * allocations alread made by the X server.
427 */
428 void
429 intel_recreate_static_regions(struct intel_context *intel)
430 {
431 intelScreenPrivate *intelScreen = intel->intelScreen;
432
433 intel->front_region =
434 intel_recreate_static(intel, "front",
435 intel->front_region,
436 &intelScreen->front,
437 DRM_BO_FLAG_MEM_TT);
438
439 intel->back_region =
440 intel_recreate_static(intel, "back",
441 intel->back_region,
442 &intelScreen->back,
443 DRM_BO_FLAG_MEM_TT);
444
445 #ifdef I915
446 if (intelScreen->third.handle) {
447 intel->third_region =
448 intel_recreate_static(intel, "third",
449 intel->third_region,
450 &intelScreen->third,
451 DRM_BO_FLAG_MEM_TT);
452 }
453 #endif /* I915 */
454
455 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
456 * private buffers.
457 */
458 intel->depth_region =
459 intel_recreate_static(intel, "depth",
460 intel->depth_region,
461 &intelScreen->depth,
462 DRM_BO_FLAG_MEM_TT);
463 }