gallium: Unify reference counting.
[mesa.git] / src / gallium / state_trackers / xorg / xorg_dri2.c
1 /*
2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * 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 above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 *
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
28 *
29 */
30
31 #include "xf86.h"
32 #include "xf86_OSproc.h"
33
34 #include "xorg_tracker.h"
35
36 #include "dri2.h"
37
38 #include "pipe/p_state.h"
39 #include "pipe/p_inlines.h"
40
41 typedef struct {
42 PixmapPtr pPixmap;
43 struct pipe_texture *tex;
44 struct pipe_buffer *buf;
45 } *BufferPrivatePtr;
46
47 static DRI2BufferPtr
48 driCreateBuffers(DrawablePtr pDraw, unsigned int *attachments, int count)
49 {
50 struct pipe_texture *depth, *tex;
51 struct pipe_buffer *buf;
52 ScreenPtr pScreen = pDraw->pScreen;
53 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
54 modesettingPtr ms = modesettingPTR(pScrn);
55 BufferPrivatePtr privates;
56 DRI2BufferPtr buffers;
57 PixmapPtr pPixmap;
58 unsigned stride, handle;
59 int i;
60
61 buffers = xcalloc(count, sizeof *buffers);
62 if (!buffers)
63 goto fail_buffers;
64
65 privates = xcalloc(count, sizeof *privates);
66 if (!privates)
67 goto fail_privates;
68
69 depth = NULL;
70 for (i = 0; i < count; i++) {
71 pPixmap = NULL;
72 tex = NULL;
73 buf = NULL;
74 if (attachments[i] == DRI2BufferFrontLeft) {
75 if (pDraw->type == DRAWABLE_PIXMAP)
76 pPixmap = (PixmapPtr) pDraw;
77 else
78 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
79 pPixmap->refcnt++;
80 tex = xorg_exa_get_texture(pPixmap);
81 } else if (attachments[i] == DRI2BufferStencil) {
82 pipe_texture_reference(&tex, depth);
83 } else if (attachments[i] == DRI2BufferDepth) {
84 struct pipe_texture template;
85
86 memset(&template, 0, sizeof(template));
87 template.target = PIPE_TEXTURE_2D;
88 template.compressed = 0;
89 template.format = PIPE_FORMAT_S8Z24_UNORM;
90 pf_get_block(template.format, &template.block);
91 template.width[0] = pDraw->width;
92 template.height[0] = pDraw->height;
93 template.depth[0] = 1;
94 template.last_level = 0;
95 template.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
96 tex = ms->screen->texture_create(ms->screen, &template);
97 } else {
98 struct pipe_texture template;
99 memset(&template, 0, sizeof(template));
100 template.target = PIPE_TEXTURE_2D;
101 template.compressed = 0;
102 template.format = PIPE_FORMAT_A8R8G8B8_UNORM;
103 pf_get_block(template.format, &template.block);
104 template.width[0] = pDraw->width;
105 template.height[0] = pDraw->height;
106 template.depth[0] = 1;
107 template.last_level = 0;
108 template.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
109 tex = ms->screen->texture_create(ms->screen, &template);
110 }
111
112 drm_api_hooks.buffer_from_texture(tex, &buf, &stride);
113 drm_api_hooks.global_handle_from_buffer(ms->screen, buf, &handle);
114
115 buffers[i].name = handle;
116 buffers[i].attachment = attachments[i];
117 buffers[i].pitch = stride;
118 buffers[i].cpp = 4;
119 buffers[i].driverPrivate = &privates[i];
120 buffers[i].flags = 0; /* not tiled */
121 privates[i].pPixmap = pPixmap;
122 privates[i].buf = buf;
123 privates[i].tex = tex;
124 }
125
126 return buffers;
127
128 fail_privates:
129 xfree(buffers);
130 fail_buffers:
131 return NULL;
132 }
133
134 static void
135 driDestroyBuffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
136 {
137 ScreenPtr pScreen = pDraw->pScreen;
138 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
139 modesettingPtr ms = modesettingPTR(pScrn);
140 BufferPrivatePtr private;
141 int i;
142
143 for (i = 0; i < count; i++) {
144 private = buffers[i].driverPrivate;
145
146 if (private->pPixmap)
147 (*pScreen->DestroyPixmap)(private->pPixmap);
148
149 pipe_texture_reference(&private->tex, NULL);
150 pipe_buffer_reference(&private->buf, NULL);
151 }
152
153 if (buffers) {
154 xfree(buffers[0].driverPrivate);
155 xfree(buffers);
156 }
157 }
158
159 static void
160 driCopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
161 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
162 {
163 ScreenPtr pScreen = pDraw->pScreen;
164 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
165 modesettingPtr ms = modesettingPTR(pScrn);
166 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
167 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
168
169 struct pipe_surface *dst_surf =
170 ms->screen->get_tex_surface(ms->screen, dst_priv->tex, 0, 0, 0,
171 PIPE_BUFFER_USAGE_GPU_WRITE);
172 struct pipe_surface *src_surf =
173 ms->screen->get_tex_surface(ms->screen, src_priv->tex, 0, 0, 0,
174 PIPE_BUFFER_USAGE_GPU_READ);
175
176 ms->ctx->surface_copy(ms->ctx, 0, dst_surf, 0, 0, src_surf,
177 0, 0, pDraw->width, pDraw->height);
178
179 pipe_surface_reference(&dst_surf, NULL);
180 pipe_surface_reference(&src_surf, NULL);
181 }
182
183 Bool
184 driScreenInit(ScreenPtr pScreen)
185 {
186 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
187 modesettingPtr ms = modesettingPTR(pScrn);
188 DRI2InfoRec dri2info;
189
190 dri2info.version = 1;
191 dri2info.fd = ms->fd;
192 #if 0
193 dri2info.driverName = pScrn->name;
194 #else
195 dri2info.driverName = "i915"; /* FIXME */
196 #endif
197 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
198
199 dri2info.CreateBuffers = driCreateBuffers;
200 dri2info.DestroyBuffers = driDestroyBuffers;
201 dri2info.CopyRegion = driCopyRegion;
202
203 return DRI2ScreenInit(pScreen, &dri2info);
204 }
205
206 void
207 driCloseScreen(ScreenPtr pScreen)
208 {
209 DRI2CloseScreen(pScreen);
210 }
211
212 /* vim: set sw=4 ts=8 sts=4: */