+
+/**
+ * Check if the given texture object is bound to the current draw or
+ * read framebuffer. If so, Unbind it.
+ */
+static void
+unbind_texobj_from_fbo(GLcontext *ctx, struct gl_texture_object *texObj)
+{
+ const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
+ GLuint i;
+
+ for (i = 0; i < n; i++) {
+ struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
+ if (fb->Name) {
+ GLuint j;
+ for (j = 0; j < BUFFER_COUNT; j++) {
+ if (fb->Attachment[j].Type == GL_TEXTURE &&
+ fb->Attachment[j].Texture == texObj) {
+ _mesa_remove_attachment(ctx, fb->Attachment + j);
+ }
+ }
+ }
+ }
+}
+
+
+/**
+ * Check if the given texture object is bound to any texture image units and
+ * unbind it if so.
+ * XXX all RefCount accesses should be protected by a mutex.
+ */
+static void
+unbind_texobj_from_texunits(GLcontext *ctx, struct gl_texture_object *texObj)
+{
+ GLuint u;
+
+ for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
+ struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
+ struct gl_texture_object **curr = NULL;
+
+ if (texObj == unit->Current1D) {
+ curr = &unit->Current1D;
+ unit->Current1D = ctx->Shared->Default1D;
+ }
+ else if (texObj == unit->Current2D) {
+ curr = &unit->Current2D;
+ unit->Current2D = ctx->Shared->Default2D;
+ }
+ else if (texObj == unit->Current3D) {
+ curr = &unit->Current3D;
+ unit->Current3D = ctx->Shared->Default3D;
+ }
+ else if (texObj == unit->CurrentCubeMap) {
+ curr = &unit->CurrentCubeMap;
+ unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
+ }
+ else if (texObj == unit->CurrentRect) {
+ curr = &unit->CurrentRect;
+ unit->CurrentRect = ctx->Shared->DefaultRect;
+ }
+ else if (texObj == unit->Current1DArray) {
+ curr = &unit->Current1DArray;
+ unit->CurrentRect = ctx->Shared->Default1DArray;
+ }
+ else if (texObj == unit->Current2DArray) {
+ curr = &unit->Current1DArray;
+ unit->CurrentRect = ctx->Shared->Default2DArray;
+ }
+
+ if (curr) {
+ (*curr)->RefCount++;
+ texObj->RefCount--;
+ if (texObj == unit->_Current)
+ unit->_Current = *curr;
+ }
+ }
+}
+
+