|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
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/. */ |
|
6 |
|
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 |
|
13 |
|
14 #include "nsShmImage.h" |
|
15 #include "gfxPlatform.h" |
|
16 #include "gfxImageSurface.h" |
|
17 |
|
18 #ifdef MOZ_HAVE_SHMIMAGE |
|
19 |
|
20 using namespace mozilla::ipc; |
|
21 |
|
22 // If XShm isn't available to our client, we'll try XShm once, fail, |
|
23 // set this to false and then never try again. |
|
24 static bool gShmAvailable = true; |
|
25 bool nsShmImage::UseShm() |
|
26 { |
|
27 return gfxPlatform::GetPlatform()-> |
|
28 ScreenReferenceSurface()->GetType() == gfxSurfaceType::Image |
|
29 && gShmAvailable; |
|
30 } |
|
31 |
|
32 already_AddRefed<nsShmImage> |
|
33 nsShmImage::Create(const gfxIntSize& aSize, |
|
34 Visual* aVisual, unsigned int aDepth) |
|
35 { |
|
36 Display* dpy = DISPLAY(); |
|
37 |
|
38 nsRefPtr<nsShmImage> shm = new nsShmImage(); |
|
39 shm->mImage = XShmCreateImage(dpy, aVisual, aDepth, |
|
40 ZPixmap, nullptr, |
|
41 &(shm->mInfo), |
|
42 aSize.width, aSize.height); |
|
43 if (!shm->mImage) { |
|
44 return nullptr; |
|
45 } |
|
46 |
|
47 size_t size = SharedMemory::PageAlignedSize( |
|
48 shm->mImage->bytes_per_line * shm->mImage->height); |
|
49 shm->mSegment = new SharedMemorySysV(); |
|
50 if (!shm->mSegment->Create(size) || !shm->mSegment->Map(size)) { |
|
51 return nullptr; |
|
52 } |
|
53 |
|
54 shm->mInfo.shmid = shm->mSegment->GetHandle(); |
|
55 shm->mInfo.shmaddr = |
|
56 shm->mImage->data = static_cast<char*>(shm->mSegment->memory()); |
|
57 shm->mInfo.readOnly = False; |
|
58 |
|
59 int xerror = 0; |
|
60 #if defined(MOZ_WIDGET_GTK) |
|
61 gdk_error_trap_push(); |
|
62 Status attachOk = XShmAttach(dpy, &shm->mInfo); |
|
63 XSync(dpy, False); |
|
64 xerror = gdk_error_trap_pop(); |
|
65 #elif defined(MOZ_WIDGET_QT) |
|
66 Status attachOk = XShmAttach(dpy, &shm->mInfo); |
|
67 #endif |
|
68 |
|
69 if (!attachOk || xerror) { |
|
70 // Assume XShm isn't available, and don't attempt to use it |
|
71 // again. |
|
72 gShmAvailable = false; |
|
73 return nullptr; |
|
74 } |
|
75 |
|
76 shm->mXAttached = true; |
|
77 shm->mSize = aSize; |
|
78 switch (shm->mImage->depth) { |
|
79 case 32: |
|
80 if ((shm->mImage->red_mask == 0xff0000) && |
|
81 (shm->mImage->green_mask == 0xff00) && |
|
82 (shm->mImage->blue_mask == 0xff)) { |
|
83 shm->mFormat = gfxImageFormat::ARGB32; |
|
84 break; |
|
85 } |
|
86 goto unsupported; |
|
87 case 24: |
|
88 // Only xRGB is supported. |
|
89 if ((shm->mImage->red_mask == 0xff0000) && |
|
90 (shm->mImage->green_mask == 0xff00) && |
|
91 (shm->mImage->blue_mask == 0xff)) { |
|
92 shm->mFormat = gfxImageFormat::RGB24; |
|
93 break; |
|
94 } |
|
95 goto unsupported; |
|
96 case 16: |
|
97 shm->mFormat = gfxImageFormat::RGB16_565; break; |
|
98 unsupported: |
|
99 default: |
|
100 NS_WARNING("Unsupported XShm Image format!"); |
|
101 gShmAvailable = false; |
|
102 return nullptr; |
|
103 } |
|
104 return shm.forget(); |
|
105 } |
|
106 |
|
107 already_AddRefed<gfxASurface> |
|
108 nsShmImage::AsSurface() |
|
109 { |
|
110 return nsRefPtr<gfxASurface>( |
|
111 new gfxImageSurface(static_cast<unsigned char*>(mSegment->memory()), |
|
112 mSize, |
|
113 mImage->bytes_per_line, |
|
114 mFormat) |
|
115 ).forget(); |
|
116 } |
|
117 |
|
118 #if (MOZ_WIDGET_GTK == 2) |
|
119 void |
|
120 nsShmImage::Put(GdkWindow* aWindow, GdkRectangle* aRects, GdkRectangle* aEnd) |
|
121 { |
|
122 GdkDrawable* gd; |
|
123 gint dx, dy; |
|
124 gdk_window_get_internal_paint_info(aWindow, &gd, &dx, &dy); |
|
125 |
|
126 Display* dpy = gdk_x11_get_default_xdisplay(); |
|
127 Drawable d = GDK_DRAWABLE_XID(gd); |
|
128 |
|
129 GC gc = XCreateGC(dpy, d, 0, nullptr); |
|
130 for (GdkRectangle* r = aRects; r < aEnd; r++) { |
|
131 XShmPutImage(dpy, d, gc, mImage, |
|
132 r->x, r->y, |
|
133 r->x - dx, r->y - dy, |
|
134 r->width, r->height, |
|
135 False); |
|
136 } |
|
137 XFreeGC(dpy, gc); |
|
138 |
|
139 // FIXME/bug 597336: we need to ensure that the shm image isn't |
|
140 // scribbled over before all its pending XShmPutImage()s complete. |
|
141 // However, XSync() is an unnecessarily heavyweight |
|
142 // synchronization mechanism; other options are possible. If this |
|
143 // XSync is shown to hurt responsiveness, we need to explore the |
|
144 // other options. |
|
145 XSync(dpy, False); |
|
146 } |
|
147 |
|
148 #elif (MOZ_WIDGET_GTK == 3) |
|
149 void |
|
150 nsShmImage::Put(GdkWindow* aWindow, cairo_rectangle_list_t* aRects) |
|
151 { |
|
152 Display* dpy = gdk_x11_get_default_xdisplay(); |
|
153 Drawable d = GDK_WINDOW_XID(aWindow); |
|
154 int dx = 0, dy = 0; |
|
155 |
|
156 GC gc = XCreateGC(dpy, d, 0, nullptr); |
|
157 cairo_rectangle_t r; |
|
158 for (int i = 0; i < aRects->num_rectangles; i++) { |
|
159 r = aRects->rectangles[i]; |
|
160 XShmPutImage(dpy, d, gc, mImage, |
|
161 r.x, r.y, |
|
162 r.x - dx, r.y - dy, |
|
163 r.width, r.height, |
|
164 False); |
|
165 } |
|
166 |
|
167 XFreeGC(dpy, gc); |
|
168 |
|
169 // FIXME/bug 597336: we need to ensure that the shm image isn't |
|
170 // scribbled over before all its pending XShmPutImage()s complete. |
|
171 // However, XSync() is an unnecessarily heavyweight |
|
172 // synchronization mechanism; other options are possible. If this |
|
173 // XSync is shown to hurt responsiveness, we need to explore the |
|
174 // other options. |
|
175 XSync(dpy, False); |
|
176 } |
|
177 |
|
178 #elif defined(MOZ_WIDGET_QT) |
|
179 void |
|
180 nsShmImage::Put(QWindow* aWindow, QRect& aRect) |
|
181 { |
|
182 Display* dpy = gfxQtPlatform::GetXDisplay(aWindow); |
|
183 Drawable d = aWindow->winId(); |
|
184 |
|
185 GC gc = XCreateGC(dpy, d, 0, nullptr); |
|
186 // Avoid out of bounds painting |
|
187 QRect inter = aRect.intersected(aWindow->geometry()); |
|
188 XShmPutImage(dpy, d, gc, mImage, |
|
189 inter.x(), inter.y(), |
|
190 inter.x(), inter.y(), |
|
191 inter.width(), inter.height(), |
|
192 False); |
|
193 XFreeGC(dpy, gc); |
|
194 } |
|
195 #endif |
|
196 |
|
197 already_AddRefed<gfxASurface> |
|
198 nsShmImage::EnsureShmImage(const gfxIntSize& aSize, Visual* aVisual, unsigned int aDepth, |
|
199 nsRefPtr<nsShmImage>& aImage) |
|
200 { |
|
201 if (!aImage || aImage->Size() != aSize) { |
|
202 // Because we XSync() after XShmAttach() to trap errors, we |
|
203 // know that the X server has the old image's memory mapped |
|
204 // into its address space, so it's OK to destroy the old image |
|
205 // here even if there are outstanding Puts. The Detach is |
|
206 // ordered after the Puts. |
|
207 aImage = nsShmImage::Create(aSize, aVisual, aDepth); |
|
208 } |
|
209 return !aImage ? nullptr : aImage->AsSurface(); |
|
210 } |
|
211 |
|
212 #endif // defined(MOZ_X11) && defined(MOZ_HAVE_SHAREDMEMORYSYSV) |