2624f01259d78d8302c06055bb8047ecc27b2ba7
[mesa.git] / src / mesa / main / renderbuffer.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "util/imports.h"
28 #include "context.h"
29 #include "fbobject.h"
30 #include "formats.h"
31 #include "mtypes.h"
32 #include "renderbuffer.h"
33 #include "util/u_memory.h"
34
35
36 /**
37 * Initialize the fields of a gl_renderbuffer to default values.
38 */
39 void
40 _mesa_init_renderbuffer(struct gl_renderbuffer *rb, GLuint name)
41 {
42 GET_CURRENT_CONTEXT(ctx);
43
44 simple_mtx_init(&rb->Mutex, mtx_plain);
45
46 rb->ClassID = 0;
47 rb->Name = name;
48 rb->RefCount = 1;
49 rb->Delete = _mesa_delete_renderbuffer;
50
51 /* The rest of these should be set later by the caller of this function or
52 * the AllocStorage method:
53 */
54 rb->AllocStorage = NULL;
55
56 rb->Width = 0;
57 rb->Height = 0;
58 rb->Depth = 0;
59
60 /* In GL 3, the initial format is GL_RGBA according to Table 6.26
61 * on page 302 of the GL 3.3 spec.
62 *
63 * In GLES 3, the initial format is GL_RGBA4 according to Table 6.15
64 * on page 258 of the GLES 3.0.4 spec.
65 *
66 * If the context is current, set the initial format based on the
67 * specs. If the context is not current, we cannot determine the
68 * API, so default to GL_RGBA.
69 */
70 if (ctx && _mesa_is_gles(ctx)) {
71 rb->InternalFormat = GL_RGBA4;
72 } else {
73 rb->InternalFormat = GL_RGBA;
74 }
75
76 rb->Format = MESA_FORMAT_NONE;
77 }
78
79
80 /**
81 * Allocate a new gl_renderbuffer object. This can be used for user-created
82 * renderbuffers or window-system renderbuffers.
83 */
84 struct gl_renderbuffer *
85 _mesa_new_renderbuffer(struct gl_context *ctx, GLuint name)
86 {
87 struct gl_renderbuffer *rb = CALLOC_STRUCT(gl_renderbuffer);
88 if (rb) {
89 _mesa_init_renderbuffer(rb, name);
90 }
91 return rb;
92 }
93
94
95 /**
96 * Delete a gl_framebuffer.
97 * This is the default function for renderbuffer->Delete().
98 * Drivers which subclass gl_renderbuffer should probably implement their
99 * own delete function. But the driver might also call this function to
100 * free the object in the end.
101 */
102 void
103 _mesa_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
104 {
105 simple_mtx_destroy(&rb->Mutex);
106 free(rb->Label);
107 free(rb);
108 }
109
110 static void
111 validate_and_init_renderbuffer_attachment(struct gl_framebuffer *fb,
112 gl_buffer_index bufferName,
113 struct gl_renderbuffer *rb)
114 {
115 assert(fb);
116 assert(rb);
117 assert(bufferName < BUFFER_COUNT);
118
119 /* There should be no previous renderbuffer on this attachment point,
120 * with the exception of depth/stencil since the same renderbuffer may
121 * be used for both.
122 */
123 assert(bufferName == BUFFER_DEPTH ||
124 bufferName == BUFFER_STENCIL ||
125 fb->Attachment[bufferName].Renderbuffer == NULL);
126
127 /* winsys vs. user-created buffer cross check */
128 if (_mesa_is_user_fbo(fb)) {
129 assert(rb->Name);
130 }
131 else {
132 assert(!rb->Name);
133 }
134
135 fb->Attachment[bufferName].Type = GL_RENDERBUFFER_EXT;
136 fb->Attachment[bufferName].Complete = GL_TRUE;
137 }
138
139
140 /**
141 * Attach a renderbuffer to a framebuffer.
142 * \param bufferName one of the BUFFER_x tokens
143 *
144 * This function avoids adding a reference and is therefore intended to be
145 * used with a freshly created renderbuffer.
146 */
147 void
148 _mesa_attach_and_own_rb(struct gl_framebuffer *fb,
149 gl_buffer_index bufferName,
150 struct gl_renderbuffer *rb)
151 {
152 assert(rb->RefCount == 1);
153
154 validate_and_init_renderbuffer_attachment(fb, bufferName, rb);
155
156 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer,
157 NULL);
158 fb->Attachment[bufferName].Renderbuffer = rb;
159 }
160
161 /**
162 * Attach a renderbuffer to a framebuffer.
163 * \param bufferName one of the BUFFER_x tokens
164 */
165 void
166 _mesa_attach_and_reference_rb(struct gl_framebuffer *fb,
167 gl_buffer_index bufferName,
168 struct gl_renderbuffer *rb)
169 {
170 validate_and_init_renderbuffer_attachment(fb, bufferName, rb);
171 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, rb);
172 }
173
174
175 /**
176 * Remove the named renderbuffer from the given framebuffer.
177 * \param bufferName one of the BUFFER_x tokens
178 */
179 void
180 _mesa_remove_renderbuffer(struct gl_framebuffer *fb,
181 gl_buffer_index bufferName)
182 {
183 assert(bufferName < BUFFER_COUNT);
184 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer,
185 NULL);
186 }
187
188
189 /**
190 * Set *ptr to point to rb. If *ptr points to another renderbuffer,
191 * dereference that buffer first. The new renderbuffer's refcount will
192 * be incremented. The old renderbuffer's refcount will be decremented.
193 * This is normally only called from the _mesa_reference_renderbuffer() macro
194 * when there's a real pointer change.
195 */
196 void
197 _mesa_reference_renderbuffer_(struct gl_renderbuffer **ptr,
198 struct gl_renderbuffer *rb)
199 {
200 if (*ptr) {
201 /* Unreference the old renderbuffer */
202 GLboolean deleteFlag = GL_FALSE;
203 struct gl_renderbuffer *oldRb = *ptr;
204
205 simple_mtx_lock(&oldRb->Mutex);
206 assert(oldRb->RefCount > 0);
207 oldRb->RefCount--;
208 deleteFlag = (oldRb->RefCount == 0);
209 simple_mtx_unlock(&oldRb->Mutex);
210
211 if (deleteFlag) {
212 GET_CURRENT_CONTEXT(ctx);
213 oldRb->Delete(ctx, oldRb);
214 }
215
216 *ptr = NULL;
217 }
218 assert(!*ptr);
219
220 if (rb) {
221 /* reference new renderbuffer */
222 simple_mtx_lock(&rb->Mutex);
223 rb->RefCount++;
224 simple_mtx_unlock(&rb->Mutex);
225 *ptr = rb;
226 }
227 }