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_batchbuffer.h"
49
#define FILE_DEBUG_FLAG DEBUG_REGION
52
intel_region_idle(intelScreenPrivate *intelScreen, struct intel_region *region)
54
DBG("%s\n", __FUNCTION__);
55
if (region && region->buffer)
56
driBOWaitIdle(region->buffer, GL_FALSE);
59
/* XXX: Thread safety?
62
intel_region_map(intelScreenPrivate *intelScreen, struct intel_region *region)
64
DBG("%s\n", __FUNCTION__);
65
if (!region->map_refcount++) {
67
intel_region_cow(intelScreen, region);
69
region->map = driBOMap(region->buffer,
70
DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0);
77
intel_region_unmap(intelScreenPrivate *intelScreen, struct intel_region *region)
79
DBG("%s\n", __FUNCTION__);
80
if (!--region->map_refcount) {
81
driBOUnmap(region->buffer);
86
#undef TEST_CACHED_TEXTURES
89
intel_region_alloc(intelScreenPrivate *intelScreen,
90
GLuint cpp, GLuint pitch, GLuint height)
92
struct intel_region *region = calloc(sizeof(*region), 1);
94
DBG("%s\n", __FUNCTION__);
97
region->pitch = pitch;
98
region->height = height; /* needed? */
101
driGenBuffers(intelScreen->regionPool,
102
"region", 1, ®ion->buffer, 64,
103
#ifdef TEST_CACHED_TEXTURES
104
DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_BIND_CACHED |
105
DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE,
110
driBOData(region->buffer, pitch * cpp * height, NULL, 0);
115
intel_region_reference(struct intel_region **dst, struct intel_region *src)
117
assert(*dst == NULL);
125
intel_region_release(struct intel_region **region)
130
DBG("%s %d\n", __FUNCTION__, (*region)->refcount - 1);
132
ASSERT((*region)->refcount > 0);
133
(*region)->refcount--;
135
if ((*region)->refcount == 0) {
136
assert((*region)->map_refcount == 0);
139
(*region)->pbo->region = NULL;
140
(*region)->pbo = NULL;
141
driBOUnReference((*region)->buffer);
148
struct intel_region *
149
intel_region_create_static(intelScreenPrivate *intelScreen,
153
GLuint cpp, GLuint pitch, GLuint height)
155
struct intel_region *region = calloc(sizeof(*region), 1);
156
DBG("%s\n", __FUNCTION__);
159
region->pitch = pitch;
160
region->height = height; /* needed? */
161
region->refcount = 1;
164
* We use a "shared" buffer type to indicate buffers created and
168
driGenBuffers(intelScreen->staticPool, "static region", 1,
170
DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_NO_MOVE |
171
DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0);
172
driBOSetStatic(region->buffer, offset, pitch * cpp * height, virtual, 0);
180
intel_region_update_static(intelScreenPrivate *intelScreen,
181
struct intel_region *region,
185
GLuint cpp, GLuint pitch, GLuint height)
187
DBG("%s\n", __FUNCTION__);
190
region->pitch = pitch;
191
region->height = height; /* needed? */
194
* We use a "shared" buffer type to indicate buffers created and
198
driDeleteBuffers(1, ®ion->buffer);
199
driGenBuffers(intelScreen->staticPool, "static region", 1,
201
DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_NO_MOVE |
202
DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0);
203
driBOSetStatic(region->buffer, offset, pitch * cpp * height, virtual, 0);
210
* XXX Move this into core Mesa?
213
_mesa_copy_rect(GLubyte * dst,
220
GLubyte * src, GLuint src_pitch, GLuint src_x, GLuint src_y)
228
dst += dst_y * dst_pitch;
229
src += src_y * dst_pitch;
232
if (width == dst_pitch && width == src_pitch)
233
memcpy(dst, src, height * width);
235
for (i = 0; i < height; i++) {
236
memcpy(dst, src, width);
244
/* Upload data to a rectangular sub-region. Lots of choices how to do this:
246
* - memcpy by span to current destination
247
* - upload data as new buffer and blit
249
* Currently always memcpy.
252
intel_region_data(intelScreenPrivate *intelScreen,
253
struct intel_region *dst,
255
GLuint dstx, GLuint dsty,
256
void *src, GLuint src_pitch,
257
GLuint srcx, GLuint srcy, GLuint width, GLuint height)
259
struct intel_context *intel = intelScreenContext(intelScreen);
261
DBG("%s\n", __FUNCTION__);
268
dsty == 0 && width == dst->pitch && height == dst->height)
269
intel_region_release_pbo(intelScreen, dst);
271
intel_region_cow(intelScreen, dst);
275
LOCK_HARDWARE(intel);
277
_mesa_copy_rect(intel_region_map(intelScreen, dst) + dst_offset,
280
dstx, dsty, width, height, src, src_pitch, srcx, srcy);
282
intel_region_unmap(intelScreen, dst);
284
UNLOCK_HARDWARE(intel);
288
/* Copy rectangular sub-regions. Need better logic about when to
289
* push buffers into AGP - will currently do so whenever possible.
292
intel_region_copy(intelScreenPrivate *intelScreen,
293
struct intel_region *dst,
295
GLuint dstx, GLuint dsty,
296
struct intel_region *src,
298
GLuint srcx, GLuint srcy, GLuint width, GLuint height)
300
struct intel_context *intel = intelScreenContext(intelScreen);
302
DBG("%s\n", __FUNCTION__);
309
dsty == 0 && width == dst->pitch && height == dst->height)
310
intel_region_release_pbo(intelScreen, dst);
312
intel_region_cow(intelScreen, dst);
315
assert(src->cpp == dst->cpp);
317
intelEmitCopyBlit(intel,
319
src->pitch, src->buffer, src_offset,
320
dst->pitch, dst->buffer, dst_offset,
321
srcx, srcy, dstx, dsty, width, height,
325
/* Fill a rectangular sub-region. Need better logic about when to
326
* push buffers into AGP - will currently do so whenever possible.
329
intel_region_fill(intelScreenPrivate *intelScreen,
330
struct intel_region *dst,
332
GLuint dstx, GLuint dsty,
333
GLuint width, GLuint height, GLuint color)
335
struct intel_context *intel = intelScreenContext(intelScreen);
337
DBG("%s\n", __FUNCTION__);
344
dsty == 0 && width == dst->pitch && height == dst->height)
345
intel_region_release_pbo(intelScreen, dst);
347
intel_region_cow(intelScreen, dst);
350
intelEmitFillBlit(intel,
352
dst->pitch, dst->buffer, dst_offset,
353
dstx, dsty, width, height, color);
356
/* Attach to a pbo, discarding our data. Effectively zero-copy upload
360
intel_region_attach_pbo(intelScreenPrivate *intelScreen,
361
struct intel_region *region,
362
struct intel_buffer_object *pbo)
364
if (region->pbo == pbo)
367
/* If there is already a pbo attached, break the cow tie now.
368
* Don't call intel_region_release_pbo() as that would
369
* unnecessarily allocate a new buffer we would have to immediately
373
region->pbo->region = NULL;
377
if (region->buffer) {
378
driDeleteBuffers(1, ®ion->buffer);
379
region->buffer = NULL;
383
region->pbo->region = region;
384
region->buffer = driBOReference(pbo->buffer);
388
/* Break the COW tie to the pbo. The pbo gets to keep the data.
391
intel_region_release_pbo(intelScreenPrivate *intelScreen,
392
struct intel_region *region)
394
assert(region->buffer == region->pbo->buffer);
395
region->pbo->region = NULL;
397
driBOUnReference(region->buffer);
398
region->buffer = NULL;
400
driGenBuffers(intelScreen->regionPool,
401
"region", 1, ®ion->buffer, 64, 0, 0);
402
driBOData(region->buffer,
403
region->cpp * region->pitch * region->height, NULL, 0);
406
/* Break the COW tie to the pbo. Both the pbo and the region end up
407
* with a copy of the data.
410
intel_region_cow(intelScreenPrivate *intelScreen, struct intel_region *region)
412
struct intel_context *intel = intelScreenContext(intelScreen);
413
struct intel_buffer_object *pbo = region->pbo;
418
intel_region_release_pbo(intelScreen, region);
420
assert(region->cpp * region->pitch * region->height == pbo->Base.Size);
422
DBG("%s (%d bytes)\n", __FUNCTION__, pbo->Base.Size);
424
/* Now blit from the texture buffer to the new buffer:
427
intel_batchbuffer_flush(intel->batch);
429
if (!intel->locked) {
430
LOCK_HARDWARE(intel);
431
intelEmitCopyBlit(intel,
438
region->pitch, region->height,
441
intel_batchbuffer_flush(intel->batch);
442
UNLOCK_HARDWARE(intel);
445
intelEmitCopyBlit(intel,
452
region->pitch, region->height,
455
intel_batchbuffer_flush(intel->batch);
459
struct _DriBufferObject *
460
intel_region_buffer(intelScreenPrivate *intelScreen,
461
struct intel_region *region, GLuint flag)
464
if (flag == INTEL_WRITE_PART)
465
intel_region_cow(intelScreen, region);
466
else if (flag == INTEL_WRITE_FULL)
467
intel_region_release_pbo(intelScreen, region);
470
return region->buffer;