2020-02-05 09:32:25 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
|
|
|
|
/* 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 "SharedSurfaceDMABUF.h"
|
2020-06-15 21:25:55 +03:00
|
|
|
|
2020-06-25 08:57:40 +03:00
|
|
|
#include "gfxPlatformGtk.h"
|
2020-02-05 09:32:25 +03:00
|
|
|
#include "GLContextEGL.h"
|
2020-06-15 21:25:55 +03:00
|
|
|
#include "MozFramebuffer.h"
|
2020-02-05 09:32:25 +03:00
|
|
|
#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc
|
|
|
|
|
|
|
|
namespace mozilla::gl {
|
|
|
|
|
|
|
|
/*static*/
|
|
|
|
UniquePtr<SharedSurface_DMABUF> SharedSurface_DMABUF::Create(
|
2020-06-15 21:25:55 +03:00
|
|
|
const SharedSurfaceDesc& desc) {
|
2020-06-21 16:59:17 +03:00
|
|
|
const auto flags = static_cast<DMABufSurfaceFlags>(
|
2020-06-15 21:25:55 +03:00
|
|
|
DMABUF_TEXTURE | DMABUF_USE_MODIFIERS | DMABUF_ALPHA);
|
2020-06-21 16:59:17 +03:00
|
|
|
const RefPtr<DMABufSurface> surface = DMABufSurfaceRGBA::CreateDMABufSurface(
|
|
|
|
desc.size.width, desc.size.height, flags);
|
2020-06-15 21:25:55 +03:00
|
|
|
if (!surface || !surface->CreateTexture(desc.gl)) {
|
2020-06-11 19:44:20 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-06-11 09:37:35 +03:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto tex = surface->GetTexture();
|
|
|
|
auto fb = MozFramebuffer::CreateForBacking(desc.gl, desc.size, 0, false,
|
|
|
|
LOCAL_GL_TEXTURE_2D, tex);
|
|
|
|
if (!fb) return nullptr;
|
|
|
|
|
|
|
|
return AsUnique(new SharedSurface_DMABUF(desc, std::move(fb), surface));
|
2020-02-05 09:32:25 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:17 +03:00
|
|
|
SharedSurface_DMABUF::SharedSurface_DMABUF(const SharedSurfaceDesc& desc,
|
|
|
|
UniquePtr<MozFramebuffer> fb,
|
|
|
|
const RefPtr<DMABufSurface> surface)
|
2020-06-15 21:25:55 +03:00
|
|
|
: SharedSurface(desc, std::move(fb)), mSurface(surface) {}
|
2020-02-05 09:32:25 +03:00
|
|
|
|
|
|
|
SharedSurface_DMABUF::~SharedSurface_DMABUF() {
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto& gl = mDesc.gl;
|
|
|
|
if (!gl || !gl->MakeCurrent()) {
|
2020-02-05 09:32:25 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-02-13 01:35:02 +03:00
|
|
|
mSurface->ReleaseTextures();
|
2020-02-05 09:32:25 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 18:13:28 +03:00
|
|
|
void SharedSurface_DMABUF::ProducerReleaseImpl() { mSurface->FenceSet(); }
|
2020-02-05 09:32:25 +03:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
Maybe<layers::SurfaceDescriptor> SharedSurface_DMABUF::ToSurfaceDescriptor() {
|
|
|
|
layers::SurfaceDescriptor desc;
|
|
|
|
if (!mSurface->Serialize(desc)) return {};
|
|
|
|
return Some(desc);
|
2020-02-05 09:32:25 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 08:57:40 +03:00
|
|
|
/*static*/
|
|
|
|
UniquePtr<SurfaceFactory_DMABUF> SurfaceFactory_DMABUF::Create(GLContext& gl) {
|
2020-07-01 14:22:42 +03:00
|
|
|
if (!gfxPlatformGtk::GetPlatform()->UseDMABufWebGL()) {
|
2020-06-25 08:57:40 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dmabufFactory = MakeUnique<SurfaceFactory_DMABUF>(gl);
|
|
|
|
if (dmabufFactory->CanCreateSurface()) {
|
|
|
|
return dmabufFactory;
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:22:42 +03:00
|
|
|
gfxPlatformGtk::GetPlatform()->DisableDMABufWebGL();
|
2020-06-25 08:57:40 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
SurfaceFactory_DMABUF::SurfaceFactory_DMABUF(GLContext& gl)
|
|
|
|
: SurfaceFactory({&gl, SharedSurfaceType::EGLSurfaceDMABUF,
|
2020-06-21 16:59:17 +03:00
|
|
|
layers::TextureType::DMABUF, true}) {}
|
2020-06-15 21:25:55 +03:00
|
|
|
|
2020-02-05 09:32:25 +03:00
|
|
|
} // namespace mozilla::gl
|