2010-10-27 08:56:31 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2010-10-27 08:56:31 +04:00
|
|
|
|
|
|
|
#include "nsShmImage.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_HAVE_SHMIMAGE
|
2016-02-18 18:56:15 +03:00
|
|
|
# include "mozilla/X11Util.h"
|
2016-08-16 03:44:33 +03:00
|
|
|
# include "mozilla/gfx/gfxVars.h"
|
2016-02-18 18:56:15 +03:00
|
|
|
# include "mozilla/ipc/SharedMemory.h"
|
2016-03-01 22:33:53 +03:00
|
|
|
# include "gfxPlatform.h"
|
2016-02-28 07:16:57 +03:00
|
|
|
# include "nsPrintfCString.h"
|
|
|
|
# include "nsTArray.h"
|
2019-01-18 12:16:18 +03:00
|
|
|
|
2017-09-22 07:10:35 +03:00
|
|
|
# include <dlfcn.h>
|
2016-02-18 18:56:15 +03:00
|
|
|
# include <errno.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <sys/ipc.h>
|
|
|
|
# include <sys/shm.h>
|
2010-10-27 08:56:31 +04:00
|
|
|
|
2017-02-21 18:00:52 +03:00
|
|
|
extern "C" {
|
|
|
|
# include <X11/ImUtil.h>
|
|
|
|
}
|
|
|
|
|
2010-11-05 10:17:07 +03:00
|
|
|
using namespace mozilla::ipc;
|
2015-09-15 23:46:39 +03:00
|
|
|
using namespace mozilla::gfx;
|
2010-11-05 10:17:07 +03:00
|
|
|
|
2016-02-28 07:16:57 +03:00
|
|
|
nsShmImage::nsShmImage(Display* aDisplay, Drawable aWindow, Visual* aVisual,
|
|
|
|
unsigned int aDepth)
|
2017-02-21 18:00:52 +03:00
|
|
|
: mDisplay(aDisplay),
|
|
|
|
mConnection(XGetXCBConnection(aDisplay)),
|
|
|
|
mWindow(aWindow),
|
2016-02-28 07:16:57 +03:00
|
|
|
mVisual(aVisual),
|
|
|
|
mDepth(aDepth),
|
|
|
|
mFormat(mozilla::gfx::SurfaceFormat::UNKNOWN),
|
2016-07-13 23:28:28 +03:00
|
|
|
mSize(0, 0),
|
2017-02-21 18:00:52 +03:00
|
|
|
mStride(0),
|
2016-07-13 23:28:28 +03:00
|
|
|
mPixmap(XCB_NONE),
|
|
|
|
mGC(XCB_NONE),
|
2016-07-21 20:41:09 +03:00
|
|
|
mRequestPending(false),
|
2016-07-13 23:28:28 +03:00
|
|
|
mShmSeg(XCB_NONE),
|
|
|
|
mShmId(-1),
|
|
|
|
mShmAddr(nullptr) {
|
2016-07-21 20:41:09 +03:00
|
|
|
mozilla::PodZero(&mSyncRequest);
|
2016-02-28 07:16:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsShmImage::~nsShmImage() { DestroyImage(); }
|
|
|
|
|
2010-10-27 08:56:31 +04:00
|
|
|
// If XShm isn't available to our client, we'll try XShm once, fail,
|
|
|
|
// set this to false and then never try again.
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gShmAvailable = true;
|
|
|
|
bool nsShmImage::UseShm() { return gShmAvailable; }
|
2010-10-27 08:56:31 +04:00
|
|
|
|
2016-02-18 18:56:15 +03:00
|
|
|
bool nsShmImage::CreateShmSegment() {
|
2017-02-21 18:00:52 +03:00
|
|
|
size_t size = SharedMemory::PageAlignedSize(mStride * mSize.height);
|
2016-02-18 18:56:15 +03:00
|
|
|
|
2018-08-22 12:29:00 +03:00
|
|
|
# if defined(__OpenBSD__) && defined(MOZ_SANDBOX)
|
|
|
|
static mozilla::LazyLogModule sPledgeLog("SandboxPledge");
|
|
|
|
MOZ_LOG(sPledgeLog, mozilla::LogLevel::Debug,
|
|
|
|
("%s called when pledged, returning false\n", __func__));
|
|
|
|
return false;
|
|
|
|
# endif
|
2016-07-13 23:28:28 +03:00
|
|
|
mShmId = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
|
|
|
|
if (mShmId == -1) {
|
2016-02-18 18:56:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-13 23:28:28 +03:00
|
|
|
mShmAddr = (uint8_t*)shmat(mShmId, nullptr, 0);
|
|
|
|
mShmSeg = xcb_generate_id(mConnection);
|
2016-02-25 22:38:05 +03:00
|
|
|
|
|
|
|
// Mark the handle removed so that it will destroy the segment when unmapped.
|
2016-07-13 23:28:28 +03:00
|
|
|
shmctl(mShmId, IPC_RMID, nullptr);
|
2016-02-25 22:38:05 +03:00
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
if (mShmAddr == (void*)-1) {
|
2016-02-25 22:38:05 +03:00
|
|
|
// Since mapping failed, the segment is already destroyed.
|
2016-07-13 23:28:28 +03:00
|
|
|
mShmId = -1;
|
2016-02-25 22:38:05 +03:00
|
|
|
|
2016-02-18 18:56:15 +03:00
|
|
|
nsPrintfCString warning("shmat(): %s (%d)\n", strerror(errno), errno);
|
|
|
|
NS_WARNING(warning.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef DEBUG
|
|
|
|
struct shmid_ds info;
|
2016-07-13 23:28:28 +03:00
|
|
|
if (shmctl(mShmId, IPC_STAT, &info) < 0) {
|
2016-02-18 18:56:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(size <= info.shm_segsz, "Segment doesn't have enough space!");
|
|
|
|
# endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsShmImage::DestroyShmSegment() {
|
2016-07-13 23:28:28 +03:00
|
|
|
if (mShmId != -1) {
|
|
|
|
shmdt(mShmAddr);
|
|
|
|
mShmId = -1;
|
2016-02-18 18:56:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 07:16:57 +03:00
|
|
|
static bool gShmInitialized = false;
|
2016-07-13 23:28:28 +03:00
|
|
|
static bool gUseShmPixmaps = false;
|
2016-02-28 07:16:57 +03:00
|
|
|
|
|
|
|
bool nsShmImage::InitExtension() {
|
|
|
|
if (gShmInitialized) {
|
|
|
|
return gShmAvailable;
|
|
|
|
}
|
|
|
|
|
|
|
|
gShmInitialized = true;
|
|
|
|
|
2017-09-22 07:10:35 +03:00
|
|
|
// Bugs 1397918, 1293474 - race condition in libxcb fixed upstream as of
|
|
|
|
// version 1.11. Since we can't query libxcb's version directly, the only
|
|
|
|
// other option is to check for symbols that were added after 1.11.
|
|
|
|
// xcb_discard_reply64 was added in 1.11.1, so check for existence of
|
|
|
|
// that to verify we are using a version of libxcb with the bug fixed.
|
|
|
|
// Otherwise, we can't risk using libxcb due to aforementioned crashes.
|
|
|
|
if (!dlsym(RTLD_DEFAULT, "xcb_discard_reply64")) {
|
|
|
|
gShmAvailable = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
const xcb_query_extension_reply_t* extReply;
|
|
|
|
extReply = xcb_get_extension_data(mConnection, &xcb_shm_id);
|
|
|
|
if (!extReply || !extReply->present) {
|
2016-02-28 07:16:57 +03:00
|
|
|
gShmAvailable = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
xcb_shm_query_version_reply_t* shmReply = xcb_shm_query_version_reply(
|
|
|
|
mConnection, xcb_shm_query_version(mConnection), nullptr);
|
2016-02-28 07:16:57 +03:00
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
if (!shmReply) {
|
2016-02-28 07:16:57 +03:00
|
|
|
gShmAvailable = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
gUseShmPixmaps = shmReply->shared_pixmaps &&
|
|
|
|
shmReply->pixmap_format == XCB_IMAGE_FORMAT_Z_PIXMAP;
|
|
|
|
|
|
|
|
free(shmReply);
|
2016-02-28 07:16:57 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-25 22:38:05 +03:00
|
|
|
bool nsShmImage::CreateImage(const IntSize& aSize) {
|
2016-07-13 23:28:28 +03:00
|
|
|
MOZ_ASSERT(mConnection && mVisual);
|
2010-10-27 08:56:31 +04:00
|
|
|
|
2016-02-28 07:16:57 +03:00
|
|
|
if (!InitExtension()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
mSize = aSize;
|
|
|
|
|
2016-08-16 03:44:33 +03:00
|
|
|
BackendType backend = gfxVars::ContentBackend();
|
2016-03-08 19:18:13 +03:00
|
|
|
|
2016-02-18 18:56:15 +03:00
|
|
|
mFormat = SurfaceFormat::UNKNOWN;
|
2016-02-25 22:38:05 +03:00
|
|
|
switch (mDepth) {
|
2016-02-18 18:56:15 +03:00
|
|
|
case 32:
|
2016-02-25 22:38:05 +03:00
|
|
|
if (mVisual->red_mask == 0xff0000 && mVisual->green_mask == 0xff00 &&
|
|
|
|
mVisual->blue_mask == 0xff) {
|
2016-02-18 18:56:15 +03:00
|
|
|
mFormat = SurfaceFormat::B8G8R8A8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 24:
|
2016-02-20 00:14:02 +03:00
|
|
|
// Only support the BGRX layout, and report it as BGRA to the compositor.
|
|
|
|
// The alpha channel will be discarded when we put the image.
|
2016-03-08 19:18:13 +03:00
|
|
|
// Cairo/pixman lacks some fast paths for compositing BGRX onto BGRA, so
|
|
|
|
// just report it as BGRX directly in that case.
|
2016-02-25 22:38:05 +03:00
|
|
|
if (mVisual->red_mask == 0xff0000 && mVisual->green_mask == 0xff00 &&
|
|
|
|
mVisual->blue_mask == 0xff) {
|
2016-03-08 19:18:13 +03:00
|
|
|
mFormat = backend == BackendType::CAIRO ? SurfaceFormat::B8G8R8X8
|
|
|
|
: SurfaceFormat::B8G8R8A8;
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
2016-02-18 18:56:15 +03:00
|
|
|
break;
|
|
|
|
case 16:
|
2016-02-28 07:16:57 +03:00
|
|
|
if (mVisual->red_mask == 0xf800 && mVisual->green_mask == 0x07e0 &&
|
|
|
|
mVisual->blue_mask == 0x1f) {
|
|
|
|
mFormat = SurfaceFormat::R5G6B5_UINT16;
|
|
|
|
}
|
2016-02-18 18:56:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFormat == SurfaceFormat::UNKNOWN) {
|
|
|
|
NS_WARNING("Unsupported XShm Image format!");
|
|
|
|
gShmAvailable = false;
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-27 08:56:31 +04:00
|
|
|
|
2017-02-21 18:00:52 +03:00
|
|
|
// Round up stride to the display's scanline pad (in bits) as XShm expects.
|
|
|
|
int scanlinePad = _XGetScanlinePad(mDisplay, mDepth);
|
|
|
|
int bitsPerPixel = _XGetBitsPerPixel(mDisplay, mDepth);
|
|
|
|
int bitsPerLine =
|
|
|
|
((bitsPerPixel * aSize.width + scanlinePad - 1) / scanlinePad) *
|
|
|
|
scanlinePad;
|
|
|
|
mStride = bitsPerLine / 8;
|
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
if (!CreateShmSegment()) {
|
2016-02-25 22:38:05 +03:00
|
|
|
DestroyImage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
xcb_generic_error_t* error;
|
|
|
|
xcb_void_cookie_t cookie;
|
2016-02-25 22:38:05 +03:00
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
cookie = xcb_shm_attach_checked(mConnection, mShmSeg, mShmId, 0);
|
2016-02-25 22:38:05 +03:00
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
if ((error = xcb_request_check(mConnection, cookie))) {
|
|
|
|
NS_WARNING("Failed to attach MIT-SHM segment.");
|
|
|
|
DestroyImage();
|
2016-02-25 22:38:05 +03:00
|
|
|
gShmAvailable = false;
|
2016-07-13 23:28:28 +03:00
|
|
|
free(error);
|
2016-02-25 22:38:05 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
if (gUseShmPixmaps) {
|
|
|
|
mPixmap = xcb_generate_id(mConnection);
|
|
|
|
cookie = xcb_shm_create_pixmap_checked(mConnection, mPixmap, mWindow,
|
|
|
|
aSize.width, aSize.height, mDepth,
|
|
|
|
mShmSeg, 0);
|
|
|
|
|
|
|
|
if ((error = xcb_request_check(mConnection, cookie))) {
|
|
|
|
// Disable shared pixmaps permanently if creation failed.
|
|
|
|
mPixmap = XCB_NONE;
|
|
|
|
gUseShmPixmaps = false;
|
|
|
|
free(error);
|
|
|
|
}
|
2016-02-28 07:16:57 +03:00
|
|
|
}
|
|
|
|
|
2016-02-18 18:56:15 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-25 22:38:05 +03:00
|
|
|
void nsShmImage::DestroyImage() {
|
2016-02-28 07:16:57 +03:00
|
|
|
if (mGC) {
|
2016-07-13 23:28:28 +03:00
|
|
|
xcb_free_gc(mConnection, mGC);
|
|
|
|
mGC = XCB_NONE;
|
2016-02-28 07:16:57 +03:00
|
|
|
}
|
2016-07-13 23:28:28 +03:00
|
|
|
if (mPixmap != XCB_NONE) {
|
|
|
|
xcb_free_pixmap(mConnection, mPixmap);
|
|
|
|
mPixmap = XCB_NONE;
|
2016-02-28 07:16:57 +03:00
|
|
|
}
|
2016-07-13 23:28:28 +03:00
|
|
|
if (mShmSeg != XCB_NONE) {
|
|
|
|
xcb_shm_detach_checked(mConnection, mShmSeg);
|
|
|
|
mShmSeg = XCB_NONE;
|
2016-02-18 18:56:15 +03:00
|
|
|
}
|
|
|
|
DestroyShmSegment();
|
2016-11-08 22:56:27 +03:00
|
|
|
// Avoid leaking any pending reply. No real need to wait but CentOS 6 build
|
|
|
|
// machines don't have xcb_discard_reply().
|
|
|
|
WaitIfPendingReply();
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2016-11-08 22:56:27 +03:00
|
|
|
// Wait for any in-flight shm-affected requests to complete.
|
|
|
|
// Typically X clients would wait for a XShmCompletionEvent to be received,
|
|
|
|
// but this works as it's sent immediately after the request is sent.
|
|
|
|
void nsShmImage::WaitIfPendingReply() {
|
2016-07-21 20:41:09 +03:00
|
|
|
if (mRequestPending) {
|
2016-11-08 22:56:27 +03:00
|
|
|
xcb_get_input_focus_reply_t* reply =
|
|
|
|
xcb_get_input_focus_reply(mConnection, mSyncRequest, nullptr);
|
|
|
|
free(reply);
|
2016-07-21 20:41:09 +03:00
|
|
|
mRequestPending = false;
|
2016-07-13 23:28:28 +03:00
|
|
|
}
|
2016-11-08 22:56:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DrawTarget> nsShmImage::CreateDrawTarget(
|
|
|
|
const mozilla::LayoutDeviceIntRegion& aRegion) {
|
|
|
|
WaitIfPendingReply();
|
2016-02-28 07:16:57 +03:00
|
|
|
|
2016-02-25 22:38:05 +03:00
|
|
|
// Due to bug 1205045, we must avoid making GTK calls off the main thread to
|
|
|
|
// query window size. Instead we just track the largest offset within the
|
|
|
|
// image we are drawing to and grow the image to accomodate it. Since usually
|
|
|
|
// the entire window is invalidated on the first paint to it, this should grow
|
|
|
|
// the image to the necessary size quickly without many intermediate
|
|
|
|
// reallocations.
|
|
|
|
IntRect bounds = aRegion.GetBounds().ToUnknownRect();
|
|
|
|
IntSize size(bounds.XMost(), bounds.YMost());
|
2016-07-13 23:28:28 +03:00
|
|
|
if (size.width > mSize.width || size.height > mSize.height) {
|
2016-02-25 22:38:05 +03:00
|
|
|
DestroyImage();
|
|
|
|
if (!CreateImage(size)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 00:54:08 +03:00
|
|
|
return gfxPlatform::CreateDrawTargetForData(
|
2016-07-13 23:28:28 +03:00
|
|
|
reinterpret_cast<unsigned char*>(mShmAddr) + bounds.y * mStride +
|
2017-02-21 18:00:52 +03:00
|
|
|
bounds.x * BytesPerPixel(mFormat),
|
2016-02-25 22:38:05 +03:00
|
|
|
bounds.Size(), mStride, mFormat);
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2016-02-28 07:16:57 +03:00
|
|
|
void nsShmImage::Put(const mozilla::LayoutDeviceIntRegion& aRegion) {
|
2016-07-13 23:28:28 +03:00
|
|
|
AutoTArray<xcb_rectangle_t, 32> xrects;
|
2016-02-28 07:16:57 +03:00
|
|
|
xrects.SetCapacity(aRegion.GetNumRects());
|
|
|
|
|
|
|
|
for (auto iter = aRegion.RectIter(); !iter.Done(); iter.Next()) {
|
|
|
|
const mozilla::LayoutDeviceIntRect& r = iter.Get();
|
2016-07-13 23:28:28 +03:00
|
|
|
xcb_rectangle_t xrect = {(short)r.x, (short)r.y, (unsigned short)r.width,
|
|
|
|
(unsigned short)r.height};
|
2016-02-28 07:16:57 +03:00
|
|
|
xrects.AppendElement(xrect);
|
2016-02-18 18:56:15 +03:00
|
|
|
}
|
2016-02-25 22:38:05 +03:00
|
|
|
|
2016-02-28 07:16:57 +03:00
|
|
|
if (!mGC) {
|
2016-07-13 23:28:28 +03:00
|
|
|
mGC = xcb_generate_id(mConnection);
|
|
|
|
xcb_create_gc(mConnection, mGC, mWindow, 0, nullptr);
|
2016-02-28 07:16:57 +03:00
|
|
|
}
|
2016-02-28 07:16:57 +03:00
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
xcb_set_clip_rectangles(mConnection, XCB_CLIP_ORDERING_YX_BANDED, mGC, 0, 0,
|
|
|
|
xrects.Length(), xrects.Elements());
|
|
|
|
|
|
|
|
if (mPixmap != XCB_NONE) {
|
2016-11-04 03:57:08 +03:00
|
|
|
xcb_copy_area(mConnection, mPixmap, mWindow, mGC, 0, 0, 0, 0, mSize.width,
|
|
|
|
mSize.height);
|
2016-02-28 07:16:57 +03:00
|
|
|
} else {
|
2016-11-04 03:57:08 +03:00
|
|
|
xcb_shm_put_image(mConnection, mWindow, mGC, mSize.width, mSize.height, 0,
|
|
|
|
0, mSize.width, mSize.height, 0, 0, mDepth,
|
|
|
|
XCB_IMAGE_FORMAT_Z_PIXMAP, 0, mShmSeg, 0);
|
2016-02-28 07:16:57 +03:00
|
|
|
}
|
2016-02-25 22:38:05 +03:00
|
|
|
|
2016-07-21 20:41:09 +03:00
|
|
|
// Send a request that returns a response so that we don't have to start a
|
2016-11-04 03:57:08 +03:00
|
|
|
// sync in nsShmImage::CreateDrawTarget.
|
2016-07-21 20:41:09 +03:00
|
|
|
mSyncRequest = xcb_get_input_focus(mConnection);
|
|
|
|
mRequestPending = true;
|
|
|
|
|
2016-07-13 23:28:28 +03:00
|
|
|
xcb_flush(mConnection);
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2016-02-18 18:56:15 +03:00
|
|
|
#endif // MOZ_HAVE_SHMIMAGE
|