mesa: remove a bunch of gl_renderbuffer fields
[mesa.git] / src / mesa / drivers / dri / common / drirenderbuffer.c
1
2 #include "main/mtypes.h"
3 #include "main/formats.h"
4 #include "main/framebuffer.h"
5 #include "main/renderbuffer.h"
6 #include "main/imports.h"
7 #include "drirenderbuffer.h"
8
9
10 /**
11 * This will get called when a window (gl_framebuffer) is resized (probably
12 * via driUpdateFramebufferSize(), below).
13 * Just update width, height and internal format fields for now.
14 * There's usually no memory allocation above because the present
15 * DRI drivers use statically-allocated full-screen buffers. If that's not
16 * the case for a DRI driver, a different AllocStorage method should
17 * be used.
18 */
19 static GLboolean
20 driRenderbufferStorage(GLcontext *ctx, struct gl_renderbuffer *rb,
21 GLenum internalFormat, GLuint width, GLuint height)
22 {
23 rb->Width = width;
24 rb->Height = height;
25 rb->InternalFormat = internalFormat;
26 return GL_TRUE;
27 }
28
29
30 static void
31 driDeleteRenderbuffer(struct gl_renderbuffer *rb)
32 {
33 /* don't free rb->Data Chances are it's a memory mapped region for
34 * the dri drivers.
35 */
36 _mesa_free(rb);
37 }
38
39
40 /**
41 * Allocate a new driRenderbuffer object.
42 * Individual drivers are free to implement different versions of
43 * this function.
44 *
45 * At this time, this function can only be used for window-system
46 * renderbuffers, not user-created RBOs.
47 *
48 * \param format Either GL_RGBA, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24,
49 * GL_DEPTH_COMPONENT32, or GL_STENCIL_INDEX8_EXT (for now).
50 * \param addr address in main memory of the buffer. Probably a memory
51 * mapped region.
52 * \param cpp chars or bytes per pixel
53 * \param offset start of renderbuffer with respect to start of framebuffer
54 * \param pitch pixels per row
55 */
56 driRenderbuffer *
57 driNewRenderbuffer(GLenum format, GLvoid *addr,
58 GLint cpp, GLint offset, GLint pitch,
59 __DRIdrawablePrivate *dPriv)
60 {
61 driRenderbuffer *drb;
62
63 assert(format == GL_RGBA ||
64 format == GL_RGB5 ||
65 format == GL_RGBA8 ||
66 format == GL_DEPTH_COMPONENT16 ||
67 format == GL_DEPTH_COMPONENT24 ||
68 format == GL_DEPTH_COMPONENT32 ||
69 format == GL_STENCIL_INDEX8_EXT);
70
71 assert(cpp > 0);
72 assert(pitch > 0);
73
74 drb = _mesa_calloc(sizeof(driRenderbuffer));
75 if (drb) {
76 const GLuint name = 0;
77
78 _mesa_init_renderbuffer(&drb->Base, name);
79
80 /* Make sure we're using a null-valued GetPointer routine */
81 assert(drb->Base.GetPointer(NULL, &drb->Base, 0, 0) == NULL);
82
83 drb->Base.InternalFormat = format;
84
85 if (format == GL_RGBA || format == GL_RGB5 || format == GL_RGBA8) {
86 /* Color */
87 drb->Base.DataType = GL_UNSIGNED_BYTE;
88 if (format == GL_RGB5) {
89 drb->Base.Format = MESA_FORMAT_RGB565;
90 }
91 else {
92 drb->Base.Format = MESA_FORMAT_ARGB8888;
93 }
94 }
95 else if (format == GL_DEPTH_COMPONENT16) {
96 /* Depth */
97 /* we always Get/Put 32-bit Z values */
98 drb->Base.DataType = GL_UNSIGNED_INT;
99 drb->Base.Format = MESA_FORMAT_Z16;
100 }
101 else if (format == GL_DEPTH_COMPONENT24) {
102 /* Depth */
103 /* we always Get/Put 32-bit Z values */
104 drb->Base.DataType = GL_UNSIGNED_INT;
105 drb->Base.Format = MESA_FORMAT_Z32;
106 }
107 else if (format == GL_DEPTH_COMPONENT32) {
108 /* Depth */
109 /* we always Get/Put 32-bit Z values */
110 drb->Base.DataType = GL_UNSIGNED_INT;
111 drb->Base.Format = MESA_FORMAT_Z32;
112 }
113 else {
114 /* Stencil */
115 ASSERT(format == GL_STENCIL_INDEX8_EXT);
116 drb->Base.DataType = GL_UNSIGNED_BYTE;
117 drb->Base.Format = MESA_FORMAT_S8;
118 }
119
120 /* XXX if we were allocating a user-created renderbuffer, we'd have
121 * to fill in the Red/Green/Blue/.../Bits values too.
122 */
123
124 drb->Base.AllocStorage = driRenderbufferStorage;
125 drb->Base.Delete = driDeleteRenderbuffer;
126
127 drb->Base.Data = addr;
128
129 /* DRI renderbuffer-specific fields: */
130 drb->dPriv = dPriv;
131 drb->offset = offset;
132 drb->pitch = pitch;
133 drb->cpp = cpp;
134
135 /* may be changed if page flipping is active: */
136 drb->flippedOffset = offset;
137 drb->flippedPitch = pitch;
138 drb->flippedData = addr;
139 }
140 return drb;
141 }
142
143
144 /**
145 * Update the front and back renderbuffers' flippedPitch/Offset/Data fields.
146 * If stereo, flip both the left and right pairs.
147 * This is used when we do double buffering via page flipping.
148 * \param fb the framebuffer we're page flipping
149 * \param flipped if true, set flipped values, else set non-flipped values
150 */
151 void
152 driFlipRenderbuffers(struct gl_framebuffer *fb, GLboolean flipped)
153 {
154 const GLuint count = fb->Visual.stereoMode ? 2 : 1;
155 GLuint lr; /* left or right */
156
157 /* we shouldn't really call this function if single-buffered, but
158 * play it safe.
159 */
160 if (!fb->Visual.doubleBufferMode)
161 return;
162
163 for (lr = 0; lr < count; lr++) {
164 GLuint frontBuf = (lr == 0) ? BUFFER_FRONT_LEFT : BUFFER_FRONT_RIGHT;
165 GLuint backBuf = (lr == 0) ? BUFFER_BACK_LEFT : BUFFER_BACK_RIGHT;
166 driRenderbuffer *front_drb
167 = (driRenderbuffer *) fb->Attachment[frontBuf].Renderbuffer;
168 driRenderbuffer *back_drb
169 = (driRenderbuffer *) fb->Attachment[backBuf].Renderbuffer;
170
171 if (flipped) {
172 front_drb->flippedOffset = back_drb->offset;
173 front_drb->flippedPitch = back_drb->pitch;
174 front_drb->flippedData = back_drb->Base.Data;
175 back_drb->flippedOffset = front_drb->offset;
176 back_drb->flippedPitch = front_drb->pitch;
177 back_drb->flippedData = front_drb->Base.Data;
178 }
179 else {
180 front_drb->flippedOffset = front_drb->offset;
181 front_drb->flippedPitch = front_drb->pitch;
182 front_drb->flippedData = front_drb->Base.Data;
183 back_drb->flippedOffset = back_drb->offset;
184 back_drb->flippedPitch = back_drb->pitch;
185 back_drb->flippedData = back_drb->Base.Data;
186 }
187 }
188 }
189
190
191 /**
192 * Check that the gl_framebuffer associated with dPriv is the right size.
193 * Resize the gl_framebuffer if needed.
194 * It's expected that the dPriv->driverPrivate member points to a
195 * gl_framebuffer object.
196 */
197 void
198 driUpdateFramebufferSize(GLcontext *ctx, const __DRIdrawablePrivate *dPriv)
199 {
200 struct gl_framebuffer *fb = (struct gl_framebuffer *) dPriv->driverPrivate;
201 if (fb && (dPriv->w != fb->Width || dPriv->h != fb->Height)) {
202 ctx->Driver.ResizeBuffers(ctx, fb, dPriv->w, dPriv->h);
203 /* if the driver needs the hw lock for ResizeBuffers, the drawable
204 might have changed again by now */
205 assert(fb->Width == dPriv->w);
206 assert(fb->Height == dPriv->h);
207 }
208 }