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, DRM_BO_FLAG_MEM_TT);
98 intel_region_reference(struct intel_region **dst, struct intel_region *src)
100 assert(*dst == NULL);
108 intel_region_release(struct intel_region **region)
113 DBG("%s %d\n", __FUNCTION__, (*region)->refcount - 1);
115 ASSERT((*region)->refcount > 0);
116 (*region)->refcount--;
118 if ((*region)->refcount == 0) {
119 assert((*region)->map_refcount == 0);
122 (*region)->pbo->region = NULL;
123 (*region)->pbo = NULL;
124 dri_bo_unreference((*region)->buffer);
131 * XXX Move this into core Mesa?
134 _mesa_copy_rect(GLubyte * dst,
142 GLuint src_pitch, GLuint src_x, GLuint src_y)
150 dst += dst_y * dst_pitch;
151 src += src_y * dst_pitch;
154 if (width == dst_pitch && width == src_pitch)
155 memcpy(dst, src, height * width);
157 for (i = 0; i < height; i++) {
158 memcpy(dst, src, width);
166 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
168 * - memcpy by span to current destination
169 * - upload data as new buffer and blit
171 * Currently always memcpy.
174 intel_region_data(struct intel_context *intel,
175 struct intel_region *dst,
177 GLuint dstx, GLuint dsty,
178 const void *src, GLuint src_pitch,
179 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
181 GLboolean locked = GL_FALSE;
183 DBG("%s\n", __FUNCTION__);
190 dsty == 0 && width == dst->pitch && height == dst->height)
191 intel_region_release_pbo(intel, dst);
193 intel_region_cow(intel, dst);
196 if (!intel->locked) {
197 LOCK_HARDWARE(intel);
201 _mesa_copy_rect(intel_region_map(intel, dst) + dst_offset,
204 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
206 intel_region_unmap(intel, dst);
209 UNLOCK_HARDWARE(intel);
213 /* Copy rectangular sub-regions. Need better logic about when to
214 * push buffers into AGP - will currently do so whenever possible.
217 intel_region_copy(struct intel_context *intel,
218 struct intel_region *dst,
220 GLuint dstx, GLuint dsty,
221 struct intel_region *src,
223 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
225 DBG("%s\n", __FUNCTION__);
232 dsty == 0 && width == dst->pitch && height == dst->height)
233 intel_region_release_pbo(intel, dst);
235 intel_region_cow(intel, dst);
238 assert(src->cpp == dst->cpp);
240 intelEmitCopyBlit(intel,
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,
248 /* Fill a rectangular sub-region. Need better logic about when to
249 * push buffers into AGP - will currently do so whenever possible.
252 intel_region_fill(struct intel_context *intel,
253 struct intel_region *dst,
255 GLuint dstx, GLuint dsty,
256 GLuint width, GLuint height, GLuint color)
258 DBG("%s\n", __FUNCTION__);
265 dsty == 0 && width == dst->pitch && height == dst->height)
266 intel_region_release_pbo(intel, dst);
268 intel_region_cow(intel, dst);
271 intelEmitFillBlit(intel,
273 dst->pitch, dst->buffer, dst_offset, dst->tiled,
274 dstx, dsty, width, height, color);
277 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
281 intel_region_attach_pbo(struct intel_context *intel,
282 struct intel_region *region,
283 struct intel_buffer_object *pbo)
285 if (region->pbo == pbo)
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
294 region->pbo->region = NULL;
298 if (region->buffer) {
299 dri_bo_unreference(region->buffer);
300 region->buffer = NULL;
304 region->pbo->region = region;
305 dri_bo_reference(pbo->buffer);
306 region->buffer = pbo->buffer;
310 /* Break the COW tie to the pbo and allocate a new buffer.
311 * The pbo gets to keep the data.
314 intel_region_release_pbo(struct intel_context *intel,
315 struct intel_region *region)
317 assert(region->buffer == region->pbo->buffer);
318 region->pbo->region = NULL;
320 dri_bo_unreference(region->buffer);
321 region->buffer = NULL;
323 region->buffer = dri_bo_alloc(intel->bufmgr, "region",
324 region->pitch * region->cpp * region->height,
325 64, DRM_BO_FLAG_MEM_TT);
328 /* Break the COW tie to the pbo. Both the pbo and the region end up
329 * with a copy of the data.
332 intel_region_cow(struct intel_context *intel, struct intel_region *region)
334 struct intel_buffer_object *pbo = region->pbo;
335 GLboolean was_locked = intel->locked;
340 intel_region_release_pbo(intel, region);
342 assert(region->cpp * region->pitch * region->height == pbo->Base.Size);
344 DBG("%s (%d bytes)\n", __FUNCTION__, pbo->Base.Size);
346 /* Now blit from the texture buffer to the new buffer:
349 intel_batchbuffer_flush(intel->batch);
351 was_locked = intel->locked;
353 LOCK_HARDWARE(intel);
355 intelEmitCopyBlit(intel,
357 region->pitch, region->buffer, 0, region->tiled,
358 region->pitch, pbo->buffer, 0, region->tiled,
360 region->pitch, region->height,
363 intel_batchbuffer_flush(intel->batch);
366 UNLOCK_HARDWARE(intel);
370 intel_region_buffer(struct intel_context *intel,
371 struct intel_region *region, GLuint flag)
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);
380 return region->buffer;
383 static struct intel_region *
384 intel_recreate_static(struct intel_context *intel,
386 struct intel_region *region,
387 intelRegion *region_desc,
390 intelScreenPrivate *intelScreen = intel->intelScreen;
392 if (region == NULL) {
393 region = calloc(sizeof(*region), 1);
394 region->refcount = 1;
397 region->cpp = intel->ctx.Visual.rgbBits / 8;
398 region->pitch = intelScreen->pitch;
399 region->height = intelScreen->height; /* needed? */
400 region->tiled = region_desc->tiled;
403 assert(region_desc->bo_handle != -1);
404 region->buffer = intel_ttm_bo_create_from_handle(intel->bufmgr,
406 region_desc->bo_handle);
408 region->buffer = dri_bo_alloc_static(intel->bufmgr,
417 assert(region->buffer != NULL);
423 * Create intel_region structs to describe the static front, back, and depth
424 * buffers created by the xserver.
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.
430 * Note that these don't allocate video memory, just describe
431 * allocations alread made by the X server.
434 intel_recreate_static_regions(struct intel_context *intel)
436 intelScreenPrivate *intelScreen = intel->intelScreen;
438 intel->front_region =
439 intel_recreate_static(intel, "front",
445 intel_recreate_static(intel, "back",
451 if (intelScreen->third.handle) {
452 intel->third_region =
453 intel_recreate_static(intel, "third",
460 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
463 intel->depth_region =
464 intel_recreate_static(intel, "depth",