mesa: Restore 78-column wrapping of license text in C-style comments.
[mesa.git] / src / mesa / main / renderbuffer.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "glheader.h"
28 #include "imports.h"
29 #include "context.h"
30 #include "fbobject.h"
31 #include "formats.h"
32 #include "mtypes.h"
33 #include "renderbuffer.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 _glthread_INIT_MUTEX(rb->Mutex);
43
44 rb->ClassID = 0;
45 rb->Name = name;
46 rb->RefCount = 0;
47 rb->Delete = _mesa_delete_renderbuffer;
48
49 /* The rest of these should be set later by the caller of this function or
50 * the AllocStorage method:
51 */
52 rb->AllocStorage = NULL;
53
54 rb->Width = 0;
55 rb->Height = 0;
56 rb->InternalFormat = GL_RGBA;
57 rb->Format = MESA_FORMAT_NONE;
58 }
59
60
61 /**
62 * Allocate a new gl_renderbuffer object. This can be used for user-created
63 * renderbuffers or window-system renderbuffers.
64 */
65 struct gl_renderbuffer *
66 _mesa_new_renderbuffer(struct gl_context *ctx, GLuint name)
67 {
68 struct gl_renderbuffer *rb = CALLOC_STRUCT(gl_renderbuffer);
69 if (rb) {
70 _mesa_init_renderbuffer(rb, name);
71 }
72 return rb;
73 }
74
75
76 /**
77 * Delete a gl_framebuffer.
78 * This is the default function for renderbuffer->Delete().
79 * Drivers which subclass gl_renderbuffer should probably implement their
80 * own delete function. But the driver might also call this function to
81 * free the object in the end.
82 */
83 void
84 _mesa_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
85 {
86 _glthread_DESTROY_MUTEX(rb->Mutex);
87 free(rb);
88 }
89
90
91 /**
92 * Attach a renderbuffer to a framebuffer.
93 * \param bufferName one of the BUFFER_x tokens
94 */
95 void
96 _mesa_add_renderbuffer(struct gl_framebuffer *fb,
97 gl_buffer_index bufferName, struct gl_renderbuffer *rb)
98 {
99 assert(fb);
100 assert(rb);
101 assert(bufferName < BUFFER_COUNT);
102
103 /* There should be no previous renderbuffer on this attachment point,
104 * with the exception of depth/stencil since the same renderbuffer may
105 * be used for both.
106 */
107 assert(bufferName == BUFFER_DEPTH ||
108 bufferName == BUFFER_STENCIL ||
109 fb->Attachment[bufferName].Renderbuffer == NULL);
110
111 /* winsys vs. user-created buffer cross check */
112 if (_mesa_is_user_fbo(fb)) {
113 assert(rb->Name);
114 }
115 else {
116 assert(!rb->Name);
117 }
118
119 fb->Attachment[bufferName].Type = GL_RENDERBUFFER_EXT;
120 fb->Attachment[bufferName].Complete = GL_TRUE;
121 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, rb);
122 }
123
124
125 /**
126 * Remove the named renderbuffer from the given framebuffer.
127 * \param bufferName one of the BUFFER_x tokens
128 */
129 void
130 _mesa_remove_renderbuffer(struct gl_framebuffer *fb,
131 gl_buffer_index bufferName)
132 {
133 assert(bufferName < BUFFER_COUNT);
134 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer,
135 NULL);
136 }
137
138
139 /**
140 * Set *ptr to point to rb. If *ptr points to another renderbuffer,
141 * dereference that buffer first. The new renderbuffer's refcount will
142 * be incremented. The old renderbuffer's refcount will be decremented.
143 * This is normally only called from the _mesa_reference_renderbuffer() macro
144 * when there's a real pointer change.
145 */
146 void
147 _mesa_reference_renderbuffer_(struct gl_renderbuffer **ptr,
148 struct gl_renderbuffer *rb)
149 {
150 if (*ptr) {
151 /* Unreference the old renderbuffer */
152 GLboolean deleteFlag = GL_FALSE;
153 struct gl_renderbuffer *oldRb = *ptr;
154
155 _glthread_LOCK_MUTEX(oldRb->Mutex);
156 ASSERT(oldRb->RefCount > 0);
157 oldRb->RefCount--;
158 /*printf("RB DECR %p (%d) to %d\n", (void*) oldRb, oldRb->Name, oldRb->RefCount);*/
159 deleteFlag = (oldRb->RefCount == 0);
160 _glthread_UNLOCK_MUTEX(oldRb->Mutex);
161
162 if (deleteFlag) {
163 GET_CURRENT_CONTEXT(ctx);
164 oldRb->Delete(ctx, oldRb);
165 }
166
167 *ptr = NULL;
168 }
169 assert(!*ptr);
170
171 if (rb) {
172 /* reference new renderbuffer */
173 _glthread_LOCK_MUTEX(rb->Mutex);
174 rb->RefCount++;
175 /*printf("RB INCR %p (%d) to %d\n", (void*) rb, rb->Name, rb->RefCount);*/
176 _glthread_UNLOCK_MUTEX(rb->Mutex);
177 *ptr = rb;
178 }
179 }