Initial code release
[profile/ivi/xorg-x11-drv-intel.git] / src / xvmc / intel_batchbuffer.c
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.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
53 static 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
65 Bool intelInitBatchBuffer(void)
66 {
67         if ((xvmc_driver->batch.buf =
68              drm_intel_bo_alloc(xvmc_driver->bufmgr,
69                                 "batch buffer", BATCH_SIZE, 0x1000)) == NULL) {
70                 fprintf(stderr, "unable to alloc batch buffer\n");
71                 return False;
72         }
73
74         drm_intel_gem_bo_map_gtt(xvmc_driver->batch.buf);
75
76         xvmc_driver->batch.init_ptr = xvmc_driver->batch.buf->virtual;
77         xvmc_driver->batch.size = BATCH_SIZE;
78         xvmc_driver->batch.space = BATCH_SIZE;
79         xvmc_driver->batch.ptr = xvmc_driver->batch.init_ptr;
80         return True;
81 }
82
83 void intelFiniBatchBuffer(void)
84 {
85         drm_intel_gem_bo_unmap_gtt(xvmc_driver->batch.buf);
86
87         drm_intel_bo_unreference(xvmc_driver->batch.buf);
88 }
89
90 void intelFlushBatch(Bool refill)
91 {
92         i965_end_batch();
93
94         drm_intel_gem_bo_unmap_gtt(xvmc_driver->batch.buf);
95
96         drm_intel_bo_exec(xvmc_driver->batch.buf,
97                           xvmc_driver->batch.ptr - xvmc_driver->batch.init_ptr,
98                           0, 0, 0);
99
100         drm_intel_bo_unreference(xvmc_driver->batch.buf);
101         if ((xvmc_driver->batch.buf =
102              drm_intel_bo_alloc(xvmc_driver->bufmgr,
103                                 "batch buffer", BATCH_SIZE, 0x1000)) == NULL) {
104                 fprintf(stderr, "unable to alloc batch buffer\n");
105         }
106
107         drm_intel_gem_bo_map_gtt(xvmc_driver->batch.buf);
108
109         xvmc_driver->batch.init_ptr = xvmc_driver->batch.buf->virtual;
110         xvmc_driver->batch.size = BATCH_SIZE;
111         xvmc_driver->batch.space = BATCH_SIZE;
112         xvmc_driver->batch.ptr = xvmc_driver->batch.init_ptr;
113 }
114
115 void intelBatchbufferRequireSpace(int size)
116 {
117         assert(xvmc_driver->batch.ptr - xvmc_driver->batch.init_ptr + size <
118                xvmc_driver->batch.size - 8);
119         if (xvmc_driver->batch.ptr - xvmc_driver->batch.init_ptr + size
120             >= xvmc_driver->batch.size - 8)
121                 intelFlushBatch(1);
122 }
123
124 void intelBatchbufferData(const void *data, unsigned bytes, unsigned flags)
125 {
126         intelBatchbufferRequireSpace(bytes);
127         memcpy(xvmc_driver->batch.ptr, data, bytes);
128         xvmc_driver->batch.ptr += bytes;
129         xvmc_driver->batch.space -= bytes;
130 }
131
132 void 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 }