556d3f54fe8feec5d337af589d196332d9dfbf5c
[mesa.git] / src / gallium / winsys / drm / radeon / core / radeon_drm.c
1 /*
2 * Copyright © 2009 Corbin Simpson
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26 /*
27 * Authors:
28 * Corbin Simpson <MostAwesomeDude@gmail.com>
29 */
30
31 #include "radeon_drm.h"
32
33 /* Create a pipe_screen. */
34 struct pipe_screen* radeon_create_screen(struct drm_api* api,
35 int drmFB,
36 struct drm_create_screen_arg *arg)
37 {
38 struct radeon_winsys* winsys = radeon_pipe_winsys(drmFB);
39
40 if (getenv("RADEON_SOFTPIPE")) {
41 return softpipe_create_screen((struct pipe_winsys*)winsys);
42 } else {
43 struct r300_winsys* r300 = radeon_create_r300_winsys(drmFB, winsys);
44 FREE(winsys);
45 return r300_create_screen(r300);
46 }
47 }
48
49 /* Create a pipe_context. */
50 struct pipe_context* radeon_create_context(struct drm_api* api,
51 struct pipe_screen* screen)
52 {
53 if (getenv("RADEON_SOFTPIPE")) {
54 return radeon_create_softpipe(screen->winsys);
55 } else {
56 return r300_create_context(screen, screen->winsys);
57 }
58 }
59
60 boolean radeon_buffer_from_texture(struct drm_api* api,
61 struct pipe_texture* texture,
62 struct pipe_buffer** buffer,
63 unsigned* stride)
64 {
65 /* XXX fix this */
66 return r300_get_texture_buffer(texture, buffer, stride);
67 }
68
69 /* Create a buffer from a handle. */
70 /* XXX what's up with name? */
71 struct pipe_buffer* radeon_buffer_from_handle(struct drm_api* api,
72 struct pipe_screen* screen,
73 const char* name,
74 unsigned handle)
75 {
76 struct radeon_bo_manager* bom =
77 ((struct radeon_winsys*)screen->winsys)->priv->bom;
78 struct radeon_pipe_buffer* radeon_buffer;
79 struct radeon_bo* bo = NULL;
80
81 bo = radeon_bo_open(bom, handle, 0, 0, 0, 0);
82 if (bo == NULL) {
83 return NULL;
84 }
85
86 radeon_buffer = CALLOC_STRUCT(radeon_pipe_buffer);
87 if (radeon_buffer == NULL) {
88 radeon_bo_unref(bo);
89 return NULL;
90 }
91
92 pipe_reference_init(&radeon_buffer->base.reference, 1);
93 radeon_buffer->base.screen = screen;
94 radeon_buffer->base.usage = PIPE_BUFFER_USAGE_PIXEL;
95 radeon_buffer->bo = bo;
96 return &radeon_buffer->base;
97 }
98
99 boolean radeon_handle_from_buffer(struct drm_api* api,
100 struct pipe_screen* screen,
101 struct pipe_buffer* buffer,
102 unsigned* handle)
103 {
104 struct radeon_pipe_buffer* radeon_buffer =
105 (struct radeon_pipe_buffer*)buffer;
106 *handle = radeon_buffer->bo->handle;
107 return TRUE;
108 }
109
110 boolean radeon_global_handle_from_buffer(struct drm_api* api,
111 struct pipe_screen* screen,
112 struct pipe_buffer* buffer,
113 unsigned* handle)
114 {
115 /* XXX WTF is the difference here? global? */
116 struct radeon_pipe_buffer* radeon_buffer =
117 (struct radeon_pipe_buffer*)buffer;
118 *handle = radeon_buffer->bo->handle;
119 return TRUE;
120 }
121
122 struct drm_api drm_api_hooks = {
123 .create_screen = radeon_create_screen,
124 .create_context = radeon_create_context,
125 .buffer_from_texture = radeon_buffer_from_texture,
126 .buffer_from_handle = radeon_buffer_from_handle,
127 .handle_from_buffer = radeon_handle_from_buffer,
128 .global_handle_from_buffer = radeon_global_handle_from_buffer,
129 };
130
131 struct drm_api* drm_api_create()
132 {
133 return &drm_api_hooks;
134 }