Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "TexturePoolOGL.h" |
michael@0 | 6 | #include <stdlib.h> // for malloc |
michael@0 | 7 | #include "GLContext.h" // for GLContext |
michael@0 | 8 | #include "mozilla/Monitor.h" // for Monitor, MonitorAutoLock |
michael@0 | 9 | #include "mozilla/mozalloc.h" // for operator delete, etc |
michael@0 | 10 | #include "nsDebug.h" // for NS_ASSERTION, NS_ERROR, etc |
michael@0 | 11 | #include "nsDeque.h" // for nsDeque |
michael@0 | 12 | |
michael@0 | 13 | #define TEXTURE_POOL_SIZE 10 |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | namespace gl { |
michael@0 | 17 | |
michael@0 | 18 | static GLContext* sActiveContext = nullptr; |
michael@0 | 19 | |
michael@0 | 20 | static Monitor* sMonitor = nullptr; |
michael@0 | 21 | static nsDeque* sTextures = nullptr; |
michael@0 | 22 | |
michael@0 | 23 | GLuint TexturePoolOGL::AcquireTexture() |
michael@0 | 24 | { |
michael@0 | 25 | NS_ASSERTION(sMonitor, "not initialized"); |
michael@0 | 26 | |
michael@0 | 27 | MonitorAutoLock lock(*sMonitor); |
michael@0 | 28 | |
michael@0 | 29 | if (!sActiveContext) { |
michael@0 | 30 | // Wait for a context |
michael@0 | 31 | sMonitor->Wait(); |
michael@0 | 32 | |
michael@0 | 33 | if (!sActiveContext) |
michael@0 | 34 | return 0; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | GLuint texture = 0; |
michael@0 | 38 | if (sActiveContext->IsOwningThreadCurrent()) { |
michael@0 | 39 | sActiveContext->MakeCurrent(); |
michael@0 | 40 | |
michael@0 | 41 | sActiveContext->fGenTextures(1, &texture); |
michael@0 | 42 | } else { |
michael@0 | 43 | while (sTextures->GetSize() == 0) { |
michael@0 | 44 | NS_WARNING("Waiting for texture"); |
michael@0 | 45 | sMonitor->Wait(); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | GLuint* popped = (GLuint*) sTextures->Pop(); |
michael@0 | 49 | if (!popped) { |
michael@0 | 50 | NS_ERROR("Failed to pop texture pool item"); |
michael@0 | 51 | return 0; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | texture = *popped; |
michael@0 | 55 | delete popped; |
michael@0 | 56 | |
michael@0 | 57 | NS_ASSERTION(texture, "Failed to retrieve texture from pool"); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | return texture; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | static void Clear() |
michael@0 | 64 | { |
michael@0 | 65 | if (!sActiveContext) |
michael@0 | 66 | return; |
michael@0 | 67 | |
michael@0 | 68 | sActiveContext->MakeCurrent(); |
michael@0 | 69 | |
michael@0 | 70 | GLuint* item; |
michael@0 | 71 | while (sTextures->GetSize()) { |
michael@0 | 72 | item = (GLuint*)sTextures->Pop(); |
michael@0 | 73 | sActiveContext->fDeleteTextures(1, item); |
michael@0 | 74 | delete item; |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | void TexturePoolOGL::Fill(GLContext* aContext) |
michael@0 | 79 | { |
michael@0 | 80 | NS_ASSERTION(aContext, "NULL GLContext"); |
michael@0 | 81 | NS_ASSERTION(sMonitor, "not initialized"); |
michael@0 | 82 | |
michael@0 | 83 | MonitorAutoLock lock(*sMonitor); |
michael@0 | 84 | |
michael@0 | 85 | if (sActiveContext != aContext) { |
michael@0 | 86 | Clear(); |
michael@0 | 87 | sActiveContext = aContext; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | if (sTextures->GetSize() == TEXTURE_POOL_SIZE) |
michael@0 | 91 | return; |
michael@0 | 92 | |
michael@0 | 93 | sActiveContext->MakeCurrent(); |
michael@0 | 94 | |
michael@0 | 95 | GLuint* texture = nullptr; |
michael@0 | 96 | while (sTextures->GetSize() < TEXTURE_POOL_SIZE) { |
michael@0 | 97 | texture = (GLuint*)malloc(sizeof(GLuint)); |
michael@0 | 98 | sActiveContext->fGenTextures(1, texture); |
michael@0 | 99 | sTextures->Push((void*) texture); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | sMonitor->NotifyAll(); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void TexturePoolOGL::Init() |
michael@0 | 106 | { |
michael@0 | 107 | sMonitor = new Monitor("TexturePoolOGL.sMonitor"); |
michael@0 | 108 | sTextures = new nsDeque(); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | void TexturePoolOGL::Shutdown() |
michael@0 | 112 | { |
michael@0 | 113 | delete sMonitor; |
michael@0 | 114 | delete sTextures; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | } // gl |
michael@0 | 118 | } // mozilla |