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: #include "MacIOSurfaceTextureHostOGL.h" michael@0: #include "mozilla/gfx/MacIOSurface.h" michael@0: #include "mozilla/layers/CompositorOGL.h" michael@0: #include "GLContextCGL.h" michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: MacIOSurfaceTextureHostOGL::MacIOSurfaceTextureHostOGL(TextureFlags aFlags, michael@0: const SurfaceDescriptorMacIOSurface& aDescriptor) michael@0: : TextureHost(aFlags) michael@0: { michael@0: mSurface = MacIOSurface::LookupSurface(aDescriptor.surface(), michael@0: aDescriptor.scaleFactor(), michael@0: aDescriptor.hasAlpha()); michael@0: } michael@0: michael@0: bool michael@0: MacIOSurfaceTextureHostOGL::Lock() michael@0: { michael@0: if (!mCompositor || !mSurface) { michael@0: return false; michael@0: } michael@0: michael@0: if (!mTextureSource) { michael@0: mTextureSource = new MacIOSurfaceTextureSourceOGL(mCompositor, mSurface); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: MacIOSurfaceTextureHostOGL::SetCompositor(Compositor* aCompositor) michael@0: { michael@0: CompositorOGL* glCompositor = static_cast(aCompositor); michael@0: mCompositor = glCompositor; michael@0: if (mTextureSource) { michael@0: mTextureSource->SetCompositor(glCompositor); michael@0: } michael@0: } michael@0: michael@0: gfx::SurfaceFormat michael@0: MacIOSurfaceTextureHostOGL::GetFormat() const { michael@0: if (!mSurface) { michael@0: return gfx::SurfaceFormat::UNKNOWN; michael@0: } michael@0: return mSurface->HasAlpha() ? gfx::SurfaceFormat::R8G8B8A8 : gfx::SurfaceFormat::B8G8R8X8; michael@0: } michael@0: michael@0: gfx::IntSize michael@0: MacIOSurfaceTextureHostOGL::GetSize() const { michael@0: if (!mSurface) { michael@0: return gfx::IntSize(); michael@0: } michael@0: return gfx::IntSize(mSurface->GetDevicePixelWidth(), michael@0: mSurface->GetDevicePixelHeight()); michael@0: } michael@0: michael@0: MacIOSurfaceTextureSourceOGL::MacIOSurfaceTextureSourceOGL( michael@0: CompositorOGL* aCompositor, michael@0: MacIOSurface* aSurface) michael@0: : mCompositor(aCompositor) michael@0: , mSurface(aSurface) michael@0: {} michael@0: michael@0: MacIOSurfaceTextureSourceOGL::~MacIOSurfaceTextureSourceOGL() michael@0: {} michael@0: michael@0: gfx::IntSize michael@0: MacIOSurfaceTextureSourceOGL::GetSize() const michael@0: { michael@0: return gfx::IntSize(mSurface->GetDevicePixelWidth(), michael@0: mSurface->GetDevicePixelHeight()); michael@0: } michael@0: michael@0: gfx::SurfaceFormat michael@0: MacIOSurfaceTextureSourceOGL::GetFormat() const michael@0: { michael@0: return mSurface->HasAlpha() ? gfx::SurfaceFormat::R8G8B8A8 : gfx::SurfaceFormat::B8G8R8X8; michael@0: } michael@0: michael@0: void michael@0: MacIOSurfaceTextureSourceOGL::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter) michael@0: { michael@0: if (!gl()) { michael@0: NS_WARNING("Trying to bind a texture without a GLContext"); michael@0: return; michael@0: } michael@0: GLuint tex = mCompositor->GetTemporaryTexture(GetTextureTarget(), aTextureUnit); michael@0: michael@0: gl()->fActiveTexture(aTextureUnit); michael@0: gl()->fBindTexture(LOCAL_GL_TEXTURE_RECTANGLE_ARB, tex); michael@0: mSurface->CGLTexImageIOSurface2D(gl::GLContextCGL::Cast(gl())->GetCGLContext()); michael@0: ApplyFilterToBoundTexture(gl(), aFilter, LOCAL_GL_TEXTURE_RECTANGLE_ARB); michael@0: } michael@0: michael@0: void michael@0: MacIOSurfaceTextureSourceOGL::SetCompositor(Compositor* aCompositor) michael@0: { michael@0: mCompositor = static_cast(aCompositor); michael@0: } michael@0: michael@0: gl::GLContext* michael@0: MacIOSurfaceTextureSourceOGL::gl() const michael@0: { michael@0: return mCompositor ? mCompositor->gl() : nullptr; michael@0: } michael@0: michael@0: } michael@0: }