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
|
|
|
|
2013-06-04 05:23:29 -07:00
|
|
|
#if defined(MOZ_WIDGET_GTK)
|
2010-10-26 21:56:31 -07:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gdk/gdkx.h>
|
|
|
|
#elif defined(MOZ_WIDGET_QT)
|
2014-02-20 18:08:53 -08:00
|
|
|
#include <QWindow>
|
2010-10-26 21:56:31 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nsShmImage.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_HAVE_SHMIMAGE
|
|
|
|
|
2010-11-05 00:17:07 -07:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return gfxPlatform::GetPlatform()->
|
2014-01-23 10:26:40 -08:00
|
|
|
ScreenReferenceSurface()->GetType() == gfxSurfaceType::Image
|
2010-10-26 21:56:31 -07:00
|
|
|
&& gShmAvailable;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsShmImage>
|
|
|
|
nsShmImage::Create(const gfxIntSize& aSize,
|
|
|
|
Visual* aVisual, unsigned int aDepth)
|
|
|
|
{
|
|
|
|
Display* dpy = DISPLAY();
|
|
|
|
|
|
|
|
nsRefPtr<nsShmImage> shm = new nsShmImage();
|
|
|
|
shm->mImage = XShmCreateImage(dpy, aVisual, aDepth,
|
2012-07-30 07:20:58 -07:00
|
|
|
ZPixmap, nullptr,
|
2010-10-26 21:56:31 -07:00
|
|
|
&(shm->mInfo),
|
|
|
|
aSize.width, aSize.height);
|
|
|
|
if (!shm->mImage) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
|
2010-11-05 00:17:07 -07:00
|
|
|
size_t size = SharedMemory::PageAlignedSize(
|
|
|
|
shm->mImage->bytes_per_line * shm->mImage->height);
|
2010-10-26 21:56:31 -07:00
|
|
|
shm->mSegment = new SharedMemorySysV();
|
|
|
|
if (!shm->mSegment->Create(size) || !shm->mSegment->Map(size)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
shm->mInfo.shmid = shm->mSegment->GetHandle();
|
|
|
|
shm->mInfo.shmaddr =
|
|
|
|
shm->mImage->data = static_cast<char*>(shm->mSegment->memory());
|
|
|
|
shm->mInfo.readOnly = False;
|
|
|
|
|
|
|
|
int xerror = 0;
|
2013-06-04 05:23:29 -07:00
|
|
|
#if defined(MOZ_WIDGET_GTK)
|
2010-10-26 21:56:31 -07:00
|
|
|
gdk_error_trap_push();
|
|
|
|
Status attachOk = XShmAttach(dpy, &shm->mInfo);
|
2011-03-31 01:39:29 -07:00
|
|
|
XSync(dpy, False);
|
2010-10-26 21:56:31 -07:00
|
|
|
xerror = gdk_error_trap_pop();
|
|
|
|
#elif defined(MOZ_WIDGET_QT)
|
|
|
|
Status attachOk = XShmAttach(dpy, &shm->mInfo);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!attachOk || xerror) {
|
|
|
|
// Assume XShm isn't available, and don't attempt to use it
|
|
|
|
// again.
|
2011-10-17 07:59:28 -07:00
|
|
|
gShmAvailable = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
shm->mXAttached = true;
|
2010-10-26 21:56:31 -07:00
|
|
|
shm->mSize = aSize;
|
|
|
|
switch (shm->mImage->depth) {
|
2014-04-10 13:51:59 -07:00
|
|
|
case 32:
|
|
|
|
if ((shm->mImage->red_mask == 0xff0000) &&
|
|
|
|
(shm->mImage->green_mask == 0xff00) &&
|
|
|
|
(shm->mImage->blue_mask == 0xff)) {
|
|
|
|
shm->mFormat = gfxImageFormat::ARGB32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto unsupported;
|
2010-10-26 21:56:31 -07:00
|
|
|
case 24:
|
2010-11-23 23:36:46 -08:00
|
|
|
// Only xRGB is supported.
|
|
|
|
if ((shm->mImage->red_mask == 0xff0000) &&
|
|
|
|
(shm->mImage->green_mask == 0xff00) &&
|
|
|
|
(shm->mImage->blue_mask == 0xff)) {
|
2014-01-23 10:26:40 -08:00
|
|
|
shm->mFormat = gfxImageFormat::RGB24;
|
2010-11-23 23:36:46 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto unsupported;
|
2010-10-26 21:56:31 -07:00
|
|
|
case 16:
|
2014-01-23 10:26:40 -08:00
|
|
|
shm->mFormat = gfxImageFormat::RGB16_565; break;
|
2010-11-23 23:36:46 -08:00
|
|
|
unsupported:
|
2010-10-26 21:56:31 -07:00
|
|
|
default:
|
2010-11-23 23:36:46 -08:00
|
|
|
NS_WARNING("Unsupported XShm Image format!");
|
2011-10-17 07:59:28 -07:00
|
|
|
gShmAvailable = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
return shm.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
nsShmImage::AsSurface()
|
|
|
|
{
|
|
|
|
return nsRefPtr<gfxASurface>(
|
|
|
|
new gfxImageSurface(static_cast<unsigned char*>(mSegment->memory()),
|
|
|
|
mSize,
|
|
|
|
mImage->bytes_per_line,
|
|
|
|
mFormat)
|
|
|
|
).forget();
|
|
|
|
}
|
|
|
|
|
2013-06-04 05:23:29 -07:00
|
|
|
#if (MOZ_WIDGET_GTK == 2)
|
2010-10-26 21:56:31 -07:00
|
|
|
void
|
|
|
|
nsShmImage::Put(GdkWindow* aWindow, GdkRectangle* aRects, GdkRectangle* aEnd)
|
|
|
|
{
|
|
|
|
GdkDrawable* gd;
|
|
|
|
gint dx, dy;
|
|
|
|
gdk_window_get_internal_paint_info(aWindow, &gd, &dx, &dy);
|
|
|
|
|
|
|
|
Display* dpy = gdk_x11_get_default_xdisplay();
|
|
|
|
Drawable d = GDK_DRAWABLE_XID(gd);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
GC gc = XCreateGC(dpy, d, 0, nullptr);
|
2010-10-26 21:56:31 -07:00
|
|
|
for (GdkRectangle* r = aRects; r < aEnd; r++) {
|
|
|
|
XShmPutImage(dpy, d, gc, mImage,
|
|
|
|
r->x, r->y,
|
|
|
|
r->x - dx, r->y - dy,
|
|
|
|
r->width, r->height,
|
|
|
|
False);
|
|
|
|
}
|
|
|
|
XFreeGC(dpy, 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(dpy, False);
|
|
|
|
}
|
2011-09-22 13:58:07 -07:00
|
|
|
|
2013-06-04 05:23:29 -07:00
|
|
|
#elif (MOZ_WIDGET_GTK == 3)
|
2011-09-22 13:58:07 -07:00
|
|
|
void
|
|
|
|
nsShmImage::Put(GdkWindow* aWindow, cairo_rectangle_list_t* aRects)
|
|
|
|
{
|
|
|
|
Display* dpy = gdk_x11_get_default_xdisplay();
|
|
|
|
Drawable d = GDK_WINDOW_XID(aWindow);
|
|
|
|
int dx = 0, dy = 0;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
GC gc = XCreateGC(dpy, d, 0, nullptr);
|
2011-09-22 13:58:07 -07:00
|
|
|
cairo_rectangle_t r;
|
|
|
|
for (int i = 0; i < aRects->num_rectangles; i++) {
|
|
|
|
r = aRects->rectangles[i];
|
|
|
|
XShmPutImage(dpy, d, gc, mImage,
|
|
|
|
r.x, r.y,
|
|
|
|
r.x - dx, r.y - dy,
|
|
|
|
r.width, r.height,
|
|
|
|
False);
|
|
|
|
}
|
|
|
|
|
|
|
|
XFreeGC(dpy, 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(dpy, False);
|
|
|
|
}
|
|
|
|
|
2010-10-26 21:56:31 -07:00
|
|
|
#elif defined(MOZ_WIDGET_QT)
|
|
|
|
void
|
2014-02-20 18:08:53 -08:00
|
|
|
nsShmImage::Put(QWindow* aWindow, QRect& aRect)
|
2010-10-26 21:56:31 -07:00
|
|
|
{
|
2012-03-22 16:24:40 -07:00
|
|
|
Display* dpy = gfxQtPlatform::GetXDisplay(aWindow);
|
|
|
|
Drawable d = aWindow->winId();
|
2010-10-26 21:56:31 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
GC gc = XCreateGC(dpy, d, 0, nullptr);
|
2010-10-26 21:56:31 -07:00
|
|
|
// Avoid out of bounds painting
|
2014-02-20 18:08:53 -08:00
|
|
|
QRect inter = aRect.intersected(aWindow->geometry());
|
2010-10-26 21:56:31 -07:00
|
|
|
XShmPutImage(dpy, d, gc, mImage,
|
|
|
|
inter.x(), inter.y(),
|
|
|
|
inter.x(), inter.y(),
|
|
|
|
inter.width(), inter.height(),
|
|
|
|
False);
|
|
|
|
XFreeGC(dpy, gc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
nsShmImage::EnsureShmImage(const gfxIntSize& aSize, Visual* aVisual, unsigned int aDepth,
|
|
|
|
nsRefPtr<nsShmImage>& aImage)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
aImage = nsShmImage::Create(aSize, aVisual, aDepth);
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return !aImage ? nullptr : aImage->AsSurface();
|
2010-10-26 21:56:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(MOZ_X11) && defined(MOZ_HAVE_SHAREDMEMORYSYSV)
|