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 "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 * Initialize the fields of a gl_renderbuffer to default values.
37 */
38void
39_mesa_init_renderbuffer(struct gl_renderbuffer *rb, GLuint name)
40{
41   GET_CURRENT_CONTEXT(ctx);
42
43   simple_mtx_init(&rb->Mutex, mtx_plain);
44
45   rb->ClassID = 0;
46   rb->Name = name;
47   rb->RefCount = 1;
48   rb->Delete = _mesa_delete_renderbuffer;
49
50   /* The rest of these should be set later by the caller of this function or
51    * the AllocStorage method:
52    */
53   rb->AllocStorage = NULL;
54
55   rb->Width = 0;
56   rb->Height = 0;
57   rb->Depth = 0;
58
59   /* In GL 3, the initial format is GL_RGBA according to Table 6.26
60    * on page 302 of the GL 3.3 spec.
61    *
62    * In GLES 3, the initial format is GL_RGBA4 according to Table 6.15
63    * on page 258 of the GLES 3.0.4 spec.
64    *
65    * If the context is current, set the initial format based on the
66    * specs. If the context is not current, we cannot determine the
67    * API, so default to GL_RGBA.
68    */
69   if (ctx && _mesa_is_gles(ctx)) {
70      rb->InternalFormat = GL_RGBA4;
71   } else {
72      rb->InternalFormat = GL_RGBA;
73   }
74
75   rb->Format = MESA_FORMAT_NONE;
76}
77
78
79/**
80 * Allocate a new gl_renderbuffer object.  This can be used for user-created
81 * renderbuffers or window-system renderbuffers.
82 */
83struct gl_renderbuffer *
84_mesa_new_renderbuffer(struct gl_context *ctx, GLuint name)
85{
86   struct gl_renderbuffer *rb = CALLOC_STRUCT(gl_renderbuffer);
87   if (rb) {
88      _mesa_init_renderbuffer(rb, name);
89   }
90   return rb;
91}
92
93
94/**
95 * Delete a gl_framebuffer.
96 * This is the default function for renderbuffer->Delete().
97 * Drivers which subclass gl_renderbuffer should probably implement their
98 * own delete function.  But the driver might also call this function to
99 * free the object in the end.
100 */
101void
102_mesa_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
103{
104   simple_mtx_destroy(&rb->Mutex);
105   free(rb->Label);
106   free(rb);
107}
108
109static void
110validate_and_init_renderbuffer_attachment(struct gl_framebuffer *fb,
111                                          gl_buffer_index bufferName,
112                                          struct gl_renderbuffer *rb)
113{
114   assert(fb);
115   assert(rb);
116   assert(bufferName < BUFFER_COUNT);
117
118   /* There should be no previous renderbuffer on this attachment point,
119    * with the exception of depth/stencil since the same renderbuffer may
120    * be used for both.
121    */
122   assert(bufferName == BUFFER_DEPTH ||
123          bufferName == BUFFER_STENCIL ||
124          fb->Attachment[bufferName].Renderbuffer == NULL);
125
126   /* winsys vs. user-created buffer cross check */
127   if (_mesa_is_user_fbo(fb)) {
128      assert(rb->Name);
129   }
130   else {
131      assert(!rb->Name);
132   }
133
134   fb->Attachment[bufferName].Type = GL_RENDERBUFFER_EXT;
135   fb->Attachment[bufferName].Complete = GL_TRUE;
136}
137
138
139/**
140 * Attach a renderbuffer to a framebuffer.
141 * \param bufferName  one of the BUFFER_x tokens
142 *
143 * This function avoids adding a reference and is therefore intended to be
144 * used with a freshly created renderbuffer.
145 */
146void
147_mesa_attach_and_own_rb(struct gl_framebuffer *fb,
148                        gl_buffer_index bufferName,
149                        struct gl_renderbuffer *rb)
150{
151   assert(rb->RefCount == 1);
152
153   validate_and_init_renderbuffer_attachment(fb, bufferName, rb);
154
155   _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer,
156                                NULL);
157   fb->Attachment[bufferName].Renderbuffer = rb;
158}
159
160/**
161 * Attach a renderbuffer to a framebuffer.
162 * \param bufferName  one of the BUFFER_x tokens
163 */
164void
165_mesa_attach_and_reference_rb(struct gl_framebuffer *fb,
166                              gl_buffer_index bufferName,
167                              struct gl_renderbuffer *rb)
168{
169   validate_and_init_renderbuffer_attachment(fb, bufferName, rb);
170   _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, rb);
171}
172
173
174/**
175 * Remove the named renderbuffer from the given framebuffer.
176 * \param bufferName  one of the BUFFER_x tokens
177 */
178void
179_mesa_remove_renderbuffer(struct gl_framebuffer *fb,
180                          gl_buffer_index bufferName)
181{
182   assert(bufferName < BUFFER_COUNT);
183   _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer,
184                                NULL);
185}
186
187
188/**
189 * Set *ptr to point to rb.  If *ptr points to another renderbuffer,
190 * dereference that buffer first.  The new renderbuffer's refcount will
191 * be incremented.  The old renderbuffer's refcount will be decremented.
192 * This is normally only called from the _mesa_reference_renderbuffer() macro
193 * when there's a real pointer change.
194 */
195void
196_mesa_reference_renderbuffer_(struct gl_renderbuffer **ptr,
197                              struct gl_renderbuffer *rb)
198{
199   if (*ptr) {
200      /* Unreference the old renderbuffer */
201      GLboolean deleteFlag = GL_FALSE;
202      struct gl_renderbuffer *oldRb = *ptr;
203
204      simple_mtx_lock(&oldRb->Mutex);
205      assert(oldRb->RefCount > 0);
206      oldRb->RefCount--;
207      deleteFlag = (oldRb->RefCount == 0);
208      simple_mtx_unlock(&oldRb->Mutex);
209
210      if (deleteFlag) {
211         GET_CURRENT_CONTEXT(ctx);
212         oldRb->Delete(ctx, oldRb);
213      }
214
215      *ptr = NULL;
216   }
217   assert(!*ptr);
218
219   if (rb) {
220      /* reference new renderbuffer */
221      simple_mtx_lock(&rb->Mutex);
222      rb->RefCount++;
223      simple_mtx_unlock(&rb->Mutex);
224      *ptr = rb;
225   }
226}
227