some clean-up and re-org for renderbuffers
[mesa.git] / src / mesa / drivers / x11 / xm_buffer.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions 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 MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glxheader.h"
27 #include "GL/xmesa.h"
28 #include "xmesaP.h"
29 #include "imports.h"
30 #include "renderbuffer.h"
31
32
33 static void
34 xmesa_delete_renderbuffer(struct gl_renderbuffer *rb)
35 {
36 /* XXX Note: the ximage or Pixmap attached to this renderbuffer
37 * should probably get freed here, but that's currently done in
38 * XMesaDestroyBuffer().
39 */
40 _mesa_free(rb);
41 }
42
43
44 /**
45 * Reallocate renderbuffer storage for front color buffer.
46 */
47 static GLboolean
48 xmesa_alloc_front_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
49 GLenum internalFormat, GLuint width, GLuint height)
50 {
51 struct xmesa_renderbuffer *xrb = (struct xmesa_renderbuffer *) rb;
52
53 /* just clear these to be sure we don't accidentally use them */
54 xrb->origin1 = NULL;
55 xrb->origin2 = NULL;
56 xrb->origin4 = NULL;
57
58 /* for the FLIP macro: */
59 xrb->bottom = height - 1;
60
61 rb->Width = width;
62 rb->Height = height;
63 rb->InternalFormat = internalFormat;
64
65 return GL_TRUE;
66 }
67
68
69 /**
70 * Reallocate renderbuffer storage for back color buffer.
71 * XXX we should resize the back pixmap/ximage here.
72 */
73 static GLboolean
74 xmesa_alloc_back_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
75 GLenum internalFormat, GLuint width, GLuint height)
76 {
77 struct xmesa_renderbuffer *xrb = (struct xmesa_renderbuffer *) rb;
78
79 /* same as front buffer */
80 (void) xmesa_alloc_front_storage(ctx, rb, internalFormat, width, height);
81
82 /* plus... */
83 if (xrb->ximage) {
84 /* Needed by PIXELADDR1 macro */
85 xrb->width1 = xrb->ximage->bytes_per_line;
86 xrb->origin1 = (GLubyte *) xrb->ximage->data + xrb->width1 * (height - 1);
87
88 /* Needed by PIXELADDR2 macro */
89 xrb->width2 = xrb->ximage->bytes_per_line / 2;
90 xrb->origin2 = (GLushort *) xrb->ximage->data + xrb->width2 * (height - 1);
91
92 /* Needed by PIXELADDR3 macro */
93 xrb->width3 = xrb->ximage->bytes_per_line;
94 xrb->origin3 = (GLubyte *) xrb->ximage->data + xrb->width3 * (height - 1);
95
96 /* Needed by PIXELADDR4 macro */
97 xrb->width4 = xrb->ximage->width;
98 xrb->origin4 = (GLuint *) xrb->ximage->data + xrb->width4 * (height - 1);
99 }
100 else {
101 assert(xrb->pixmap);
102 }
103
104 return GL_TRUE;
105 }
106
107
108 struct xmesa_renderbuffer *
109 xmesa_new_renderbuffer(GLcontext *ctx, GLuint name, GLboolean rgbMode,
110 GLboolean backBuffer)
111 {
112 struct xmesa_renderbuffer *xrb = CALLOC_STRUCT(xmesa_renderbuffer);
113 if (xrb) {
114 GLuint name = 0;
115 _mesa_init_renderbuffer(&xrb->Base, name);
116
117 xrb->Base.Delete = xmesa_delete_renderbuffer;
118 if (backBuffer)
119 xrb->Base.AllocStorage = xmesa_alloc_back_storage;
120 else
121 xrb->Base.AllocStorage = xmesa_alloc_front_storage;
122
123 if (rgbMode) {
124 xrb->Base.InternalFormat = GL_RGBA;
125 xrb->Base._BaseFormat = GL_RGBA;
126 xrb->Base.DataType = GL_UNSIGNED_BYTE;
127 }
128 else {
129 xrb->Base.InternalFormat = GL_COLOR_INDEX;
130 xrb->Base._BaseFormat = GL_COLOR_INDEX;
131 xrb->Base.DataType = GL_UNSIGNED_INT;
132 }
133 xrb->Base.ComponentSizes[0] = 0; /* XXX fix? */
134 }
135 return xrb;
136 }
137
138
139
140