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