summaryrefslogtreecommitdiffstats
path: root/gfx/webrender_bindings/RenderEGLImageTextureHost.cpp
blob: 6ca9e74072bb3fa3f1246743a3f332a7446e2876 (plain)
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
/* -*- 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/. */

#include "RenderEGLImageTextureHost.h"

#include "mozilla/gfx/Logging.h"
#include "GLContextEGL.h"
#include "GLLibraryEGL.h"

namespace mozilla {
namespace wr {

RenderEGLImageTextureHost::RenderEGLImageTextureHost(EGLImage aImage,
                                                     EGLSync aSync,
                                                     gfx::IntSize aSize)
    : mImage(aImage),
      mSync(aSync),
      mSize(aSize),
      mTextureTarget(LOCAL_GL_TEXTURE_2D),
      mTextureHandle(0) {
  MOZ_COUNT_CTOR_INHERITED(RenderEGLImageTextureHost, RenderTextureHost);
}

RenderEGLImageTextureHost::~RenderEGLImageTextureHost() {
  MOZ_COUNT_DTOR_INHERITED(RenderEGLImageTextureHost, RenderTextureHost);
  DeleteTextureHandle();
}

wr::WrExternalImage RenderEGLImageTextureHost::Lock(uint8_t aChannelIndex,
                                                    gl::GLContext* aGL) {
  MOZ_ASSERT(aChannelIndex == 0);

  if (mGL.get() != aGL) {
    if (mGL) {
      // This should not happen. SharedSurface_EGLImage is created only in
      // parent process.
      MOZ_ASSERT_UNREACHABLE("Unexpected GL context");
      return InvalidToWrExternalImage();
    }
    mGL = aGL;
  }

  if (!mImage || !mGL || !mGL->MakeCurrent()) {
    return InvalidToWrExternalImage();
  }

  EGLint status = LOCAL_EGL_CONDITION_SATISFIED;
  if (mSync) {
    const auto& gle = gl::GLContextEGL::Cast(mGL);
    const auto& egl = gle->mEgl;
    MOZ_ASSERT(egl->IsExtensionSupported(gl::EGLExtension::KHR_fence_sync));
    status = egl->fClientWaitSync(mSync, 0, LOCAL_EGL_FOREVER);
    // We do not need to delete sync here. It is deleted by
    // SharedSurface_EGLImage.
    mSync = 0;
  }

  if (status != LOCAL_EGL_CONDITION_SATISFIED) {
    MOZ_ASSERT(
        status != 0,
        "ClientWaitSync generated an error. Has mSync already been destroyed?");
    return InvalidToWrExternalImage();
  }

  if (!mTextureHandle) {
    mTextureTarget = mGL->GetPreferredEGLImageTextureTarget();
    MOZ_ASSERT(mTextureTarget == LOCAL_GL_TEXTURE_2D ||
               mTextureTarget == LOCAL_GL_TEXTURE_EXTERNAL);

    mGL->fGenTextures(1, &mTextureHandle);
    ActivateBindAndTexParameteri(mGL, LOCAL_GL_TEXTURE0, mTextureTarget,
                                 mTextureHandle);
    mGL->fEGLImageTargetTexture2D(mTextureTarget, mImage);
  }

  const auto uvs = GetUvCoords(mSize);
  return NativeTextureToWrExternalImage(
      mTextureHandle, uvs.first.x, uvs.first.y, uvs.second.x, uvs.second.y);
}

void RenderEGLImageTextureHost::Unlock() {}

void RenderEGLImageTextureHost::DeleteTextureHandle() {
  if (mTextureHandle) {
    // XXX recycle gl texture, since SharedSurface_EGLImage and
    // RenderEGLImageTextureHost is not recycled.
    mGL->fDeleteTextures(1, &mTextureHandle);
    mTextureHandle = 0;
  }
}

}  // namespace wr
}  // namespace mozilla