Bug 980647 - Part 1 - Move the compositor's texture recycling startegy behind a pool interface. r=Cwiiis

This commit is contained in:
Nicolas Silva
2014-03-12 17:28:47 +01:00
parent 56c9fe4198
commit b8b1cfaac0
3 changed files with 219 additions and 142 deletions

View File

@@ -137,72 +137,6 @@ DrawQuads(GLContext *aGLContext,
aGLContext->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, 0);
}
#ifdef MOZ_WIDGET_GONK
CompositorOGLGonkBackendSpecificData::CompositorOGLGonkBackendSpecificData(CompositorOGL* aCompositor)
: mCompositor(aCompositor)
{
}
CompositorOGLGonkBackendSpecificData::~CompositorOGLGonkBackendSpecificData()
{
// Delete all textures by calling EndFrame twice
gl()->MakeCurrent();
EndFrame();
EndFrame();
}
GLContext*
CompositorOGLGonkBackendSpecificData::gl() const
{
return mCompositor->gl();
}
GLuint
CompositorOGLGonkBackendSpecificData::GetTexture()
{
GLuint texture = 0;
if (!mUnusedTextures.IsEmpty()) {
// Try to reuse one from the unused pile first
texture = mUnusedTextures[0];
mUnusedTextures.RemoveElementAt(0);
} else if (gl()->MakeCurrent()) {
// There isn't one to reuse, create one.
gl()->fGenTextures(1, &texture);
}
if (texture) {
mCreatedTextures.AppendElement(texture);
}
return texture;
}
void
CompositorOGLGonkBackendSpecificData::EndFrame()
{
gl()->MakeCurrent();
// Some platforms have issues unlocking Gralloc buffers even when they're
// rebound.
if (gfxPrefs::OverzealousGrallocUnlocking()) {
mUnusedTextures.AppendElements(mCreatedTextures);
mCreatedTextures.Clear();
}
// Delete unused textures
for (size_t i = 0; i < mUnusedTextures.Length(); i++) {
GLuint texture = mUnusedTextures[i];
gl()->fDeleteTextures(1, &texture);
}
mUnusedTextures.Clear();
// Move all created textures into the unused pile
mUnusedTextures.AppendElements(mCreatedTextures);
mCreatedTextures.Clear();
}
#endif
CompositorOGL::CompositorOGL(nsIWidget *aWidget, int aSurfaceWidth,
int aSurfaceHeight, bool aUseExternalSurfaceSize)
: mWidget(aWidget)
@@ -246,38 +180,18 @@ CompositorOGL::CreateContext()
return context.forget();
}
GLuint
CompositorOGL::GetTemporaryTexture(GLenum aTextureUnit)
{
size_t index = aTextureUnit - LOCAL_GL_TEXTURE0;
// lazily grow the array of temporary textures
if (mTextures.Length() <= index) {
size_t prevLength = mTextures.Length();
mTextures.SetLength(index + 1);
for(unsigned int i = prevLength; i <= index; ++i) {
mTextures[i] = 0;
}
}
// lazily initialize the temporary textures
if (!mTextures[index]) {
if (!gl()->MakeCurrent()) {
return 0;
}
gl()->fGenTextures(1, &mTextures[index]);
}
return mTextures[index];
}
void
CompositorOGL::Destroy()
{
if (gl() && gl()->MakeCurrent()) {
if (mTextures.Length() > 0) {
gl()->fDeleteTextures(mTextures.Length(), &mTextures[0]);
}
mVBOs.Flush(gl());
}
mTextures.SetLength(0);
if (mTexturePool) {
mTexturePool->Clear();
mTexturePool = nullptr;
}
if (!mDestroyed) {
mDestroyed = true;
CleanupResources();
@@ -1366,11 +1280,9 @@ CompositorOGL::EndFrame()
mCurrentRenderTarget = nullptr;
#ifdef MOZ_WIDGET_GONK
if (mCompositorBackendSpecificData) {
static_cast<CompositorOGLGonkBackendSpecificData*>(mCompositorBackendSpecificData.get())->EndFrame();
if (mTexturePool) {
mTexturePool->EndFrame();
}
#endif
mGLContext->SwapBuffers();
mGLContext->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, 0);
@@ -1480,17 +1392,6 @@ CompositorOGL::Resume()
return true;
}
#ifdef MOZ_WIDGET_GONK
CompositorBackendSpecificData*
CompositorOGL::GetCompositorBackendSpecificData()
{
if (!mCompositorBackendSpecificData) {
mCompositorBackendSpecificData = new CompositorOGLGonkBackendSpecificData(this);
}
return mCompositorBackendSpecificData;
}
#endif
TemporaryRef<DataTextureSource>
CompositorOGL::CreateDataTextureSource(TextureFlags aFlags)
{
@@ -1593,5 +1494,120 @@ CompositorOGL::BindAndDrawQuad(ShaderProgramOGL *aProg,
aFlipped, aDrawMode);
}
GLuint
CompositorOGL::GetTemporaryTexture(GLenum aUnit)
{
if (!mTexturePool) {
#ifdef MOZ_WIDGET_GONK
mTexturePool = new PerFrameTexturePoolOGL(gl());
#else
mTexturePool = new PerUnitTexturePoolOGL(gl());
#endif
}
return mTexturePool->GetTexture(aUnit);
}
GLuint
PerUnitTexturePoolOGL::GetTexture(GLenum aTextureUnit)
{
size_t index = aTextureUnit - LOCAL_GL_TEXTURE0;
// lazily grow the array of temporary textures
if (mTextures.Length() <= index) {
size_t prevLength = mTextures.Length();
mTextures.SetLength(index + 1);
for(unsigned int i = prevLength; i <= index; ++i) {
mTextures[i] = 0;
}
}
// lazily initialize the temporary textures
if (!mTextures[index]) {
if (!mGL->MakeCurrent()) {
return 0;
}
mGL->fGenTextures(1, &mTextures[index]);
}
return mTextures[index];
}
void
PerUnitTexturePoolOGL::DestroyTextures()
{
if (mGL && mGL->MakeCurrent()) {
if (mTextures.Length() > 0) {
mGL->fDeleteTextures(mTextures.Length(), &mTextures[0]);
}
}
mTextures.SetLength(0);
}
void
PerFrameTexturePoolOGL::DestroyTextures()
{
if (!mGL->MakeCurrent()) {
return;
}
if (mUnusedTextures.Length() > 0) {
mGL->fDeleteTextures(mUnusedTextures.Length(), &mUnusedTextures[0]);
mUnusedTextures.Clear();
}
if (mCreatedTextures.Length() > 0) {
mGL->fDeleteTextures(mCreatedTextures.Length(), &mCreatedTextures[0]);
mCreatedTextures.Clear();
}
}
GLuint
PerFrameTexturePoolOGL::GetTexture(GLenum)
{
GLuint texture = 0;
if (!mUnusedTextures.IsEmpty()) {
// Try to reuse one from the unused pile first
texture = mUnusedTextures[0];
mUnusedTextures.RemoveElementAt(0);
} else if (mGL->MakeCurrent()) {
// There isn't one to reuse, create one.
mGL->fGenTextures(1, &texture);
}
if (texture) {
mCreatedTextures.AppendElement(texture);
}
return texture;
}
void
PerFrameTexturePoolOGL::EndFrame()
{
if (!mGL->MakeCurrent()) {
// this means the context got destroyed underneith us somehow, and the driver
// already has destroyed the textures.
mCreatedTextures.Clear();
mUnusedTextures.Clear();
return;
}
// Some platforms have issues unlocking Gralloc buffers even when they're
// rebound.
if (gfxPrefs::OverzealousGrallocUnlocking()) {
mUnusedTextures.AppendElements(mCreatedTextures);
mCreatedTextures.Clear();
}
// Delete unused textures
for (size_t i = 0; i < mUnusedTextures.Length(); i++) {
GLuint texture = mUnusedTextures[i];
mGL->fDeleteTextures(1, &texture);
}
mUnusedTextures.Clear();
// Move all created textures into the unused pile
mUnusedTextures.AppendElements(mCreatedTextures);
mCreatedTextures.Clear();
}
} /* layers */
} /* mozilla */