2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04: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/. */
|
2006-02-23 04:01:29 +03:00
|
|
|
|
2010-07-02 08:04:09 +04:00
|
|
|
#include "gfxXlibNativeRenderer.h"
|
2006-02-23 04:01:29 +03:00
|
|
|
|
2010-07-02 08:04:09 +04:00
|
|
|
#include "gfxXlibSurface.h"
|
2010-07-23 02:39:22 +04:00
|
|
|
#include "gfxImageSurface.h"
|
2010-07-02 08:04:09 +04:00
|
|
|
#include "gfxContext.h"
|
2013-09-20 06:00:35 +04:00
|
|
|
#include "gfxPlatform.h"
|
2010-08-09 06:19:17 +04:00
|
|
|
#include "gfxAlphaRecovery.h"
|
2006-02-23 04:01:29 +03:00
|
|
|
#include "cairo-xlib.h"
|
2010-07-02 08:04:09 +04:00
|
|
|
#include "cairo-xlib-xrender.h"
|
2013-09-23 07:28:16 +04:00
|
|
|
#include "mozilla/gfx/BorrowedContext.h"
|
2016-01-08 07:57:38 +03:00
|
|
|
#include "mozilla/gfx/HelpersCairo.h"
|
2013-10-26 01:25:40 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2006-02-23 04:01:29 +03:00
|
|
|
|
2013-09-20 06:00:35 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2006-03-18 01:24:30 +03:00
|
|
|
#if 0
|
|
|
|
# include <stdio.h>
|
2010-07-02 08:04:09 +04:00
|
|
|
# define NATIVE_DRAWING_NOTE(m) fprintf(stderr, m)
|
2006-03-18 01:24:30 +03:00
|
|
|
#else
|
2010-07-02 08:04:09 +04:00
|
|
|
# define NATIVE_DRAWING_NOTE(m) \
|
|
|
|
do { \
|
|
|
|
} while (0)
|
2006-03-18 01:24:30 +03:00
|
|
|
#endif
|
2006-02-23 04:01:29 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
/* We have four basic strategies available:
|
|
|
|
|
|
|
|
1) 'direct': If the target is an xlib surface, and other conditions are met,
|
|
|
|
we can pass the underlying drawable directly to the callback.
|
|
|
|
|
|
|
|
2) 'simple': If the drawing is opaque, or we can draw to a surface with an
|
|
|
|
alpha channel, then we can create a temporary xlib surface, pass its
|
|
|
|
underlying drawable to the callback, and composite the result using
|
|
|
|
cairo.
|
|
|
|
|
|
|
|
3) 'copy-background': If the drawing is not opaque but the target is
|
|
|
|
opaque, and we can draw to a surface with format such that pixel
|
|
|
|
conversion to and from the target format is exact, we can create a
|
|
|
|
temporary xlib surface, copy the background from the target, pass the
|
|
|
|
underlying drawable to the callback, and copy back to the target.
|
|
|
|
|
|
|
|
This strategy is not used if the pixel format conversion is not exact,
|
|
|
|
because that would mean that drawing intended to be very transparent
|
|
|
|
messes with other content.
|
|
|
|
|
|
|
|
The strategy is prefered over simple for non-opaque drawing and opaque
|
|
|
|
targets on the same screen as compositing without alpha is a simpler
|
|
|
|
operation.
|
|
|
|
|
|
|
|
4) 'alpha-extraction': create a temporary xlib surface, fill with black,
|
|
|
|
pass its underlying drawable to the callback, copy the results to a
|
|
|
|
cairo image surface, repeat with a white background, update the on-black
|
2006-02-23 04:01:29 +03:00
|
|
|
image alpha values by comparing the two images, then paint the on-black
|
2010-07-23 02:39:22 +04:00
|
|
|
image using cairo.
|
|
|
|
|
|
|
|
Sure would be nice to have an X extension or GL to do this for us on the
|
|
|
|
server...
|
2006-02-23 04:01:29 +03:00
|
|
|
*/
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static cairo_bool_t _convert_coord_to_int(double coord, int32_t* v) {
|
|
|
|
*v = (int32_t)coord;
|
2006-02-23 04:01:29 +03:00
|
|
|
/* XXX allow some tolerance here? */
|
|
|
|
return *v == coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool _get_rectangular_clip(cairo_t* cr, const IntRect& bounds,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* need_clip, IntRect* rectangles,
|
2015-04-21 18:04:57 +03:00
|
|
|
int max_rectangles, int* num_rectangles) {
|
2006-09-27 02:21:56 +04:00
|
|
|
cairo_rectangle_list_t* cliplist;
|
|
|
|
cairo_rectangle_t* clips;
|
2006-02-23 04:01:29 +03:00
|
|
|
int i;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool retval = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-01-25 03:27:34 +03:00
|
|
|
cliplist = cairo_copy_clip_rectangle_list(cr);
|
2006-09-27 02:21:56 +04:00
|
|
|
if (cliplist->status != CAIRO_STATUS_SUCCESS) {
|
2011-10-17 18:59:28 +04:00
|
|
|
retval = false;
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: non-rectangular clip");
|
2006-09-27 02:21:56 +04:00
|
|
|
goto FINISH;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
/* the clip is always in surface backend coordinates (i.e. native backend
|
|
|
|
* coords) */
|
2006-09-27 02:21:56 +04:00
|
|
|
clips = cliplist->rectangles;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2006-09-27 02:21:56 +04:00
|
|
|
for (i = 0; i < cliplist->num_rectangles; ++i) {
|
2015-04-21 18:04:57 +03:00
|
|
|
IntRect rect;
|
2010-07-23 02:25:19 +04:00
|
|
|
if (!_convert_coord_to_int(clips[i].x, &rect.x) ||
|
|
|
|
!_convert_coord_to_int(clips[i].y, &rect.y) ||
|
|
|
|
!_convert_coord_to_int(clips[i].width, &rect.width) ||
|
|
|
|
!_convert_coord_to_int(clips[i].height, &rect.height)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
retval = false;
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: non-integer clip");
|
2006-09-27 02:21:56 +04:00
|
|
|
goto FINISH;
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
2006-03-18 01:24:30 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
if (rect.IsEqualInterior(bounds)) {
|
|
|
|
/* the bounds are entirely inside the clip region so we don't need to
|
|
|
|
* clip. */
|
2011-10-17 18:59:28 +04:00
|
|
|
*need_clip = false;
|
2010-07-23 02:25:19 +04:00
|
|
|
goto FINISH;
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
NS_ASSERTION(bounds.Contains(rect),
|
|
|
|
"Was expecting to be clipped to bounds");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
if (i >= max_rectangles) {
|
2011-10-17 18:59:28 +04:00
|
|
|
retval = false;
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: unsupported clip rectangle count");
|
2010-07-23 02:25:19 +04:00
|
|
|
goto FINISH;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
rectangles[i] = rect;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*need_clip = true;
|
2010-07-23 02:25:19 +04:00
|
|
|
*num_rectangles = cliplist->num_rectangles;
|
2006-09-27 02:21:56 +04:00
|
|
|
|
|
|
|
FINISH:
|
|
|
|
cairo_rectangle_list_destroy(cliplist);
|
|
|
|
|
|
|
|
return retval;
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
|
|
|
|
2006-09-27 02:21:56 +04:00
|
|
|
#define MAX_STATIC_CLIP_RECTANGLES 50
|
|
|
|
|
2006-02-23 04:01:29 +03:00
|
|
|
/**
|
|
|
|
* Try the direct path.
|
|
|
|
* @return True if we took the direct path
|
|
|
|
*/
|
2015-12-03 02:32:17 +03:00
|
|
|
bool gfxXlibNativeRenderer::DrawDirect(DrawTarget* aDT, IntSize size,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags, Screen* screen,
|
2010-07-02 08:04:09 +04:00
|
|
|
Visual* visual) {
|
2013-09-20 06:00:35 +04:00
|
|
|
// We need to actually borrow the context because we want to read out the
|
|
|
|
// clip rectangles.
|
2015-12-03 02:32:17 +03:00
|
|
|
BorrowedCairoContext borrowed(aDT);
|
2013-09-20 06:00:35 +04:00
|
|
|
if (!borrowed.mCairo) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool direct = DrawCairo(borrowed.mCairo, size, flags, screen, visual);
|
|
|
|
borrowed.Finish();
|
2006-09-29 01:59:49 +04:00
|
|
|
|
2013-09-20 06:00:35 +04:00
|
|
|
return direct;
|
|
|
|
}
|
|
|
|
|
2015-05-28 16:00:17 +03:00
|
|
|
bool gfxXlibNativeRenderer::DrawCairo(cairo_t* cr, IntSize size, uint32_t flags,
|
2013-09-20 06:00:35 +04:00
|
|
|
Screen* screen, Visual* visual) {
|
2010-07-23 02:25:19 +04:00
|
|
|
/* Check that the target surface is an xlib surface. */
|
|
|
|
cairo_surface_t* target = cairo_get_group_target(cr);
|
2010-07-02 08:04:09 +04:00
|
|
|
if (cairo_surface_get_type(target) != CAIRO_SURFACE_TYPE_XLIB) {
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: non-X surface");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
cairo_matrix_t matrix;
|
|
|
|
cairo_get_matrix(cr, &matrix);
|
|
|
|
double device_offset_x, device_offset_y;
|
|
|
|
cairo_surface_get_device_offset(target, &device_offset_x, &device_offset_y);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
/* Draw() checked that the matrix contained only a very-close-to-integer
|
|
|
|
translation. Here (and in several other places and thebes) device
|
|
|
|
offsets are assumed to be integer. */
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_ASSERTION(int32_t(device_offset_x) == device_offset_x &&
|
|
|
|
int32_t(device_offset_y) == device_offset_y,
|
2010-07-23 02:25:19 +04:00
|
|
|
"Expected integer device offsets");
|
2015-04-21 18:04:57 +03:00
|
|
|
IntPoint offset(NS_lroundf(matrix.x0 + device_offset_x),
|
2010-07-23 02:25:19 +04:00
|
|
|
NS_lroundf(matrix.y0 + device_offset_y));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
int max_rectangles = 0;
|
|
|
|
if (flags & DRAW_SUPPORTS_CLIP_RECT) {
|
|
|
|
max_rectangles = 1;
|
|
|
|
}
|
|
|
|
if (flags & DRAW_SUPPORTS_CLIP_LIST) {
|
|
|
|
max_rectangles = MAX_STATIC_CLIP_RECTANGLES;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
/* The client won't draw outside the surface so consider this when
|
|
|
|
analysing clip rectangles. */
|
2015-04-21 18:04:57 +03:00
|
|
|
IntRect bounds(offset, size);
|
|
|
|
bounds.IntersectRect(bounds,
|
2010-07-23 02:25:19 +04:00
|
|
|
IntRect(0, 0, cairo_xlib_surface_get_width(target),
|
|
|
|
cairo_xlib_surface_get_height(target)));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
bool needs_clip = true;
|
|
|
|
IntRect rectangles[MAX_STATIC_CLIP_RECTANGLES];
|
2010-11-07 23:21:04 +03:00
|
|
|
int rect_count = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-11-07 23:21:04 +03:00
|
|
|
/* Check that the clip is rectangular and aligned on unit boundaries. */
|
2013-09-20 06:00:35 +04:00
|
|
|
/* Temporarily set the matrix for _get_rectangular_clip. It's basically
|
2011-09-29 10:19:26 +04:00
|
|
|
the identity matrix, but we must adjust for the fact that our
|
|
|
|
offset-rect is in device coordinates. */
|
2010-11-07 23:21:04 +03:00
|
|
|
cairo_identity_matrix(cr);
|
|
|
|
cairo_translate(cr, -device_offset_x, -device_offset_y);
|
|
|
|
bool have_rectangular_clip = _get_rectangular_clip(
|
|
|
|
cr, bounds, &needs_clip, rectangles, max_rectangles, &rect_count);
|
|
|
|
cairo_set_matrix(cr, &matrix);
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!have_rectangular_clip) return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-11-07 23:21:04 +03:00
|
|
|
/* Stop now if everything is clipped out */
|
2011-10-17 18:59:28 +04:00
|
|
|
if (needs_clip && rect_count == 0) return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-11-07 23:21:04 +03:00
|
|
|
/* Check that the screen is supported.
|
|
|
|
Visuals belong to screens, so, if alternate visuals are not supported,
|
|
|
|
then alternate screens cannot be supported. */
|
|
|
|
bool supports_alternate_visual =
|
|
|
|
(flags & DRAW_SUPPORTS_ALTERNATE_VISUAL) != 0;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool supports_alternate_screen =
|
2010-11-07 23:21:04 +03:00
|
|
|
supports_alternate_visual && (flags & DRAW_SUPPORTS_ALTERNATE_SCREEN);
|
|
|
|
if (!supports_alternate_screen &&
|
|
|
|
cairo_xlib_surface_get_screen(target) != screen) {
|
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: non-default screen");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-11-07 23:21:04 +03:00
|
|
|
}
|
2013-09-20 06:00:35 +04:00
|
|
|
|
2006-02-23 04:01:29 +03:00
|
|
|
/* Check that there is a visual */
|
2010-07-02 08:04:09 +04:00
|
|
|
Visual* target_visual = cairo_xlib_surface_get_visual(target);
|
2010-11-07 23:21:04 +03:00
|
|
|
if (!target_visual) {
|
2010-07-02 08:04:09 +04:00
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: no Visual for surface");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-07-23 02:25:19 +04:00
|
|
|
/* Check that the visual is supported */
|
2010-07-23 02:39:22 +04:00
|
|
|
if (!supports_alternate_visual && target_visual != visual) {
|
2010-11-07 23:21:04 +03:00
|
|
|
// Only the format of the visual is important (not the GLX properties)
|
|
|
|
// for Xlib or XRender drawing.
|
2013-07-31 19:44:31 +04:00
|
|
|
XRenderPictFormat* target_format =
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_xlib_surface_get_xrender_format(target);
|
2010-11-07 23:21:04 +03:00
|
|
|
if (!target_format ||
|
|
|
|
(target_format !=
|
|
|
|
XRenderFindVisualFormat(DisplayOfScreen(screen), visual))) {
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: unsupported Visual");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-23 04:01:29 +03:00
|
|
|
/* we're good to go! */
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE("TAKING FAST PATH\n");
|
2006-02-23 04:01:29 +03:00
|
|
|
cairo_surface_flush(target);
|
2013-10-26 01:25:40 +04:00
|
|
|
nsresult rv =
|
2013-10-26 01:25:40 +04:00
|
|
|
DrawWithXlib(target, offset, rectangles, needs_clip ? rect_count : 0);
|
2010-07-02 08:04:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2008-08-19 07:22:42 +04:00
|
|
|
cairo_surface_mark_dirty(target);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
static bool VisualHasAlpha(Screen* screen, Visual* visual) {
|
|
|
|
// There may be some other visuals format with alpha but usually this is
|
|
|
|
// the only one we care about.
|
|
|
|
return visual->c_class == TrueColor && visual->bits_per_rgb == 8 &&
|
|
|
|
visual->red_mask == 0xff0000 && visual->green_mask == 0xff00 &&
|
|
|
|
visual->blue_mask == 0xff &&
|
|
|
|
gfxXlibSurface::DepthOfVisual(screen, visual) == 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether pixel conversion between visual and format is exact (in
|
|
|
|
// both directions).
|
|
|
|
static bool FormatConversionIsExact(Screen* screen, Visual* visual,
|
|
|
|
XRenderPictFormat* format) {
|
|
|
|
if (!format || visual->c_class != TrueColor ||
|
|
|
|
format->type != PictTypeDirect ||
|
|
|
|
gfxXlibSurface::DepthOfVisual(screen, visual) != format->depth)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
XRenderPictFormat* visualFormat =
|
|
|
|
XRenderFindVisualFormat(DisplayOfScreen(screen), visual);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
if (visualFormat->type != PictTypeDirect) return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
const XRenderDirectFormat& a = visualFormat->direct;
|
|
|
|
const XRenderDirectFormat& b = format->direct;
|
|
|
|
return a.redMask == b.redMask && a.greenMask == b.greenMask &&
|
|
|
|
a.blueMask == b.blueMask;
|
2010-07-02 08:04:09 +04:00
|
|
|
}
|
2008-08-07 03:24:13 +04:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
// The 3 non-direct strategies described above.
|
|
|
|
// The surface format and strategy are inter-dependent.
|
|
|
|
enum DrawingMethod { eSimple, eCopyBackground, eAlphaExtraction };
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
static cairo_surface_t* CreateTempXlibSurface(
|
|
|
|
cairo_surface_t* cairoTarget, DrawTarget* drawTarget, IntSize size,
|
2012-08-22 19:56:38 +04:00
|
|
|
bool canDrawOverBackground, uint32_t flags, Screen* screen, Visual* visual,
|
2010-07-23 02:39:22 +04:00
|
|
|
DrawingMethod* method) {
|
2013-10-26 01:25:40 +04:00
|
|
|
NS_ASSERTION(cairoTarget || drawTarget, "Must have some type");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool drawIsOpaque = (flags & gfxXlibNativeRenderer::DRAW_IS_OPAQUE) != 0;
|
|
|
|
bool supportsAlternateVisual =
|
2010-07-23 02:39:22 +04:00
|
|
|
(flags & gfxXlibNativeRenderer::DRAW_SUPPORTS_ALTERNATE_VISUAL) != 0;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool supportsAlternateScreen =
|
|
|
|
supportsAlternateVisual &&
|
2010-07-23 02:39:22 +04:00
|
|
|
(flags & gfxXlibNativeRenderer::DRAW_SUPPORTS_ALTERNATE_SCREEN);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_type_t cairoTargetType =
|
|
|
|
cairoTarget ? cairo_surface_get_type(cairoTarget)
|
|
|
|
: (cairo_surface_type_t)0xFF;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
Screen* target_screen = cairoTargetType == CAIRO_SURFACE_TYPE_XLIB
|
|
|
|
? cairo_xlib_surface_get_screen(cairoTarget)
|
|
|
|
: screen;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
// When the background has an alpha channel, we need to draw with an alpha
|
|
|
|
// channel anyway, so there is no need to copy the background. If
|
|
|
|
// doCopyBackground is set here, we'll also need to check below that the
|
|
|
|
// background can copied without any loss in format conversions.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doCopyBackground =
|
|
|
|
!drawIsOpaque && canDrawOverBackground && cairoTarget &&
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_get_content(cairoTarget) == CAIRO_CONTENT_COLOR;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
if (supportsAlternateScreen && screen != target_screen && drawIsOpaque) {
|
|
|
|
// Prefer a visual on the target screen.
|
|
|
|
// (If !drawIsOpaque, we'll need doCopyBackground or an alpha channel.)
|
|
|
|
visual = DefaultVisualOfScreen(target_screen);
|
|
|
|
screen = target_screen;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
} else if (doCopyBackground || (supportsAlternateVisual && drawIsOpaque)) {
|
|
|
|
// Analyse the pixel formats either to check whether we can
|
2013-10-26 01:25:40 +04:00
|
|
|
// doCopyBackground or to see if we can find a better visual for
|
2010-07-23 02:39:22 +04:00
|
|
|
// opaque drawing.
|
2013-10-26 01:25:40 +04:00
|
|
|
Visual* target_visual = nullptr;
|
|
|
|
XRenderPictFormat* target_format = nullptr;
|
|
|
|
if (cairoTargetType == CAIRO_SURFACE_TYPE_XLIB) {
|
|
|
|
target_visual = cairo_xlib_surface_get_visual(cairoTarget);
|
2016-01-08 07:57:38 +03:00
|
|
|
target_format = cairo_xlib_surface_get_xrender_format(cairoTarget);
|
2010-07-23 02:39:22 +04:00
|
|
|
} else if (cairoTargetType == CAIRO_SURFACE_TYPE_IMAGE || drawTarget) {
|
2011-03-25 06:38:59 +03:00
|
|
|
gfxImageFormat imageFormat =
|
|
|
|
drawTarget ? SurfaceFormatToImageFormat(drawTarget->GetFormat())
|
2010-07-23 02:39:22 +04:00
|
|
|
: CairoFormatToGfxFormat(
|
|
|
|
cairo_image_surface_get_format(cairoTarget));
|
|
|
|
target_visual = gfxXlibSurface::FindVisual(screen, imageFormat);
|
|
|
|
Display* dpy = DisplayOfScreen(screen);
|
|
|
|
if (target_visual) {
|
|
|
|
target_format = XRenderFindVisualFormat(dpy, target_visual);
|
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
target_format = gfxXlibSurface::FindRenderFormat(dpy, imageFormat);
|
2010-07-23 02:39:22 +04:00
|
|
|
}
|
|
|
|
}
|
2008-08-07 00:48:55 +04:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
if (supportsAlternateVisual &&
|
|
|
|
(supportsAlternateScreen || screen == target_screen)) {
|
|
|
|
if (target_visual) {
|
|
|
|
visual = target_visual;
|
|
|
|
screen = target_screen;
|
|
|
|
}
|
2008-08-07 03:24:13 +04:00
|
|
|
}
|
2013-10-26 01:25:40 +04:00
|
|
|
// Could try harder to match formats across screens for background
|
|
|
|
// copying when !supportsAlternateScreen, if we cared. Preferably
|
|
|
|
// we'll find a visual below with an alpha channel anyway; if so, the
|
|
|
|
// background won't need to be copied.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
if (doCopyBackground && visual != target_visual &&
|
|
|
|
!FormatConversionIsExact(screen, visual, target_format)) {
|
|
|
|
doCopyBackground = false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
if (supportsAlternateVisual && !drawIsOpaque &&
|
2013-10-26 01:25:40 +04:00
|
|
|
(screen != target_screen ||
|
|
|
|
!(doCopyBackground || VisualHasAlpha(screen, visual)))) {
|
2010-07-23 02:39:22 +04:00
|
|
|
// Try to find a visual with an alpha channel.
|
|
|
|
Screen* visualScreen = supportsAlternateScreen ? target_screen : screen;
|
|
|
|
Visual* argbVisual = gfxXlibSurface::FindVisual(
|
|
|
|
visualScreen, SurfaceFormat::A8R8G8B8_UINT32);
|
|
|
|
if (argbVisual) {
|
|
|
|
visual = argbVisual;
|
|
|
|
screen = visualScreen;
|
|
|
|
} else if (!doCopyBackground &&
|
|
|
|
gfxXlibSurface::DepthOfVisual(screen, visual) != 24) {
|
|
|
|
// Will need to do alpha extraction; prefer a 24-bit visual.
|
|
|
|
// No advantage in using the target screen.
|
|
|
|
Visual* rgb24Visual =
|
|
|
|
gfxXlibSurface::FindVisual(screen, SurfaceFormat::X8R8G8B8_UINT32);
|
|
|
|
if (rgb24Visual) {
|
|
|
|
visual = rgb24Visual;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
Drawable drawable =
|
2013-10-26 01:25:40 +04:00
|
|
|
(screen == target_screen && cairoTargetType == CAIRO_SURFACE_TYPE_XLIB)
|
|
|
|
? cairo_xlib_surface_get_drawable(cairoTarget)
|
2010-07-23 02:39:22 +04:00
|
|
|
: RootWindowOfScreen(screen);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_t* surface = gfxXlibSurface::CreateCairoSurface(
|
2015-06-01 11:26:19 +03:00
|
|
|
screen, visual, IntSize(size.width, size.height), drawable);
|
2013-10-26 01:25:40 +04:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
if (drawIsOpaque ||
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_get_content(surface) == CAIRO_CONTENT_COLOR_ALPHA) {
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE(drawIsOpaque ? ", SIMPLE OPAQUE\n"
|
|
|
|
: ", SIMPLE WITH ALPHA");
|
|
|
|
*method = eSimple;
|
|
|
|
} else if (doCopyBackground) {
|
|
|
|
NATIVE_DRAWING_NOTE(", COPY BACKGROUND\n");
|
|
|
|
*method = eCopyBackground;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE(", SLOW ALPHA EXTRACTION\n");
|
|
|
|
*method = eAlphaExtraction;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
return surface;
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
bool gfxXlibNativeRenderer::DrawOntoTempSurface(
|
|
|
|
cairo_surface_t* tempXlibSurface, IntPoint offset) {
|
|
|
|
cairo_surface_flush(tempXlibSurface);
|
2006-02-23 04:01:29 +03:00
|
|
|
/* no clipping is needed because the callback can't draw outside the native
|
|
|
|
surface anyway */
|
2013-10-26 01:25:40 +04:00
|
|
|
nsresult rv = DrawWithXlib(tempXlibSurface, offset, nullptr, 0);
|
|
|
|
cairo_surface_mark_dirty(tempXlibSurface);
|
2010-07-02 08:04:09 +04:00
|
|
|
return NS_SUCCEEDED(rv);
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
|
|
|
|
2010-08-09 06:19:17 +04:00
|
|
|
static already_AddRefed<gfxImageSurface> CopyXlibSurfaceToImage(
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_t* tempXlibSurface, IntSize size, gfxImageFormat format) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxImageSurface> result = new gfxImageSurface(size, format);
|
2006-02-23 04:01:29 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_t* copyCtx = cairo_create(result->CairoSurface());
|
|
|
|
cairo_set_source_surface(copyCtx, tempXlibSurface, 0, 0);
|
|
|
|
cairo_set_operator(copyCtx, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_paint(copyCtx);
|
|
|
|
cairo_destroy(copyCtx);
|
2006-02-23 04:01:29 +03:00
|
|
|
|
2010-08-09 06:19:17 +04:00
|
|
|
return result.forget();
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
|
|
|
|
2015-05-28 16:00:17 +03:00
|
|
|
void gfxXlibNativeRenderer::Draw(gfxContext* ctx, IntSize size, uint32_t flags,
|
2013-10-26 01:25:40 +04:00
|
|
|
Screen* screen, Visual* visual) {
|
2017-11-11 05:14:09 +03:00
|
|
|
Matrix matrix = ctx->CurrentMatrix();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
// We can only draw direct or onto a copied background if pixels align and
|
|
|
|
// native drawing is compatible with the current operator. (The matrix is
|
|
|
|
// actually also pixel-exact for flips and right-angle rotations, which
|
|
|
|
// would permit copying the background but not drawing direct.)
|
2011-09-29 10:19:26 +04:00
|
|
|
bool matrixIsIntegerTranslation = !matrix.HasNonIntegerTranslation();
|
|
|
|
bool canDrawOverBackground =
|
2015-09-25 08:38:58 +03:00
|
|
|
matrixIsIntegerTranslation && ctx->CurrentOp() == CompositionOp::OP_OVER;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
// The padding of 0.5 for non-pixel-exact transformations used here is
|
|
|
|
// the same as what _cairo_pattern_analyze_filter uses.
|
|
|
|
const gfxFloat filterRadius = 0.5;
|
|
|
|
gfxRect affectedRect(0.0, 0.0, size.width, size.height);
|
|
|
|
if (!matrixIsIntegerTranslation) {
|
|
|
|
// The filter footprint means that the affected rectangle is a
|
|
|
|
// little larger than the drawingRect;
|
2011-04-19 07:07:51 +04:00
|
|
|
affectedRect.Inflate(filterRadius);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:39:22 +04:00
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: matrix not integer translation");
|
|
|
|
} else if (!canDrawOverBackground) {
|
|
|
|
NATIVE_DRAWING_NOTE("FALLBACK: unsupported operator");
|
2010-07-23 02:25:19 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-12-03 02:32:17 +03:00
|
|
|
DrawTarget* drawTarget = ctx->GetDrawTarget();
|
2016-01-26 17:35:11 +03:00
|
|
|
if (!drawTarget) {
|
|
|
|
gfxCriticalError() << "gfxContext without a DrawTarget";
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
// Clipping to the region affected by drawing allows us to consider only
|
|
|
|
// the portions of the clip region that will be affected by drawing.
|
|
|
|
gfxRect clipExtents;
|
2018-11-30 13:46:48 +03:00
|
|
|
{
|
2010-07-23 02:25:19 +04:00
|
|
|
gfxContextAutoSaveRestore autoSR(ctx);
|
|
|
|
ctx->Clip(affectedRect);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-23 02:25:19 +04:00
|
|
|
clipExtents = ctx->GetClipExtents();
|
2015-12-03 02:32:17 +03:00
|
|
|
if (clipExtents.IsEmpty()) {
|
2016-01-26 17:35:11 +03:00
|
|
|
return; // nothing to do
|
|
|
|
}
|
2010-07-23 02:39:22 +04:00
|
|
|
if (canDrawOverBackground &&
|
2015-12-03 02:32:17 +03:00
|
|
|
DrawDirect(drawTarget, size, flags, screen, visual)) {
|
2013-09-20 06:00:35 +04:00
|
|
|
return;
|
2010-07-23 02:25:19 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 18:04:57 +03:00
|
|
|
IntRect drawingRect(IntPoint(0, 0), size);
|
2010-07-23 02:39:22 +04:00
|
|
|
// Drawing need only be performed within the clip extents
|
|
|
|
// (and padding for the filter).
|
|
|
|
if (!matrixIsIntegerTranslation) {
|
|
|
|
// The source surface may need to be a little larger than the clip
|
|
|
|
// extents due to the filter footprint.
|
2011-04-19 07:07:51 +04:00
|
|
|
clipExtents.Inflate(filterRadius);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-07-23 02:39:22 +04:00
|
|
|
clipExtents.RoundOut();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-04-21 18:04:57 +03:00
|
|
|
IntRect intExtents(int32_t(clipExtents.X()), int32_t(clipExtents.Y()),
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t(clipExtents.Width()),
|
|
|
|
int32_t(clipExtents.Height()));
|
2010-07-23 02:39:22 +04:00
|
|
|
drawingRect.IntersectRect(drawingRect, intExtents);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
gfxPoint offset(drawingRect.x, drawingRect.y);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
DrawingMethod method;
|
2014-07-10 13:58:07 +04:00
|
|
|
Matrix dtTransform = drawTarget->GetTransform();
|
|
|
|
gfxPoint deviceTranslation = gfxPoint(dtTransform._31, dtTransform._32);
|
2016-01-06 01:24:08 +03:00
|
|
|
cairo_t* cairo = static_cast<cairo_t*>(
|
|
|
|
drawTarget->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT));
|
2016-03-01 22:33:53 +03:00
|
|
|
cairo_surface_t* cairoTarget =
|
|
|
|
cairo ? cairo_get_group_target(cairo) : nullptr;
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_t* tempXlibSurface = CreateTempXlibSurface(
|
2010-07-23 02:39:22 +04:00
|
|
|
cairoTarget, drawTarget, size, canDrawOverBackground, flags, screen,
|
|
|
|
visual, &method);
|
2016-01-26 17:35:11 +03:00
|
|
|
if (!tempXlibSurface) {
|
2006-02-23 04:01:29 +03:00
|
|
|
return;
|
2016-01-26 17:35:11 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-09-20 06:00:35 +04:00
|
|
|
bool drawIsOpaque = (flags & DRAW_IS_OPAQUE) != 0;
|
2010-07-23 02:25:19 +04:00
|
|
|
if (!drawIsOpaque) {
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_t* tmpCtx = cairo_create(tempXlibSurface);
|
2010-07-23 02:39:22 +04:00
|
|
|
if (method == eCopyBackground) {
|
2013-10-26 01:25:40 +04:00
|
|
|
NS_ASSERTION(cairoTarget,
|
|
|
|
"eCopyBackground only used when there's a cairoTarget");
|
|
|
|
cairo_set_operator(tmpCtx, CAIRO_OPERATOR_SOURCE);
|
2013-12-02 01:10:50 +04:00
|
|
|
gfxPoint pt = -(offset + deviceTranslation);
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_set_source_surface(tmpCtx, cairoTarget, pt.x, pt.y);
|
2010-07-23 02:39:22 +04:00
|
|
|
// The copy from the tempXlibSurface to the target context should
|
|
|
|
// use operator SOURCE, but that would need a mask to bound the
|
|
|
|
// operation. Here we only copy opaque backgrounds so operator
|
|
|
|
// OVER will behave like SOURCE masked by the surface.
|
2013-10-26 01:25:40 +04:00
|
|
|
NS_ASSERTION(
|
|
|
|
cairo_surface_get_content(tempXlibSurface) == CAIRO_CONTENT_COLOR,
|
2010-07-23 02:39:22 +04:00
|
|
|
"Don't copy background with a transparent surface");
|
|
|
|
} else {
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_set_operator(tmpCtx, CAIRO_OPERATOR_CLEAR);
|
2010-07-23 02:25:19 +04:00
|
|
|
}
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_paint(tmpCtx);
|
|
|
|
cairo_destroy(tmpCtx);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-07-23 02:25:19 +04:00
|
|
|
|
|
|
|
if (!DrawOntoTempSurface(tempXlibSurface, -drawingRect.TopLeft())) {
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_destroy(tempXlibSurface);
|
2006-02-23 04:01:29 +03:00
|
|
|
return;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
SurfaceFormat moz2DFormat =
|
|
|
|
cairo_surface_get_content(tempXlibSurface) == CAIRO_CONTENT_COLOR
|
2014-01-10 23:06:16 +04:00
|
|
|
? SurfaceFormat::B8G8R8A8
|
|
|
|
: SurfaceFormat::B8G8R8X8;
|
2010-07-23 02:39:22 +04:00
|
|
|
if (method != eAlphaExtraction) {
|
2016-01-26 17:35:11 +03:00
|
|
|
RefPtr<SourceSurface> sourceSurface =
|
|
|
|
Factory::CreateSourceSurfaceForCairoSurface(tempXlibSurface, size,
|
|
|
|
moz2DFormat);
|
|
|
|
if (sourceSurface) {
|
|
|
|
drawTarget->DrawSurface(sourceSurface,
|
|
|
|
Rect(offset.x, offset.y, size.width, size.height),
|
|
|
|
Rect(0, 0, size.width, size.height));
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_destroy(tempXlibSurface);
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
RefPtr<gfxImageSurface> blackImage = CopyXlibSurfaceToImage(
|
|
|
|
tempXlibSurface, size, SurfaceFormat::A8R8G8B8_UINT32);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_t* tmpCtx = cairo_create(tempXlibSurface);
|
|
|
|
cairo_set_source_rgba(tmpCtx, 1.0, 1.0, 1.0, 1.0);
|
2014-01-10 23:06:16 +04:00
|
|
|
cairo_set_operator(tmpCtx, CAIRO_OPERATOR_SOURCE);
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_paint(tmpCtx);
|
|
|
|
cairo_destroy(tmpCtx);
|
2014-01-10 23:06:16 +04:00
|
|
|
DrawOntoTempSurface(tempXlibSurface, -drawingRect.TopLeft());
|
2016-01-26 17:35:11 +03:00
|
|
|
RefPtr<gfxImageSurface> whiteImage = CopyXlibSurfaceToImage(
|
|
|
|
tempXlibSurface, size, SurfaceFormat::X8R8G8B8_UINT32);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-01-26 17:35:11 +03:00
|
|
|
if (blackImage->CairoStatus() == CAIRO_STATUS_SUCCESS &&
|
|
|
|
whiteImage->CairoStatus() == CAIRO_STATUS_SUCCESS) {
|
|
|
|
if (!gfxAlphaRecovery::RecoverAlpha(blackImage, whiteImage)) {
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_destroy(tempXlibSurface);
|
2006-02-23 04:01:29 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-01-26 17:35:11 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
gfxASurface* paintSurface = blackImage;
|
|
|
|
RefPtr<SourceSurface> sourceSurface =
|
2016-01-08 07:57:38 +03:00
|
|
|
Factory::CreateSourceSurfaceForCairoSurface(
|
2016-01-26 17:35:11 +03:00
|
|
|
paintSurface->CairoSurface(), size, moz2DFormat);
|
|
|
|
if (sourceSurface) {
|
|
|
|
drawTarget->DrawSurface(sourceSurface,
|
|
|
|
Rect(offset.x, offset.y, size.width, size.height),
|
|
|
|
Rect(0, 0, size.width, size.height));
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-10-26 01:25:40 +04:00
|
|
|
cairo_surface_destroy(tempXlibSurface);
|
2006-02-23 04:01:29 +03:00
|
|
|
}
|