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