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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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 "WaylandBuffer.h"
#include "WaylandSurface.h"
#include "WaylandSurfaceLock.h"
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include "gfx2DGlue.h"
#include "gfxPlatform.h"
#include "mozilla/WidgetUtilsGtk.h"
#include "mozilla/gfx/Tools.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/ipc/SharedMemoryHandle.h"
#include "nsGtkUtils.h"
#include "nsPrintfCString.h"
#include "prenv.h" // For PR_GetEnv
#ifdef MOZ_LOGGING
# include "mozilla/Logging.h"
# include "mozilla/ScopeExit.h"
# include "Units.h"
extern mozilla::LazyLogModule gWidgetWaylandLog;
# define LOGWAYLAND(...) \
MOZ_LOG(gWidgetWaylandLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
#else
# define LOGWAYLAND(...)
#endif /* MOZ_LOGGING */
using namespace mozilla::gl;
namespace mozilla::widget {
#define BUFFER_BPP 4
#ifdef MOZ_LOGGING
MOZ_RUNINIT int WaylandBuffer::mDumpSerial =
PR_GetEnv("MOZ_WAYLAND_DUMP_WL_BUFFERS") ? 1 : 0;
MOZ_RUNINIT char* WaylandBuffer::mDumpDir = PR_GetEnv("MOZ_WAYLAND_DUMP_DIR");
#endif
/* static */
RefPtr<WaylandShmPool> WaylandShmPool::Create(nsWaylandDisplay* aWaylandDisplay,
int aSize) {
if (!aWaylandDisplay->GetShm()) {
NS_WARNING("WaylandShmPool: Missing Wayland shm interface!");
return nullptr;
}
RefPtr<WaylandShmPool> shmPool = new WaylandShmPool();
auto handle = ipc::shared_memory::Create(aSize);
if (!handle) {
NS_WARNING("WaylandShmPool: Unable to allocate shared memory!");
return nullptr;
}
shmPool->mShmHandle = handle.Clone();
shmPool->mShmPool =
wl_shm_create_pool(aWaylandDisplay->GetShm(),
handle.Clone().TakePlatformHandle().get(), aSize);
if (!shmPool->mShmPool) {
NS_WARNING("WaylandShmPool: Unable to allocate shared memory pool!");
return nullptr;
}
return shmPool;
}
void* WaylandShmPool::GetImageData() {
if (!mShm) {
mShm = mShmHandle.Map();
if (!mShm) {
NS_WARNING("WaylandShmPool: Failed to map Shm!");
return nullptr;
}
}
return mShm.Address();
}
WaylandShmPool::~WaylandShmPool() {
MozClearPointer(mShmPool, wl_shm_pool_destroy);
}
WaylandBuffer::WaylandBuffer(const LayoutDeviceIntSize& aSize) : mSize(aSize) {}
bool WaylandBuffer::IsAttached() const {
for (const auto& transaction : mBufferTransactions) {
if (transaction->IsAttached()) {
return true;
}
}
return false;
}
BufferTransaction* WaylandBuffer::GetTransaction(
const WaylandSurfaceLock& aSurfaceLock) {
for (const auto& transaction : mBufferTransactions) {
if (transaction->CanRecycle(aSurfaceLock.GetWaylandSurface())) {
LOGWAYLAND("WaylandBuffer::GetTransaction() [%p] reuse transaction [%d]",
(void*)this, (int)mBufferTransactions.Length());
return transaction;
}
}
wl_buffer* buffer = mExternalWlBuffer;
if (!buffer) {
buffer = CreateWlBuffer();
}
if (!buffer) {
gfxCriticalError()
<< "WaylandBuffer::GetTransaction() failed to create wl_buffer!";
return nullptr;
}
LOGWAYLAND(
"WaylandBuffer::GetTransaction() create new [%p] wl_buffer [%p] "
"transactions [%d] external buffer [%d] WaylandSurface [%p]",
(void*)this, buffer, (int)mBufferTransactions.Length(),
!!mExternalWlBuffer, aSurfaceLock.GetWaylandSurface());
auto* transaction = new BufferTransaction(this, buffer, !!mExternalWlBuffer);
mBufferTransactions.AppendElement(transaction);
return transaction;
}
void WaylandBuffer::RemoveTransaction(RefPtr<BufferTransaction> aTransaction) {
LOGWAYLAND("WaylandBuffer::RemoveTransaction() [%p]", (void*)aTransaction);
[[maybe_unused]] bool removed =
mBufferTransactions.RemoveElement(aTransaction);
MOZ_DIAGNOSTIC_ASSERT(removed);
MOZ_DIAGNOSTIC_ASSERT(!mBufferTransactions.Contains(aTransaction));
}
void WaylandBuffer::SetExternalWLBuffer(wl_buffer* aWLBuffer) {
LOGWAYLAND("WaylandBuffer::SetExternalWLBuffer() [%p] wl_buffer %p",
(void*)this, aWLBuffer);
MOZ_DIAGNOSTIC_ASSERT(!mExternalWlBuffer);
mExternalWlBuffer = aWLBuffer;
}
/* static */
RefPtr<WaylandBufferSHM> WaylandBufferSHM::Create(
const LayoutDeviceIntSize& aSize) {
RefPtr<WaylandBufferSHM> buffer = new WaylandBufferSHM(aSize);
nsWaylandDisplay* waylandDisplay = WaylandDisplayGet();
LOGWAYLAND("WaylandBufferSHM::Create() [%p] [%d x %d]", (void*)buffer,
aSize.width, aSize.height);
int size = aSize.width * aSize.height * BUFFER_BPP;
buffer->mShmPool = WaylandShmPool::Create(waylandDisplay, size);
if (!buffer->mShmPool) {
LOGWAYLAND(" failed to create shmPool");
return nullptr;
}
LOGWAYLAND(" created [%p] WaylandDisplay [%p]\n", buffer.get(),
waylandDisplay);
return buffer;
}
wl_buffer* WaylandBufferSHM::CreateWlBuffer() {
MOZ_DIAGNOSTIC_ASSERT(!mExternalWlBuffer);
auto* buffer = wl_shm_pool_create_buffer(
mShmPool->GetShmPool(), 0, mSize.width, mSize.height,
mSize.width * BUFFER_BPP, WL_SHM_FORMAT_ARGB8888);
LOGWAYLAND("WaylandBufferSHM::CreateWlBuffer() [%p] wl_buffer [%p]",
(void*)this, buffer);
return buffer;
}
WaylandBufferSHM::WaylandBufferSHM(const LayoutDeviceIntSize& aSize)
: WaylandBuffer(aSize) {
LOGWAYLAND("WaylandBufferSHM::WaylandBufferSHM() [%p]\n", (void*)this);
}
WaylandBufferSHM::~WaylandBufferSHM() {
LOGWAYLAND("WaylandBufferSHM::~WaylandBufferSHM() [%p]\n", (void*)this);
MOZ_RELEASE_ASSERT(!IsAttached());
}
already_AddRefed<gfx::DrawTarget> WaylandBufferSHM::Lock() {
LOGWAYLAND("WaylandBufferSHM::lock() [%p]\n", (void*)this);
return gfxPlatform::CreateDrawTargetForData(
static_cast<unsigned char*>(mShmPool->GetImageData()),
mSize.ToUnknownSize(), BUFFER_BPP * mSize.width, GetSurfaceFormat());
}
void WaylandBufferSHM::Clear() {
LOGWAYLAND("WaylandBufferSHM::Clear() [%p]\n", (void*)this);
memset(mShmPool->GetImageData(), 0xff,
mSize.height * mSize.width * BUFFER_BPP);
}
#ifdef MOZ_LOGGING
void WaylandBufferSHM::DumpToFile(const char* aHint) {
if (!mDumpSerial) {
return;
}
cairo_surface_t* surface = nullptr;
auto unmap = MakeScopeExit([&] {
if (surface) {
cairo_surface_destroy(surface);
}
});
surface = cairo_image_surface_create_for_data(
(unsigned char*)mShmPool->GetImageData(), CAIRO_FORMAT_ARGB32,
mSize.width, mSize.height, BUFFER_BPP * mSize.width);
if (cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS) {
nsCString filename;
if (mDumpDir) {
filename.Append(mDumpDir);
filename.Append('/');
}
filename.Append(nsPrintfCString("firefox-wl-sw-buffer-%.5d-%s.png",
mDumpSerial++, aHint));
cairo_surface_write_to_png(surface, filename.get());
LOGWAYLAND("Dumped wl_buffer to %s\n", filename.get());
}
}
#endif
/* static */
already_AddRefed<WaylandBufferDMABUF> WaylandBufferDMABUF::CreateRGBA(
const LayoutDeviceIntSize& aSize, GLContext* aGL,
RefPtr<DRMFormat> aFormat) {
RefPtr<WaylandBufferDMABUF> buffer = new WaylandBufferDMABUF(aSize);
buffer->mDMABufSurface = DMABufSurfaceRGBA::CreateDMABufSurface(
aGL, aSize.width, aSize.height, DMABUF_SCANOUT | DMABUF_USE_MODIFIERS,
aFormat);
if (!buffer->mDMABufSurface || !buffer->mDMABufSurface->CreateTexture(aGL)) {
LOGWAYLAND(" failed to create texture");
return nullptr;
}
LOGWAYLAND("WaylandBufferDMABUF::CreateRGBA() [%p] UID %d [%d x %d]",
(void*)buffer, buffer->mDMABufSurface->GetUID(), aSize.width,
aSize.height);
return buffer.forget();
}
/* static */
already_AddRefed<WaylandBufferDMABUF> WaylandBufferDMABUF::CreateExternal(
RefPtr<DMABufSurface> aSurface) {
const auto size =
LayoutDeviceIntSize(aSurface->GetWidth(), aSurface->GetHeight());
RefPtr<WaylandBufferDMABUF> buffer = new WaylandBufferDMABUF(size);
LOGWAYLAND("WaylandBufferDMABUF::CreateExternal() [%p] UID %d [%d x %d]",
(void*)buffer, aSurface->GetUID(), size.width, size.height);
buffer->mDMABufSurface = aSurface;
return buffer.forget();
}
wl_buffer* WaylandBufferDMABUF::CreateWlBuffer() {
MOZ_DIAGNOSTIC_ASSERT(mDMABufSurface);
MOZ_DIAGNOSTIC_ASSERT(!mExternalWlBuffer);
auto* buffer = mDMABufSurface->CreateWlBuffer();
LOGWAYLAND("WaylandBufferDMABUF::CreateWlBuffer() [%p] UID %d wl_buffer [%p]",
(void*)this, mDMABufSurface->GetUID(), buffer);
return buffer;
}
WaylandBufferDMABUF::WaylandBufferDMABUF(const LayoutDeviceIntSize& aSize)
: WaylandBuffer(aSize) {
LOGWAYLAND("WaylandBufferDMABUF::WaylandBufferDMABUF [%p]\n", (void*)this);
}
WaylandBufferDMABUF::~WaylandBufferDMABUF() {
LOGWAYLAND("WaylandBufferDMABUF::~WaylandBufferDMABUF [%p] UID %d\n",
(void*)this, mDMABufSurface ? mDMABufSurface->GetUID() : -1);
MOZ_RELEASE_ASSERT(!IsAttached());
}
#ifdef MOZ_LOGGING
void WaylandBufferDMABUF::DumpToFile(const char* aHint) {
if (!mDumpSerial) {
return;
}
nsCString filename;
if (mDumpDir) {
filename.Append(mDumpDir);
filename.Append('/');
}
filename.AppendPrintf("firefox-wl-buffer-dmabuf-%.5d-%s.png", mDumpSerial++,
aHint);
mDMABufSurface->DumpToFile(filename.get());
LOGWAYLAND("Dumped wl_buffer to %s\n", filename.get());
}
#endif
WaylandBufferDMABUFHolder::WaylandBufferDMABUFHolder(DMABufSurface* aSurface,
wl_buffer* aWLBuffer)
: mWLBuffer(aWLBuffer) {
mUID = aSurface->GetUID();
mPID = aSurface->GetPID();
LOGWAYLAND(
"WaylandBufferDMABUFHolder::WaylandBufferDMABUFHolder wl_buffer [%p] UID "
"%d PID %d",
mWLBuffer, mUID, mPID);
}
WaylandBufferDMABUFHolder::~WaylandBufferDMABUFHolder() {
LOGWAYLAND(
"WaylandBufferDMABUFHolder::~WaylandBufferDMABUFHolder wl_buffer [%p] "
"UID %d PID %d",
mWLBuffer, mUID, mPID);
MozClearPointer(mWLBuffer, wl_buffer_destroy);
}
bool WaylandBufferDMABUFHolder::Matches(DMABufSurface* aSurface) const {
return mUID == aSurface->GetUID() && mPID == aSurface->GetPID();
}
wl_buffer* BufferTransaction::BufferBorrowLocked(
const WaylandSurfaceLock& aSurfaceLock) {
LOGWAYLAND(
"BufferTransaction::BufferBorrow() [%p] widget [%p] WaylandSurface [%p] "
"WaylandBuffer [%p]",
this, aSurfaceLock.GetWaylandSurface()->GetLoggingWidget(),
aSurfaceLock.GetWaylandSurface(), mBuffer.get());
MOZ_DIAGNOSTIC_ASSERT(
!mSurface || mSurface == aSurfaceLock.GetWaylandSurface(),
"Can't transfer transaction between WaylandSurfaces!");
MOZ_DIAGNOSTIC_ASSERT(mBufferState == BufferState::Detached);
mSurface = aSurfaceLock.GetWaylandSurface();
// We don't take reference to this. Some compositors doesn't send
// buffer release callback and we may leak BufferTransaction then.
// Rather we destroy wl_buffer at end which makes sure no release callback
// comes after BufferTransaction release.
if (wl_proxy_get_listener((wl_proxy*)mWLBuffer)) {
wl_proxy_set_user_data((wl_proxy*)mWLBuffer, this);
} else {
static const struct wl_buffer_listener listener{
[](void* aData, wl_buffer* aBuffer) {
auto transaction = static_cast<BufferTransaction*>(aData);
if (transaction) {
// BufferDetachCallback can delete transaction
RefPtr grip{transaction};
transaction->BufferDetachCallback();
}
}};
if (wl_buffer_add_listener(mWLBuffer, &listener, this) < 0) {
gfxCriticalError() << "wl_buffer_add_listener() failed";
}
}
mBufferState = BufferState::WaitingForDetach;
return mWLBuffer;
}
void BufferTransaction::BufferDetachCallback() {
WaylandSurfaceLock lock(mSurface);
LOGWAYLAND(
"BufferTransaction::BufferDetach() [%p] WaylandBuffer [%p] attached to "
"WaylandSurface %d",
this, (void*)mBuffer, mSurface->IsBufferAttached(mBuffer));
if (mBufferState != BufferState::WaitingForDelete) {
mBufferState = BufferState::Detached;
// Delete this transaction if WaylandSurface uses different WaylandBuffer
// already, we don't need to keep it for recycling.
if (!mSurface->IsBufferAttached(mBuffer)) {
DeleteTransactionLocked(lock);
}
}
}
void BufferTransaction::BufferDeleteCallback() {
LOGWAYLAND("BufferTransaction::DeleteCallback() [%p] WaylandBuffer [%p] ",
this, (void*)mBuffer);
WaylandSurfaceLock lock(mSurface);
mBufferState = BufferState::Deleted;
DeleteLocked(lock);
}
void BufferTransaction::WlBufferDeleteLocked(
const WaylandSurfaceLock& aSurfaceLock) {
LOGWAYLAND(
"BufferTransaction::WlBufferDeleteLocked() [%p] WaylandBuffer [%p] ",
this, (void*)mBuffer);
MOZ_DIAGNOSTIC_ASSERT(mWLBuffer);
if (mIsExternalBuffer) {
wl_proxy_set_user_data((wl_proxy*)mWLBuffer, nullptr);
mWLBuffer = nullptr;
} else {
MozClearPointer(mWLBuffer, wl_buffer_destroy);
}
}
void BufferTransaction::DeleteTransactionLocked(
const WaylandSurfaceLock& aSurfaceLock) {
// It's possible that the transaction is already deleted. It happens
// if one WaylandBuffer is attached to WaylandSurface,
// then detached/deleted from WaylandSurface::UnmapLocked() where all buffers
// are removed and then attached to another WaylandSurface.
if (mBufferState == BufferState::WaitingForDelete ||
mBufferState == BufferState::Deleted) {
return;
}
LOGWAYLAND(
"BufferTransaction::BufferDelete() [%p] WaylandBuffer [%p] wl_buffer "
"[%p] "
"external %d state %d",
this, (void*)mBuffer, mWLBuffer, mIsExternalBuffer, (int)mBufferState);
WlBufferDeleteLocked(aSurfaceLock);
// wl_buffer is detached so we can't get any release event so
// delete the transaction now.
if (mBufferState == BufferState::Detached) {
mBufferState = BufferState::Deleted;
DeleteLocked(aSurfaceLock);
return;
}
mBufferState = BufferState::WaitingForDelete;
// There are various Wayland queues processed for every thread.
// It's possible that wl_buffer release event is pending in any
// queue while we already asked for wl_buffer delete.
// We need to finish wl_buffer removal when all events from this
// point are processed so we use sync callback.
//
// When wl_display_sync comes back to us (from main thread)
// we know all events are processed and there isn't any
// wl_buffer operation pending so we can safely release WaylandSurface
// and WaylandBuffer objects.
// We need to wait with WaylandMonitor
AddRef();
static const struct wl_callback_listener listener{
[](void* aData, struct wl_callback* callback, uint32_t time) {
RefPtr t = dont_AddRef(static_cast<BufferTransaction*>(aData));
t->BufferDeleteCallback();
}};
wl_callback_add_listener(wl_display_sync(WaylandDisplayGetWLDisplay()),
&listener, this);
}
void BufferTransaction::DeleteLocked(const WaylandSurfaceLock& aSurfaceLock) {
LOGWAYLAND("BufferTransaction::DeleteLocked() [%p] WaylandBuffer [%p]", this,
(void*)mBuffer);
MOZ_DIAGNOSTIC_ASSERT(mBufferState == BufferState::Deleted);
MOZ_DIAGNOSTIC_ASSERT(mSurface);
// Unlink from Surface
mSurface->RemoveTransactionLocked(aSurfaceLock, this);
mSurface = nullptr;
// This can destroy us
RefPtr grip{this};
mBuffer->RemoveTransaction(this);
mBuffer = nullptr;
}
BufferTransaction::BufferTransaction(WaylandBuffer* aBuffer,
wl_buffer* aWLBuffer,
bool aIsExternalBuffer)
: mBuffer(aBuffer),
mWLBuffer(aWLBuffer),
mIsExternalBuffer(aIsExternalBuffer) {
MOZ_COUNT_CTOR(BufferTransaction);
LOGWAYLAND(
"BufferTransaction::BufferTransaction() [%p] WaylandBuffer [%p] "
"wl_buffer [%p] external [%d]",
this, aBuffer, mWLBuffer, mIsExternalBuffer);
}
BufferTransaction::~BufferTransaction() {
MOZ_COUNT_DTOR(BufferTransaction);
LOGWAYLAND("BufferTransaction::~BufferTransaction() [%p] ", this);
MOZ_DIAGNOSTIC_ASSERT(mBufferState == BufferState::Deleted);
MOZ_DIAGNOSTIC_ASSERT(!mWLBuffer);
}
} // namespace mozilla::widget
|