Bumping manifests a=b2g-bump
[gecko.git] / widget / nsShmImage.cpp
blob2d328aece99fb592baf477d0dc1e014dbcc6c03a
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)
8 #include <gtk/gtk.h>
9 #include <gdk/gdkx.h>
10 #elif defined(MOZ_WIDGET_QT)
11 #include <QWindow>
12 #endif
14 #include "nsShmImage.h"
15 #ifdef MOZ_WIDGET_GTK
16 #include "gfxPlatformGtk.h"
17 #endif
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()
29 #ifdef MOZ_WIDGET_GTK
30 return (gShmAvailable && !gfxPlatformGtk::GetPlatform()->UseXRender());
31 #else
32 return gShmAvailable;
33 #endif
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,
44 ZPixmap, nullptr,
45 &(shm->mInfo),
46 aSize.width, aSize.height);
47 if (!shm->mImage) {
48 return nullptr;
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)) {
55 return nullptr;
58 shm->mInfo.shmid = shm->mSegment->GetHandle();
59 shm->mInfo.shmaddr =
60 shm->mImage->data = static_cast<char*>(shm->mSegment->memory());
61 shm->mInfo.readOnly = False;
63 int xerror = 0;
64 #if defined(MOZ_WIDGET_GTK)
65 gdk_error_trap_push();
66 Status attachOk = XShmAttach(dpy, &shm->mInfo);
67 XSync(dpy, False);
68 xerror = gdk_error_trap_pop();
69 #elif defined(MOZ_WIDGET_QT)
70 Status attachOk = XShmAttach(dpy, &shm->mInfo);
71 #endif
73 if (!attachOk || xerror) {
74 // Assume XShm isn't available, and don't attempt to use it
75 // again.
76 gShmAvailable = false;
77 return nullptr;
80 shm->mXAttached = true;
81 shm->mSize = aSize;
82 switch (shm->mImage->depth) {
83 case 32:
84 if ((shm->mImage->red_mask == 0xff0000) &&
85 (shm->mImage->green_mask == 0xff00) &&
86 (shm->mImage->blue_mask == 0xff)) {
87 shm->mFormat = gfxImageFormat::ARGB32;
88 break;
90 goto unsupported;
91 case 24:
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;
97 break;
99 goto unsupported;
100 case 16:
101 shm->mFormat = gfxImageFormat::RGB16_565; break;
102 unsupported:
103 default:
104 NS_WARNING("Unsupported XShm Image format!");
105 gShmAvailable = false;
106 return nullptr;
108 return shm.forget();
111 already_AddRefed<gfxASurface>
112 nsShmImage::AsSurface()
114 return nsRefPtr<gfxASurface>(
115 new gfxImageSurface(static_cast<unsigned char*>(mSegment->memory()),
116 mSize,
117 mImage->bytes_per_line,
118 mFormat)
119 ).forget();
122 #if (MOZ_WIDGET_GTK == 2)
123 void
124 nsShmImage::Put(GdkWindow* aWindow, GdkRectangle* aRects, GdkRectangle* aEnd)
126 GdkDrawable* gd;
127 gint dx, dy;
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,
136 r->x, r->y,
137 r->x - dx, r->y - dy,
138 r->width, r->height,
139 False);
141 XFreeGC(dpy, gc);
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
148 // other options.
149 XSync(dpy, False);
152 #elif (MOZ_WIDGET_GTK == 3)
153 void
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);
158 int dx = 0, dy = 0;
160 GC gc = XCreateGC(dpy, d, 0, nullptr);
161 cairo_rectangle_t r;
162 for (int i = 0; i < aRects->num_rectangles; i++) {
163 r = aRects->rectangles[i];
164 XShmPutImage(dpy, d, gc, mImage,
165 r.x, r.y,
166 r.x - dx, r.y - dy,
167 r.width, r.height,
168 False);
171 XFreeGC(dpy, gc);
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
178 // other options.
179 XSync(dpy, False);
182 #elif defined(MOZ_WIDGET_QT)
183 void
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(),
196 False);
197 XFreeGC(dpy, gc);
199 #endif
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)