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 **************************************************************************/
32 #include <va/va_backend.h>
34 #include "intel_batchbuffer.h"
37 intel_batchbuffer_reset(struct intel_batchbuffer *batch)
39 struct intel_driver_data *intel = batch->intel;
41 if (batch->buffer != NULL) {
42 dri_bo_unreference(batch->buffer);
46 batch->buffer = dri_bo_alloc(intel->bufmgr, "batch buffer",
49 assert(batch->buffer);
50 dri_bo_map(batch->buffer, 1);
51 batch->map = batch->buffer->virtual;
52 batch->size = BATCH_SIZE;
53 batch->ptr = batch->map;
58 intel_batchbuffer_init(struct intel_driver_data *intel)
60 intel->batch = calloc(1, sizeof(*(intel->batch)));
63 intel->batch->intel = intel;
64 intel_batchbuffer_reset(intel->batch);
70 intel_batchbuffer_terminate(struct intel_driver_data *intel)
73 if (intel->batch->map) {
74 dri_bo_unmap(intel->batch->buffer);
75 intel->batch->map = NULL;
78 dri_bo_unreference(intel->batch->buffer);
87 intel_batchbuffer_flush(VADriverContextP ctx)
89 struct intel_driver_data *intel = intel_driver_data(ctx);
90 struct intel_batchbuffer *batch = intel->batch;
91 unsigned int used = batch->ptr - batch->map;
92 int is_locked = intel->locked;
98 if ((used & 4) == 0) {
99 *(unsigned int*)batch->ptr = 0;
103 *(unsigned int*)batch->ptr = MI_BATCH_BUFFER_END;
105 dri_bo_unmap(batch->buffer);
106 used = batch->ptr - batch->map;
109 intel_lock_hardware(ctx);
111 dri_bo_exec(batch->buffer, used, 0, 0, 0);
114 intel_unlock_hardware(ctx);
116 intel_batchbuffer_reset(intel->batch);
122 intel_batchbuffer_space(struct intel_batchbuffer *batch)
124 return (batch->size - BATCH_RESERVED) - (batch->ptr - batch->map);
128 intel_batchbuffer_emit_dword(VADriverContextP ctx, unsigned int x)
130 struct intel_driver_data *intel = intel_driver_data(ctx);
131 struct intel_batchbuffer *batch = intel->batch;
133 assert(intel_batchbuffer_space(batch) >= 4);
134 *(unsigned int*)batch->ptr = x;
139 intel_batchbuffer_emit_reloc(VADriverContextP ctx, dri_bo *bo,
140 uint32_t read_domains, uint32_t write_domains,
143 struct intel_driver_data *intel = intel_driver_data(ctx);
144 struct intel_batchbuffer *batch = intel->batch;
146 assert(batch->ptr - batch->map < batch->size);
147 dri_bo_emit_reloc(batch->buffer, read_domains, write_domains,
148 delta, batch->ptr - batch->map, bo);
149 intel_batchbuffer_emit_dword(ctx, bo->offset + delta);
153 intel_batchbuffer_require_space(VADriverContextP ctx, unsigned int size)
155 struct intel_driver_data *intel = intel_driver_data(ctx);
156 struct intel_batchbuffer *batch = intel->batch;
158 assert(size < batch->size - 8);
160 if (intel_batchbuffer_space(batch) < size) {
161 intel_batchbuffer_flush(ctx);
166 intel_batchbuffer_data(VADriverContextP ctx, void *data, unsigned int size)
168 struct intel_driver_data *intel = intel_driver_data(ctx);
169 struct intel_batchbuffer *batch = intel->batch;
171 assert((size & 3) == 0);
172 intel_batchbuffer_require_space(ctx, size);
175 memcpy(batch->ptr, data, size);
180 intel_batchbuffer_emit_mi_flush(VADriverContextP ctx)
182 intel_batchbuffer_require_space(ctx, 4);
183 intel_batchbuffer_emit_dword(ctx, MI_FLUSH | STATE_INSTRUCTION_CACHE_INVALIDATE);
187 intel_batchbuffer_start_atomic(VADriverContextP ctx, unsigned int size)
189 struct intel_driver_data *intel = intel_driver_data(ctx);
190 struct intel_batchbuffer *batch = intel->batch;
192 assert(!batch->atomic);
193 intel_batchbuffer_require_space(ctx, size);
198 intel_batchbuffer_end_atomic(VADriverContextP ctx)
200 struct intel_driver_data *intel = intel_driver_data(ctx);
201 struct intel_batchbuffer *batch = intel->batch;
203 assert(batch->atomic);