[QMGRPRXY]
[reactos.git] / reactos / dll / opengl / 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 #include <precomp.h>
26
27 /**
28 * Initialize the fields of a gl_renderbuffer to default values.
29 */
30 void
31 _mesa_init_renderbuffer(struct gl_renderbuffer *rb, GLuint name)
32 {
33 _glthread_INIT_MUTEX(rb->Mutex);
34
35 rb->ClassID = 0;
36 rb->RefCount = 0;
37 rb->Delete = _mesa_delete_renderbuffer;
38
39 /* The rest of these should be set later by the caller of this function or
40 * the AllocStorage method:
41 */
42 rb->AllocStorage = NULL;
43
44 rb->Width = 0;
45 rb->Height = 0;
46 rb->InternalFormat = GL_RGBA;
47 rb->Format = MESA_FORMAT_NONE;
48 }
49
50
51 /**
52 * Delete a gl_framebuffer.
53 * This is the default function for renderbuffer->Delete().
54 */
55 void
56 _mesa_delete_renderbuffer(struct gl_renderbuffer *rb)
57 {
58 /* no-op */
59 }
60
61
62 /**
63 * Attach a renderbuffer to a framebuffer.
64 * \param bufferName one of the BUFFER_x tokens
65 */
66 void
67 _mesa_add_renderbuffer(struct gl_framebuffer *fb,
68 gl_buffer_index bufferName, struct gl_renderbuffer *rb)
69 {
70 assert(fb);
71 assert(rb);
72 assert(bufferName < BUFFER_COUNT);
73
74 /* There should be no previous renderbuffer on this attachment point,
75 * with the exception of depth/stencil since the same renderbuffer may
76 * be used for both.
77 */
78 assert(bufferName == BUFFER_DEPTH ||
79 bufferName == BUFFER_STENCIL ||
80 fb->Attachment[bufferName].Renderbuffer == NULL);
81
82 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, rb);
83 }
84
85
86 /**
87 * Remove the named renderbuffer from the given framebuffer.
88 * \param bufferName one of the BUFFER_x tokens
89 */
90 void
91 _mesa_remove_renderbuffer(struct gl_framebuffer *fb,
92 gl_buffer_index bufferName)
93 {
94 assert(bufferName < BUFFER_COUNT);
95 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer,
96 NULL);
97 }
98
99
100 /**
101 * Set *ptr to point to rb. If *ptr points to another renderbuffer,
102 * dereference that buffer first. The new renderbuffer's refcount will
103 * be incremented. The old renderbuffer's refcount will be decremented.
104 * This is normally only called from the _mesa_reference_renderbuffer() macro
105 * when there's a real pointer change.
106 */
107 void
108 _mesa_reference_renderbuffer_(struct gl_renderbuffer **ptr,
109 struct gl_renderbuffer *rb)
110 {
111 if (*ptr) {
112 /* Unreference the old renderbuffer */
113 GLboolean deleteFlag = GL_FALSE;
114 struct gl_renderbuffer *oldRb = *ptr;
115
116 _glthread_LOCK_MUTEX(oldRb->Mutex);
117 ASSERT(oldRb->RefCount > 0);
118 oldRb->RefCount--;
119 /*printf("RB DECR %p (%d) to %d\n", (void*) oldRb, oldRb->Name, oldRb->RefCount);*/
120 deleteFlag = (oldRb->RefCount == 0);
121 _glthread_UNLOCK_MUTEX(oldRb->Mutex);
122
123 if (deleteFlag) {
124 oldRb->Delete(oldRb);
125 }
126
127 *ptr = NULL;
128 }
129 assert(!*ptr);
130
131 if (rb) {
132 /* reference new renderbuffer */
133 _glthread_LOCK_MUTEX(rb->Mutex);
134 rb->RefCount++;
135 /*printf("RB INCR %p (%d) to %d\n", (void*) rb, rb->Name, rb->RefCount);*/
136 _glthread_UNLOCK_MUTEX(rb->Mutex);
137 *ptr = rb;
138 }
139 }