mesa: remove software-based renderbuffer code from core Mesa
[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 * 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 "glheader.h"
27 #include "imports.h"
28 #include "context.h"
29 #include "fbobject.h"
30 #include "formats.h"
31 #include "mtypes.h"
32 #include "renderbuffer.h"
33
34
35 /**
36 * Default GetPointer routine. Always return NULL to indicate that
37 * direct buffer access is not supported.
38 */
39 static void *
40 nop_get_pointer(struct gl_context *ctx, struct gl_renderbuffer *rb, GLint x, GLint y)
41 {
42 return NULL;
43 }
44
45
46 /**
47 * Initialize the fields of a gl_renderbuffer to default values.
48 */
49 void
50 _mesa_init_renderbuffer(struct gl_renderbuffer *rb, GLuint name)
51 {
52 _glthread_INIT_MUTEX(rb->Mutex);
53
54 rb->ClassID = 0;
55 rb->Name = name;
56 rb->RefCount = 0;
57 rb->Delete = _mesa_delete_renderbuffer;
58
59 /* The rest of these should be set later by the caller of this function or
60 * the AllocStorage method:
61 */
62 rb->AllocStorage = NULL;
63
64 rb->Width = 0;
65 rb->Height = 0;
66 rb->InternalFormat = GL_RGBA;
67 rb->Format = MESA_FORMAT_NONE;
68
69 rb->DataType = GL_NONE;
70 rb->Data = NULL;
71
72 /* Point back to ourself so that we don't have to check for Wrapped==NULL
73 * all over the drivers.
74 */
75 rb->Wrapped = rb;
76
77 rb->GetPointer = nop_get_pointer;
78 rb->GetRow = NULL;
79 rb->GetValues = NULL;
80 rb->PutRow = NULL;
81 rb->PutRowRGB = NULL;
82 rb->PutMonoRow = NULL;
83 rb->PutValues = NULL;
84 rb->PutMonoValues = NULL;
85 }
86
87
88 /**
89 * Allocate a new gl_renderbuffer object. This can be used for user-created
90 * renderbuffers or window-system renderbuffers.
91 */
92 struct gl_renderbuffer *
93 _mesa_new_renderbuffer(struct gl_context *ctx, GLuint name)
94 {
95 struct gl_renderbuffer *rb = CALLOC_STRUCT(gl_renderbuffer);
96 if (rb) {
97 _mesa_init_renderbuffer(rb, name);
98 }
99 return rb;
100 }
101
102
103 /**
104 * Delete a gl_framebuffer.
105 * This is the default function for renderbuffer->Delete().
106 */
107 void
108 _mesa_delete_renderbuffer(struct gl_renderbuffer *rb)
109 {
110 if (rb->Data) {
111 free(rb->Data);
112 }
113 free(rb);
114 }
115
116
117 /**
118 * Attach a renderbuffer to a framebuffer.
119 * \param bufferName one of the BUFFER_x tokens
120 */
121 void
122 _mesa_add_renderbuffer(struct gl_framebuffer *fb,
123 gl_buffer_index bufferName, struct gl_renderbuffer *rb)
124 {
125 assert(fb);
126 assert(rb);
127 assert(bufferName < BUFFER_COUNT);
128
129 /* There should be no previous renderbuffer on this attachment point,
130 * with the exception of depth/stencil since the same renderbuffer may
131 * be used for both.
132 */
133 assert(bufferName == BUFFER_DEPTH ||
134 bufferName == BUFFER_STENCIL ||
135 fb->Attachment[bufferName].Renderbuffer == NULL);
136
137 /* winsys vs. user-created buffer cross check */
138 if (fb->Name) {
139 assert(rb->Name);
140 }
141 else {
142 assert(!rb->Name);
143 }
144
145 fb->Attachment[bufferName].Type = GL_RENDERBUFFER_EXT;
146 fb->Attachment[bufferName].Complete = GL_TRUE;
147 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, rb);
148 }
149
150
151 /**
152 * Remove the named renderbuffer from the given framebuffer.
153 * \param bufferName one of the BUFFER_x tokens
154 */
155 void
156 _mesa_remove_renderbuffer(struct gl_framebuffer *fb,
157 gl_buffer_index bufferName)
158 {
159 struct gl_renderbuffer *rb;
160
161 assert(bufferName < BUFFER_COUNT);
162
163 rb = fb->Attachment[bufferName].Renderbuffer;
164 if (!rb)
165 return;
166
167 _mesa_reference_renderbuffer(&rb, NULL);
168
169 fb->Attachment[bufferName].Renderbuffer = NULL;
170 }
171
172
173 /**
174 * Set *ptr to point to rb. If *ptr points to another renderbuffer,
175 * dereference that buffer first. The new renderbuffer's refcount will
176 * be incremented. The old renderbuffer's refcount will be decremented.
177 * This is normally only called from the _mesa_reference_renderbuffer() macro
178 * when there's a real pointer change.
179 */
180 void
181 _mesa_reference_renderbuffer_(struct gl_renderbuffer **ptr,
182 struct gl_renderbuffer *rb)
183 {
184 if (*ptr) {
185 /* Unreference the old renderbuffer */
186 GLboolean deleteFlag = GL_FALSE;
187 struct gl_renderbuffer *oldRb = *ptr;
188
189 _glthread_LOCK_MUTEX(oldRb->Mutex);
190 ASSERT(oldRb->RefCount > 0);
191 oldRb->RefCount--;
192 /*printf("RB DECR %p (%d) to %d\n", (void*) oldRb, oldRb->Name, oldRb->RefCount);*/
193 deleteFlag = (oldRb->RefCount == 0);
194 _glthread_UNLOCK_MUTEX(oldRb->Mutex);
195
196 if (deleteFlag) {
197 oldRb->Delete(oldRb);
198 }
199
200 *ptr = NULL;
201 }
202 assert(!*ptr);
203
204 if (rb) {
205 /* reference new renderbuffer */
206 _glthread_LOCK_MUTEX(rb->Mutex);
207 rb->RefCount++;
208 /*printf("RB INCR %p (%d) to %d\n", (void*) rb, rb->Name, rb->RefCount);*/
209 _glthread_UNLOCK_MUTEX(rb->Mutex);
210 *ptr = rb;
211 }
212 }