DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (290d570566d6)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef MOZ_WIDGET_ANDROID

#  include "AndroidSurfaceTexture.h"

#  include "GeneratedJNINatives.h"

#  include "AndroidNativeWindow.h"
#  include "GLContextEGL.h"
#  include "GLBlitHelper.h"
#  include "GLImages.h"

using namespace mozilla;

namespace mozilla {
namespace gl {

void AndroidSurfaceTexture::GetTransformMatrix(
    java::sdk::SurfaceTexture::Param surfaceTexture,
    gfx::Matrix4x4* outMatrix) {
  JNIEnv* const env = jni::GetEnvForThread();

  auto jarray = jni::FloatArray::LocalRef::Adopt(env, env->NewFloatArray(16));
  surfaceTexture->GetTransformMatrix(jarray);

  jfloat* array = env->GetFloatArrayElements(jarray.Get(), nullptr);

  memcpy(&(outMatrix->_11), array, sizeof(float) * 16);

  env->ReleaseFloatArrayElements(jarray.Get(), array, 0);
}

class SharedGL final {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedGL);

  explicit SharedGL(AndroidNativeWindow& window) {
    MutexAutoLock lock(sMutex);

    if (!sContext) {
      MOZ_ASSERT(sInstanceCount == 0);
      sContext = CreateContext();
      if (!sContext) {
        return;
      }
    }

    InitSurface(window);
    ++sInstanceCount;
  }

  void Blit(const AndroidSurfaceTextureHandle& sourceTextureHandle,
            const gfx::IntSize& imageSize) {
    MutexAutoLock lock(sMutex);
    MOZ_ASSERT(sContext);

    // Setting overide also makes conext and surface current.
    sContext->SetEGLSurfaceOverride(mTargetSurface);
    RefPtr<layers::SurfaceTextureImage> img = new layers::SurfaceTextureImage(
        sourceTextureHandle, imageSize, false, OriginPos::TopLeft);
    sContext->BlitHelper()->BlitImage(img, imageSize, OriginPos::BottomLeft);
    sContext->SwapBuffers();
    // This method is called through binder IPC and could run on any thread in
    // the pool. Release the context and surface from this thread after use so
    // they can be bound to another thread later.
    UnmakeCurrent(sContext);
  }

 private:
  ~SharedGL() {
    MutexAutoLock lock(sMutex);

    if (mTargetSurface != EGL_NO_SURFACE) {
      const auto& egl = *(sContext->mEgl);
      egl.fDestroySurface(egl.Display(), mTargetSurface);
    }

    // Destroy shared GL context when no one uses it.
    if (--sInstanceCount == 0) {
      sContext = nullptr;
    }
  }

  static already_AddRefed<GLContextEGL> CreateContext() {
    sMutex.AssertCurrentThreadOwns();
    MOZ_ASSERT(!sContext);

    auto* egl = gl::GLLibraryEGL::Get();
    EGLDisplay eglDisplay = egl->fGetDisplay(EGL_DEFAULT_DISPLAY);
    MOZ_ASSERT(eglDisplay == egl->Display());
    EGLConfig eglConfig;
    CreateConfig(egl, &eglConfig, /* bpp */ 24, /* depth buffer? */ false);
    EGLint attributes[] = {LOCAL_EGL_CONTEXT_CLIENT_VERSION, 2, LOCAL_EGL_NONE};
    EGLContext eglContext =
        egl->fCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, attributes);
    RefPtr<GLContextEGL> gl = new GLContextEGL(
        egl, CreateContextFlags::NONE, SurfaceCaps::Any(),
        /* offscreen? */ false, eglConfig, EGL_NO_SURFACE, eglContext);
    if (!gl->Init()) {
      NS_WARNING("Fail to create GL context for native blitter.");
      return nullptr;
    }

    // Yield the current state made in constructor.
    UnmakeCurrent(gl);
    return gl.forget();
  }

  void InitSurface(AndroidNativeWindow& window) {
    sMutex.AssertCurrentThreadOwns();
    MOZ_ASSERT(sContext);

    const auto& egl = *(sContext->mEgl);
    mTargetSurface = egl.fCreateWindowSurface(egl.Display(), sContext->mConfig,
                                              window.NativeWindow(), 0);
  }

  static bool UnmakeCurrent(RefPtr<GLContextEGL>& gl) {
    sMutex.AssertCurrentThreadOwns();
    MOZ_ASSERT(gl);

    if (!gl->IsCurrent()) {
      return true;
    }
    const auto& egl = *(gl->mEgl);
    return egl.fMakeCurrent(egl.Display(), EGL_NO_SURFACE, EGL_NO_SURFACE,
                            EGL_NO_CONTEXT);
  }

  static Mutex sMutex;
  static RefPtr<GLContextEGL> sContext;
  static size_t sInstanceCount;

  EGLSurface mTargetSurface;
};

Mutex SharedGL::sMutex("SharedGLContext::sMutex");
RefPtr<GLContextEGL> SharedGL::sContext(nullptr);
size_t SharedGL::sInstanceCount = 0;

class GLBlitterSupport final
    : public java::GeckoSurfaceTexture::NativeGLBlitHelper::Natives<
          GLBlitterSupport> {
 public:
  using Base =
      java::GeckoSurfaceTexture::NativeGLBlitHelper::Natives<GLBlitterSupport>;
  using Base::AttachNative;
  using Base::DisposeNative;
  using Base::GetNative;

  static java::GeckoSurfaceTexture::NativeGLBlitHelper::LocalRef Create(
      jint sourceTextureHandle, jni::Object::Param targetSurface, jint width,
      jint height) {
    AndroidNativeWindow win(java::GeckoSurface::Ref::From(targetSurface));
    auto helper = java::GeckoSurfaceTexture::NativeGLBlitHelper::New();
    RefPtr<SharedGL> gl = new SharedGL(win);
    GLBlitterSupport::AttachNative(
        helper, MakeUnique<GLBlitterSupport>(std::move(gl), sourceTextureHandle,
                                             width, height));
    return helper;
  }

  GLBlitterSupport(RefPtr<SharedGL>&& gl, jint sourceTextureHandle, jint width,
                   jint height)
      : mGl(gl),
        mSourceTextureHandle(sourceTextureHandle),
        mSize(width, height) {}

  void Blit() { mGl->Blit(mSourceTextureHandle, mSize); }

 private:
  const RefPtr<SharedGL> mGl;
  const AndroidSurfaceTextureHandle mSourceTextureHandle;
  const gfx::IntSize mSize;
};

void AndroidSurfaceTexture::Init() { GLBlitterSupport::Init(); }

}  // namespace gl
}  // namespace mozilla
#endif  // MOZ_WIDGET_ANDROID