Add memory manager test
[glamo-dri-tests.git] / gdri-mem-manager.c
1 /*
2  * gdri-cmdq-submission.c
3  *
4  * Test for Glamo DRI's memory management
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 <drm/drm.h>
31 #include <drm/glamo_drm.h>
32 #include <drm/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         uint16_t *cmds;
45         int r;
46         uint32_t offset = 0x000000;
47         RING_LOCALS;
48         int fd;
49         struct glamo_bo_manager *bufmgr;
50         struct glamo_bo *bo;
51         struct glamo_bo *bo2;
52         struct glamo_bo *bo3;
53         char tmp[3];
54
55         /* Open display */
56         dpy = XOpenDisplay(NULL);
57         if ( dpy == NULL ) {
58                 fprintf(stderr, "Can't open display '%s'\n",
59                         XDisplayName(NULL));
60                 return 1;
61         }
62
63         fd = do_drm_authentication(dpy);
64         if ( fd < 0 ) {
65                 fprintf(stderr, "Couldn't authenticate\n");
66                 return 1;
67         }
68
69         bufmgr = glamo_bo_manager_gem_ctor(fd);
70         printf("Allocated a new glamo_bo_manager: 0x%p\n", bufmgr);
71
72         bo = bufmgr->funcs->bo_open(bufmgr,
73                                     0, /* handle */
74                                     1024,
75                                     2,
76                                     GLAMO_GEM_DOMAIN_VRAM,
77                                     0); /* flags */
78
79         printf("Allocated a 1024 byte buffer object: 0x%p\n", bo);
80         printf(" handle = %i\n", bo->handle);
81
82         bo2 = bufmgr->funcs->bo_open(bufmgr,
83                                     0, /* handle */
84                                     16384,
85                                     2,
86                                     GLAMO_GEM_DOMAIN_VRAM,
87                                     0); /* flags */
88
89         printf("Allocated a 16384 byte buffer object: 0x%p\n", bo2);
90         printf(" handle = %i\n", bo2->handle);
91
92         bo3 = bufmgr->funcs->bo_open(bufmgr,
93                                     0, /* handle */
94                                     1024*1024,
95                                     2,
96                                     GLAMO_GEM_DOMAIN_VRAM,
97                                     0); /* flags */
98
99         printf("Allocated a 1024 kilobyte buffer object: 0x%p\n", bo3);
100         printf(" handle = %i\n", bo3->handle);
101
102         printf("Press enter to continue...\n");
103         fgets(tmp, 3, stdin);
104
105         bufmgr->funcs->bo_unref(bo);
106         printf("Unreferenced the first buffer object.\n");
107
108         printf("I *didn't* unreference the second buffer object.\n");
109
110         bufmgr->funcs->bo_unref(bo3);
111         printf("Unreferenced the third buffer object.\n");
112
113         glamo_bo_manager_gem_dtor(bufmgr);
114         printf("Destroyed the glamo_bo_manager.\n");
115
116         close(fd);
117         XCloseDisplay(dpy);
118
119         return 0;
120 }