c551b3494842d846135d001dcef14e59943b0f78
[glamo-dri-tests.git] / gdri-buf-cmdq.c
1 /*
2  * gdri-buf-cmdq.c
3  *
4  * Test for Glamo referencing buffer objects from the command queue
5  *
6  * (c) 2009 Thomas White <taw@bitwiz.org.uk>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <X11/Xlib.h>
27
28 #include <X11/Xmd.h>
29 #include <stdint.h>
30 #include <libdrm/drm.h>
31 #include <libdrm/glamo_drm.h>
32 #include <libdrm/glamo_bo.h>
33 #include <glamo_bo_gem.h>
34
35 #include "dri2.h"
36 #include "glamo-regs.h"
37 #include "utils.h"
38
39
40 int main()
41 {
42         Display *dpy;
43         drm_glamo_cmd_buffer_t cmdbuf;
44         int r;
45         int fd;
46         struct glamo_bo_manager *bufmgr;
47         struct glamo_bo *bo;
48
49         RING_LOCALS;
50         uint16_t *cmds;
51         uint32_t *objs;
52         int *obj_pos;
53
54         printf("NOTE: This test is no longer useful.\n");
55
56         /* Open display */
57         dpy = XOpenDisplay(NULL);
58         if ( dpy == NULL ) {
59                 fprintf(stderr, "Can't open display '%s'\n",
60                         XDisplayName(NULL));
61                 return 1;
62         }
63
64         fd = do_drm_authentication(dpy);
65         if ( fd < 0 ) {
66                 fprintf(stderr, "Couldn't authenticate\n");
67                 return 1;
68         }
69
70         bufmgr = glamo_bo_manager_gem_ctor(fd);
71         printf("Allocated a new glamo_bo_manager: 0x%p\n", bufmgr);
72
73         bo = glamo_bo_open(bufmgr, 0, 480*480*2, 2, GLAMO_GEM_DOMAIN_VRAM, 0);
74
75         printf("Allocated a 480x480x2 byte buffer object: 0x%p\n", bo);
76         printf(" handle = %i\n", bo->handle);
77
78         cmds = malloc(1024);
79         objs = malloc(1024);
80         obj_pos = malloc(1024);
81
82         /* Render something to this buffer */
83         OUT_REG_BO(GLAMO_REG_2D_DST_ADDRL, bo->handle);
84         OUT_REG(GLAMO_REG_2D_DST_PITCH, (480*2) & 0x7ff);
85         OUT_REG(GLAMO_REG_2D_DST_HEIGHT, 480);
86         OUT_REG(GLAMO_REG_2D_PAT_FG, 0xDCBA);
87         OUT_REG(GLAMO_REG_2D_COMMAND2, 0xf000);
88         OUT_REG(GLAMO_REG_2D_ID1, 0);
89         OUT_REG(GLAMO_REG_2D_ID2, 0);
90         OUT_REG(GLAMO_REG_2D_DST_X, 0);
91         OUT_REG(GLAMO_REG_2D_DST_Y, 0);
92         OUT_REG(GLAMO_REG_2D_RECT_WIDTH, 480);
93         OUT_REG(GLAMO_REG_2D_RECT_HEIGHT, 480);
94         OUT_REG(GLAMO_REG_2D_COMMAND3, 0);
95
96         OUT_REG(GLAMO_REG_2D_PAT_FG, 0xABAB);
97         OUT_REG(GLAMO_REG_2D_DST_X, 5);
98         OUT_REG(GLAMO_REG_2D_DST_Y, 5);
99         OUT_REG(GLAMO_REG_2D_RECT_WIDTH, 470);
100         OUT_REG(GLAMO_REG_2D_RECT_HEIGHT, 470);
101         OUT_REG(GLAMO_REG_2D_COMMAND3, 0);
102
103         cmdbuf.buf = (char *)cmds;
104         cmdbuf.bufsz = 2*__count;
105         cmdbuf.objs = objs;
106         cmdbuf.obj_pos = obj_pos;
107         cmdbuf.nobjs = __nobjs;
108         r = drmCommandWrite(fd, DRM_GLAMO_CMDBUF, &cmdbuf, sizeof(cmdbuf));
109         if ( r != 0 ) {
110             fprintf(stderr, "DRM_GLAMO_CMDBUF failed");
111             return 1;
112         }
113         printf("Rendered something to the buffer\n");
114
115         /* Blit the buffer to the screen */
116         __nobjs = 0;    __count = 0;
117         OUT_REG_BO(GLAMO_REG_2D_SRC_ADDRL, bo->handle);
118         OUT_REG(GLAMO_REG_2D_SRC_PITCH, (480*2) & 0x7ff);
119         OUT_REG(GLAMO_REG_2D_DST_ADDRL, 0x0000);
120         OUT_REG(GLAMO_REG_2D_DST_ADDRH, 0x0000);
121         OUT_REG(GLAMO_REG_2D_DST_PITCH, (480*2) & 0x7ff);  /* The screen */
122         OUT_REG(GLAMO_REG_2D_DST_HEIGHT, 640);
123         OUT_REG(GLAMO_REG_2D_COMMAND2, 0xcc00);
124         OUT_REG(GLAMO_REG_2D_ID1, 0);
125         OUT_REG(GLAMO_REG_2D_ID2, 0);
126         OUT_REG(GLAMO_REG_2D_DST_X, 100);
127         OUT_REG(GLAMO_REG_2D_DST_Y, 100);
128         OUT_REG(GLAMO_REG_2D_RECT_WIDTH, 480);
129         OUT_REG(GLAMO_REG_2D_RECT_HEIGHT, 480);
130         OUT_REG(GLAMO_REG_2D_COMMAND3, 0);
131         cmdbuf.buf = (char *)cmds;
132         cmdbuf.bufsz = 2*__count;
133         cmdbuf.objs = objs;
134         cmdbuf.obj_pos = obj_pos;
135         cmdbuf.nobjs = __nobjs;
136         r = drmCommandWrite(fd, DRM_GLAMO_CMDBUF, &cmdbuf, sizeof(cmdbuf));
137         if ( r != 0 ) {
138             fprintf(stderr, "DRM_GLAMO_CMDBUF failed");
139             return 1;
140         }
141         printf("Copied the buffer to the screen\n");
142
143         glamo_bo_unref(bo);
144
145         glamo_bo_manager_gem_dtor(bufmgr);
146         printf("Destroyed the glamo_bo_manager.\n");
147
148         close(fd);
149         XCloseDisplay(dpy);
150
151         return 0;
152 }