tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SharedSurface.cpp (3970B)


      1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "SharedSurface.h"
      7 
      8 #include "../2d/2D.h"
      9 #include "GLBlitHelper.h"
     10 #include "GLContext.h"
     11 #include "GLReadTexImageHelper.h"
     12 #include "GLScreenBuffer.h"
     13 #include "nsThreadUtils.h"
     14 #include "ScopedGLHelpers.h"
     15 #include "SharedSurfaceGL.h"
     16 #include "SharedSurfaceEGL.h"
     17 #include "mozilla/gfx/gfxVars.h"
     18 #include "mozilla/gfx/Logging.h"
     19 #include "mozilla/layers/CompositorTypes.h"
     20 #include "mozilla/layers/TextureClientSharedSurface.h"
     21 #include "mozilla/layers/TextureForwarder.h"
     22 #include "mozilla/StaticPrefs_webgl.h"
     23 #include "VRManagerChild.h"
     24 
     25 #ifdef XP_WIN
     26 #  include "SharedSurfaceANGLE.h"
     27 #  include "SharedSurfaceD3D11Interop.h"
     28 #endif
     29 
     30 #ifdef XP_MACOSX
     31 #  include "SharedSurfaceIO.h"
     32 #endif
     33 
     34 #ifdef MOZ_WIDGET_GTK
     35 #  include "gfxPlatformGtk.h"
     36 #  include "SharedSurfaceDMABUF.h"
     37 #  include "mozilla/widget/DMABufDevice.h"
     38 #endif
     39 
     40 #ifdef MOZ_WIDGET_ANDROID
     41 #  include "SharedSurfaceAndroidHardwareBuffer.h"
     42 #endif
     43 
     44 namespace mozilla {
     45 namespace gl {
     46 
     47 ////////////////////////////////////////////////////////////////////////
     48 // SharedSurface
     49 
     50 SharedSurface::SharedSurface(const SharedSurfaceDesc& desc,
     51                             UniquePtr<MozFramebuffer> fb)
     52    : mDesc(desc), mFb(std::move(fb)) {}
     53 
     54 SharedSurface::~SharedSurface() = default;
     55 
     56 void SharedSurface::LockProd() {
     57  MOZ_ASSERT(!mIsLocked);
     58 
     59  LockProdImpl();
     60 
     61  mDesc.gl->LockSurface(this);
     62  mIsLocked = true;
     63 }
     64 
     65 void SharedSurface::UnlockProd() {
     66  if (!mIsLocked) return;
     67 
     68  UnlockProdImpl();
     69 
     70  mDesc.gl->UnlockSurface(this);
     71  mIsLocked = false;
     72 }
     73 
     74 ////////////////////////////////////////////////////////////////////////
     75 // SurfaceFactory
     76 
     77 /* static */
     78 UniquePtr<SurfaceFactory> SurfaceFactory::Create(
     79    GLContext* const pGl, const layers::TextureType consumerType) {
     80  auto& gl = *pGl;
     81 
     82  switch (consumerType) {
     83    case layers::TextureType::D3D11:
     84 #ifdef XP_WIN
     85      if (gl.IsANGLE()) {
     86        return SurfaceFactory_ANGLEShareHandle::Create(gl);
     87      }
     88      if (StaticPrefs::webgl_dxgl_enabled()) {
     89        return SurfaceFactory_D3D11Interop::Create(gl);
     90      }
     91 #endif
     92      break;
     93 
     94    case layers::TextureType::MacIOSurface:
     95 #ifdef XP_MACOSX
     96      return MakeUnique<SurfaceFactory_IOSurface>(gl);
     97 #else
     98      break;
     99 #endif
    100 
    101    case layers::TextureType::DMABUF:
    102 #ifdef MOZ_WIDGET_GTK
    103      if (gl.GetContextType() == GLContextType::EGL &&
    104          widget::DMABufDevice::IsDMABufWebGLEnabled()) {
    105        return SurfaceFactory_DMABUF::Create(gl);
    106      }
    107 #endif
    108      break;
    109 
    110    case layers::TextureType::AndroidNativeWindow:
    111 #ifdef MOZ_WIDGET_ANDROID
    112      return MakeUnique<SurfaceFactory_SurfaceTexture>(gl);
    113 #else
    114      break;
    115 #endif
    116 
    117    case layers::TextureType::AndroidHardwareBuffer:
    118 #ifdef MOZ_WIDGET_ANDROID
    119      if (XRE_IsGPUProcess()) {
    120        // Enable SharedSurface of AndroidHardwareBuffer only in GPU process.
    121        return SurfaceFactory_AndroidHardwareBuffer::Create(gl);
    122      }
    123 #endif
    124      break;
    125 
    126    case layers::TextureType::EGLImage:
    127 #ifdef MOZ_WIDGET_ANDROID
    128      // EGLImages cannot be shared cross-process, so only create them if we are
    129      // in the process that will consume them.
    130      if ((XRE_IsParentProcess() && !gfx::gfxVars::GPUProcessEnabled()) ||
    131          XRE_IsGPUProcess()) {
    132        return SurfaceFactory_EGLImage::Create(gl);
    133      }
    134 #endif
    135      break;
    136 
    137    case layers::TextureType::Unknown:
    138    case layers::TextureType::Last:
    139      break;
    140  }
    141 
    142  // Silence a warning.
    143  (void)gl;
    144 
    145  return nullptr;
    146 }
    147 
    148 SurfaceFactory::SurfaceFactory(const PartialSharedSurfaceDesc& partialDesc)
    149    : mDesc(partialDesc), mMutex("SurfaceFactor::mMutex") {}
    150 
    151 SurfaceFactory::~SurfaceFactory() = default;
    152 
    153 }  // namespace gl
    154 }  // namespace mozilla