a2c63f6a12cc97c6f670ba73d46659128f4c2e80
[gecko.git] / TexturePoolOGL.cpp
bloba2c63f6a12cc97c6f670ba73d46659128f4c2e80
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "TexturePoolOGL.h"
6 #include <stdlib.h> // for malloc
7 #include "GLContext.h" // for GLContext
8 #include "mozilla/Monitor.h" // for Monitor, MonitorAutoLock
9 #include "mozilla/mozalloc.h" // for operator delete, etc
10 #include "nsDebug.h" // for NS_ASSERTION, NS_ERROR, etc
11 #include "nsDeque.h" // for nsDeque
13 #define TEXTURE_POOL_SIZE 10
15 namespace mozilla {
16 namespace gl {
18 static GLContext* sActiveContext = nullptr;
20 static Monitor* sMonitor = nullptr;
21 static nsDeque* sTextures = nullptr;
23 GLuint TexturePoolOGL::AcquireTexture()
25 NS_ASSERTION(sMonitor, "not initialized");
27 MonitorAutoLock lock(*sMonitor);
29 if (!sActiveContext) {
30 // Wait for a context
31 sMonitor->Wait();
33 if (!sActiveContext)
34 return 0;
37 GLuint texture = 0;
38 if (sActiveContext->IsOwningThreadCurrent()) {
39 sActiveContext->MakeCurrent();
41 sActiveContext->fGenTextures(1, &texture);
42 } else {
43 while (sTextures->GetSize() == 0) {
44 NS_WARNING("Waiting for texture");
45 sMonitor->Wait();
48 GLuint* popped = (GLuint*) sTextures->Pop();
49 if (!popped) {
50 NS_ERROR("Failed to pop texture pool item");
51 return 0;
54 texture = *popped;
55 delete popped;
57 NS_ASSERTION(texture, "Failed to retrieve texture from pool");
60 return texture;
63 static void Clear()
65 if (!sActiveContext)
66 return;
68 sActiveContext->MakeCurrent();
70 GLuint* item;
71 while (sTextures->GetSize()) {
72 item = (GLuint*)sTextures->Pop();
73 sActiveContext->fDeleteTextures(1, item);
74 delete item;
78 void TexturePoolOGL::Fill(GLContext* aContext)
80 NS_ASSERTION(aContext, "NULL GLContext");
81 NS_ASSERTION(sMonitor, "not initialized");
83 MonitorAutoLock lock(*sMonitor);
85 if (sActiveContext != aContext) {
86 Clear();
87 sActiveContext = aContext;
90 if (sTextures->GetSize() == TEXTURE_POOL_SIZE)
91 return;
93 sActiveContext->MakeCurrent();
95 GLuint* texture = nullptr;
96 while (sTextures->GetSize() < TEXTURE_POOL_SIZE) {
97 texture = (GLuint*)malloc(sizeof(GLuint));
98 sActiveContext->fGenTextures(1, texture);
99 sTextures->Push((void*) texture);
102 sMonitor->NotifyAll();
105 void TexturePoolOGL::Init()
107 sMonitor = new Monitor("TexturePoolOGL.sMonitor");
108 sTextures = new nsDeque();
111 void TexturePoolOGL::Shutdown()
113 delete sMonitor;
114 delete sTextures;
117 } // gl
118 } // mozilla