intel_batchbuffer.c revision 428d7b3d
1/**************************************************************************
2 *
3 * Copyright 2003 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#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <errno.h>
32#include <signal.h>
33#include <fcntl.h>
34#include <dirent.h>
35#include <string.h>
36#include <assert.h>
37
38#include <sys/ioctl.h>
39#include <X11/Xlibint.h>
40#include <fourcc.h>
41#include <X11/extensions/Xv.h>
42#include <X11/extensions/Xvlib.h>
43#include <X11/extensions/XvMC.h>
44#include <X11/extensions/XvMClib.h>
45
46#include "intel_xvmc_private.h"
47#include "intel_batchbuffer.h"
48#include "brw_defines.h"
49#include "brw_structs.h"
50#define MI_BATCH_BUFFER_END     (0xA << 23)
51#define BATCH_SIZE 8*1024	/* one bo is allocated each time, so the size can be small */
52
53static void i965_end_batch(void)
54{
55	unsigned int size = xvmc_driver->batch.ptr -
56	    xvmc_driver->batch.init_ptr;
57	if ((size & 4) == 0) {
58		*(unsigned int *)xvmc_driver->batch.ptr = 0;
59		xvmc_driver->batch.ptr += 4;
60	}
61	*(unsigned int *)xvmc_driver->batch.ptr = MI_BATCH_BUFFER_END;
62	xvmc_driver->batch.ptr += 4;
63}
64
65static void reset_batch(void)
66{
67	dri_bo *bo = xvmc_driver->batch.buf;
68
69	xvmc_driver->batch.ptr = xvmc_driver->batch.init_ptr = bo->virtual;
70	xvmc_driver->batch.size = bo->size;
71	xvmc_driver->batch.space = bo->size - 8;
72}
73
74Bool intelInitBatchBuffer(void)
75{
76	if ((xvmc_driver->batch.buf =
77	     drm_intel_bo_alloc(xvmc_driver->bufmgr,
78				"batch buffer", BATCH_SIZE, 0x1000)) == NULL) {
79		fprintf(stderr, "unable to alloc batch buffer\n");
80		return False;
81	}
82
83	if (drm_intel_gem_bo_map_gtt(xvmc_driver->batch.buf)) {
84		drm_intel_bo_unreference(xvmc_driver->batch.buf);
85		return False;
86	}
87
88	reset_batch();
89	return True;
90}
91
92void intelFiniBatchBuffer(void)
93{
94	if (xvmc_driver->batch.buf == NULL)
95		return;
96
97	drm_intel_bo_unreference(xvmc_driver->batch.buf);
98}
99
100void intelFlushBatch(void)
101{
102	dri_bo *bo;
103
104	i965_end_batch();
105
106	drm_intel_bo_exec(xvmc_driver->batch.buf,
107			  xvmc_driver->batch.ptr - xvmc_driver->batch.init_ptr,
108			  0, 0, 0);
109
110	bo = drm_intel_bo_alloc(xvmc_driver->bufmgr,
111				"batch buffer", BATCH_SIZE, 0x1000);
112	if (bo != NULL && drm_intel_gem_bo_map_gtt(bo) == 0) {
113		drm_intel_bo_unreference(xvmc_driver->batch.buf);
114		xvmc_driver->batch.buf = bo;
115	} else {
116		if (bo != NULL)
117			drm_intel_bo_unreference(bo);
118		drm_intel_gem_bo_map_gtt(xvmc_driver->batch.buf);
119	}
120
121	reset_batch();
122}
123
124void intelBatchbufferData(const void *data, unsigned bytes, unsigned flags)
125{
126	assert(bytes <= xvmc_driver->batch.space);
127	memcpy(xvmc_driver->batch.ptr, data, bytes);
128	xvmc_driver->batch.ptr += bytes;
129	xvmc_driver->batch.space -= bytes;
130}
131
132void intel_batch_emit_reloc(dri_bo * bo, uint32_t read_domain,
133			    uint32_t write_domain, uint32_t delta,
134			    unsigned char *ptr)
135{
136	drm_intel_bo_emit_reloc(xvmc_driver->batch.buf,
137				ptr - xvmc_driver->batch.init_ptr, bo, delta,
138				read_domain, write_domain);
139}
140