Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / gfx / webrender_bindings / RenderEGLImageTextureHost.cpp
blob6ca9e74072bb3fa3f1246743a3f332a7446e2876
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/. */
7 #include "RenderEGLImageTextureHost.h"
9 #include "mozilla/gfx/Logging.h"
10 #include "GLContextEGL.h"
11 #include "GLLibraryEGL.h"
13 namespace mozilla {
14 namespace wr {
16 RenderEGLImageTextureHost::RenderEGLImageTextureHost(EGLImage aImage,
17 EGLSync aSync,
18 gfx::IntSize aSize)
19 : mImage(aImage),
20 mSync(aSync),
21 mSize(aSize),
22 mTextureTarget(LOCAL_GL_TEXTURE_2D),
23 mTextureHandle(0) {
24 MOZ_COUNT_CTOR_INHERITED(RenderEGLImageTextureHost, RenderTextureHost);
27 RenderEGLImageTextureHost::~RenderEGLImageTextureHost() {
28 MOZ_COUNT_DTOR_INHERITED(RenderEGLImageTextureHost, RenderTextureHost);
29 DeleteTextureHandle();
32 wr::WrExternalImage RenderEGLImageTextureHost::Lock(uint8_t aChannelIndex,
33 gl::GLContext* aGL) {
34 MOZ_ASSERT(aChannelIndex == 0);
36 if (mGL.get() != aGL) {
37 if (mGL) {
38 // This should not happen. SharedSurface_EGLImage is created only in
39 // parent process.
40 MOZ_ASSERT_UNREACHABLE("Unexpected GL context");
41 return InvalidToWrExternalImage();
43 mGL = aGL;
46 if (!mImage || !mGL || !mGL->MakeCurrent()) {
47 return InvalidToWrExternalImage();
50 EGLint status = LOCAL_EGL_CONDITION_SATISFIED;
51 if (mSync) {
52 const auto& gle = gl::GLContextEGL::Cast(mGL);
53 const auto& egl = gle->mEgl;
54 MOZ_ASSERT(egl->IsExtensionSupported(gl::EGLExtension::KHR_fence_sync));
55 status = egl->fClientWaitSync(mSync, 0, LOCAL_EGL_FOREVER);
56 // We do not need to delete sync here. It is deleted by
57 // SharedSurface_EGLImage.
58 mSync = 0;
61 if (status != LOCAL_EGL_CONDITION_SATISFIED) {
62 MOZ_ASSERT(
63 status != 0,
64 "ClientWaitSync generated an error. Has mSync already been destroyed?");
65 return InvalidToWrExternalImage();
68 if (!mTextureHandle) {
69 mTextureTarget = mGL->GetPreferredEGLImageTextureTarget();
70 MOZ_ASSERT(mTextureTarget == LOCAL_GL_TEXTURE_2D ||
71 mTextureTarget == LOCAL_GL_TEXTURE_EXTERNAL);
73 mGL->fGenTextures(1, &mTextureHandle);
74 ActivateBindAndTexParameteri(mGL, LOCAL_GL_TEXTURE0, mTextureTarget,
75 mTextureHandle);
76 mGL->fEGLImageTargetTexture2D(mTextureTarget, mImage);
79 const auto uvs = GetUvCoords(mSize);
80 return NativeTextureToWrExternalImage(
81 mTextureHandle, uvs.first.x, uvs.first.y, uvs.second.x, uvs.second.y);
84 void RenderEGLImageTextureHost::Unlock() {}
86 void RenderEGLImageTextureHost::DeleteTextureHandle() {
87 if (mTextureHandle) {
88 // XXX recycle gl texture, since SharedSurface_EGLImage and
89 // RenderEGLImageTextureHost is not recycled.
90 mGL->fDeleteTextures(1, &mTextureHandle);
91 mTextureHandle = 0;
95 } // namespace wr
96 } // namespace mozilla