2019-06-04 14:11:28 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
#include "DMABufSurface.h"
|
2019-06-04 14:11:28 +03:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <dlfcn.h>
|
2020-05-29 18:21:50 +03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#include <poll.h>
|
2021-03-10 01:56:40 +03:00
|
|
|
#include <sys/ioctl.h>
|
2019-06-04 14:11:28 +03:00
|
|
|
|
2019-06-21 00:52:42 +03:00
|
|
|
#include "mozilla/widget/gbm.h"
|
2020-02-13 01:35:09 +03:00
|
|
|
#include "mozilla/widget/va_drmcommon.h"
|
2021-07-15 19:19:51 +03:00
|
|
|
#include "YCbCrUtils.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
2019-09-03 12:41:32 +03:00
|
|
|
#include "GLContextTypes.h" // for GLContext, etc
|
|
|
|
#include "GLContextEGL.h"
|
|
|
|
#include "GLContextProvider.h"
|
2020-09-01 10:24:46 +03:00
|
|
|
#include "ScopedGLHelpers.h"
|
2019-09-03 12:41:32 +03:00
|
|
|
|
|
|
|
#include "mozilla/layers/LayersSurfaces.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2019-09-03 12:41:32 +03:00
|
|
|
|
|
|
|
/*
|
2019-10-17 13:38:12 +03:00
|
|
|
TODO:
|
2019-09-03 12:41:32 +03:00
|
|
|
DRM device selection:
|
|
|
|
https://lists.freedesktop.org/archives/wayland-devel/2018-November/039660.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* C++ / C typecast macros for special EGL handle values */
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
# define EGL_CAST(type, value) (static_cast<type>(value))
|
|
|
|
#else
|
|
|
|
# define EGL_CAST(type, value) ((type)(value))
|
|
|
|
#endif
|
2019-06-04 14:11:28 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::widget;
|
2019-09-03 12:41:32 +03:00
|
|
|
using namespace mozilla::gl;
|
|
|
|
using namespace mozilla::layers;
|
2019-06-04 14:11:28 +03:00
|
|
|
|
|
|
|
#define BUFFER_FLAGS 0
|
|
|
|
|
2021-07-15 19:19:51 +03:00
|
|
|
static RefPtr<GLContext> sSnapshotContext;
|
2020-08-06 22:38:31 +03:00
|
|
|
static Atomic<int> gNewSurfaceUID(1);
|
2020-08-05 10:45:07 +03:00
|
|
|
|
2021-07-15 19:19:51 +03:00
|
|
|
bool EnsureSnapshotGLContext() {
|
|
|
|
if (!sSnapshotContext) {
|
|
|
|
nsCString discardFailureId;
|
|
|
|
sSnapshotContext = GLContextProvider::CreateHeadless({}, &discardFailureId);
|
|
|
|
if (!sSnapshotContext) {
|
|
|
|
NS_WARNING("Failed to create snapshot GLContext");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurface::IsGlobalRefSet() const {
|
2020-05-29 18:21:50 +03:00
|
|
|
if (!mGlobalRefCountFd) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct pollfd pfd;
|
|
|
|
pfd.fd = mGlobalRefCountFd;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
return poll(&pfd, 1, 0) == 1;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::GlobalRefRelease() {
|
2020-05-29 18:21:50 +03:00
|
|
|
MOZ_ASSERT(mGlobalRefCountFd);
|
|
|
|
uint64_t counter;
|
|
|
|
if (read(mGlobalRefCountFd, &counter, sizeof(counter)) != sizeof(counter)) {
|
|
|
|
// EAGAIN means the refcount is already zero. It happens when we release
|
|
|
|
// last reference to the surface.
|
|
|
|
if (errno != EAGAIN) {
|
2021-03-16 10:57:41 +03:00
|
|
|
NS_WARNING(nsPrintfCString("Failed to unref dmabuf global ref count: %s",
|
|
|
|
strerror(errno))
|
|
|
|
.get());
|
2020-05-29 18:21:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::GlobalRefAdd() {
|
2020-05-29 18:21:50 +03:00
|
|
|
MOZ_ASSERT(mGlobalRefCountFd);
|
|
|
|
uint64_t counter = 1;
|
|
|
|
if (write(mGlobalRefCountFd, &counter, sizeof(counter)) != sizeof(counter)) {
|
2021-03-16 10:57:41 +03:00
|
|
|
NS_WARNING(nsPrintfCString("Failed to ref dmabuf global ref count: %s",
|
|
|
|
strerror(errno))
|
|
|
|
.get());
|
2020-05-29 18:21:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::GlobalRefCountCreate() {
|
2020-05-29 18:21:50 +03:00
|
|
|
MOZ_ASSERT(!mGlobalRefCountFd);
|
|
|
|
mGlobalRefCountFd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE);
|
|
|
|
if (mGlobalRefCountFd < 0) {
|
2021-03-16 10:57:41 +03:00
|
|
|
NS_WARNING(nsPrintfCString("Failed to create dmabuf global ref count: %s",
|
|
|
|
strerror(errno))
|
|
|
|
.get());
|
2020-05-29 18:21:50 +03:00
|
|
|
mGlobalRefCountFd = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::GlobalRefCountImport(int aFd) {
|
2020-05-29 18:21:50 +03:00
|
|
|
MOZ_ASSERT(!mGlobalRefCountFd);
|
|
|
|
mGlobalRefCountFd = aFd;
|
|
|
|
GlobalRefAdd();
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::GlobalRefCountDelete() {
|
2020-05-29 18:21:50 +03:00
|
|
|
if (mGlobalRefCountFd) {
|
|
|
|
GlobalRefRelease();
|
|
|
|
close(mGlobalRefCountFd);
|
|
|
|
mGlobalRefCountFd = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::ReleaseDMABuf() {
|
2021-03-18 12:51:47 +03:00
|
|
|
LOGDMABUF(("DMABufSurface::ReleaseDMABuf() UID %d", mUID));
|
2020-06-13 21:38:31 +03:00
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
Unmap(i);
|
|
|
|
}
|
|
|
|
|
2021-03-18 12:51:47 +03:00
|
|
|
MutexAutoLock lockFD(mSurfaceLock);
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptors(lockFD, /* aForceClose */ true);
|
2021-03-16 10:57:41 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
if (mGbmBufferObject[i]) {
|
|
|
|
nsGbmLib::Destroy(mGbmBufferObject[i]);
|
|
|
|
mGbmBufferObject[i] = nullptr;
|
|
|
|
}
|
2020-06-13 21:38:31 +03:00
|
|
|
}
|
2021-03-18 12:51:47 +03:00
|
|
|
mBufferPlaneCount = 0;
|
2020-06-13 21:38:31 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
DMABufSurface::DMABufSurface(SurfaceType aSurfaceType)
|
2020-03-06 13:40:39 +03:00
|
|
|
: mSurfaceType(aSurfaceType),
|
|
|
|
mBufferModifier(DRM_FORMAT_MOD_INVALID),
|
|
|
|
mBufferPlaneCount(0),
|
|
|
|
mDrmFormats(),
|
|
|
|
mStrides(),
|
|
|
|
mOffsets(),
|
2020-06-13 21:38:31 +03:00
|
|
|
mGbmBufferObject(),
|
|
|
|
mMappedRegion(),
|
|
|
|
mMappedRegionStride(),
|
2020-07-24 11:54:37 +03:00
|
|
|
mSyncFd(-1),
|
2020-05-29 18:21:50 +03:00
|
|
|
mSync(0),
|
|
|
|
mGlobalRefCountFd(0),
|
2021-03-16 10:57:41 +03:00
|
|
|
mUID(gNewSurfaceUID++),
|
|
|
|
mSurfaceLock("DMABufSurface") {
|
2020-03-06 13:40:39 +03:00
|
|
|
for (auto& slot : mDmabufFds) {
|
|
|
|
slot = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
DMABufSurface::~DMABufSurface() {
|
2020-05-29 18:21:50 +03:00
|
|
|
FenceDelete();
|
|
|
|
GlobalRefCountDelete();
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
already_AddRefed<DMABufSurface> DMABufSurface::CreateDMABufSurface(
|
2020-02-13 01:35:09 +03:00
|
|
|
const mozilla::layers::SurfaceDescriptor& aDesc) {
|
|
|
|
const SurfaceDescriptorDMABuf& desc = aDesc.get_SurfaceDescriptorDMABuf();
|
2020-06-21 16:59:24 +03:00
|
|
|
RefPtr<DMABufSurface> surf;
|
2020-02-13 01:35:09 +03:00
|
|
|
|
|
|
|
switch (desc.bufferType()) {
|
|
|
|
case SURFACE_RGBA:
|
2020-06-21 16:59:24 +03:00
|
|
|
surf = new DMABufSurfaceRGBA();
|
2020-02-13 01:35:09 +03:00
|
|
|
break;
|
|
|
|
case SURFACE_NV12:
|
2020-07-30 21:52:32 +03:00
|
|
|
case SURFACE_YUV420:
|
2020-06-21 16:59:24 +03:00
|
|
|
surf = new DMABufSurfaceYUV();
|
2020-02-13 01:35:09 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!surf->Create(desc)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::FenceDelete() {
|
2021-11-19 02:29:10 +03:00
|
|
|
if (mSyncFd > 0) {
|
|
|
|
close(mSyncFd);
|
|
|
|
mSyncFd = -1;
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:19:51 +03:00
|
|
|
if (!mGL) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-07 10:14:46 +03:00
|
|
|
const auto& gle = gl::GLContextEGL::Cast(mGL);
|
|
|
|
const auto& egl = gle->mEgl;
|
2020-02-19 18:13:28 +03:00
|
|
|
|
|
|
|
if (mSync) {
|
2020-08-07 10:14:46 +03:00
|
|
|
egl->fDestroySync(mSync);
|
2020-02-19 18:13:28 +03:00
|
|
|
mSync = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::FenceSet() {
|
2020-02-19 18:13:28 +03:00
|
|
|
if (!mGL || !mGL->MakeCurrent()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-07 10:14:46 +03:00
|
|
|
const auto& gle = gl::GLContextEGL::Cast(mGL);
|
|
|
|
const auto& egl = gle->mEgl;
|
2020-02-19 18:13:28 +03:00
|
|
|
|
2020-08-07 10:14:46 +03:00
|
|
|
if (egl->IsExtensionSupported(EGLExtension::KHR_fence_sync) &&
|
|
|
|
egl->IsExtensionSupported(EGLExtension::ANDROID_native_fence_sync)) {
|
2020-08-05 10:45:12 +03:00
|
|
|
FenceDelete();
|
2020-02-19 18:13:28 +03:00
|
|
|
|
2020-08-07 10:14:46 +03:00
|
|
|
mSync = egl->fCreateSync(LOCAL_EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
|
2020-02-19 18:13:28 +03:00
|
|
|
if (mSync) {
|
2020-08-07 10:14:46 +03:00
|
|
|
mSyncFd = egl->fDupNativeFenceFDANDROID(mSync);
|
2020-02-19 18:13:28 +03:00
|
|
|
mGL->fFlush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ANDROID_native_fence_sync may not be supported so call glFinish()
|
|
|
|
// as a slow path.
|
|
|
|
mGL->fFinish();
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::FenceWait() {
|
2022-01-09 23:23:03 +03:00
|
|
|
if (!mGL || mSyncFd < 0) {
|
2021-12-22 11:37:31 +03:00
|
|
|
return;
|
2021-07-15 19:19:51 +03:00
|
|
|
}
|
2022-01-09 23:23:03 +03:00
|
|
|
|
2020-08-07 10:14:46 +03:00
|
|
|
const auto& gle = gl::GLContextEGL::Cast(mGL);
|
|
|
|
const auto& egl = gle->mEgl;
|
|
|
|
|
2020-07-24 11:54:37 +03:00
|
|
|
const EGLint attribs[] = {LOCAL_EGL_SYNC_NATIVE_FENCE_FD_ANDROID, mSyncFd,
|
2020-02-19 18:13:28 +03:00
|
|
|
LOCAL_EGL_NONE};
|
2021-12-22 11:37:31 +03:00
|
|
|
EGLSync sync = egl->fCreateSync(LOCAL_EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
|
|
|
|
if (!sync) {
|
2020-02-19 18:13:28 +03:00
|
|
|
MOZ_ASSERT(false, "Failed to create GLFence!");
|
2021-12-22 11:37:31 +03:00
|
|
|
// We failed to create GLFence so clear mSyncFd to avoid another try.
|
|
|
|
close(mSyncFd);
|
|
|
|
mSyncFd = -1;
|
|
|
|
return;
|
2020-02-19 18:13:28 +03:00
|
|
|
}
|
|
|
|
|
2022-01-09 23:23:03 +03:00
|
|
|
// mSyncFd is owned by GLFence so clear local reference to avoid double close
|
2021-12-22 11:37:31 +03:00
|
|
|
// at DMABufSurface::FenceDelete().
|
|
|
|
mSyncFd = -1;
|
|
|
|
|
|
|
|
egl->fClientWaitSync(sync, 0, LOCAL_EGL_FOREVER);
|
|
|
|
egl->fDestroySync(sync);
|
2020-02-19 18:13:28 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
bool DMABufSurface::OpenFileDescriptors(const MutexAutoLock& aProofOfLock) {
|
2021-03-16 10:57:41 +03:00
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
2021-07-12 23:12:00 +03:00
|
|
|
if (!OpenFileDescriptorForPlane(aProofOfLock, i)) {
|
2021-03-16 10:57:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can safely close DMABuf file descriptors only when we have a valid
|
|
|
|
// GbmBufferObject. When we don't have a valid GbmBufferObject and a DMABuf
|
|
|
|
// file descriptor is closed, whole surface is released.
|
2021-07-12 23:12:00 +03:00
|
|
|
void DMABufSurface::CloseFileDescriptors(const MutexAutoLock& aProofOfLock,
|
|
|
|
bool aForceClose) {
|
2021-03-16 10:57:41 +03:00
|
|
|
for (int i = 0; i < DMABUF_BUFFER_PLANES; i++) {
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptorForPlane(aProofOfLock, i, aForceClose);
|
2021-03-16 10:57:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
DMABufSurfaceRGBA::DMABufSurfaceRGBA()
|
|
|
|
: DMABufSurface(SURFACE_RGBA),
|
2020-02-13 01:34:35 +03:00
|
|
|
mSurfaceFlags(0),
|
2020-02-13 01:34:27 +03:00
|
|
|
mWidth(0),
|
2019-06-04 14:11:28 +03:00
|
|
|
mHeight(0),
|
|
|
|
mGmbFormat(nullptr),
|
2019-09-03 12:41:32 +03:00
|
|
|
mEGLImage(LOCAL_EGL_NO_IMAGE),
|
2020-02-04 20:34:34 +03:00
|
|
|
mTexture(0),
|
2021-07-12 23:12:00 +03:00
|
|
|
mGbmBufferFlags(0),
|
|
|
|
mWlBuffer(nullptr) {}
|
2019-06-04 14:11:28 +03:00
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
DMABufSurfaceRGBA::~DMABufSurfaceRGBA() {
|
|
|
|
ReleaseWlBuffer();
|
|
|
|
ReleaseSurface();
|
|
|
|
}
|
2019-06-04 14:11:28 +03:00
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
bool DMABufSurfaceRGBA::OpenFileDescriptorForPlane(
|
|
|
|
const MutexAutoLock& aProofOfLock, int aPlane) {
|
2021-03-16 10:57:41 +03:00
|
|
|
if (mDmabufFds[aPlane] >= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mBufferPlaneCount == 1) {
|
|
|
|
MOZ_ASSERT(aPlane == 0, "DMABuf: wrong surface plane!");
|
|
|
|
mDmabufFds[0] = nsGbmLib::GetFd(mGbmBufferObject[0]);
|
|
|
|
} else {
|
|
|
|
uint32_t handle =
|
|
|
|
nsGbmLib::GetHandleForPlane(mGbmBufferObject[0], aPlane).u32;
|
|
|
|
int ret = nsGbmLib::DrmPrimeHandleToFD(GetDMABufDevice()->GetGbmDeviceFd(),
|
|
|
|
handle, 0, &mDmabufFds[aPlane]);
|
|
|
|
if (ret < 0) {
|
|
|
|
mDmabufFds[aPlane] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDmabufFds[aPlane] < 0) {
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptors(aProofOfLock);
|
2021-03-16 10:57:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
void DMABufSurfaceRGBA::CloseFileDescriptorForPlane(
|
|
|
|
const MutexAutoLock& aProofOfLock, int aPlane, bool aForceClose = false) {
|
2021-03-16 10:57:41 +03:00
|
|
|
if ((aForceClose || mGbmBufferObject[0]) && mDmabufFds[aPlane] >= 0) {
|
|
|
|
close(mDmabufFds[aPlane]);
|
|
|
|
mDmabufFds[aPlane] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceRGBA::Create(int aWidth, int aHeight,
|
|
|
|
int aDMABufSurfaceFlags) {
|
2020-06-13 21:38:31 +03:00
|
|
|
MOZ_ASSERT(mGbmBufferObject[0] == nullptr, "Already created?");
|
2019-06-04 14:11:28 +03:00
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
mSurfaceFlags = aDMABufSurfaceFlags;
|
2019-06-04 14:11:28 +03:00
|
|
|
mWidth = aWidth;
|
|
|
|
mHeight = aHeight;
|
|
|
|
|
2020-09-01 10:24:46 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceRGBA::Create() UID %d size %d x %d\n", mUID, mWidth,
|
|
|
|
mHeight));
|
|
|
|
|
2020-07-26 16:02:56 +03:00
|
|
|
mGmbFormat = GetDMABufDevice()->GetGbmFormat(mSurfaceFlags & DMABUF_ALPHA);
|
2019-06-04 14:11:28 +03:00
|
|
|
if (!mGmbFormat) {
|
|
|
|
// Requested DRM format is not supported.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool useModifiers = (aDMABufSurfaceFlags & DMABUF_USE_MODIFIERS) &&
|
2020-01-14 16:13:51 +03:00
|
|
|
mGmbFormat->mModifiersCount > 0;
|
|
|
|
if (useModifiers) {
|
2020-09-01 10:24:46 +03:00
|
|
|
LOGDMABUF((" Creating with modifiers\n"));
|
2020-06-13 21:38:31 +03:00
|
|
|
mGbmBufferObject[0] = nsGbmLib::CreateWithModifiers(
|
2020-06-23 09:27:09 +03:00
|
|
|
GetDMABufDevice()->GetGbmDevice(), mWidth, mHeight, mGmbFormat->mFormat,
|
2019-06-04 14:11:28 +03:00
|
|
|
mGmbFormat->mModifiers, mGmbFormat->mModifiersCount);
|
2020-06-13 21:38:31 +03:00
|
|
|
if (mGbmBufferObject[0]) {
|
|
|
|
mBufferModifier = nsGbmLib::GetModifier(mGbmBufferObject[0]);
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 21:38:31 +03:00
|
|
|
if (!mGbmBufferObject[0]) {
|
2020-09-01 10:24:46 +03:00
|
|
|
LOGDMABUF((" Creating without modifiers\n"));
|
2021-03-10 01:56:40 +03:00
|
|
|
mGbmBufferFlags = GBM_BO_USE_LINEAR;
|
2020-06-13 21:38:31 +03:00
|
|
|
mGbmBufferObject[0] =
|
2020-06-23 09:27:09 +03:00
|
|
|
nsGbmLib::Create(GetDMABufDevice()->GetGbmDevice(), mWidth, mHeight,
|
2019-09-03 12:41:32 +03:00
|
|
|
mGmbFormat->mFormat, mGbmBufferFlags);
|
2020-03-04 10:13:47 +03:00
|
|
|
mBufferModifier = DRM_FORMAT_MOD_INVALID;
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-13 21:38:31 +03:00
|
|
|
if (!mGbmBufferObject[0]) {
|
2020-09-01 10:24:46 +03:00
|
|
|
LOGDMABUF((" Failed to create GbmBufferObject\n"));
|
2019-06-04 14:11:28 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:13:51 +03:00
|
|
|
if (mBufferModifier != DRM_FORMAT_MOD_INVALID) {
|
2020-06-13 21:38:31 +03:00
|
|
|
mBufferPlaneCount = nsGbmLib::GetPlaneCount(mGbmBufferObject[0]);
|
2020-02-13 01:35:09 +03:00
|
|
|
if (mBufferPlaneCount > DMABUF_BUFFER_PLANES) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" There's too many dmabuf planes!"));
|
2020-02-13 01:35:09 +03:00
|
|
|
ReleaseSurface();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-04 14:11:28 +03:00
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
2020-06-13 21:38:31 +03:00
|
|
|
mStrides[i] = nsGbmLib::GetStrideForPlane(mGbmBufferObject[0], i);
|
|
|
|
mOffsets[i] = nsGbmLib::GetOffset(mGbmBufferObject[0], i);
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
2019-06-21 00:51:15 +03:00
|
|
|
} else {
|
2019-06-04 14:11:28 +03:00
|
|
|
mBufferPlaneCount = 1;
|
2020-06-13 21:38:31 +03:00
|
|
|
mStrides[0] = nsGbmLib::GetStride(mGbmBufferObject[0]);
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
|
2020-09-01 10:24:46 +03:00
|
|
|
LOGDMABUF((" Success\n"));
|
2019-09-03 12:41:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
bool DMABufSurfaceRGBA::ImportSurfaceDescriptor(
|
2019-09-03 12:41:32 +03:00
|
|
|
const SurfaceDescriptor& aDesc) {
|
|
|
|
const SurfaceDescriptorDMABuf& desc = aDesc.get_SurfaceDescriptorDMABuf();
|
|
|
|
|
2020-02-13 01:34:27 +03:00
|
|
|
mWidth = desc.width()[0];
|
|
|
|
mHeight = desc.height()[0];
|
2020-01-14 16:13:51 +03:00
|
|
|
mBufferModifier = desc.modifier();
|
2020-03-06 13:40:39 +03:00
|
|
|
if (mBufferModifier != DRM_FORMAT_MOD_INVALID) {
|
2020-07-26 16:02:56 +03:00
|
|
|
mGmbFormat = GetDMABufDevice()->GetExactGbmFormat(desc.format()[0]);
|
2020-03-06 13:40:39 +03:00
|
|
|
} else {
|
|
|
|
mDrmFormats[0] = desc.format()[0];
|
|
|
|
}
|
|
|
|
mBufferPlaneCount = desc.fds().Length();
|
2019-09-03 12:41:32 +03:00
|
|
|
mGbmBufferFlags = desc.flags();
|
2020-02-13 01:35:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mBufferPlaneCount <= DMABUF_BUFFER_PLANES);
|
2020-05-29 18:21:50 +03:00
|
|
|
mUID = desc.uid();
|
2020-01-14 16:13:51 +03:00
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(
|
|
|
|
("DMABufSurfaceRGBA::ImportSurfaceDescriptor() UID %d size %d x %d\n",
|
|
|
|
mUID, mWidth, mHeight));
|
|
|
|
|
2020-01-14 16:13:51 +03:00
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
mDmabufFds[i] = desc.fds()[i].ClonePlatformHandle().release();
|
2021-03-16 10:57:41 +03:00
|
|
|
if (mDmabufFds[i] < 0) {
|
|
|
|
LOGDMABUF(
|
|
|
|
(" failed to get DMABuf file descriptor: %s", strerror(errno)));
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-14 16:13:51 +03:00
|
|
|
mStrides[i] = desc.strides()[i];
|
|
|
|
mOffsets[i] = desc.offsets()[i];
|
|
|
|
}
|
2020-02-19 18:13:28 +03:00
|
|
|
|
2020-02-25 16:48:39 +03:00
|
|
|
if (desc.fence().Length() > 0) {
|
2020-07-24 11:54:37 +03:00
|
|
|
mSyncFd = desc.fence()[0].ClonePlatformHandle().release();
|
2021-03-16 10:57:41 +03:00
|
|
|
if (mSyncFd < 0) {
|
|
|
|
LOGDMABUF(
|
|
|
|
(" failed to get GL fence file descriptor: %s", strerror(errno)));
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-19 18:13:28 +03:00
|
|
|
}
|
2020-05-29 18:21:50 +03:00
|
|
|
|
|
|
|
if (desc.refCount().Length() > 0) {
|
|
|
|
GlobalRefCountImport(desc.refCount()[0].ClonePlatformHandle().release());
|
|
|
|
}
|
2020-09-01 10:24:46 +03:00
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
return true;
|
2019-09-03 12:41:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceRGBA::Create(const SurfaceDescriptor& aDesc) {
|
2021-03-16 10:57:41 +03:00
|
|
|
return ImportSurfaceDescriptor(aDesc);
|
2019-09-03 12:41:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceRGBA::Serialize(
|
2019-09-03 12:41:32 +03:00
|
|
|
mozilla::layers::SurfaceDescriptor& aOutDescriptor) {
|
2020-02-13 01:34:27 +03:00
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> width;
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> height;
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> format;
|
2020-01-14 16:13:51 +03:00
|
|
|
AutoTArray<ipc::FileDescriptor, DMABUF_BUFFER_PLANES> fds;
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> strides;
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> offsets;
|
2020-02-13 01:34:27 +03:00
|
|
|
AutoTArray<uintptr_t, DMABUF_BUFFER_PLANES> images;
|
2020-02-25 16:48:39 +03:00
|
|
|
AutoTArray<ipc::FileDescriptor, 1> fenceFDs;
|
2020-05-29 18:21:50 +03:00
|
|
|
AutoTArray<ipc::FileDescriptor, 1> refCountFDs;
|
2019-09-03 12:41:32 +03:00
|
|
|
|
2020-09-01 10:24:46 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceRGBA::Serialize() UID %d\n", mUID));
|
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
MutexAutoLock lockFD(mSurfaceLock);
|
2021-07-12 23:12:00 +03:00
|
|
|
if (!OpenFileDescriptors(lockFD)) {
|
2021-03-16 10:57:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-13 01:34:27 +03:00
|
|
|
width.AppendElement(mWidth);
|
|
|
|
height.AppendElement(mHeight);
|
|
|
|
format.AppendElement(mGmbFormat->mFormat);
|
2020-01-14 16:13:51 +03:00
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
fds.AppendElement(ipc::FileDescriptor(mDmabufFds[i]));
|
|
|
|
strides.AppendElement(mStrides[i]);
|
|
|
|
offsets.AppendElement(mOffsets[i]);
|
|
|
|
}
|
2019-09-03 12:41:32 +03:00
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptors(lockFD);
|
2021-03-16 10:57:41 +03:00
|
|
|
|
2020-02-19 18:13:28 +03:00
|
|
|
if (mSync) {
|
2020-08-05 10:45:12 +03:00
|
|
|
fenceFDs.AppendElement(ipc::FileDescriptor(mSyncFd));
|
2020-02-19 18:13:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:21:50 +03:00
|
|
|
if (mGlobalRefCountFd) {
|
|
|
|
refCountFDs.AppendElement(ipc::FileDescriptor(mGlobalRefCountFd));
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:32:45 +03:00
|
|
|
aOutDescriptor = SurfaceDescriptorDMABuf(
|
|
|
|
mSurfaceType, mBufferModifier, mGbmBufferFlags, fds, width, height,
|
|
|
|
format, strides, offsets, GetYUVColorSpace(), mColorRange, fenceFDs, mUID,
|
|
|
|
refCountFDs);
|
2019-09-03 12:41:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceRGBA::CreateTexture(GLContext* aGLContext, int aPlane) {
|
2020-02-04 20:34:34 +03:00
|
|
|
MOZ_ASSERT(!mEGLImage && !mTexture, "EGLImage is already created!");
|
2019-09-03 12:41:32 +03:00
|
|
|
|
|
|
|
nsTArray<EGLint> attribs;
|
|
|
|
attribs.AppendElement(LOCAL_EGL_WIDTH);
|
|
|
|
attribs.AppendElement(mWidth);
|
|
|
|
attribs.AppendElement(LOCAL_EGL_HEIGHT);
|
|
|
|
attribs.AppendElement(mHeight);
|
|
|
|
attribs.AppendElement(LOCAL_EGL_LINUX_DRM_FOURCC_EXT);
|
2020-03-06 13:40:39 +03:00
|
|
|
if (mGmbFormat) {
|
|
|
|
attribs.AppendElement(mGmbFormat->mFormat);
|
|
|
|
} else {
|
|
|
|
attribs.AppendElement(mDrmFormats[0]);
|
|
|
|
}
|
2019-09-03 12:41:32 +03:00
|
|
|
#define ADD_PLANE_ATTRIBS(plane_idx) \
|
|
|
|
{ \
|
|
|
|
attribs.AppendElement(LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_FD_EXT); \
|
|
|
|
attribs.AppendElement(mDmabufFds[plane_idx]); \
|
|
|
|
attribs.AppendElement(LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_OFFSET_EXT); \
|
|
|
|
attribs.AppendElement((int)mOffsets[plane_idx]); \
|
|
|
|
attribs.AppendElement(LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_PITCH_EXT); \
|
|
|
|
attribs.AppendElement((int)mStrides[plane_idx]); \
|
2020-01-14 16:13:51 +03:00
|
|
|
if (mBufferModifier != DRM_FORMAT_MOD_INVALID) { \
|
|
|
|
attribs.AppendElement( \
|
|
|
|
LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_MODIFIER_LO_EXT); \
|
|
|
|
attribs.AppendElement(mBufferModifier & 0xFFFFFFFF); \
|
|
|
|
attribs.AppendElement( \
|
|
|
|
LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_MODIFIER_HI_EXT); \
|
|
|
|
attribs.AppendElement(mBufferModifier >> 32); \
|
|
|
|
} \
|
2019-09-03 12:41:32 +03:00
|
|
|
}
|
2021-03-16 10:57:41 +03:00
|
|
|
|
|
|
|
MutexAutoLock lockFD(mSurfaceLock);
|
2021-07-12 23:12:00 +03:00
|
|
|
if (!OpenFileDescriptors(lockFD)) {
|
2021-03-16 10:57:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-09-03 12:41:32 +03:00
|
|
|
ADD_PLANE_ATTRIBS(0);
|
2020-01-14 16:13:51 +03:00
|
|
|
if (mBufferPlaneCount > 1) ADD_PLANE_ATTRIBS(1);
|
|
|
|
if (mBufferPlaneCount > 2) ADD_PLANE_ATTRIBS(2);
|
|
|
|
if (mBufferPlaneCount > 3) ADD_PLANE_ATTRIBS(3);
|
2019-09-03 12:41:32 +03:00
|
|
|
#undef ADD_PLANE_ATTRIBS
|
|
|
|
attribs.AppendElement(LOCAL_EGL_NONE);
|
|
|
|
|
2020-08-07 23:50:04 +03:00
|
|
|
if (!aGLContext) return false;
|
|
|
|
const auto& gle = gl::GLContextEGL::Cast(aGLContext);
|
2020-08-07 10:14:46 +03:00
|
|
|
const auto& egl = gle->mEgl;
|
|
|
|
mEGLImage =
|
|
|
|
egl->fCreateImage(LOCAL_EGL_NO_CONTEXT, LOCAL_EGL_LINUX_DMA_BUF_EXT,
|
|
|
|
nullptr, attribs.Elements());
|
2021-11-18 23:17:45 +03:00
|
|
|
|
|
|
|
CloseFileDescriptors(lockFD);
|
|
|
|
|
2019-09-03 12:41:32 +03:00
|
|
|
if (mEGLImage == LOCAL_EGL_NO_IMAGE) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("EGLImageKHR creation failed"));
|
2019-09-03 12:41:32 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aGLContext->MakeCurrent();
|
2019-11-07 15:29:56 +03:00
|
|
|
aGLContext->fGenTextures(1, &mTexture);
|
2020-09-01 10:24:46 +03:00
|
|
|
const ScopedBindTexture savedTex(aGLContext, mTexture);
|
2019-09-03 12:41:32 +03:00
|
|
|
aGLContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
aGLContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
aGLContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MAG_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
aGLContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MIN_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
aGLContext->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, mEGLImage);
|
2019-09-24 14:30:16 +03:00
|
|
|
mGL = aGLContext;
|
2020-09-01 10:24:46 +03:00
|
|
|
|
2020-02-04 20:34:34 +03:00
|
|
|
return true;
|
2019-09-03 12:41:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurfaceRGBA::ReleaseTextures() {
|
2020-02-19 18:13:28 +03:00
|
|
|
FenceDelete();
|
|
|
|
|
2021-07-15 19:19:51 +03:00
|
|
|
if (!mGL) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-07 10:14:46 +03:00
|
|
|
const auto& gle = gl::GLContextEGL::Cast(mGL);
|
|
|
|
const auto& egl = gle->mEgl;
|
|
|
|
|
2020-02-04 20:34:34 +03:00
|
|
|
if (mTexture && mGL->MakeCurrent()) {
|
|
|
|
mGL->fDeleteTextures(1, &mTexture);
|
|
|
|
mTexture = 0;
|
|
|
|
mGL = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-09-03 12:41:32 +03:00
|
|
|
if (mEGLImage) {
|
2020-08-07 10:14:46 +03:00
|
|
|
egl->fDestroyImage(mEGLImage);
|
2019-09-03 12:41:32 +03:00
|
|
|
mEGLImage = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurfaceRGBA::ReleaseSurface() {
|
2019-06-04 14:11:28 +03:00
|
|
|
MOZ_ASSERT(!IsMapped(), "We can't release mapped buffer!");
|
2019-09-24 14:30:16 +03:00
|
|
|
|
2020-02-13 01:34:27 +03:00
|
|
|
ReleaseTextures();
|
2020-06-13 21:38:31 +03:00
|
|
|
ReleaseDMABuf();
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
bool DMABufSurfaceRGBA::CreateWlBuffer() {
|
|
|
|
MutexAutoLock lockFD(mSurfaceLock);
|
|
|
|
if (!OpenFileDescriptors(lockFD)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsWaylandDisplay> waylandDisplay = widget::WaylandDisplayGet();
|
|
|
|
if (!waylandDisplay->GetDmabuf()) {
|
2021-11-18 23:17:45 +03:00
|
|
|
CloseFileDescriptors(lockFD);
|
2021-07-12 23:12:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct zwp_linux_buffer_params_v1* params =
|
|
|
|
zwp_linux_dmabuf_v1_create_params(waylandDisplay->GetDmabuf());
|
|
|
|
zwp_linux_buffer_params_v1_add(params, mDmabufFds[0], 0, mOffsets[0],
|
|
|
|
mStrides[0], mBufferModifier >> 32,
|
|
|
|
mBufferModifier & 0xffffffff);
|
|
|
|
|
|
|
|
mWlBuffer = zwp_linux_buffer_params_v1_create_immed(
|
|
|
|
params, GetWidth(), GetHeight(), mGmbFormat->mFormat, 0);
|
|
|
|
|
|
|
|
CloseFileDescriptors(lockFD);
|
|
|
|
|
|
|
|
return mWlBuffer != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DMABufSurfaceRGBA::ReleaseWlBuffer() {
|
|
|
|
g_clear_pointer(&mWlBuffer, wl_buffer_destroy);
|
|
|
|
}
|
|
|
|
|
2021-03-10 01:56:40 +03:00
|
|
|
// We should synchronize DMA Buffer object access from CPU to avoid potential
|
|
|
|
// cache incoherency and data loss.
|
|
|
|
// See
|
|
|
|
// https://01.org/linuxgraphics/gfx-docs/drm/driver-api/dma-buf.html#cpu-access-to-dma-buffer-objects
|
|
|
|
struct dma_buf_sync {
|
|
|
|
uint64_t flags;
|
|
|
|
};
|
|
|
|
#define DMA_BUF_SYNC_READ (1 << 0)
|
|
|
|
#define DMA_BUF_SYNC_WRITE (2 << 0)
|
|
|
|
#define DMA_BUF_SYNC_START (0 << 2)
|
|
|
|
#define DMA_BUF_SYNC_END (1 << 2)
|
|
|
|
#define DMA_BUF_BASE 'b'
|
|
|
|
#define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
|
|
|
|
|
|
|
|
static void SyncDmaBuf(int aFd, uint64_t aFlags) {
|
|
|
|
struct dma_buf_sync sync = {0};
|
|
|
|
|
|
|
|
sync.flags = aFlags | DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE;
|
|
|
|
while (true) {
|
|
|
|
int ret;
|
|
|
|
ret = ioctl(aFd, DMA_BUF_IOCTL_SYNC, &sync);
|
|
|
|
if (ret == -1 && errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
} else if (ret == -1) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(
|
|
|
|
("Failed to synchronize DMA buffer: %s FD %d", strerror(errno), aFd));
|
2021-03-10 01:56:40 +03:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void* DMABufSurface::MapInternal(uint32_t aX, uint32_t aY, uint32_t aWidth,
|
|
|
|
uint32_t aHeight, uint32_t* aStride,
|
|
|
|
int aGbmFlags, int aPlane) {
|
2020-06-13 21:38:31 +03:00
|
|
|
NS_ASSERTION(!IsMapped(aPlane), "Already mapped!");
|
|
|
|
if (!mGbmBufferObject[aPlane]) {
|
2020-06-21 16:59:24 +03:00
|
|
|
NS_WARNING("We can't map DMABufSurfaceRGBA without mGbmBufferObject");
|
2020-03-06 13:40:39 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-09-01 10:24:46 +03:00
|
|
|
LOGDMABUF(
|
2021-03-10 01:56:40 +03:00
|
|
|
("DMABufSurfaceRGBA::MapInternal() UID %d plane %d size %d x %d -> %d x "
|
|
|
|
"%d\n",
|
|
|
|
mUID, aPlane, aX, aY, aWidth, aHeight));
|
2020-09-01 10:24:46 +03:00
|
|
|
|
2020-06-13 21:38:31 +03:00
|
|
|
mMappedRegionStride[aPlane] = 0;
|
|
|
|
mMappedRegionData[aPlane] = nullptr;
|
|
|
|
mMappedRegion[aPlane] = nsGbmLib::Map(
|
|
|
|
mGbmBufferObject[aPlane], aX, aY, aWidth, aHeight, aGbmFlags,
|
|
|
|
&mMappedRegionStride[aPlane], &mMappedRegionData[aPlane]);
|
2021-03-10 01:56:40 +03:00
|
|
|
if (!mMappedRegion[aPlane]) {
|
|
|
|
LOGDMABUF((" Surface mapping failed: %s", strerror(errno)));
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-09-03 12:41:32 +03:00
|
|
|
if (aStride) {
|
2020-06-13 21:38:31 +03:00
|
|
|
*aStride = mMappedRegionStride[aPlane];
|
2019-09-03 12:41:32 +03:00
|
|
|
}
|
2021-03-10 01:56:40 +03:00
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
MutexAutoLock lockFD(mSurfaceLock);
|
2021-07-12 23:12:00 +03:00
|
|
|
if (OpenFileDescriptorForPlane(lockFD, aPlane)) {
|
2021-03-16 10:57:41 +03:00
|
|
|
SyncDmaBuf(mDmabufFds[aPlane], DMA_BUF_SYNC_START);
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptorForPlane(lockFD, aPlane);
|
2021-03-16 10:57:41 +03:00
|
|
|
}
|
2021-03-10 01:56:40 +03:00
|
|
|
|
2020-06-13 21:38:31 +03:00
|
|
|
return mMappedRegion[aPlane];
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void* DMABufSurfaceRGBA::MapReadOnly(uint32_t aX, uint32_t aY, uint32_t aWidth,
|
|
|
|
uint32_t aHeight, uint32_t* aStride) {
|
2020-01-14 16:13:41 +03:00
|
|
|
return MapInternal(aX, aY, aWidth, aHeight, aStride, GBM_BO_TRANSFER_READ);
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void* DMABufSurfaceRGBA::MapReadOnly(uint32_t* aStride) {
|
2020-01-14 16:13:41 +03:00
|
|
|
return MapInternal(0, 0, mWidth, mHeight, aStride, GBM_BO_TRANSFER_READ);
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void* DMABufSurfaceRGBA::Map(uint32_t aX, uint32_t aY, uint32_t aWidth,
|
|
|
|
uint32_t aHeight, uint32_t* aStride) {
|
2020-01-14 16:13:41 +03:00
|
|
|
return MapInternal(aX, aY, aWidth, aHeight, aStride,
|
|
|
|
GBM_BO_TRANSFER_READ_WRITE);
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void* DMABufSurfaceRGBA::Map(uint32_t* aStride) {
|
2020-01-14 16:13:41 +03:00
|
|
|
return MapInternal(0, 0, mWidth, mHeight, aStride,
|
|
|
|
GBM_BO_TRANSFER_READ_WRITE);
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurface::Unmap(int aPlane) {
|
2020-06-13 21:38:31 +03:00
|
|
|
if (mMappedRegion[aPlane]) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurface::Unmap() UID %d plane %d\n", mUID, aPlane));
|
|
|
|
MutexAutoLock lockFD(mSurfaceLock);
|
2021-07-12 23:12:00 +03:00
|
|
|
if (OpenFileDescriptorForPlane(lockFD, aPlane)) {
|
2021-03-16 10:57:41 +03:00
|
|
|
SyncDmaBuf(mDmabufFds[aPlane], DMA_BUF_SYNC_END);
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptorForPlane(lockFD, aPlane);
|
2021-03-16 10:57:41 +03:00
|
|
|
}
|
2020-06-13 21:38:31 +03:00
|
|
|
nsGbmLib::Unmap(mGbmBufferObject[aPlane], mMappedRegionData[aPlane]);
|
|
|
|
mMappedRegion[aPlane] = nullptr;
|
|
|
|
mMappedRegionData[aPlane] = nullptr;
|
|
|
|
mMappedRegionStride[aPlane] = 0;
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 10:24:46 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
void DMABufSurfaceRGBA::DumpToFile(const char* pFile) {
|
|
|
|
uint32_t stride;
|
|
|
|
|
|
|
|
if (!MapReadOnly(&stride)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cairo_surface_t* surface = nullptr;
|
|
|
|
|
|
|
|
auto unmap = MakeScopeExit([&] {
|
|
|
|
if (surface) {
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
Unmap();
|
|
|
|
});
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create_for_data(
|
|
|
|
(unsigned char*)mMappedRegion[0], CAIRO_FORMAT_ARGB32, mWidth, mHeight,
|
|
|
|
stride);
|
|
|
|
if (cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS) {
|
|
|
|
cairo_surface_write_to_png(surface, pFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-13 01:34:27 +03:00
|
|
|
#if 0
|
|
|
|
// Copy from source surface by GL
|
|
|
|
# include "GLBlitHelper.h"
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceRGBA::CopyFrom(class DMABufSurface* aSourceSurface,
|
2021-07-15 19:19:51 +03:00
|
|
|
GLContext* aGLContext) {
|
2020-02-13 01:34:27 +03:00
|
|
|
MOZ_ASSERT(aSourceSurface->GetTexture());
|
|
|
|
MOZ_ASSERT(GetTexture());
|
|
|
|
|
|
|
|
gfx::IntSize size(GetWidth(), GetHeight());
|
|
|
|
aGLContext->BlitHelper()->BlitTextureToTexture(aSourceSurface->GetTexture(),
|
|
|
|
GetTexture(), size, size);
|
|
|
|
return true;
|
2019-06-04 14:11:28 +03:00
|
|
|
}
|
2020-02-13 01:34:27 +03:00
|
|
|
#endif
|
2019-06-04 14:11:28 +03:00
|
|
|
|
|
|
|
// TODO - Clear the surface by EGL
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurfaceRGBA::Clear() {
|
2019-06-04 14:11:28 +03:00
|
|
|
uint32_t destStride;
|
|
|
|
void* destData = Map(&destStride);
|
|
|
|
memset(destData, 0, GetHeight() * destStride);
|
|
|
|
Unmap();
|
|
|
|
}
|
2019-09-03 12:41:32 +03:00
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceRGBA::HasAlpha() {
|
2021-07-15 19:19:51 +03:00
|
|
|
return !mGmbFormat || mGmbFormat->mHasAlpha;
|
2019-09-03 12:41:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
gfx::SurfaceFormat DMABufSurfaceRGBA::GetFormat() {
|
2019-09-03 12:41:32 +03:00
|
|
|
return HasAlpha() ? gfx::SurfaceFormat::B8G8R8A8
|
|
|
|
: gfx::SurfaceFormat::B8G8R8X8;
|
|
|
|
}
|
2019-09-24 14:30:16 +03:00
|
|
|
|
2020-02-13 01:34:27 +03:00
|
|
|
// GL uses swapped R and B components so report accordingly.
|
2020-06-21 16:59:24 +03:00
|
|
|
gfx::SurfaceFormat DMABufSurfaceRGBA::GetFormatGL() {
|
2020-02-13 01:34:27 +03:00
|
|
|
return HasAlpha() ? gfx::SurfaceFormat::R8G8B8A8
|
|
|
|
: gfx::SurfaceFormat::R8G8B8X8;
|
2019-09-24 14:30:16 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
already_AddRefed<DMABufSurfaceRGBA> DMABufSurfaceRGBA::CreateDMABufSurface(
|
|
|
|
int aWidth, int aHeight, int aDMABufSurfaceFlags) {
|
|
|
|
RefPtr<DMABufSurfaceRGBA> surf = new DMABufSurfaceRGBA();
|
|
|
|
if (!surf->Create(aWidth, aHeight, aDMABufSurfaceFlags)) {
|
2020-02-13 01:34:27 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return surf.forget();
|
|
|
|
}
|
2020-02-13 01:34:35 +03:00
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
already_AddRefed<DMABufSurfaceYUV> DMABufSurfaceYUV::CreateYUVSurface(
|
2020-02-19 23:25:28 +03:00
|
|
|
const VADRMPRIMESurfaceDescriptor& aDesc) {
|
2020-06-21 16:59:24 +03:00
|
|
|
RefPtr<DMABufSurfaceYUV> surf = new DMABufSurfaceYUV();
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::CreateYUVSurface() UID %d from desc\n",
|
|
|
|
surf->GetUID()));
|
2020-06-21 16:59:24 +03:00
|
|
|
if (!surf->UpdateYUVData(aDesc)) {
|
2020-06-13 21:38:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
already_AddRefed<DMABufSurfaceYUV> DMABufSurfaceYUV::CreateYUVSurface(
|
|
|
|
int aWidth, int aHeight, void** aPixelData, int* aLineSizes) {
|
|
|
|
RefPtr<DMABufSurfaceYUV> surf = new DMABufSurfaceYUV();
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::CreateYUVSurface() UID %d %d x %d\n",
|
|
|
|
surf->GetUID(), aWidth, aHeight));
|
2020-06-13 21:38:31 +03:00
|
|
|
if (!surf->Create(aWidth, aHeight, aPixelData, aLineSizes)) {
|
2020-02-19 23:25:28 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
DMABufSurfaceYUV::DMABufSurfaceYUV()
|
2021-03-19 03:58:23 +03:00
|
|
|
: DMABufSurface(SURFACE_NV12), mWidth(), mHeight(), mTexture() {
|
2020-02-13 01:34:35 +03:00
|
|
|
for (int i = 0; i < DMABUF_BUFFER_PLANES; i++) {
|
|
|
|
mEGLImage[i] = LOCAL_EGL_NO_IMAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
DMABufSurfaceYUV::~DMABufSurfaceYUV() { ReleaseSurface(); }
|
2020-02-13 01:34:35 +03:00
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
bool DMABufSurfaceYUV::OpenFileDescriptorForPlane(
|
|
|
|
const MutexAutoLock& aProofOfLock, int aPlane) {
|
2021-03-17 17:30:14 +03:00
|
|
|
// The fd is already opened, no need to reopen.
|
|
|
|
// This can happen when we import dmabuf surface from VA-API decoder,
|
|
|
|
// mGbmBufferObject is null and we don't close
|
|
|
|
// file descriptors for surface as they are our only reference to it.
|
2021-03-16 10:57:41 +03:00
|
|
|
if (mDmabufFds[aPlane] >= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-18 12:51:47 +03:00
|
|
|
|
2021-07-15 19:19:51 +03:00
|
|
|
if (mGbmBufferObject[aPlane] == nullptr) {
|
|
|
|
LOGDMABUF(
|
|
|
|
("DMABufSurfaceYUV::OpenFileDescriptorForPlane: Missing "
|
|
|
|
"mGbmBufferObject object!"));
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-18 12:51:47 +03:00
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
mDmabufFds[aPlane] = nsGbmLib::GetFd(mGbmBufferObject[aPlane]);
|
|
|
|
if (mDmabufFds[aPlane] < 0) {
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptors(aProofOfLock);
|
2021-03-16 10:57:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
void DMABufSurfaceYUV::CloseFileDescriptorForPlane(
|
|
|
|
const MutexAutoLock& aProofOfLock, int aPlane, bool aForceClose = false) {
|
2021-03-16 10:57:41 +03:00
|
|
|
if ((aForceClose || mGbmBufferObject[aPlane]) && mDmabufFds[aPlane] >= 0) {
|
|
|
|
close(mDmabufFds[aPlane]);
|
|
|
|
mDmabufFds[aPlane] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceYUV::UpdateYUVData(const VADRMPRIMESurfaceDescriptor& aDesc) {
|
2020-02-13 01:35:09 +03:00
|
|
|
if (aDesc.num_layers > DMABUF_BUFFER_PLANES ||
|
|
|
|
aDesc.num_objects > DMABUF_BUFFER_PLANES) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-16 10:57:41 +03:00
|
|
|
|
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::UpdateYUVData() UID %d", mUID));
|
2020-06-13 21:38:31 +03:00
|
|
|
if (mDmabufFds[0] >= 0) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" Already created!"));
|
2020-08-06 14:12:45 +03:00
|
|
|
return false;
|
2020-06-13 21:38:31 +03:00
|
|
|
}
|
2020-07-30 21:52:32 +03:00
|
|
|
if (aDesc.fourcc == VA_FOURCC_NV12) {
|
|
|
|
mSurfaceType = SURFACE_NV12;
|
2022-01-21 14:16:43 +03:00
|
|
|
} else if (aDesc.fourcc == VA_FOURCC_P010) {
|
|
|
|
mSurfaceType = SURFACE_NV12;
|
2020-07-30 21:52:32 +03:00
|
|
|
} else if (aDesc.fourcc == VA_FOURCC_YV12) {
|
|
|
|
mSurfaceType = SURFACE_YUV420;
|
|
|
|
} else {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("UpdateYUVData(): Can't import surface data of 0x%x format",
|
|
|
|
aDesc.fourcc));
|
2020-07-30 21:52:32 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-13 01:34:35 +03:00
|
|
|
|
|
|
|
mBufferPlaneCount = aDesc.num_layers;
|
|
|
|
mBufferModifier = aDesc.objects[0].drm_format_modifier;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < aDesc.num_layers; i++) {
|
|
|
|
// Intel exports VA-API surfaces in one object,planes have the same FD.
|
|
|
|
// AMD exports surfaces in two objects with different FDs.
|
|
|
|
bool dupFD = (aDesc.layers[i].object_index[0] != i);
|
|
|
|
int fd = aDesc.objects[aDesc.layers[i].object_index[0]].fd;
|
|
|
|
mDmabufFds[i] = dupFD ? dup(fd) : fd;
|
|
|
|
|
|
|
|
mDrmFormats[i] = aDesc.layers[i].drm_format;
|
|
|
|
mOffsets[i] = aDesc.layers[i].offset[0];
|
|
|
|
mStrides[i] = aDesc.layers[i].pitch[0];
|
|
|
|
mWidth[i] = aDesc.width >> i;
|
|
|
|
mHeight[i] = aDesc.height >> i;
|
2021-03-16 10:57:41 +03:00
|
|
|
|
2021-06-16 23:14:25 +03:00
|
|
|
LOGDMABUF((" plane %d size %d x %d format %x", i, mWidth[i], mHeight[i],
|
2021-03-16 10:57:41 +03:00
|
|
|
mDrmFormats[i]));
|
2020-02-13 01:34:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-26 16:02:56 +03:00
|
|
|
bool DMABufSurfaceYUV::CreateYUVPlane(int aPlane, int aWidth, int aHeight,
|
|
|
|
int aDrmFormat) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::CreateYUVPlane() UID %d size %d x %d", mUID,
|
|
|
|
aWidth, aHeight));
|
|
|
|
|
2020-06-13 21:38:31 +03:00
|
|
|
mWidth[aPlane] = aWidth;
|
|
|
|
mHeight[aPlane] = aHeight;
|
|
|
|
mDrmFormats[aPlane] = aDrmFormat;
|
|
|
|
|
|
|
|
mGbmBufferObject[aPlane] =
|
2020-06-23 09:27:09 +03:00
|
|
|
nsGbmLib::Create(GetDMABufDevice()->GetGbmDevice(), aWidth, aHeight,
|
2021-03-10 01:56:40 +03:00
|
|
|
aDrmFormat, GBM_BO_USE_LINEAR);
|
2020-06-13 21:38:31 +03:00
|
|
|
if (!mGbmBufferObject[aPlane]) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" Failed to create GbmBufferObject: %s", strerror(errno)));
|
2020-06-13 21:38:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStrides[aPlane] = nsGbmLib::GetStride(mGbmBufferObject[aPlane]);
|
2021-03-16 10:57:41 +03:00
|
|
|
mDmabufFds[aPlane] = -1;
|
2020-06-13 21:38:31 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-19 09:14:09 +03:00
|
|
|
void DMABufSurfaceYUV::UpdateYUVPlane(int aPlane, void* aPixelData,
|
|
|
|
int aLineSize) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(
|
|
|
|
("DMABufSurfaceYUV::UpdateYUVPlane() UID %d plane %d", mUID, aPlane));
|
2020-08-19 09:14:09 +03:00
|
|
|
if (aLineSize == mWidth[aPlane] &&
|
|
|
|
(int)mMappedRegionStride[aPlane] == mWidth[aPlane]) {
|
|
|
|
memcpy(mMappedRegion[aPlane], aPixelData, aLineSize * mHeight[aPlane]);
|
|
|
|
} else {
|
|
|
|
char* src = (char*)aPixelData;
|
|
|
|
char* dest = (char*)mMappedRegion[aPlane];
|
|
|
|
for (int i = 0; i < mHeight[aPlane]; i++) {
|
|
|
|
memcpy(dest, src, mWidth[aPlane]);
|
|
|
|
src += aLineSize;
|
|
|
|
dest += mMappedRegionStride[aPlane];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceYUV::UpdateYUVData(void** aPixelData, int* aLineSizes) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::UpdateYUVData() UID %d", mUID));
|
2020-07-30 21:52:32 +03:00
|
|
|
if (mSurfaceType != SURFACE_YUV420) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" UpdateYUVData can upload YUV420 surface type only!"));
|
2020-07-30 21:52:32 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-19 09:14:09 +03:00
|
|
|
if (mBufferPlaneCount != 3) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" DMABufSurfaceYUV planes does not match!"));
|
2020-06-13 21:38:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto unmapBuffers = MakeScopeExit([&] {
|
|
|
|
Unmap(0);
|
|
|
|
Unmap(1);
|
2020-07-30 21:52:32 +03:00
|
|
|
Unmap(2);
|
2020-06-13 21:38:31 +03:00
|
|
|
});
|
|
|
|
|
2020-08-19 09:14:09 +03:00
|
|
|
// Map planes
|
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
MapInternal(0, 0, mWidth[i], mHeight[i], nullptr, GBM_BO_TRANSFER_WRITE, i);
|
|
|
|
if (!mMappedRegion[i]) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" DMABufSurfaceYUV plane can't be mapped!"));
|
2020-08-19 09:14:09 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((int)mMappedRegionStride[i] < mWidth[i]) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" DMABufSurfaceYUV plane size stride does not match!"));
|
2020-08-19 09:14:09 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-30 21:52:32 +03:00
|
|
|
}
|
2020-06-13 21:38:31 +03:00
|
|
|
|
2020-07-30 21:52:32 +03:00
|
|
|
// Copy planes
|
2020-08-19 09:14:09 +03:00
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
UpdateYUVPlane(i, aPixelData[i], aLineSizes[i]);
|
|
|
|
}
|
2020-06-13 21:38:31 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceYUV::Create(int aWidth, int aHeight, void** aPixelData,
|
|
|
|
int* aLineSizes) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::Create() UID %d size %d x %d", mUID, aWidth,
|
|
|
|
aHeight));
|
|
|
|
|
2020-07-30 21:52:32 +03:00
|
|
|
mSurfaceType = SURFACE_YUV420;
|
|
|
|
mBufferPlaneCount = 3;
|
2020-06-13 21:38:31 +03:00
|
|
|
|
2020-07-26 16:02:56 +03:00
|
|
|
if (!CreateYUVPlane(0, aWidth, aHeight, GBM_FORMAT_R8)) {
|
2020-06-13 21:38:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-30 21:52:32 +03:00
|
|
|
if (!CreateYUVPlane(1, aWidth >> 1, aHeight >> 1, GBM_FORMAT_R8)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!CreateYUVPlane(2, aWidth >> 1, aHeight >> 1, GBM_FORMAT_R8)) {
|
2020-06-13 21:38:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-15 19:19:51 +03:00
|
|
|
if (!aPixelData || !aLineSizes) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return UpdateYUVData(aPixelData, aLineSizes);
|
2020-06-13 21:38:31 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceYUV::Create(const SurfaceDescriptor& aDesc) {
|
2021-03-16 10:57:41 +03:00
|
|
|
return ImportSurfaceDescriptor(aDesc);
|
2020-02-13 01:34:35 +03:00
|
|
|
}
|
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
bool DMABufSurfaceYUV::ImportSurfaceDescriptor(
|
2020-02-13 01:34:35 +03:00
|
|
|
const SurfaceDescriptorDMABuf& aDesc) {
|
|
|
|
mBufferPlaneCount = aDesc.fds().Length();
|
2020-07-30 21:52:32 +03:00
|
|
|
mSurfaceType = (mBufferPlaneCount == 2) ? SURFACE_NV12 : SURFACE_YUV420;
|
2020-02-13 01:34:35 +03:00
|
|
|
mBufferModifier = aDesc.modifier();
|
|
|
|
mColorSpace = aDesc.yUVColorSpace();
|
2021-12-02 12:32:45 +03:00
|
|
|
mColorRange = aDesc.colorRange();
|
2020-05-29 18:21:50 +03:00
|
|
|
mUID = aDesc.uid();
|
2020-02-13 01:34:35 +03:00
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::ImportSurfaceDescriptor() UID %d", mUID));
|
|
|
|
|
2020-02-13 01:35:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mBufferPlaneCount <= DMABUF_BUFFER_PLANES);
|
2020-02-13 01:34:35 +03:00
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
mDmabufFds[i] = aDesc.fds()[i].ClonePlatformHandle().release();
|
2021-03-16 10:57:41 +03:00
|
|
|
if (mDmabufFds[i] < 0) {
|
|
|
|
LOGDMABUF((" failed to get DMABuf plane file descriptor: %s",
|
|
|
|
strerror(errno)));
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-13 01:34:35 +03:00
|
|
|
mWidth[i] = aDesc.width()[i];
|
|
|
|
mHeight[i] = aDesc.height()[i];
|
|
|
|
mDrmFormats[i] = aDesc.format()[i];
|
|
|
|
mStrides[i] = aDesc.strides()[i];
|
|
|
|
mOffsets[i] = aDesc.offsets()[i];
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" plane %d fd %d size %d x %d format %x", i, mDmabufFds[i],
|
|
|
|
mWidth[i], mHeight[i], mDrmFormats[i]));
|
2020-02-13 01:34:35 +03:00
|
|
|
}
|
2020-02-19 18:13:28 +03:00
|
|
|
|
2020-02-25 16:48:39 +03:00
|
|
|
if (aDesc.fence().Length() > 0) {
|
2020-07-24 11:54:37 +03:00
|
|
|
mSyncFd = aDesc.fence()[0].ClonePlatformHandle().release();
|
2021-03-16 10:57:41 +03:00
|
|
|
if (mSyncFd < 0) {
|
|
|
|
LOGDMABUF(
|
|
|
|
(" failed to get GL fence file descriptor: %s", strerror(errno)));
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-19 18:13:28 +03:00
|
|
|
}
|
2020-05-29 18:21:50 +03:00
|
|
|
|
|
|
|
if (aDesc.refCount().Length() > 0) {
|
|
|
|
GlobalRefCountImport(aDesc.refCount()[0].ClonePlatformHandle().release());
|
|
|
|
}
|
2021-03-16 10:57:41 +03:00
|
|
|
|
|
|
|
return true;
|
2020-02-13 01:34:35 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceYUV::Serialize(
|
2020-02-13 01:34:35 +03:00
|
|
|
mozilla::layers::SurfaceDescriptor& aOutDescriptor) {
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> width;
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> height;
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> format;
|
|
|
|
AutoTArray<ipc::FileDescriptor, DMABUF_BUFFER_PLANES> fds;
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> strides;
|
|
|
|
AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> offsets;
|
2020-02-25 16:48:39 +03:00
|
|
|
AutoTArray<ipc::FileDescriptor, 1> fenceFDs;
|
2020-05-29 18:21:50 +03:00
|
|
|
AutoTArray<ipc::FileDescriptor, 1> refCountFDs;
|
2020-02-13 01:34:35 +03:00
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::Serialize() UID %d", mUID));
|
|
|
|
|
|
|
|
MutexAutoLock lockFD(mSurfaceLock);
|
2021-07-12 23:12:00 +03:00
|
|
|
if (!OpenFileDescriptors(lockFD)) {
|
2021-03-16 10:57:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-13 01:34:35 +03:00
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
width.AppendElement(mWidth[i]);
|
|
|
|
height.AppendElement(mHeight[i]);
|
|
|
|
format.AppendElement(mDrmFormats[i]);
|
|
|
|
fds.AppendElement(ipc::FileDescriptor(mDmabufFds[i]));
|
|
|
|
strides.AppendElement(mStrides[i]);
|
|
|
|
offsets.AppendElement(mOffsets[i]);
|
|
|
|
}
|
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptors(lockFD);
|
2021-03-16 10:57:41 +03:00
|
|
|
|
2020-02-19 18:13:28 +03:00
|
|
|
if (mSync) {
|
2020-08-05 10:45:12 +03:00
|
|
|
fenceFDs.AppendElement(ipc::FileDescriptor(mSyncFd));
|
2020-02-19 18:13:28 +03:00
|
|
|
}
|
2020-02-13 01:34:35 +03:00
|
|
|
|
2020-05-29 18:21:50 +03:00
|
|
|
if (mGlobalRefCountFd) {
|
|
|
|
refCountFDs.AppendElement(ipc::FileDescriptor(mGlobalRefCountFd));
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:25:28 +03:00
|
|
|
aOutDescriptor = SurfaceDescriptorDMABuf(
|
|
|
|
mSurfaceType, mBufferModifier, 0, fds, width, height, format, strides,
|
2021-12-02 12:32:45 +03:00
|
|
|
offsets, GetYUVColorSpace(), mColorRange, fenceFDs, mUID, refCountFDs);
|
2020-02-13 01:34:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
bool DMABufSurfaceYUV::CreateTexture(GLContext* aGLContext, int aPlane) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(
|
|
|
|
("DMABufSurfaceYUV::CreateTexture() UID %d plane %d", mUID, aPlane));
|
2020-02-13 01:34:35 +03:00
|
|
|
MOZ_ASSERT(!mEGLImage[aPlane] && !mTexture[aPlane],
|
|
|
|
"EGLImage/Texture is already created!");
|
|
|
|
|
2020-08-07 23:50:04 +03:00
|
|
|
if (!aGLContext) return false;
|
|
|
|
const auto& gle = gl::GLContextEGL::Cast(aGLContext);
|
2020-08-07 10:14:46 +03:00
|
|
|
const auto& egl = gle->mEgl;
|
|
|
|
|
2021-03-16 10:57:41 +03:00
|
|
|
MutexAutoLock lockFD(mSurfaceLock);
|
2021-07-12 23:12:00 +03:00
|
|
|
if (!OpenFileDescriptorForPlane(lockFD, aPlane)) {
|
2021-03-16 10:57:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-13 01:34:35 +03:00
|
|
|
nsTArray<EGLint> attribs;
|
|
|
|
attribs.AppendElement(LOCAL_EGL_WIDTH);
|
|
|
|
attribs.AppendElement(mWidth[aPlane]);
|
|
|
|
attribs.AppendElement(LOCAL_EGL_HEIGHT);
|
|
|
|
attribs.AppendElement(mHeight[aPlane]);
|
|
|
|
attribs.AppendElement(LOCAL_EGL_LINUX_DRM_FOURCC_EXT);
|
|
|
|
attribs.AppendElement(mDrmFormats[aPlane]);
|
2020-02-19 23:25:28 +03:00
|
|
|
#define ADD_PLANE_ATTRIBS_NV12(plane_idx) \
|
|
|
|
attribs.AppendElement(LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_FD_EXT); \
|
|
|
|
attribs.AppendElement(mDmabufFds[aPlane]); \
|
|
|
|
attribs.AppendElement(LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_OFFSET_EXT); \
|
|
|
|
attribs.AppendElement((int)mOffsets[aPlane]); \
|
|
|
|
attribs.AppendElement(LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_PITCH_EXT); \
|
2021-11-23 11:27:48 +03:00
|
|
|
attribs.AppendElement((int)mStrides[aPlane]); \
|
|
|
|
if (mBufferModifier != DRM_FORMAT_MOD_INVALID) { \
|
|
|
|
attribs.AppendElement( \
|
|
|
|
LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_MODIFIER_LO_EXT); \
|
|
|
|
attribs.AppendElement(mBufferModifier & 0xFFFFFFFF); \
|
|
|
|
attribs.AppendElement( \
|
|
|
|
LOCAL_EGL_DMA_BUF_PLANE##plane_idx##_MODIFIER_HI_EXT); \
|
|
|
|
attribs.AppendElement(mBufferModifier >> 32); \
|
|
|
|
}
|
2020-02-19 23:25:28 +03:00
|
|
|
ADD_PLANE_ATTRIBS_NV12(0);
|
2020-02-13 01:34:35 +03:00
|
|
|
#undef ADD_PLANE_ATTRIBS_NV12
|
|
|
|
attribs.AppendElement(LOCAL_EGL_NONE);
|
|
|
|
|
2020-08-07 10:14:46 +03:00
|
|
|
mEGLImage[aPlane] =
|
|
|
|
egl->fCreateImage(LOCAL_EGL_NO_CONTEXT, LOCAL_EGL_LINUX_DMA_BUF_EXT,
|
|
|
|
nullptr, attribs.Elements());
|
2021-03-16 10:57:41 +03:00
|
|
|
|
2021-07-12 23:12:00 +03:00
|
|
|
CloseFileDescriptorForPlane(lockFD, aPlane);
|
2021-03-16 10:57:41 +03:00
|
|
|
|
2020-02-13 01:34:35 +03:00
|
|
|
if (mEGLImage[aPlane] == LOCAL_EGL_NO_IMAGE) {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF((" EGLImageKHR creation failed"));
|
2020-02-13 01:34:35 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aGLContext->MakeCurrent();
|
|
|
|
aGLContext->fGenTextures(1, &mTexture[aPlane]);
|
2020-09-01 10:24:46 +03:00
|
|
|
const ScopedBindTexture savedTex(aGLContext, mTexture[aPlane]);
|
2020-02-13 01:34:35 +03:00
|
|
|
aGLContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
aGLContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
aGLContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MAG_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
aGLContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MIN_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
aGLContext->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, mEGLImage[aPlane]);
|
|
|
|
mGL = aGLContext;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurfaceYUV::ReleaseTextures() {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::ReleaseTextures() UID %d", mUID));
|
|
|
|
|
2020-02-19 18:13:28 +03:00
|
|
|
FenceDelete();
|
|
|
|
|
2020-02-19 23:25:28 +03:00
|
|
|
bool textureActive = false;
|
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
if (mTexture[i]) {
|
|
|
|
textureActive = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:19:51 +03:00
|
|
|
if (!mGL) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-07 10:14:46 +03:00
|
|
|
const auto& gle = gl::GLContextEGL::Cast(mGL);
|
|
|
|
const auto& egl = gle->mEgl;
|
|
|
|
|
2020-02-19 23:25:28 +03:00
|
|
|
if (textureActive && mGL->MakeCurrent()) {
|
|
|
|
mGL->fDeleteTextures(DMABUF_BUFFER_PLANES, mTexture);
|
|
|
|
for (int i = 0; i < DMABUF_BUFFER_PLANES; i++) {
|
|
|
|
mTexture[i] = 0;
|
2020-02-13 01:34:35 +03:00
|
|
|
}
|
|
|
|
mGL = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mBufferPlaneCount; i++) {
|
|
|
|
if (mEGLImage[i]) {
|
2020-08-07 10:14:46 +03:00
|
|
|
egl->fDestroyImage(mEGLImage[i]);
|
2020-02-13 01:34:35 +03:00
|
|
|
mEGLImage[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
gfx::SurfaceFormat DMABufSurfaceYUV::GetFormat() {
|
2020-07-30 21:52:32 +03:00
|
|
|
switch (mSurfaceType) {
|
|
|
|
case SURFACE_NV12:
|
|
|
|
return gfx::SurfaceFormat::NV12;
|
|
|
|
case SURFACE_YUV420:
|
|
|
|
return gfx::SurfaceFormat::YUV;
|
|
|
|
default:
|
|
|
|
NS_WARNING("DMABufSurfaceYUV::GetFormat(): Wrong surface format!");
|
|
|
|
return gfx::SurfaceFormat::UNKNOWN;
|
|
|
|
}
|
2020-02-13 01:34:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GL uses swapped R and B components so report accordingly.
|
2020-07-30 21:52:32 +03:00
|
|
|
gfx::SurfaceFormat DMABufSurfaceYUV::GetFormatGL() { return GetFormat(); }
|
|
|
|
|
2021-07-15 19:19:51 +03:00
|
|
|
int DMABufSurfaceYUV::GetTextureCount() {
|
2020-07-30 21:52:32 +03:00
|
|
|
switch (mSurfaceType) {
|
|
|
|
case SURFACE_NV12:
|
|
|
|
return 2;
|
|
|
|
case SURFACE_YUV420:
|
|
|
|
return 3;
|
|
|
|
default:
|
|
|
|
NS_WARNING("DMABufSurfaceYUV::GetTextureCount(): Wrong surface format!");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-02-13 01:34:35 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:59:24 +03:00
|
|
|
void DMABufSurfaceYUV::ReleaseSurface() {
|
2021-03-16 10:57:41 +03:00
|
|
|
LOGDMABUF(("DMABufSurfaceYUV::ReleaseSurface() UID %d", mUID));
|
2020-02-13 01:34:35 +03:00
|
|
|
ReleaseTextures();
|
2020-06-13 21:38:31 +03:00
|
|
|
ReleaseDMABuf();
|
2020-02-13 01:34:35 +03:00
|
|
|
}
|