michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifdef GL_PROVIDER_GLX michael@0: michael@0: #include "X11TextureSourceOGL.h" michael@0: #include "mozilla/layers/CompositorOGL.h" michael@0: #include "gfxXlibSurface.h" michael@0: #include "gfx2DGlue.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::layers; michael@0: using namespace mozilla::gfx; michael@0: michael@0: X11TextureSourceOGL::X11TextureSourceOGL(CompositorOGL* aCompositor, gfxXlibSurface* aSurface) michael@0: : mCompositor(aCompositor) michael@0: , mSurface(aSurface) michael@0: , mTexture(0) michael@0: { michael@0: } michael@0: michael@0: X11TextureSourceOGL::~X11TextureSourceOGL() michael@0: { michael@0: DeallocateDeviceData(); michael@0: } michael@0: michael@0: void michael@0: X11TextureSourceOGL::DeallocateDeviceData() michael@0: { michael@0: if (mTexture) { michael@0: if (gl() && gl()->MakeCurrent()) { michael@0: gl::sGLXLibrary.ReleaseTexImage(mSurface->XDisplay(), mSurface->GetGLXPixmap()); michael@0: gl()->fDeleteTextures(1, &mTexture); michael@0: mTexture = 0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: X11TextureSourceOGL::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter) michael@0: { michael@0: gl()->fActiveTexture(aTextureUnit); michael@0: michael@0: if (!mTexture) { michael@0: gl()->fGenTextures(1, &mTexture); michael@0: michael@0: gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexture); michael@0: michael@0: gl::sGLXLibrary.BindTexImage(mSurface->XDisplay(), mSurface->GetGLXPixmap()); michael@0: } else { michael@0: gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexture); michael@0: gl::sGLXLibrary.UpdateTexImage(mSurface->XDisplay(), mSurface->GetGLXPixmap()); michael@0: } michael@0: michael@0: ApplyFilterToBoundTexture(gl(), aFilter, LOCAL_GL_TEXTURE_2D); michael@0: } michael@0: michael@0: IntSize michael@0: X11TextureSourceOGL::GetSize() const michael@0: { michael@0: return ToIntSize(mSurface->GetSize()); michael@0: } michael@0: michael@0: SurfaceFormat michael@0: X11TextureSourceOGL::GetFormat() const { michael@0: gfxContentType type = mSurface->GetContentType(); michael@0: return X11TextureSourceOGL::ContentTypeToSurfaceFormat(type); michael@0: } michael@0: michael@0: void michael@0: X11TextureSourceOGL::SetCompositor(Compositor* aCompositor) michael@0: { michael@0: MOZ_ASSERT(!aCompositor || aCompositor->GetBackendType() == LayersBackend::LAYERS_OPENGL); michael@0: if (mCompositor == aCompositor) { michael@0: return; michael@0: } michael@0: DeallocateDeviceData(); michael@0: mCompositor = static_cast(aCompositor); michael@0: } michael@0: michael@0: gl::GLContext* michael@0: X11TextureSourceOGL::gl() const michael@0: { michael@0: return mCompositor ? mCompositor->gl() : nullptr; michael@0: } michael@0: michael@0: SurfaceFormat michael@0: X11TextureSourceOGL::ContentTypeToSurfaceFormat(gfxContentType aType) michael@0: { michael@0: // X11 uses a switched format and the OGL compositor michael@0: // doesn't support ALPHA / A8. michael@0: switch (aType) { michael@0: case gfxContentType::COLOR: michael@0: return SurfaceFormat::R8G8B8X8; michael@0: case gfxContentType::COLOR_ALPHA: michael@0: return SurfaceFormat::R8G8B8A8; michael@0: default: michael@0: return SurfaceFormat::UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: #endif