gfx/layers/opengl/TexturePoolOGL.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/opengl/TexturePoolOGL.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "TexturePoolOGL.h"
     1.9 +#include <stdlib.h>                     // for malloc
    1.10 +#include "GLContext.h"                  // for GLContext
    1.11 +#include "mozilla/Monitor.h"            // for Monitor, MonitorAutoLock
    1.12 +#include "mozilla/mozalloc.h"           // for operator delete, etc
    1.13 +#include "nsDebug.h"                    // for NS_ASSERTION, NS_ERROR, etc
    1.14 +#include "nsDeque.h"                    // for nsDeque
    1.15 +
    1.16 +#define TEXTURE_POOL_SIZE 10
    1.17 +
    1.18 +namespace mozilla {
    1.19 +namespace gl {
    1.20 +
    1.21 +static GLContext* sActiveContext = nullptr;
    1.22 +
    1.23 +static Monitor* sMonitor = nullptr;
    1.24 +static nsDeque* sTextures = nullptr;
    1.25 +
    1.26 +GLuint TexturePoolOGL::AcquireTexture()
    1.27 +{
    1.28 +  NS_ASSERTION(sMonitor, "not initialized");
    1.29 +
    1.30 +  MonitorAutoLock lock(*sMonitor);
    1.31 +
    1.32 +  if (!sActiveContext) {
    1.33 +    // Wait for a context
    1.34 +    sMonitor->Wait();
    1.35 +
    1.36 +    if (!sActiveContext)
    1.37 +      return 0;
    1.38 +  }
    1.39 +
    1.40 +  GLuint texture = 0;
    1.41 +  if (sActiveContext->IsOwningThreadCurrent()) {
    1.42 +    sActiveContext->MakeCurrent();
    1.43 +
    1.44 +    sActiveContext->fGenTextures(1, &texture);
    1.45 +  } else {
    1.46 +    while (sTextures->GetSize() == 0) {
    1.47 +      NS_WARNING("Waiting for texture");
    1.48 +      sMonitor->Wait();
    1.49 +    }
    1.50 +
    1.51 +    GLuint* popped = (GLuint*) sTextures->Pop();
    1.52 +    if (!popped) {
    1.53 +      NS_ERROR("Failed to pop texture pool item");
    1.54 +      return 0;
    1.55 +    }
    1.56 +
    1.57 +    texture = *popped;
    1.58 +    delete popped;
    1.59 +
    1.60 +    NS_ASSERTION(texture, "Failed to retrieve texture from pool");
    1.61 +  }
    1.62 +
    1.63 +  return texture;
    1.64 +}
    1.65 +
    1.66 +static void Clear()
    1.67 +{
    1.68 +  if (!sActiveContext)
    1.69 +    return;
    1.70 +
    1.71 +  sActiveContext->MakeCurrent();
    1.72 +
    1.73 +  GLuint* item;
    1.74 +  while (sTextures->GetSize()) {
    1.75 +    item = (GLuint*)sTextures->Pop();
    1.76 +    sActiveContext->fDeleteTextures(1, item);
    1.77 +    delete item;
    1.78 +  }
    1.79 +}
    1.80 +
    1.81 +void TexturePoolOGL::Fill(GLContext* aContext)
    1.82 +{
    1.83 +  NS_ASSERTION(aContext, "NULL GLContext");
    1.84 +  NS_ASSERTION(sMonitor, "not initialized");
    1.85 +
    1.86 +  MonitorAutoLock lock(*sMonitor);
    1.87 +
    1.88 +  if (sActiveContext != aContext) {
    1.89 +    Clear();
    1.90 +    sActiveContext = aContext;
    1.91 +  }
    1.92 +
    1.93 +  if (sTextures->GetSize() == TEXTURE_POOL_SIZE)
    1.94 +    return;
    1.95 +
    1.96 +  sActiveContext->MakeCurrent();
    1.97 +
    1.98 +  GLuint* texture = nullptr;
    1.99 +  while (sTextures->GetSize() < TEXTURE_POOL_SIZE) {
   1.100 +    texture = (GLuint*)malloc(sizeof(GLuint));
   1.101 +    sActiveContext->fGenTextures(1, texture);
   1.102 +    sTextures->Push((void*) texture);
   1.103 +  }
   1.104 +
   1.105 +  sMonitor->NotifyAll();
   1.106 +}
   1.107 +
   1.108 +void TexturePoolOGL::Init()
   1.109 +{
   1.110 +  sMonitor = new Monitor("TexturePoolOGL.sMonitor");
   1.111 +  sTextures = new nsDeque();
   1.112 +}
   1.113 +
   1.114 +void TexturePoolOGL::Shutdown()
   1.115 +{
   1.116 +  delete sMonitor;
   1.117 +  delete sTextures;
   1.118 +}
   1.119 +
   1.120 +} // gl
   1.121 +} // mozilla

mercurial