1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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
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
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"
50 #define FILE_DEBUG_FLAG DEBUG_REGION
52 /* XXX: Thread safety?
55 intel_region_map(struct intel_context
*intel
, struct intel_region
*region
)
57 DBG("%s\n", __FUNCTION__
);
58 if (!region
->map_refcount
++) {
60 intel_region_cow(intel
, region
);
62 dri_bo_map(region
->buffer
, GL_TRUE
);
63 region
->map
= region
->buffer
->virtual;
70 intel_region_unmap(struct intel_context
*intel
, struct intel_region
*region
)
72 DBG("%s\n", __FUNCTION__
);
73 if (!--region
->map_refcount
) {
74 dri_bo_unmap(region
->buffer
);
80 intel_region_alloc(struct intel_context
*intel
,
81 GLuint cpp
, GLuint pitch
, GLuint height
)
83 struct intel_region
*region
= calloc(sizeof(*region
), 1);
85 DBG("%s\n", __FUNCTION__
);
88 region
->pitch
= pitch
;
89 region
->height
= height
; /* needed? */
92 region
->buffer
= dri_bo_alloc(intel
->bufmgr
, "region",
93 pitch
* cpp
* height
, 64,
94 DRM_BO_FLAG_MEM_LOCAL
|
96 DRM_BO_FLAG_CACHED_MAPPED
);
101 intel_region_reference(struct intel_region
**dst
, struct intel_region
*src
)
103 assert(*dst
== NULL
);
111 intel_region_release(struct intel_region
**region
)
116 DBG("%s %d\n", __FUNCTION__
, (*region
)->refcount
- 1);
118 ASSERT((*region
)->refcount
> 0);
119 (*region
)->refcount
--;
121 if ((*region
)->refcount
== 0) {
122 assert((*region
)->map_refcount
== 0);
125 (*region
)->pbo
->region
= NULL
;
126 (*region
)->pbo
= NULL
;
127 dri_bo_unreference((*region
)->buffer
);
134 * XXX Move this into core Mesa?
137 _mesa_copy_rect(GLubyte
* dst
,
145 GLuint src_pitch
, GLuint src_x
, GLuint src_y
)
153 dst
+= dst_y
* dst_pitch
;
154 src
+= src_y
* dst_pitch
;
157 if (width
== dst_pitch
&& width
== src_pitch
)
158 memcpy(dst
, src
, height
* width
);
160 for (i
= 0; i
< height
; i
++) {
161 memcpy(dst
, src
, width
);
169 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
171 * - memcpy by span to current destination
172 * - upload data as new buffer and blit
174 * Currently always memcpy.
177 intel_region_data(struct intel_context
*intel
,
178 struct intel_region
*dst
,
180 GLuint dstx
, GLuint dsty
,
181 const void *src
, GLuint src_pitch
,
182 GLuint srcx
, GLuint srcy
, GLuint width
, GLuint height
)
184 GLboolean locked
= GL_FALSE
;
186 DBG("%s\n", __FUNCTION__
);
193 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
194 intel_region_release_pbo(intel
, dst
);
196 intel_region_cow(intel
, dst
);
199 if (!intel
->locked
) {
200 LOCK_HARDWARE(intel
);
204 _mesa_copy_rect(intel_region_map(intel
, dst
) + dst_offset
,
207 dstx
, dsty
, width
, height
, src
, src_pitch
, srcx
, srcy
);
209 intel_region_unmap(intel
, dst
);
212 UNLOCK_HARDWARE(intel
);
216 /* Copy rectangular sub-regions. Need better logic about when to
217 * push buffers into AGP - will currently do so whenever possible.
220 intel_region_copy(struct intel_context
*intel
,
221 struct intel_region
*dst
,
223 GLuint dstx
, GLuint dsty
,
224 struct intel_region
*src
,
226 GLuint srcx
, GLuint srcy
, GLuint width
, GLuint height
)
228 DBG("%s\n", __FUNCTION__
);
235 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
236 intel_region_release_pbo(intel
, dst
);
238 intel_region_cow(intel
, dst
);
241 assert(src
->cpp
== dst
->cpp
);
243 intelEmitCopyBlit(intel
,
245 src
->pitch
, src
->buffer
, src_offset
, src
->tiled
,
246 dst
->pitch
, dst
->buffer
, dst_offset
, dst
->tiled
,
247 srcx
, srcy
, dstx
, dsty
, width
, height
,
251 /* Fill a rectangular sub-region. Need better logic about when to
252 * push buffers into AGP - will currently do so whenever possible.
255 intel_region_fill(struct intel_context
*intel
,
256 struct intel_region
*dst
,
258 GLuint dstx
, GLuint dsty
,
259 GLuint width
, GLuint height
, GLuint color
)
261 DBG("%s\n", __FUNCTION__
);
268 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
269 intel_region_release_pbo(intel
, dst
);
271 intel_region_cow(intel
, dst
);
274 intelEmitFillBlit(intel
,
276 dst
->pitch
, dst
->buffer
, dst_offset
, dst
->tiled
,
277 dstx
, dsty
, width
, height
, color
);
280 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
284 intel_region_attach_pbo(struct intel_context
*intel
,
285 struct intel_region
*region
,
286 struct intel_buffer_object
*pbo
)
288 if (region
->pbo
== pbo
)
291 /* If there is already a pbo attached, break the cow tie now.
292 * Don't call intel_region_release_pbo() as that would
293 * unnecessarily allocate a new buffer we would have to immediately
297 region
->pbo
->region
= NULL
;
301 if (region
->buffer
) {
302 dri_bo_unreference(region
->buffer
);
303 region
->buffer
= NULL
;
307 region
->pbo
->region
= region
;
308 dri_bo_reference(pbo
->buffer
);
309 region
->buffer
= pbo
->buffer
;
313 /* Break the COW tie to the pbo and allocate a new buffer.
314 * The pbo gets to keep the data.
317 intel_region_release_pbo(struct intel_context
*intel
,
318 struct intel_region
*region
)
320 assert(region
->buffer
== region
->pbo
->buffer
);
321 region
->pbo
->region
= NULL
;
323 dri_bo_unreference(region
->buffer
);
324 region
->buffer
= NULL
;
326 region
->buffer
= dri_bo_alloc(intel
->bufmgr
, "region",
327 region
->pitch
* region
->cpp
* region
->height
,
329 DRM_BO_FLAG_MEM_LOCAL
|
331 DRM_BO_FLAG_CACHED_MAPPED
);
334 /* Break the COW tie to the pbo. Both the pbo and the region end up
335 * with a copy of the data.
338 intel_region_cow(struct intel_context
*intel
, struct intel_region
*region
)
340 struct intel_buffer_object
*pbo
= region
->pbo
;
341 GLboolean was_locked
= intel
->locked
;
346 intel_region_release_pbo(intel
, region
);
348 assert(region
->cpp
* region
->pitch
* region
->height
== pbo
->Base
.Size
);
350 DBG("%s (%d bytes)\n", __FUNCTION__
, pbo
->Base
.Size
);
352 /* Now blit from the texture buffer to the new buffer:
355 intel_batchbuffer_flush(intel
->batch
);
357 was_locked
= intel
->locked
;
359 LOCK_HARDWARE(intel
);
361 intelEmitCopyBlit(intel
,
363 region
->pitch
, region
->buffer
, 0, region
->tiled
,
364 region
->pitch
, pbo
->buffer
, 0, region
->tiled
,
366 region
->pitch
, region
->height
,
369 intel_batchbuffer_flush(intel
->batch
);
372 UNLOCK_HARDWARE(intel
);
376 intel_region_buffer(struct intel_context
*intel
,
377 struct intel_region
*region
, GLuint flag
)
380 if (flag
== INTEL_WRITE_PART
)
381 intel_region_cow(intel
, region
);
382 else if (flag
== INTEL_WRITE_FULL
)
383 intel_region_release_pbo(intel
, region
);
386 return region
->buffer
;
389 static struct intel_region
*
390 intel_recreate_static(struct intel_context
*intel
,
392 struct intel_region
*region
,
393 intelRegion
*region_desc
,
396 intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
398 if (region
== NULL
) {
399 region
= calloc(sizeof(*region
), 1);
400 region
->refcount
= 1;
403 region
->cpp
= intel
->ctx
.Visual
.rgbBits
/ 8;
404 region
->pitch
= intelScreen
->pitch
;
405 region
->height
= intelScreen
->height
; /* needed? */
406 region
->tiled
= region_desc
->tiled
;
409 assert(region_desc
->bo_handle
!= -1);
410 region
->buffer
= intel_ttm_bo_create_from_handle(intel
->bufmgr
,
412 region_desc
->bo_handle
);
414 region
->buffer
= dri_bo_alloc_static(intel
->bufmgr
,
423 assert(region
->buffer
!= NULL
);
429 * Create intel_region structs to describe the static front, back, and depth
430 * buffers created by the xserver.
432 * Although FBO's mean we now no longer use these as render targets in
433 * all circumstances, they won't go away until the back and depth
434 * buffers become private, and the front buffer will remain even then.
436 * Note that these don't allocate video memory, just describe
437 * allocations alread made by the X server.
440 intel_recreate_static_regions(struct intel_context
*intel
)
442 intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
444 intel
->front_region
=
445 intel_recreate_static(intel
, "front",
451 intel_recreate_static(intel
, "back",
457 if (intelScreen
->third
.handle
) {
458 intel
->third_region
=
459 intel_recreate_static(intel
, "third",
466 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
469 intel
->depth_region
=
470 intel_recreate_static(intel
, "depth",