renderbuffer.c revision af69d88d
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 mtx_init(&rb->Mutex, mtx_plain); 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->Depth = 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 */ 65struct 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 */ 83void 84_mesa_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb) 85{ 86 mtx_destroy(&rb->Mutex); 87 free(rb->Label); 88 free(rb); 89} 90 91 92/** 93 * Attach a renderbuffer to a framebuffer. 94 * \param bufferName one of the BUFFER_x tokens 95 */ 96void 97_mesa_add_renderbuffer(struct gl_framebuffer *fb, 98 gl_buffer_index bufferName, struct gl_renderbuffer *rb) 99{ 100 assert(fb); 101 assert(rb); 102 assert(bufferName < BUFFER_COUNT); 103 104 /* There should be no previous renderbuffer on this attachment point, 105 * with the exception of depth/stencil since the same renderbuffer may 106 * be used for both. 107 */ 108 assert(bufferName == BUFFER_DEPTH || 109 bufferName == BUFFER_STENCIL || 110 fb->Attachment[bufferName].Renderbuffer == NULL); 111 112 /* winsys vs. user-created buffer cross check */ 113 if (_mesa_is_user_fbo(fb)) { 114 assert(rb->Name); 115 } 116 else { 117 assert(!rb->Name); 118 } 119 120 fb->Attachment[bufferName].Type = GL_RENDERBUFFER_EXT; 121 fb->Attachment[bufferName].Complete = GL_TRUE; 122 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, rb); 123} 124 125 126/** 127 * Remove the named renderbuffer from the given framebuffer. 128 * \param bufferName one of the BUFFER_x tokens 129 */ 130void 131_mesa_remove_renderbuffer(struct gl_framebuffer *fb, 132 gl_buffer_index bufferName) 133{ 134 assert(bufferName < BUFFER_COUNT); 135 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, 136 NULL); 137} 138 139 140/** 141 * Set *ptr to point to rb. If *ptr points to another renderbuffer, 142 * dereference that buffer first. The new renderbuffer's refcount will 143 * be incremented. The old renderbuffer's refcount will be decremented. 144 * This is normally only called from the _mesa_reference_renderbuffer() macro 145 * when there's a real pointer change. 146 */ 147void 148_mesa_reference_renderbuffer_(struct gl_renderbuffer **ptr, 149 struct gl_renderbuffer *rb) 150{ 151 if (*ptr) { 152 /* Unreference the old renderbuffer */ 153 GLboolean deleteFlag = GL_FALSE; 154 struct gl_renderbuffer *oldRb = *ptr; 155 156 mtx_lock(&oldRb->Mutex); 157 ASSERT(oldRb->RefCount > 0); 158 oldRb->RefCount--; 159 /*printf("RB DECR %p (%d) to %d\n", (void*) oldRb, oldRb->Name, oldRb->RefCount);*/ 160 deleteFlag = (oldRb->RefCount == 0); 161 mtx_unlock(&oldRb->Mutex); 162 163 if (deleteFlag) { 164 GET_CURRENT_CONTEXT(ctx); 165 oldRb->Delete(ctx, oldRb); 166 } 167 168 *ptr = NULL; 169 } 170 assert(!*ptr); 171 172 if (rb) { 173 /* reference new renderbuffer */ 174 mtx_lock(&rb->Mutex); 175 rb->RefCount++; 176 /*printf("RB INCR %p (%d) to %d\n", (void*) rb, rb->Name, rb->RefCount);*/ 177 mtx_unlock(&rb->Mutex); 178 *ptr = rb; 179 } 180} 181