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 **************************************************************************/
31 #include <va/va_backend.h>
33 #include "intel_batchbuffer.h"
36 intel_batchbuffer_reset(struct intel_batchbuffer *batch)
38 struct intel_driver_data *intel = batch->intel;
40 if (batch->buffer != NULL) {
41 dri_bo_unreference(batch->buffer);
45 batch->buffer = dri_bo_alloc(intel->bufmgr, "batch buffer",
48 assert(batch->buffer);
49 dri_bo_map(batch->buffer, 1);
50 batch->map = batch->buffer->virtual;
51 batch->size = BATCH_SIZE;
52 batch->ptr = batch->map;
57 intel_batchbuffer_init(struct intel_driver_data *intel)
59 intel->batch = calloc(1, sizeof(*(intel->batch)));
62 intel->batch->intel = intel;
63 intel_batchbuffer_reset(intel->batch);
69 intel_batchbuffer_terminate(struct intel_driver_data *intel)
72 if (intel->batch->map) {
73 dri_bo_unmap(intel->batch->buffer);
74 intel->batch->map = NULL;
77 dri_bo_unreference(intel->batch->buffer);
86 intel_batchbuffer_flush(VADriverContextP ctx)
88 struct intel_driver_data *intel = intel_driver_data(ctx);
89 struct intel_batchbuffer *batch = intel->batch;
90 unsigned int used = batch->ptr - batch->map;
91 int is_locked = intel->locked;
97 if ((used & 4) == 0) {
98 *(unsigned int*)batch->ptr = 0;
102 *(unsigned int*)batch->ptr = MI_BATCH_BUFFER_END;
104 dri_bo_unmap(batch->buffer);
105 used = batch->ptr - batch->map;
108 intel_lock_hardware(ctx);
110 dri_bo_exec(batch->buffer, used, 0, 0, 0);
113 intel_unlock_hardware(ctx);
115 intel_batchbuffer_reset(intel->batch);
121 intel_batchbuffer_space(struct intel_batchbuffer *batch)
123 return (batch->size - BATCH_RESERVED) - (batch->ptr - batch->map);
127 intel_batchbuffer_emit_dword(VADriverContextP ctx, unsigned int x)
129 struct intel_driver_data *intel = intel_driver_data(ctx);
130 struct intel_batchbuffer *batch = intel->batch;
132 assert(intel_batchbuffer_space(batch) >= 4);
133 *(unsigned int*)batch->ptr = x;
138 intel_batchbuffer_emit_reloc(VADriverContextP ctx, dri_bo *bo,
139 uint32_t read_domains, uint32_t write_domains,
142 struct intel_driver_data *intel = intel_driver_data(ctx);
143 struct intel_batchbuffer *batch = intel->batch;
145 assert(batch->ptr - batch->map < batch->size);
146 dri_bo_emit_reloc(batch->buffer, read_domains, write_domains,
147 delta, batch->ptr - batch->map, bo);
148 intel_batchbuffer_emit_dword(ctx, bo->offset + delta);
152 intel_batchbuffer_require_space(VADriverContextP ctx, unsigned int size)
154 struct intel_driver_data *intel = intel_driver_data(ctx);
155 struct intel_batchbuffer *batch = intel->batch;
157 assert(size < batch->size - 8);
159 if (intel_batchbuffer_space(batch) < size) {
160 intel_batchbuffer_flush(ctx);
165 intel_batchbuffer_data(VADriverContextP ctx, void *data, unsigned int size)
167 struct intel_driver_data *intel = intel_driver_data(ctx);
168 struct intel_batchbuffer *batch = intel->batch;
170 assert((size & 3) == 0);
171 intel_batchbuffer_require_space(ctx, size);
174 memcpy(batch->ptr, data, size);
179 intel_batchbuffer_emit_mi_flush(VADriverContextP ctx)
181 intel_batchbuffer_require_space(ctx, 4);
182 intel_batchbuffer_emit_dword(ctx, MI_FLUSH | STATE_INSTRUCTION_CACHE_INVALIDATE);
186 intel_batchbuffer_start_atomic(VADriverContextP ctx, unsigned int size)
188 struct intel_driver_data *intel = intel_driver_data(ctx);
189 struct intel_batchbuffer *batch = intel->batch;
191 assert(!batch->atomic);
192 intel_batchbuffer_require_space(ctx, size);
197 intel_batchbuffer_end_atomic(VADriverContextP ctx)
199 struct intel_driver_data *intel = intel_driver_data(ctx);
200 struct intel_batchbuffer *batch = intel->batch;
202 assert(batch->atomic);