a2c63f6a12cc97c6f670ba73d46659128f4c2e80
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
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
) {
38 if (sActiveContext
->IsOwningThreadCurrent()) {
39 sActiveContext
->MakeCurrent();
41 sActiveContext
->fGenTextures(1, &texture
);
43 while (sTextures
->GetSize() == 0) {
44 NS_WARNING("Waiting for texture");
48 GLuint
* popped
= (GLuint
*) sTextures
->Pop();
50 NS_ERROR("Failed to pop texture pool item");
57 NS_ASSERTION(texture
, "Failed to retrieve texture from pool");
68 sActiveContext
->MakeCurrent();
71 while (sTextures
->GetSize()) {
72 item
= (GLuint
*)sTextures
->Pop();
73 sActiveContext
->fDeleteTextures(1, 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
) {
87 sActiveContext
= aContext
;
90 if (sTextures
->GetSize() == TEXTURE_POOL_SIZE
)
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()