2010-10-26 21:56:31 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07: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-26 21:56:31 -07:00
|
|
|
|
|
|
|
#include "nsShmImage.h"
|
2014-07-22 16:04:47 -07:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
|
|
|
#include "gfxPlatformGtk.h"
|
|
|
|
#endif
|
2010-10-26 21:56:31 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_HAVE_SHMIMAGE
|
2016-02-18 07:56:15 -08:00
|
|
|
#include "mozilla/X11Util.h"
|
|
|
|
|
|
|
|
#include "mozilla/ipc/SharedMemory.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
2010-10-26 21:56:31 -07:00
|
|
|
|
2010-11-05 00:17:07 -07:00
|
|
|
using namespace mozilla::ipc;
|
2015-09-15 13:46:39 -07:00
|
|
|
using namespace mozilla::gfx;
|
2010-11-05 00:17:07 -07:00
|
|
|
|
2010-10-26 21:56:31 -07: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-28 23:19:26 -07:00
|
|
|
static bool gShmAvailable = true;
|
|
|
|
bool nsShmImage::UseShm()
|
2010-10-26 21:56:31 -07:00
|
|
|
{
|
2014-07-22 16:04:47 -07:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
2016-02-25 11:38:05 -08:00
|
|
|
return (gShmAvailable && !gfxPlatformGtk::GetPlatform()->UseXRender());
|
2014-07-22 16:04:47 -07:00
|
|
|
#else
|
2016-02-25 11:38:05 -08:00
|
|
|
return gShmAvailable;
|
2014-07-22 16:04:47 -07:00
|
|
|
#endif
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
|
2015-09-15 13:46:39 -07:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
|
|
|
static int gShmError = 0;
|
|
|
|
|
|
|
|
static int
|
|
|
|
TrapShmError(Display* aDisplay, XErrorEvent* aEvent)
|
|
|
|
{
|
2016-02-25 11:38:05 -08:00
|
|
|
// store the error code and ignore the error
|
|
|
|
gShmError = aEvent->error_code;
|
|
|
|
return 0;
|
2015-09-15 13:46:39 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-18 07:56:15 -08:00
|
|
|
bool
|
|
|
|
nsShmImage::CreateShmSegment()
|
2010-10-26 21:56:31 -07:00
|
|
|
{
|
2016-02-18 07:56:15 -08:00
|
|
|
if (!mImage) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-26 21:56:31 -07:00
|
|
|
|
2016-02-18 07:56:15 -08:00
|
|
|
size_t size = SharedMemory::PageAlignedSize(mImage->bytes_per_line * mImage->height);
|
|
|
|
|
|
|
|
mInfo.shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
|
|
|
|
if (mInfo.shmid == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInfo.shmaddr = (char *)shmat(mInfo.shmid, nullptr, 0);
|
2016-02-25 11:38:05 -08:00
|
|
|
|
|
|
|
// Mark the handle removed so that it will destroy the segment when unmapped.
|
|
|
|
shmctl(mInfo.shmid, IPC_RMID, nullptr);
|
|
|
|
|
2016-02-18 07:56:15 -08:00
|
|
|
if (mInfo.shmaddr == (void *)-1) {
|
2016-02-25 11:38:05 -08:00
|
|
|
// Since mapping failed, the segment is already destroyed.
|
|
|
|
mInfo.shmid = -1;
|
|
|
|
|
2016-02-18 07:56:15 -08:00
|
|
|
nsPrintfCString warning("shmat(): %s (%d)\n", strerror(errno), errno);
|
|
|
|
NS_WARNING(warning.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
struct shmid_ds info;
|
|
|
|
if (shmctl(mInfo.shmid, IPC_STAT, &info) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(size <= info.shm_segsz,
|
|
|
|
"Segment doesn't have enough space!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mInfo.readOnly = False;
|
|
|
|
|
|
|
|
mImage->data = mInfo.shmaddr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsShmImage::DestroyShmSegment()
|
|
|
|
{
|
|
|
|
if (mInfo.shmid != -1) {
|
|
|
|
shmdt(mInfo.shmaddr);
|
|
|
|
mInfo.shmid = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-02-25 11:38:05 -08:00
|
|
|
nsShmImage::CreateImage(const IntSize& aSize)
|
2016-02-18 07:56:15 -08:00
|
|
|
{
|
2016-02-25 11:38:05 -08:00
|
|
|
MOZ_ASSERT(mDisplay && mVisual);
|
2010-10-26 21:56:31 -07:00
|
|
|
|
2016-02-18 07:56:15 -08:00
|
|
|
mFormat = SurfaceFormat::UNKNOWN;
|
2016-02-25 11:38:05 -08:00
|
|
|
switch (mDepth) {
|
2016-02-18 07:56:15 -08:00
|
|
|
case 32:
|
2016-02-25 11:38:05 -08:00
|
|
|
if (mVisual->red_mask == 0xff0000 &&
|
|
|
|
mVisual->green_mask == 0xff00 &&
|
|
|
|
mVisual->blue_mask == 0xff) {
|
2016-02-18 07:56:15 -08:00
|
|
|
mFormat = SurfaceFormat::B8G8R8A8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 24:
|
2016-02-19 13:14:02 -08: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-02-25 11:38:05 -08:00
|
|
|
if (mVisual->red_mask == 0xff0000 &&
|
|
|
|
mVisual->green_mask == 0xff00 &&
|
|
|
|
mVisual->blue_mask == 0xff) {
|
2016-02-19 13:14:02 -08:00
|
|
|
mFormat = SurfaceFormat::B8G8R8A8;
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
2016-02-18 07:56:15 -08:00
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
mFormat = SurfaceFormat::R5G6B5_UINT16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFormat == SurfaceFormat::UNKNOWN) {
|
|
|
|
NS_WARNING("Unsupported XShm Image format!");
|
|
|
|
gShmAvailable = false;
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-26 21:56:31 -07:00
|
|
|
|
2016-02-25 11:38:05 -08:00
|
|
|
mImage = XShmCreateImage(mDisplay, mVisual, mDepth,
|
|
|
|
ZPixmap, nullptr,
|
|
|
|
&mInfo,
|
|
|
|
aSize.width, aSize.height);
|
|
|
|
if (!mImage || !CreateShmSegment()) {
|
|
|
|
DestroyImage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GTK
|
|
|
|
gShmError = 0;
|
|
|
|
XErrorHandler previousHandler = XSetErrorHandler(TrapShmError);
|
|
|
|
Status attachOk = XShmAttach(mDisplay, &mInfo);
|
|
|
|
XSync(mDisplay, False);
|
|
|
|
XSetErrorHandler(previousHandler);
|
|
|
|
if (gShmError) {
|
|
|
|
attachOk = 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
Status attachOk = XShmAttach(mDisplay, &mInfo);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!attachOk) {
|
|
|
|
DestroyShmSegment();
|
|
|
|
DestroyImage();
|
|
|
|
|
|
|
|
// Assume XShm isn't available, and don't attempt to use it
|
|
|
|
// again.
|
|
|
|
gShmAvailable = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-18 07:56:15 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-25 11:38:05 -08:00
|
|
|
void
|
|
|
|
nsShmImage::DestroyImage()
|
2016-02-18 07:56:15 -08:00
|
|
|
{
|
|
|
|
if (mImage) {
|
|
|
|
mozilla::FinishX(mDisplay);
|
2016-02-25 11:38:05 -08:00
|
|
|
if (mInfo.shmid != -1) {
|
2016-02-18 07:56:15 -08:00
|
|
|
XShmDetach(mDisplay, &mInfo);
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
2016-02-18 07:56:15 -08:00
|
|
|
XDestroyImage(mImage);
|
2016-02-25 11:38:05 -08:00
|
|
|
mImage = nullptr;
|
2016-02-18 07:56:15 -08:00
|
|
|
}
|
|
|
|
DestroyShmSegment();
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
|
2015-09-15 13:46:39 -07:00
|
|
|
already_AddRefed<DrawTarget>
|
2016-02-25 11:38:05 -08:00
|
|
|
nsShmImage::CreateDrawTarget(const LayoutDeviceIntRegion& aRegion)
|
2010-10-26 21:56:31 -07:00
|
|
|
{
|
2016-02-25 11:38:05 -08: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());
|
|
|
|
if (!mImage || size.width > mImage->width || size.height > mImage->height) {
|
|
|
|
DestroyImage();
|
|
|
|
if (!CreateImage(size)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 13:46:39 -07:00
|
|
|
return gfxPlatform::GetPlatform()->CreateDrawTargetForData(
|
2016-02-25 11:38:05 -08:00
|
|
|
reinterpret_cast<unsigned char*>(mImage->data)
|
|
|
|
+ bounds.y * mImage->bytes_per_line + bounds.x * BytesPerPixel(mFormat),
|
|
|
|
bounds.Size(),
|
2015-09-15 13:46:39 -07:00
|
|
|
mImage->bytes_per_line,
|
|
|
|
mFormat);
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-25 11:38:05 -08:00
|
|
|
nsShmImage::Put(const LayoutDeviceIntRegion& aRegion)
|
2010-10-26 21:56:31 -07:00
|
|
|
{
|
2016-02-25 11:38:05 -08:00
|
|
|
if (!mImage) {
|
|
|
|
return;
|
|
|
|
}
|
2011-09-22 13:58:07 -07:00
|
|
|
|
2016-02-25 11:38:05 -08:00
|
|
|
GC gc = XCreateGC(mDisplay, mWindow, 0, nullptr);
|
|
|
|
LayoutDeviceIntRegion bounded;
|
|
|
|
bounded.And(aRegion,
|
|
|
|
LayoutDeviceIntRect(0, 0, mImage->width, mImage->height));
|
|
|
|
for (auto iter = bounded.RectIter(); !iter.Done(); iter.Next()) {
|
|
|
|
const LayoutDeviceIntRect& r = iter.Get();
|
|
|
|
XShmPutImage(mDisplay, mWindow, gc, mImage,
|
|
|
|
r.x, r.y,
|
|
|
|
r.x, r.y,
|
|
|
|
r.width, r.height,
|
2010-10-26 21:56:31 -07:00
|
|
|
False);
|
2016-02-18 07:56:15 -08:00
|
|
|
}
|
2016-02-25 11:38:05 -08:00
|
|
|
|
|
|
|
XFreeGC(mDisplay, gc);
|
|
|
|
|
|
|
|
// FIXME/bug 597336: we need to ensure that the shm image isn't
|
|
|
|
// scribbled over before all its pending XShmPutImage()s complete.
|
|
|
|
// However, XSync() is an unnecessarily heavyweight
|
|
|
|
// synchronization mechanism; other options are possible. If this
|
|
|
|
// XSync is shown to hurt responsiveness, we need to explore the
|
|
|
|
// other options.
|
|
|
|
XSync(mDisplay, False);
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
|
2016-02-18 07:56:15 -08:00
|
|
|
#endif // MOZ_HAVE_SHMIMAGE
|