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
|
|
|
|
2013-06-04 16:23:29 +04:00
|
|
|
#if defined(MOZ_WIDGET_GTK)
|
2010-10-27 08:56:31 +04:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gdk/gdkx.h>
|
|
|
|
#elif defined(MOZ_WIDGET_QT)
|
2014-02-21 06:08:53 +04:00
|
|
|
#include <QWindow>
|
2010-10-27 08:56:31 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nsShmImage.h"
|
2014-07-23 03:04:47 +04:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
|
|
|
#include "gfxPlatformGtk.h"
|
|
|
|
#endif
|
2010-10-27 08:56:31 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_HAVE_SHMIMAGE
|
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2014-07-23 03:04:47 +04:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
|
|
|
return (gShmAvailable && !gfxPlatformGtk::GetPlatform()->UseXRender());
|
|
|
|
#else
|
|
|
|
return gShmAvailable;
|
|
|
|
#endif
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2015-09-15 23:46:39 +03:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
|
|
|
static int gShmError = 0;
|
|
|
|
|
|
|
|
static int
|
|
|
|
TrapShmError(Display* aDisplay, XErrorEvent* aEvent)
|
|
|
|
{
|
|
|
|
// store the error code and ignore the error
|
|
|
|
gShmError = aEvent->error_code;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-27 08:56:31 +04:00
|
|
|
already_AddRefed<nsShmImage>
|
2015-09-23 21:49:05 +03:00
|
|
|
nsShmImage::Create(const IntSize& aSize,
|
2015-09-15 23:46:39 +03:00
|
|
|
Display* aDisplay, Visual* aVisual, unsigned int aDepth)
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsShmImage> shm = new nsShmImage();
|
2015-09-15 23:46:39 +03:00
|
|
|
shm->mDisplay = aDisplay;
|
|
|
|
shm->mImage = XShmCreateImage(aDisplay, aVisual, aDepth,
|
2012-07-30 18:20:58 +04:00
|
|
|
ZPixmap, nullptr,
|
2010-10-27 08:56:31 +04:00
|
|
|
&(shm->mInfo),
|
|
|
|
aSize.width, aSize.height);
|
|
|
|
if (!shm->mImage) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2010-11-05 10:17:07 +03:00
|
|
|
size_t size = SharedMemory::PageAlignedSize(
|
|
|
|
shm->mImage->bytes_per_line * shm->mImage->height);
|
2010-10-27 08:56:31 +04:00
|
|
|
shm->mSegment = new SharedMemorySysV();
|
|
|
|
if (!shm->mSegment->Create(size) || !shm->mSegment->Map(size)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
shm->mInfo.shmid = shm->mSegment->GetHandle();
|
|
|
|
shm->mInfo.shmaddr =
|
|
|
|
shm->mImage->data = static_cast<char*>(shm->mSegment->memory());
|
|
|
|
shm->mInfo.readOnly = False;
|
|
|
|
|
2013-06-04 16:23:29 +04:00
|
|
|
#if defined(MOZ_WIDGET_GTK)
|
2015-09-15 23:46:39 +03:00
|
|
|
gShmError = 0;
|
|
|
|
XErrorHandler previousHandler = XSetErrorHandler(TrapShmError);
|
|
|
|
Status attachOk = XShmAttach(aDisplay, &shm->mInfo);
|
|
|
|
XSync(aDisplay, False);
|
|
|
|
XSetErrorHandler(previousHandler);
|
|
|
|
if (gShmError) {
|
|
|
|
attachOk = 0;
|
|
|
|
}
|
2010-10-27 08:56:31 +04:00
|
|
|
#elif defined(MOZ_WIDGET_QT)
|
2015-09-15 23:46:39 +03:00
|
|
|
Status attachOk = XShmAttach(aDisplay, &shm->mInfo);
|
2010-10-27 08:56:31 +04:00
|
|
|
#endif
|
|
|
|
|
2015-09-15 23:46:39 +03:00
|
|
|
if (!attachOk) {
|
2010-10-27 08:56:31 +04:00
|
|
|
// Assume XShm isn't available, and don't attempt to use it
|
|
|
|
// again.
|
2011-10-17 18:59:28 +04:00
|
|
|
gShmAvailable = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
shm->mXAttached = true;
|
2010-10-27 08:56:31 +04:00
|
|
|
shm->mSize = aSize;
|
|
|
|
switch (shm->mImage->depth) {
|
2014-04-11 00:51:59 +04:00
|
|
|
case 32:
|
|
|
|
if ((shm->mImage->red_mask == 0xff0000) &&
|
|
|
|
(shm->mImage->green_mask == 0xff00) &&
|
|
|
|
(shm->mImage->blue_mask == 0xff)) {
|
2015-09-15 23:46:39 +03:00
|
|
|
shm->mFormat = SurfaceFormat::B8G8R8A8;
|
2014-04-11 00:51:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto unsupported;
|
2010-10-27 08:56:31 +04:00
|
|
|
case 24:
|
2010-11-24 10:36:46 +03:00
|
|
|
// Only xRGB is supported.
|
|
|
|
if ((shm->mImage->red_mask == 0xff0000) &&
|
|
|
|
(shm->mImage->green_mask == 0xff00) &&
|
|
|
|
(shm->mImage->blue_mask == 0xff)) {
|
2015-09-15 23:46:39 +03:00
|
|
|
shm->mFormat = SurfaceFormat::B8G8R8X8;
|
2010-11-24 10:36:46 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto unsupported;
|
2010-10-27 08:56:31 +04:00
|
|
|
case 16:
|
2015-10-23 09:01:31 +03:00
|
|
|
shm->mFormat = SurfaceFormat::R5G6B5_UINT16;
|
2015-09-15 23:46:39 +03:00
|
|
|
break;
|
2010-11-24 10:36:46 +03:00
|
|
|
unsupported:
|
2010-10-27 08:56:31 +04:00
|
|
|
default:
|
2010-11-24 10:36:46 +03:00
|
|
|
NS_WARNING("Unsupported XShm Image format!");
|
2011-10-17 18:59:28 +04:00
|
|
|
gShmAvailable = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
return shm.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-15 23:46:39 +03:00
|
|
|
already_AddRefed<DrawTarget>
|
|
|
|
nsShmImage::CreateDrawTarget()
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
2015-09-15 23:46:39 +03:00
|
|
|
return gfxPlatform::GetPlatform()->CreateDrawTargetForData(
|
|
|
|
static_cast<unsigned char*>(mSegment->memory()),
|
|
|
|
mSize,
|
|
|
|
mImage->bytes_per_line,
|
|
|
|
mFormat);
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
2015-09-15 23:46:39 +03:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
2010-10-27 08:56:31 +04:00
|
|
|
void
|
2015-09-15 23:46:39 +03:00
|
|
|
nsShmImage::Put(Display* aDisplay, Drawable aWindow, const nsIntRegion& aRegion)
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
2015-09-15 23:46:39 +03:00
|
|
|
GC gc = XCreateGC(aDisplay, aWindow, 0, nullptr);
|
2015-07-13 15:03:44 +03:00
|
|
|
nsIntRegion bounded;
|
|
|
|
bounded.And(aRegion, nsIntRect(0, 0, mImage->width, mImage->height));
|
|
|
|
nsIntRegionRectIterator iter(bounded);
|
2015-05-01 21:08:04 +03:00
|
|
|
for (const nsIntRect *r = iter.Next(); r; r = iter.Next()) {
|
2015-09-15 23:46:39 +03:00
|
|
|
XShmPutImage(aDisplay, aWindow, gc, mImage,
|
2010-10-27 08:56:31 +04:00
|
|
|
r->x, r->y,
|
2015-05-01 21:08:04 +03:00
|
|
|
r->x, r->y,
|
|
|
|
r->width, r->height,
|
2011-09-23 00:58:07 +04:00
|
|
|
False);
|
|
|
|
}
|
|
|
|
|
2015-09-15 23:46:39 +03:00
|
|
|
XFreeGC(aDisplay, gc);
|
2011-09-23 00:58:07 +04:00
|
|
|
|
|
|
|
// 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.
|
2015-09-15 23:46:39 +03:00
|
|
|
XSync(aDisplay, False);
|
2011-09-23 00:58:07 +04:00
|
|
|
}
|
|
|
|
|
2010-10-27 08:56:31 +04:00
|
|
|
#elif defined(MOZ_WIDGET_QT)
|
|
|
|
void
|
2014-02-21 06:08:53 +04:00
|
|
|
nsShmImage::Put(QWindow* aWindow, QRect& aRect)
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
2012-03-23 03:24:40 +04:00
|
|
|
Display* dpy = gfxQtPlatform::GetXDisplay(aWindow);
|
|
|
|
Drawable d = aWindow->winId();
|
2010-10-27 08:56:31 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
GC gc = XCreateGC(dpy, d, 0, nullptr);
|
2010-10-27 08:56:31 +04:00
|
|
|
// Avoid out of bounds painting
|
2014-02-21 06:08:53 +04:00
|
|
|
QRect inter = aRect.intersected(aWindow->geometry());
|
2010-10-27 08:56:31 +04:00
|
|
|
XShmPutImage(dpy, d, gc, mImage,
|
|
|
|
inter.x(), inter.y(),
|
|
|
|
inter.x(), inter.y(),
|
|
|
|
inter.width(), inter.height(),
|
|
|
|
False);
|
|
|
|
XFreeGC(dpy, gc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-09-15 23:46:39 +03:00
|
|
|
already_AddRefed<DrawTarget>
|
2015-09-23 21:49:05 +03:00
|
|
|
nsShmImage::EnsureShmImage(const IntSize& aSize,
|
2015-09-15 23:46:39 +03:00
|
|
|
Display* aDisplay, Visual* aVisual, unsigned int aDepth,
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsShmImage>& aImage)
|
2010-10-27 08:56:31 +04:00
|
|
|
{
|
|
|
|
if (!aImage || aImage->Size() != aSize) {
|
|
|
|
// Because we XSync() after XShmAttach() to trap errors, we
|
|
|
|
// know that the X server has the old image's memory mapped
|
|
|
|
// into its address space, so it's OK to destroy the old image
|
|
|
|
// here even if there are outstanding Puts. The Detach is
|
|
|
|
// ordered after the Puts.
|
2015-09-15 23:46:39 +03:00
|
|
|
aImage = nsShmImage::Create(aSize, aDisplay, aVisual, aDepth);
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
2015-09-15 23:46:39 +03:00
|
|
|
return !aImage ? nullptr : aImage->CreateDrawTarget();
|
2010-10-27 08:56:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(MOZ_X11) && defined(MOZ_HAVE_SHAREDMEMORYSYSV)
|