gfx/layers/opengl/TexturePoolOGL.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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()
    24 {
    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;
    35   }
    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();
    46     }
    48     GLuint* popped = (GLuint*) sTextures->Pop();
    49     if (!popped) {
    50       NS_ERROR("Failed to pop texture pool item");
    51       return 0;
    52     }
    54     texture = *popped;
    55     delete popped;
    57     NS_ASSERTION(texture, "Failed to retrieve texture from pool");
    58   }
    60   return texture;
    61 }
    63 static void Clear()
    64 {
    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;
    75   }
    76 }
    78 void TexturePoolOGL::Fill(GLContext* aContext)
    79 {
    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;
    88   }
    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);
   100   }
   102   sMonitor->NotifyAll();
   103 }
   105 void TexturePoolOGL::Init()
   106 {
   107   sMonitor = new Monitor("TexturePoolOGL.sMonitor");
   108   sTextures = new nsDeque();
   109 }
   111 void TexturePoolOGL::Shutdown()
   112 {
   113   delete sMonitor;
   114   delete sTextures;
   115 }
   117 } // gl
   118 } // mozilla

mercurial