1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "X11TextureSourceOGL.h"
8 #include "gfxXlibSurface.h"
9 #include "gfx2DGlue.h"
10 #include "GLContext.h"
11 
12 namespace mozilla::layers {
13 
14 using namespace mozilla::gfx;
15 
X11TextureSourceOGL(CompositorOGL * aCompositor,gfxXlibSurface * aSurface)16 X11TextureSourceOGL::X11TextureSourceOGL(CompositorOGL* aCompositor,
17                                          gfxXlibSurface* aSurface)
18     : mGL(aCompositor->gl()),
19       mSurface(aSurface),
20       mTexture(0),
21       mUpdated(false) {}
22 
~X11TextureSourceOGL()23 X11TextureSourceOGL::~X11TextureSourceOGL() { DeallocateDeviceData(); }
24 
DeallocateDeviceData()25 void X11TextureSourceOGL::DeallocateDeviceData() {
26   if (mTexture) {
27     if (gl() && gl()->MakeCurrent()) {
28       gl::sGLXLibrary.ReleaseTexImage(mSurface->XDisplay(),
29                                       mSurface->GetGLXPixmap());
30       gl()->fDeleteTextures(1, &mTexture);
31       mTexture = 0;
32     }
33   }
34 }
35 
BindTexture(GLenum aTextureUnit,gfx::SamplingFilter aSamplingFilter)36 void X11TextureSourceOGL::BindTexture(GLenum aTextureUnit,
37                                       gfx::SamplingFilter aSamplingFilter) {
38   gl()->fActiveTexture(aTextureUnit);
39 
40   if (!mTexture) {
41     gl()->fGenTextures(1, &mTexture);
42 
43     gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexture);
44 
45     gl::sGLXLibrary.BindTexImage(mSurface->XDisplay(),
46                                  mSurface->GetGLXPixmap());
47   } else {
48     gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexture);
49     if (mUpdated) {
50       gl::sGLXLibrary.UpdateTexImage(mSurface->XDisplay(),
51                                      mSurface->GetGLXPixmap());
52       mUpdated = false;
53     }
54   }
55 
56   ApplySamplingFilterToBoundTexture(gl(), aSamplingFilter, LOCAL_GL_TEXTURE_2D);
57 }
58 
GetSize() const59 IntSize X11TextureSourceOGL::GetSize() const { return mSurface->GetSize(); }
60 
GetFormat() const61 SurfaceFormat X11TextureSourceOGL::GetFormat() const {
62   gfxContentType type = mSurface->GetContentType();
63   return X11TextureSourceOGL::ContentTypeToSurfaceFormat(type);
64 }
65 
SetTextureSourceProvider(TextureSourceProvider * aProvider)66 void X11TextureSourceOGL::SetTextureSourceProvider(
67     TextureSourceProvider* aProvider) {
68   gl::GLContext* newGL = aProvider ? aProvider->GetGLContext() : nullptr;
69   if (mGL != newGL) {
70     DeallocateDeviceData();
71   }
72   mGL = newGL;
73 }
74 
ContentTypeToSurfaceFormat(gfxContentType aType)75 SurfaceFormat X11TextureSourceOGL::ContentTypeToSurfaceFormat(
76     gfxContentType aType) {
77   // X11 uses a switched format and the OGL compositor
78   // doesn't support ALPHA / A8.
79   switch (aType) {
80     case gfxContentType::COLOR:
81       return SurfaceFormat::R8G8B8X8;
82     case gfxContentType::COLOR_ALPHA:
83       return SurfaceFormat::R8G8B8A8;
84     default:
85       return SurfaceFormat::UNKNOWN;
86   }
87 }
88 
89 }  // namespace mozilla::layers
90