1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #if defined(MOZ_WIDGET_GTK)
10 #elif defined(MOZ_WIDGET_QT)
14 #include "nsShmImage.h"
16 #include "gfxPlatformGtk.h"
18 #include "gfxImageSurface.h"
20 #ifdef MOZ_HAVE_SHMIMAGE
22 using namespace mozilla::ipc
;
24 // If XShm isn't available to our client, we'll try XShm once, fail,
25 // set this to false and then never try again.
26 static bool gShmAvailable
= true;
27 bool nsShmImage::UseShm()
30 return (gShmAvailable
&& !gfxPlatformGtk::GetPlatform()->UseXRender());
36 already_AddRefed
<nsShmImage
>
37 nsShmImage::Create(const gfxIntSize
& aSize
,
38 Visual
* aVisual
, unsigned int aDepth
)
40 Display
* dpy
= DISPLAY();
42 nsRefPtr
<nsShmImage
> shm
= new nsShmImage();
43 shm
->mImage
= XShmCreateImage(dpy
, aVisual
, aDepth
,
46 aSize
.width
, aSize
.height
);
51 size_t size
= SharedMemory::PageAlignedSize(
52 shm
->mImage
->bytes_per_line
* shm
->mImage
->height
);
53 shm
->mSegment
= new SharedMemorySysV();
54 if (!shm
->mSegment
->Create(size
) || !shm
->mSegment
->Map(size
)) {
58 shm
->mInfo
.shmid
= shm
->mSegment
->GetHandle();
60 shm
->mImage
->data
= static_cast<char*>(shm
->mSegment
->memory());
61 shm
->mInfo
.readOnly
= False
;
64 #if defined(MOZ_WIDGET_GTK)
65 gdk_error_trap_push();
66 Status attachOk
= XShmAttach(dpy
, &shm
->mInfo
);
68 xerror
= gdk_error_trap_pop();
69 #elif defined(MOZ_WIDGET_QT)
70 Status attachOk
= XShmAttach(dpy
, &shm
->mInfo
);
73 if (!attachOk
|| xerror
) {
74 // Assume XShm isn't available, and don't attempt to use it
76 gShmAvailable
= false;
80 shm
->mXAttached
= true;
82 switch (shm
->mImage
->depth
) {
84 if ((shm
->mImage
->red_mask
== 0xff0000) &&
85 (shm
->mImage
->green_mask
== 0xff00) &&
86 (shm
->mImage
->blue_mask
== 0xff)) {
87 shm
->mFormat
= gfxImageFormat::ARGB32
;
92 // Only xRGB is supported.
93 if ((shm
->mImage
->red_mask
== 0xff0000) &&
94 (shm
->mImage
->green_mask
== 0xff00) &&
95 (shm
->mImage
->blue_mask
== 0xff)) {
96 shm
->mFormat
= gfxImageFormat::RGB24
;
101 shm
->mFormat
= gfxImageFormat::RGB16_565
; break;
104 NS_WARNING("Unsupported XShm Image format!");
105 gShmAvailable
= false;
111 already_AddRefed
<gfxASurface
>
112 nsShmImage::AsSurface()
114 return nsRefPtr
<gfxASurface
>(
115 new gfxImageSurface(static_cast<unsigned char*>(mSegment
->memory()),
117 mImage
->bytes_per_line
,
122 #if (MOZ_WIDGET_GTK == 2)
124 nsShmImage::Put(GdkWindow
* aWindow
, GdkRectangle
* aRects
, GdkRectangle
* aEnd
)
128 gdk_window_get_internal_paint_info(aWindow
, &gd
, &dx
, &dy
);
130 Display
* dpy
= gdk_x11_get_default_xdisplay();
131 Drawable d
= GDK_DRAWABLE_XID(gd
);
133 GC gc
= XCreateGC(dpy
, d
, 0, nullptr);
134 for (GdkRectangle
* r
= aRects
; r
< aEnd
; r
++) {
135 XShmPutImage(dpy
, d
, gc
, mImage
,
137 r
->x
- dx
, r
->y
- dy
,
143 // FIXME/bug 597336: we need to ensure that the shm image isn't
144 // scribbled over before all its pending XShmPutImage()s complete.
145 // However, XSync() is an unnecessarily heavyweight
146 // synchronization mechanism; other options are possible. If this
147 // XSync is shown to hurt responsiveness, we need to explore the
152 #elif (MOZ_WIDGET_GTK == 3)
154 nsShmImage::Put(GdkWindow
* aWindow
, cairo_rectangle_list_t
* aRects
)
156 Display
* dpy
= gdk_x11_get_default_xdisplay();
157 Drawable d
= GDK_WINDOW_XID(aWindow
);
160 GC gc
= XCreateGC(dpy
, d
, 0, nullptr);
162 for (int i
= 0; i
< aRects
->num_rectangles
; i
++) {
163 r
= aRects
->rectangles
[i
];
164 XShmPutImage(dpy
, d
, gc
, mImage
,
173 // FIXME/bug 597336: we need to ensure that the shm image isn't
174 // scribbled over before all its pending XShmPutImage()s complete.
175 // However, XSync() is an unnecessarily heavyweight
176 // synchronization mechanism; other options are possible. If this
177 // XSync is shown to hurt responsiveness, we need to explore the
182 #elif defined(MOZ_WIDGET_QT)
184 nsShmImage::Put(QWindow
* aWindow
, QRect
& aRect
)
186 Display
* dpy
= gfxQtPlatform::GetXDisplay(aWindow
);
187 Drawable d
= aWindow
->winId();
189 GC gc
= XCreateGC(dpy
, d
, 0, nullptr);
190 // Avoid out of bounds painting
191 QRect inter
= aRect
.intersected(aWindow
->geometry());
192 XShmPutImage(dpy
, d
, gc
, mImage
,
193 inter
.x(), inter
.y(),
194 inter
.x(), inter
.y(),
195 inter
.width(), inter
.height(),
201 already_AddRefed
<gfxASurface
>
202 nsShmImage::EnsureShmImage(const gfxIntSize
& aSize
, Visual
* aVisual
, unsigned int aDepth
,
203 nsRefPtr
<nsShmImage
>& aImage
)
205 if (!aImage
|| aImage
->Size() != aSize
) {
206 // Because we XSync() after XShmAttach() to trap errors, we
207 // know that the X server has the old image's memory mapped
208 // into its address space, so it's OK to destroy the old image
209 // here even if there are outstanding Puts. The Detach is
210 // ordered after the Puts.
211 aImage
= nsShmImage::Create(aSize
, aVisual
, aDepth
);
213 return !aImage
? nullptr : aImage
->AsSurface();
216 #endif // defined(MOZ_X11) && defined(MOZ_HAVE_SHAREDMEMORYSYSV)