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"
|
|
|
|
#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"
|
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
|
|
|
|
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)
|
2016-07-13 23:28:28 +03:00
|
|
|
: 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)
|
|
|
|
, mPixmap(XCB_NONE)
|
|
|
|
, mGC(XCB_NONE)
|
|
|
|
, mShmSeg(XCB_NONE)
|
|
|
|
, mShmId(-1)
|
|
|
|
, mShmAddr(nullptr)
|
2016-02-28 07:16:57 +03:00
|
|
|
{
|
2016-07-13 23:28:28 +03:00
|
|
|
mConnection = XGetXCBConnection(aDisplay);
|
|
|
|
mozilla::PodZero(&mLastRequest);
|
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()
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
2016-02-25 22:38:05 +03:00
|
|
|
return gShmAvailable;
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2016-02-18 18:56:15 +03:00
|
|
|
bool
|
|
|
|
nsShmImage::CreateShmSegment()
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
2016-07-13 23:28:28 +03:00
|
|
|
size_t size = SharedMemory::PageAlignedSize(BytesPerPixel(mFormat) *
|
|
|
|
mSize.width * mSize.height);
|
2016-02-18 18:56:15 +03:00
|
|
|
|
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;
|
|
|
|
|
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-18 18:56:15 +03:00
|
|
|
bool
|
2016-02-25 22:38:05 +03:00
|
|
|
nsShmImage::CreateImage(const IntSize& aSize)
|
2016-02-18 18:56:15 +03:00
|
|
|
{
|
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-03-08 19:18:13 +03:00
|
|
|
BackendType backend = gfxPlatform::GetPlatform()->GetDefaultContentBackend();
|
|
|
|
|
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
|
|
|
|
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-18 18:56:15 +03:00
|
|
|
{
|
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();
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2015-09-15 23:46:39 +03:00
|
|
|
already_AddRefed<DrawTarget>
|
2016-02-28 07:16:57 +03:00
|
|
|
nsShmImage::CreateDrawTarget(const mozilla::LayoutDeviceIntRegion& aRegion)
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
2016-07-13 23:28:28 +03:00
|
|
|
// Wait for any in-flight 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 processed.
|
|
|
|
xcb_generic_error_t* error;
|
|
|
|
if (mLastRequest.sequence != XCB_NONE &&
|
|
|
|
(error = xcb_request_check(mConnection, mLastRequest)))
|
|
|
|
{
|
|
|
|
gShmAvailable = false;
|
|
|
|
free(error);
|
|
|
|
return nullptr;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 23:46:39 +03:00
|
|
|
return gfxPlatform::GetPlatform()->CreateDrawTargetForData(
|
2016-07-13 23:28:28 +03:00
|
|
|
reinterpret_cast<unsigned char*>(mShmAddr)
|
|
|
|
+ BytesPerPixel(mFormat) * (bounds.y * mSize.width + bounds.x),
|
2016-02-25 22:38:05 +03:00
|
|
|
bounds.Size(),
|
2016-07-13 23:28:28 +03:00
|
|
|
BytesPerPixel(mFormat) * mSize.width,
|
2015-09-15 23:46:39 +03:00
|
|
|
mFormat);
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2016-02-28 07:16:57 +03:00
|
|
|
void
|
|
|
|
nsShmImage::Put(const mozilla::LayoutDeviceIntRegion& aRegion)
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
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) {
|
|
|
|
mLastRequest = xcb_copy_area_checked(mConnection, mPixmap, mWindow, mGC,
|
|
|
|
0, 0, 0, 0, mSize.width, mSize.height);
|
2016-02-28 07:16:57 +03:00
|
|
|
} else {
|
2016-07-13 23:28:28 +03:00
|
|
|
mLastRequest = xcb_shm_put_image_checked(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-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
|