2011-06-24 17:41:16 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00: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/. */
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
#include "DrawTargetCairo.h"
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
#include "SourceSurfaceCairo.h"
|
2012-01-09 22:15:10 +00:00
|
|
|
#include "PathCairo.h"
|
|
|
|
#include "HelpersCairo.h"
|
2012-01-27 18:08:46 +00:00
|
|
|
#include "ScaledFontBase.h"
|
2013-09-23 03:28:16 +00:00
|
|
|
#include "BorrowedContext.h"
|
2013-11-27 11:22:56 +00:00
|
|
|
#include "FilterNodeSoftware.h"
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
#include "cairo.h"
|
2012-09-02 23:07:06 +00:00
|
|
|
#include "cairo-tee.h"
|
2012-07-27 21:51:53 +00:00
|
|
|
#include <string.h>
|
2012-01-09 21:50:01 +00:00
|
|
|
|
|
|
|
#include "Blur.h"
|
2012-07-24 10:18:37 +00:00
|
|
|
#include "Logging.h"
|
2012-07-24 10:18:38 +00:00
|
|
|
#include "Tools.h"
|
2012-01-09 21:50:01 +00:00
|
|
|
|
|
|
|
#ifdef CAIRO_HAS_QUARTZ_SURFACE
|
|
|
|
#include "cairo-quartz.h"
|
|
|
|
#include <ApplicationServices/ApplicationServices.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CAIRO_HAS_XLIB_SURFACE
|
|
|
|
#include "cairo-xlib.h"
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 05:08:52 +00:00
|
|
|
#ifdef CAIRO_HAS_WIN32_SURFACE
|
|
|
|
#include "cairo-win32.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2013-09-28 14:20:24 +00:00
|
|
|
cairo_surface_t *DrawTargetCairo::mDummySurface;
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
namespace {
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
// An RAII class to prepare to draw a context and optional path. Saves and
|
|
|
|
// restores the context on construction/destruction.
|
|
|
|
class AutoPrepareForDrawing
|
2011-06-24 17:41:16 +00:00
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
public:
|
|
|
|
AutoPrepareForDrawing(DrawTargetCairo* dt, cairo_t* ctx)
|
|
|
|
: mCtx(ctx)
|
2011-06-24 17:41:16 +00:00
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
dt->PrepareForDrawing(ctx);
|
|
|
|
cairo_save(mCtx);
|
2012-10-25 02:03:21 +00:00
|
|
|
MOZ_ASSERT(cairo_status(mCtx) || dt->GetTransform() == GetTransform());
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing(DrawTargetCairo* dt, cairo_t* ctx, const Path* path)
|
|
|
|
: mCtx(ctx)
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
dt->PrepareForDrawing(ctx, path);
|
|
|
|
cairo_save(mCtx);
|
2012-10-25 02:03:21 +00:00
|
|
|
MOZ_ASSERT(cairo_status(mCtx) || dt->GetTransform() == GetTransform());
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
~AutoPrepareForDrawing() { cairo_restore(mCtx); }
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
private:
|
2012-10-25 02:03:21 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
Matrix GetTransform()
|
|
|
|
{
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
cairo_get_matrix(mCtx, &mat);
|
|
|
|
return Matrix(mat.xx, mat.yx, mat.xy, mat.yy, mat.x0, mat.y0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
cairo_t* mCtx;
|
|
|
|
};
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2013-08-20 23:52:20 +00:00
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
} // end anonymous namespace
|
2012-01-09 21:50:01 +00:00
|
|
|
|
|
|
|
static bool
|
|
|
|
GetCairoSurfaceSize(cairo_surface_t* surface, IntSize& size)
|
|
|
|
{
|
|
|
|
switch (cairo_surface_get_type(surface))
|
|
|
|
{
|
|
|
|
case CAIRO_SURFACE_TYPE_IMAGE:
|
|
|
|
{
|
|
|
|
size.width = cairo_image_surface_get_width(surface);
|
|
|
|
size.height = cairo_image_surface_get_height(surface);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CAIRO_HAS_XLIB_SURFACE
|
|
|
|
case CAIRO_SURFACE_TYPE_XLIB:
|
|
|
|
{
|
|
|
|
size.width = cairo_xlib_surface_get_width(surface);
|
|
|
|
size.height = cairo_xlib_surface_get_height(surface);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CAIRO_HAS_QUARTZ_SURFACE
|
|
|
|
case CAIRO_SURFACE_TYPE_QUARTZ:
|
|
|
|
{
|
|
|
|
CGContextRef cgc = cairo_quartz_surface_get_cg_context(surface);
|
|
|
|
|
|
|
|
// It's valid to call these CGBitmapContext functions on non-bitmap
|
|
|
|
// contexts; they'll just return 0 in that case.
|
|
|
|
size.width = CGBitmapContextGetWidth(cgc);
|
2013-09-27 15:18:05 +00:00
|
|
|
size.height = CGBitmapContextGetHeight(cgc);
|
2012-01-27 18:09:20 +00:00
|
|
|
return true;
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
2013-09-11 05:08:52 +00:00
|
|
|
#ifdef CAIRO_HAS_WIN32_SURFACE
|
2013-11-03 23:57:36 +00:00
|
|
|
#ifdef MOZ2D_HAS_MOZ_CAIRO
|
2013-09-11 05:08:52 +00:00
|
|
|
case CAIRO_SURFACE_TYPE_WIN32:
|
|
|
|
case CAIRO_SURFACE_TYPE_WIN32_PRINTING:
|
|
|
|
{
|
|
|
|
size.width = cairo_win32_surface_get_width(surface);
|
|
|
|
size.height = cairo_win32_surface_get_height(surface);
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-03 23:57:36 +00:00
|
|
|
#else
|
|
|
|
case CAIRO_SURFACE_TYPE_WIN32:
|
|
|
|
{
|
|
|
|
cairo_surface_t *img = cairo_win32_surface_get_image(surface);
|
|
|
|
|
|
|
|
if (!img) {
|
|
|
|
// XXX - fix me
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
size.width = cairo_image_surface_get_width(img);
|
|
|
|
size.height = cairo_image_surface_get_height(img);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-11 05:08:52 +00:00
|
|
|
#endif
|
2012-01-09 21:50:01 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 01:55:16 +00:00
|
|
|
static bool
|
|
|
|
SupportsSelfCopy(cairo_surface_t* surface)
|
|
|
|
{
|
|
|
|
switch (cairo_surface_get_type(surface))
|
|
|
|
{
|
|
|
|
#ifdef CAIRO_HAS_QUARTZ_SURFACE
|
|
|
|
case CAIRO_SURFACE_TYPE_QUARTZ:
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
#ifdef CAIRO_HAS_WIN32_SURFACE
|
|
|
|
case CAIRO_SURFACE_TYPE_WIN32:
|
|
|
|
case CAIRO_SURFACE_TYPE_WIN32_PRINTING:
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 22:19:11 +00:00
|
|
|
static bool
|
|
|
|
PatternIsCompatible(const Pattern& aPattern)
|
|
|
|
{
|
|
|
|
switch (aPattern.GetType())
|
|
|
|
{
|
2014-01-10 19:06:17 +00:00
|
|
|
case PatternType::LINEAR_GRADIENT:
|
2012-01-09 22:19:11 +00:00
|
|
|
{
|
|
|
|
const LinearGradientPattern& pattern = static_cast<const LinearGradientPattern&>(aPattern);
|
2014-01-10 19:06:16 +00:00
|
|
|
return pattern.mStops->GetBackendType() == BackendType::CAIRO;
|
2012-01-09 22:19:11 +00:00
|
|
|
}
|
2014-01-10 19:06:17 +00:00
|
|
|
case PatternType::RADIAL_GRADIENT:
|
2012-01-09 22:19:11 +00:00
|
|
|
{
|
|
|
|
const RadialGradientPattern& pattern = static_cast<const RadialGradientPattern&>(aPattern);
|
2014-01-10 19:06:16 +00:00
|
|
|
return pattern.mStops->GetBackendType() == BackendType::CAIRO;
|
2012-01-09 22:19:11 +00:00
|
|
|
}
|
2012-02-04 12:22:47 +00:00
|
|
|
default:
|
|
|
|
return true;
|
2012-01-09 22:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:18:40 +00:00
|
|
|
static cairo_user_data_key_t surfaceDataKey;
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseData(void* aData)
|
|
|
|
{
|
|
|
|
static_cast<DataSourceSurface*>(aData)->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns cairo surface for the given SourceSurface.
|
|
|
|
* If possible, it will use the cairo_surface associated with aSurface,
|
|
|
|
* otherwise, it will create a new cairo_surface.
|
|
|
|
* In either case, the caller must call cairo_surface_destroy on the
|
|
|
|
* result when it is done with it.
|
|
|
|
*/
|
|
|
|
cairo_surface_t*
|
2013-08-20 23:52:20 +00:00
|
|
|
GetCairoSurfaceForSourceSurface(SourceSurface *aSurface, bool aExistingOnly = false)
|
2012-07-24 10:18:40 +00:00
|
|
|
{
|
2014-01-10 18:55:24 +00:00
|
|
|
if (aSurface->GetType() == SurfaceType::CAIRO) {
|
2012-07-24 10:18:40 +00:00
|
|
|
cairo_surface_t* surf = static_cast<SourceSurfaceCairo*>(aSurface)->GetSurface();
|
|
|
|
cairo_surface_reference(surf);
|
|
|
|
return surf;
|
|
|
|
}
|
|
|
|
|
2014-01-10 18:55:24 +00:00
|
|
|
if (aSurface->GetType() == SurfaceType::CAIRO_IMAGE) {
|
2012-07-24 10:18:40 +00:00
|
|
|
cairo_surface_t* surf =
|
|
|
|
static_cast<const DataSourceSurfaceCairo*>(aSurface)->GetSurface();
|
|
|
|
cairo_surface_reference(surf);
|
|
|
|
return surf;
|
|
|
|
}
|
|
|
|
|
2013-08-20 23:52:20 +00:00
|
|
|
if (aExistingOnly) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:18:40 +00:00
|
|
|
RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
|
2012-08-22 20:56:03 +00:00
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:18:40 +00:00
|
|
|
cairo_surface_t* surf =
|
|
|
|
cairo_image_surface_create_for_data(data->GetData(),
|
|
|
|
GfxFormatToCairoFormat(data->GetFormat()),
|
|
|
|
data->GetSize().width,
|
|
|
|
data->GetSize().height,
|
|
|
|
data->Stride());
|
2013-01-17 13:38:38 +00:00
|
|
|
|
|
|
|
// In certain scenarios, requesting larger than 8k image fails. Bug 803568
|
|
|
|
// covers the details of how to run into it, but the full detailed
|
|
|
|
// investigation hasn't been done to determine the underlying cause. We
|
|
|
|
// will just handle the failure to allocate the surface to avoid a crash.
|
|
|
|
if (cairo_surface_status(surf)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:18:40 +00:00
|
|
|
cairo_surface_set_user_data(surf,
|
|
|
|
&surfaceDataKey,
|
|
|
|
data.forget().drop(),
|
|
|
|
ReleaseData);
|
|
|
|
return surf;
|
|
|
|
}
|
|
|
|
|
2013-08-20 23:52:20 +00:00
|
|
|
// An RAII class to temporarily clear any device offset set
|
|
|
|
// on a surface. Note that this does not take a reference to the
|
|
|
|
// surface.
|
|
|
|
class AutoClearDeviceOffset
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoClearDeviceOffset(SourceSurface* aSurface)
|
|
|
|
: mSurface(nullptr)
|
|
|
|
{
|
|
|
|
Init(aSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoClearDeviceOffset(const Pattern& aPattern)
|
|
|
|
: mSurface(nullptr)
|
|
|
|
{
|
2014-01-10 19:06:17 +00:00
|
|
|
if (aPattern.GetType() == PatternType::SURFACE) {
|
2013-08-20 23:52:20 +00:00
|
|
|
const SurfacePattern& pattern = static_cast<const SurfacePattern&>(aPattern);
|
|
|
|
Init(pattern.mSurface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoClearDeviceOffset()
|
|
|
|
{
|
|
|
|
if (mSurface) {
|
|
|
|
cairo_surface_set_device_offset(mSurface, mX, mY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Init(SourceSurface* aSurface)
|
|
|
|
{
|
|
|
|
cairo_surface_t* surface = GetCairoSurfaceForSourceSurface(aSurface, true);
|
|
|
|
if (surface) {
|
|
|
|
Init(surface);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init(cairo_surface_t *aSurface)
|
|
|
|
{
|
|
|
|
mSurface = aSurface;
|
|
|
|
cairo_surface_get_device_offset(mSurface, &mX, &mY);
|
|
|
|
cairo_surface_set_device_offset(mSurface, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t* mSurface;
|
|
|
|
double mX;
|
|
|
|
double mY;
|
|
|
|
};
|
|
|
|
|
2012-08-14 18:06:12 +00:00
|
|
|
// Never returns nullptr. As such, you must always pass in Cairo-compatible
|
2012-01-09 22:19:11 +00:00
|
|
|
// patterns, most notably gradients with a GradientStopCairo.
|
|
|
|
// The pattern returned must have cairo_pattern_destroy() called on it by the
|
|
|
|
// caller.
|
|
|
|
// As the cairo_pattern_t returned may depend on the Pattern passed in, the
|
|
|
|
// lifetime of the cairo_pattern_t returned must not exceed the lifetime of the
|
|
|
|
// Pattern passed in.
|
2012-01-09 22:15:10 +00:00
|
|
|
static cairo_pattern_t*
|
2012-01-09 21:50:01 +00:00
|
|
|
GfxPatternToCairoPattern(const Pattern& aPattern, Float aAlpha)
|
|
|
|
{
|
2012-01-09 22:19:11 +00:00
|
|
|
cairo_pattern_t* pat;
|
2013-08-20 23:57:57 +00:00
|
|
|
const Matrix* matrix = nullptr;
|
2012-01-09 21:50:01 +00:00
|
|
|
|
|
|
|
switch (aPattern.GetType())
|
|
|
|
{
|
2014-01-10 19:06:17 +00:00
|
|
|
case PatternType::COLOR:
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
|
|
|
Color color = static_cast<const ColorPattern&>(aPattern).mColor;
|
|
|
|
pat = cairo_pattern_create_rgba(color.r, color.g, color.b, color.a * aAlpha);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-10 19:06:17 +00:00
|
|
|
case PatternType::SURFACE:
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
|
|
|
const SurfacePattern& pattern = static_cast<const SurfacePattern&>(aPattern);
|
2012-07-24 10:18:40 +00:00
|
|
|
cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(pattern.mSurface);
|
2014-04-15 04:57:29 +00:00
|
|
|
if (!surf)
|
|
|
|
return nullptr;
|
2012-01-09 21:50:01 +00:00
|
|
|
|
|
|
|
pat = cairo_pattern_create_for_surface(surf);
|
2012-09-05 01:01:57 +00:00
|
|
|
|
2013-08-20 23:57:57 +00:00
|
|
|
matrix = &pattern.mMatrix;
|
2012-09-05 01:01:57 +00:00
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
cairo_pattern_set_filter(pat, GfxFilterToCairoFilter(pattern.mFilter));
|
|
|
|
cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(pattern.mExtendMode));
|
|
|
|
|
|
|
|
cairo_surface_destroy(surf);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-10 19:06:17 +00:00
|
|
|
case PatternType::LINEAR_GRADIENT:
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
|
|
|
const LinearGradientPattern& pattern = static_cast<const LinearGradientPattern&>(aPattern);
|
2012-01-09 22:19:11 +00:00
|
|
|
|
|
|
|
pat = cairo_pattern_create_linear(pattern.mBegin.x, pattern.mBegin.y,
|
|
|
|
pattern.mEnd.x, pattern.mEnd.y);
|
|
|
|
|
2014-01-10 19:06:16 +00:00
|
|
|
MOZ_ASSERT(pattern.mStops->GetBackendType() == BackendType::CAIRO);
|
2012-10-10 10:32:36 +00:00
|
|
|
GradientStopsCairo* cairoStops = static_cast<GradientStopsCairo*>(pattern.mStops.get());
|
|
|
|
cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(cairoStops->GetExtendMode()));
|
|
|
|
|
2013-08-20 23:57:57 +00:00
|
|
|
matrix = &pattern.mMatrix;
|
|
|
|
|
2012-10-10 10:32:36 +00:00
|
|
|
const std::vector<GradientStop>& stops = cairoStops->GetStops();
|
2012-01-09 22:19:11 +00:00
|
|
|
for (size_t i = 0; i < stops.size(); ++i) {
|
|
|
|
const GradientStop& stop = stops[i];
|
|
|
|
cairo_pattern_add_color_stop_rgba(pat, stop.offset, stop.color.r,
|
|
|
|
stop.color.g, stop.color.b,
|
|
|
|
stop.color.a);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-01-10 19:06:17 +00:00
|
|
|
case PatternType::RADIAL_GRADIENT:
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
|
|
|
const RadialGradientPattern& pattern = static_cast<const RadialGradientPattern&>(aPattern);
|
2012-01-09 22:19:11 +00:00
|
|
|
|
|
|
|
pat = cairo_pattern_create_radial(pattern.mCenter1.x, pattern.mCenter1.y, pattern.mRadius1,
|
|
|
|
pattern.mCenter2.x, pattern.mCenter2.y, pattern.mRadius2);
|
|
|
|
|
2014-01-10 19:06:16 +00:00
|
|
|
MOZ_ASSERT(pattern.mStops->GetBackendType() == BackendType::CAIRO);
|
2012-10-10 10:32:36 +00:00
|
|
|
GradientStopsCairo* cairoStops = static_cast<GradientStopsCairo*>(pattern.mStops.get());
|
|
|
|
cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(cairoStops->GetExtendMode()));
|
|
|
|
|
2013-08-20 23:57:57 +00:00
|
|
|
matrix = &pattern.mMatrix;
|
|
|
|
|
2012-10-10 10:32:36 +00:00
|
|
|
const std::vector<GradientStop>& stops = cairoStops->GetStops();
|
2012-01-09 22:19:11 +00:00
|
|
|
for (size_t i = 0; i < stops.size(); ++i) {
|
|
|
|
const GradientStop& stop = stops[i];
|
|
|
|
cairo_pattern_add_color_stop_rgba(pat, stop.offset, stop.color.r,
|
|
|
|
stop.color.g, stop.color.b,
|
|
|
|
stop.color.a);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-01-09 22:19:11 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
// We should support all pattern types!
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 23:57:57 +00:00
|
|
|
// The pattern matrix is a matrix that transforms the pattern into user
|
|
|
|
// space. Cairo takes a matrix that converts from user space to pattern
|
|
|
|
// space. Cairo therefore needs the inverse.
|
|
|
|
if (matrix) {
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
GfxMatrixToCairoMatrix(*matrix, mat);
|
|
|
|
cairo_matrix_invert(&mat);
|
|
|
|
cairo_pattern_set_matrix(pat, &mat);
|
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
return pat;
|
|
|
|
}
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
static bool
|
2012-01-09 21:50:01 +00:00
|
|
|
NeedIntermediateSurface(const Pattern& aPattern, const DrawOptions& aOptions)
|
|
|
|
{
|
|
|
|
// We pre-multiply colours' alpha by the global alpha, so we don't need to
|
|
|
|
// use an intermediate surface for them.
|
2014-01-10 19:06:17 +00:00
|
|
|
if (aPattern.GetType() == PatternType::COLOR)
|
2012-01-09 21:50:01 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (aOptions.mAlpha == 1.0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
DrawTargetCairo::DrawTargetCairo()
|
2012-08-14 18:06:12 +00:00
|
|
|
: mContext(nullptr)
|
2013-10-11 20:47:47 +00:00
|
|
|
, mLockedBits(nullptr)
|
2011-06-24 17:41:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawTargetCairo::~DrawTargetCairo()
|
|
|
|
{
|
|
|
|
cairo_destroy(mContext);
|
2012-07-24 10:18:38 +00:00
|
|
|
if (mSurface) {
|
|
|
|
cairo_surface_destroy(mSurface);
|
|
|
|
}
|
2013-10-11 20:47:47 +00:00
|
|
|
MOZ_ASSERT(!mLockedBits);
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
IntSize
|
|
|
|
DrawTargetCairo::GetSize()
|
|
|
|
{
|
2012-07-24 10:18:38 +00:00
|
|
|
return mSize;
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::Snapshot()
|
|
|
|
{
|
2012-09-06 04:07:53 +00:00
|
|
|
if (mSnapshot) {
|
|
|
|
return mSnapshot;
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:18:38 +00:00
|
|
|
IntSize size = GetSize();
|
|
|
|
|
|
|
|
cairo_content_t content = cairo_surface_get_content(mSurface);
|
2012-09-06 04:07:53 +00:00
|
|
|
mSnapshot = new SourceSurfaceCairo(mSurface,
|
|
|
|
size,
|
|
|
|
CairoContentToGfxFormat(content),
|
|
|
|
this);
|
|
|
|
return mSnapshot;
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 20:47:47 +00:00
|
|
|
bool
|
|
|
|
DrawTargetCairo::LockBits(uint8_t** aData, IntSize* aSize,
|
|
|
|
int32_t* aStride, SurfaceFormat* aFormat)
|
|
|
|
{
|
|
|
|
if (cairo_surface_get_type(mSurface) == CAIRO_SURFACE_TYPE_IMAGE) {
|
|
|
|
WillChange();
|
|
|
|
|
|
|
|
mLockedBits = cairo_image_surface_get_data(mSurface);
|
|
|
|
*aData = mLockedBits;
|
|
|
|
*aSize = GetSize();
|
|
|
|
*aStride = cairo_image_surface_get_stride(mSurface);
|
|
|
|
*aFormat = GetFormat();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::ReleaseBits(uint8_t* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mLockedBits == aData);
|
|
|
|
mLockedBits = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::Flush()
|
|
|
|
{
|
|
|
|
cairo_surface_t* surf = cairo_get_target(mContext);
|
|
|
|
cairo_surface_flush(surf);
|
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
void
|
2012-08-14 18:06:12 +00:00
|
|
|
DrawTargetCairo::PrepareForDrawing(cairo_t* aContext, const Path* aPath /* = nullptr */)
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
WillChange(aPath);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 14:20:24 +00:00
|
|
|
cairo_surface_t*
|
|
|
|
DrawTargetCairo::GetDummySurface()
|
|
|
|
{
|
|
|
|
if (mDummySurface) {
|
|
|
|
return mDummySurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDummySurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
|
|
|
|
|
|
|
|
return mDummySurface;
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::DrawSurface(SourceSurface *aSurface,
|
|
|
|
const Rect &aDest,
|
|
|
|
const Rect &aSource,
|
|
|
|
const DrawSurfaceOptions &aSurfOptions,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2013-08-20 23:52:20 +00:00
|
|
|
AutoClearDeviceOffset clear(aSurface);
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
float sx = aSource.Width() / aDest.Width();
|
|
|
|
float sy = aSource.Height() / aDest.Height();
|
|
|
|
|
|
|
|
cairo_matrix_t src_mat;
|
2012-07-24 10:18:38 +00:00
|
|
|
cairo_matrix_init_translate(&src_mat, aSource.X(), aSource.Y());
|
|
|
|
cairo_matrix_scale(&src_mat, sx, sy);
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2012-07-24 10:18:39 +00:00
|
|
|
cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aSurface);
|
2012-07-26 06:48:24 +00:00
|
|
|
cairo_pattern_t* pat = cairo_pattern_create_for_surface(surf);
|
2012-07-24 10:18:39 +00:00
|
|
|
cairo_surface_destroy(surf);
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
cairo_pattern_set_matrix(pat, &src_mat);
|
|
|
|
cairo_pattern_set_filter(pat, GfxFilterToCairoFilter(aSurfOptions.mFilter));
|
2012-07-24 10:18:38 +00:00
|
|
|
cairo_pattern_set_extend(pat, CAIRO_EXTEND_PAD);
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2013-08-20 23:56:27 +00:00
|
|
|
cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
|
|
|
|
|
2013-09-11 05:08:53 +00:00
|
|
|
// If the destination rect covers the entire clipped area, then unbounded and bounded
|
|
|
|
// operations are identical, and we don't need to push a group.
|
|
|
|
bool needsGroup = !IsOperatorBoundByMask(aOptions.mCompositionOp) &&
|
2013-10-22 10:11:30 +00:00
|
|
|
!aDest.Contains(GetUserSpaceClip());
|
2013-09-11 05:08:53 +00:00
|
|
|
|
2012-07-26 06:48:24 +00:00
|
|
|
cairo_translate(mContext, aDest.X(), aDest.Y());
|
|
|
|
|
2013-09-11 05:08:53 +00:00
|
|
|
if (needsGroup) {
|
2012-07-24 10:18:38 +00:00
|
|
|
cairo_push_group(mContext);
|
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, 0, 0, aDest.Width(), aDest.Height());
|
|
|
|
cairo_set_source(mContext, pat);
|
|
|
|
cairo_fill(mContext);
|
|
|
|
cairo_pop_group_to_source(mContext);
|
2013-09-11 05:08:53 +00:00
|
|
|
} else {
|
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, 0, 0, aDest.Width(), aDest.Height());
|
|
|
|
cairo_clip(mContext);
|
|
|
|
cairo_set_source(mContext, pat);
|
2012-07-24 10:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
|
2012-07-26 06:48:24 +00:00
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
cairo_paint_with_alpha(mContext, aOptions.mAlpha);
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
cairo_pattern_destroy(pat);
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:22:56 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::DrawFilter(FilterNode *aNode,
|
|
|
|
const Rect &aSourceRect,
|
|
|
|
const Point &aDestPoint,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
|
|
|
FilterNodeSoftware* filter = static_cast<FilterNodeSoftware*>(aNode);
|
|
|
|
filter->Draw(this, aSourceRect, aDestPoint, aOptions);
|
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::DrawSurfaceWithShadow(SourceSurface *aSurface,
|
|
|
|
const Point &aDest,
|
|
|
|
const Color &aColor,
|
|
|
|
const Point &aOffset,
|
|
|
|
Float aSigma,
|
|
|
|
CompositionOp aOperator)
|
|
|
|
{
|
2014-01-10 18:55:24 +00:00
|
|
|
if (aSurface->GetType() != SurfaceType::CAIRO) {
|
2012-01-09 21:50:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-04-15 04:57:29 +00:00
|
|
|
|
2013-08-20 23:52:20 +00:00
|
|
|
AutoClearDeviceOffset clear(aSurface);
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2012-09-28 17:21:40 +00:00
|
|
|
Float width = Float(aSurface->GetSize().width);
|
|
|
|
Float height = Float(aSurface->GetSize().height);
|
|
|
|
|
2012-08-27 10:27:40 +00:00
|
|
|
SourceSurfaceCairo* source = static_cast<SourceSurfaceCairo*>(aSurface);
|
2012-09-02 23:07:06 +00:00
|
|
|
cairo_surface_t* sourcesurf = source->GetSurface();
|
2012-09-02 23:07:06 +00:00
|
|
|
cairo_surface_t* blursurf;
|
|
|
|
cairo_surface_t* surf;
|
|
|
|
|
|
|
|
// We only use the A8 surface for blurred shadows. Unblurred shadows can just
|
|
|
|
// use the RGBA surface directly.
|
|
|
|
if (cairo_surface_get_type(sourcesurf) == CAIRO_SURFACE_TYPE_TEE) {
|
|
|
|
blursurf = cairo_tee_surface_index(sourcesurf, 0);
|
|
|
|
surf = cairo_tee_surface_index(sourcesurf, 1);
|
|
|
|
|
|
|
|
MOZ_ASSERT(cairo_surface_get_type(blursurf) == CAIRO_SURFACE_TYPE_IMAGE);
|
|
|
|
Rect extents(0, 0, width, height);
|
2013-04-19 10:13:18 +00:00
|
|
|
AlphaBoxBlur blur(extents,
|
2012-09-02 23:07:06 +00:00
|
|
|
cairo_image_surface_get_stride(blursurf),
|
2013-11-27 11:22:36 +00:00
|
|
|
aSigma, aSigma);
|
2013-04-19 10:13:18 +00:00
|
|
|
blur.Blur(cairo_image_surface_get_data(blursurf));
|
2012-09-02 23:07:06 +00:00
|
|
|
} else {
|
|
|
|
blursurf = sourcesurf;
|
|
|
|
surf = sourcesurf;
|
|
|
|
}
|
2012-09-02 23:07:05 +00:00
|
|
|
|
|
|
|
WillChange();
|
|
|
|
ClearSurfaceForUnboundedSource(aOperator);
|
2012-10-25 02:03:21 +00:00
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
cairo_save(mContext);
|
2012-07-24 10:18:38 +00:00
|
|
|
cairo_set_operator(mContext, GfxOpToCairoOp(aOperator));
|
2012-01-09 21:50:01 +00:00
|
|
|
cairo_identity_matrix(mContext);
|
|
|
|
cairo_translate(mContext, aDest.x, aDest.y);
|
|
|
|
|
2012-12-05 00:02:52 +00:00
|
|
|
if (IsOperatorBoundByMask(aOperator)){
|
|
|
|
cairo_set_source_rgba(mContext, aColor.r, aColor.g, aColor.b, aColor.a);
|
|
|
|
cairo_mask_surface(mContext, blursurf, aOffset.x, aOffset.y);
|
|
|
|
|
|
|
|
// Now that the shadow has been drawn, we can draw the surface on top.
|
|
|
|
cairo_set_source_surface(mContext, surf, 0, 0);
|
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, 0, 0, width, height);
|
|
|
|
cairo_fill(mContext);
|
|
|
|
} else {
|
2012-07-24 10:18:38 +00:00
|
|
|
cairo_push_group(mContext);
|
|
|
|
cairo_set_source_rgba(mContext, aColor.r, aColor.g, aColor.b, aColor.a);
|
|
|
|
cairo_mask_surface(mContext, blursurf, aOffset.x, aOffset.y);
|
|
|
|
|
2012-09-02 23:07:05 +00:00
|
|
|
// Now that the shadow has been drawn, we can draw the surface on top.
|
|
|
|
cairo_set_source_surface(mContext, surf, 0, 0);
|
2012-07-24 10:18:38 +00:00
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, 0, 0, width, height);
|
|
|
|
cairo_fill(mContext);
|
|
|
|
cairo_pop_group_to_source(mContext);
|
2012-09-02 23:07:05 +00:00
|
|
|
cairo_paint(mContext);
|
2012-07-24 10:18:38 +00:00
|
|
|
}
|
2012-01-09 21:50:01 +00:00
|
|
|
|
|
|
|
cairo_restore(mContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-09 22:15:10 +00:00
|
|
|
DrawTargetCairo::DrawPattern(const Pattern& aPattern,
|
2012-01-09 21:50:01 +00:00
|
|
|
const StrokeOptions& aStrokeOptions,
|
|
|
|
const DrawOptions& aOptions,
|
2013-10-17 23:08:20 +00:00
|
|
|
DrawPatternType aDrawType,
|
|
|
|
bool aPathBoundsClip)
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2012-01-09 22:19:11 +00:00
|
|
|
if (!PatternIsCompatible(aPattern)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-15 04:57:29 +00:00
|
|
|
|
2013-08-20 23:52:20 +00:00
|
|
|
AutoClearDeviceOffset clear(aPattern);
|
2012-01-09 22:19:11 +00:00
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
cairo_pattern_t* pat = GfxPatternToCairoPattern(aPattern, aOptions.mAlpha);
|
2014-04-15 04:57:29 +00:00
|
|
|
if (!pat)
|
|
|
|
return;
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
cairo_set_source(mContext, pat);
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2013-08-20 23:56:27 +00:00
|
|
|
cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
|
|
|
|
|
2012-07-24 10:18:38 +00:00
|
|
|
if (NeedIntermediateSurface(aPattern, aOptions) ||
|
2013-10-17 23:08:20 +00:00
|
|
|
(!IsOperatorBoundByMask(aOptions.mCompositionOp) && !aPathBoundsClip)) {
|
2012-01-09 21:50:01 +00:00
|
|
|
cairo_push_group_with_content(mContext, CAIRO_CONTENT_COLOR_ALPHA);
|
|
|
|
|
|
|
|
// Don't want operators to be applied twice
|
2012-01-09 22:15:10 +00:00
|
|
|
cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);
|
2012-01-09 21:50:01 +00:00
|
|
|
|
|
|
|
if (aDrawType == DRAW_STROKE) {
|
2012-01-09 22:15:10 +00:00
|
|
|
SetCairoStrokeOptions(mContext, aStrokeOptions);
|
|
|
|
cairo_stroke_preserve(mContext);
|
2012-01-09 21:50:01 +00:00
|
|
|
} else {
|
2012-01-09 22:15:10 +00:00
|
|
|
cairo_fill_preserve(mContext);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cairo_pop_group_to_source(mContext);
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
// Now draw the content using the desired operator
|
|
|
|
cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
|
2012-01-09 21:50:01 +00:00
|
|
|
cairo_paint_with_alpha(mContext, aOptions.mAlpha);
|
2012-01-09 22:15:10 +00:00
|
|
|
} else {
|
|
|
|
cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
|
|
|
|
|
|
|
|
if (aDrawType == DRAW_STROKE) {
|
|
|
|
SetCairoStrokeOptions(mContext, aStrokeOptions);
|
|
|
|
cairo_stroke_preserve(mContext);
|
|
|
|
} else {
|
|
|
|
cairo_fill_preserve(mContext);
|
|
|
|
}
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
cairo_pattern_destroy(pat);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::FillRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
|
|
|
|
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, aRect.x, aRect.y, aRect.Width(), aRect.Height());
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2013-10-17 23:08:20 +00:00
|
|
|
bool pathBoundsClip = false;
|
|
|
|
|
2013-10-22 10:11:30 +00:00
|
|
|
if (aRect.Contains(GetUserSpaceClip())) {
|
2013-10-17 23:08:20 +00:00
|
|
|
pathBoundsClip = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawPattern(aPattern, StrokeOptions(), aOptions, DRAW_FILL, pathBoundsClip);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-15 01:55:16 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::CopySurfaceInternal(cairo_surface_t* aSurface,
|
|
|
|
const IntRect &aSource,
|
|
|
|
const IntPoint &aDest)
|
|
|
|
{
|
|
|
|
cairo_identity_matrix(mContext);
|
|
|
|
|
|
|
|
cairo_set_source_surface(mContext, aSurface, aDest.x - aSource.x, aDest.y - aSource.y);
|
|
|
|
cairo_set_operator(mContext, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_set_antialias(mContext, CAIRO_ANTIALIAS_NONE);
|
|
|
|
|
|
|
|
cairo_reset_clip(mContext);
|
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, aDest.x, aDest.y, aSource.width, aSource.height);
|
|
|
|
cairo_fill(mContext);
|
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::CopySurface(SourceSurface *aSurface,
|
2012-07-24 10:18:37 +00:00
|
|
|
const IntRect &aSource,
|
|
|
|
const IntPoint &aDest)
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2013-08-20 23:52:20 +00:00
|
|
|
AutoClearDeviceOffset clear(aSurface);
|
2012-07-24 10:18:37 +00:00
|
|
|
|
2013-11-08 01:39:34 +00:00
|
|
|
if (!aSurface) {
|
2012-07-24 10:18:37 +00:00
|
|
|
gfxWarning() << "Unsupported surface type specified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-08 01:39:34 +00:00
|
|
|
cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aSurface);
|
|
|
|
if (!surf) {
|
|
|
|
gfxWarning() << "Unsupported surface type specified";
|
|
|
|
return;
|
|
|
|
}
|
2012-07-24 10:18:37 +00:00
|
|
|
|
2013-10-15 01:55:16 +00:00
|
|
|
CopySurfaceInternal(surf, aSource, aDest);
|
2013-11-08 01:39:34 +00:00
|
|
|
cairo_surface_destroy(surf);
|
2013-10-15 01:55:16 +00:00
|
|
|
}
|
2012-07-24 10:18:37 +00:00
|
|
|
|
2013-10-15 01:55:16 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::CopyRect(const IntRect &aSource,
|
|
|
|
const IntPoint &aDest)
|
|
|
|
{
|
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-07-24 10:18:37 +00:00
|
|
|
|
2013-10-15 01:55:16 +00:00
|
|
|
IntRect source = aSource;
|
|
|
|
cairo_surface_t* surf = mSurface;
|
|
|
|
|
|
|
|
if (!SupportsSelfCopy(mSurface) &&
|
|
|
|
aDest.y >= aSource.y &&
|
|
|
|
aDest.y < aSource.YMost()) {
|
|
|
|
cairo_surface_t* similar = cairo_surface_create_similar(mSurface,
|
|
|
|
GfxFormatToCairoContent(GetFormat()),
|
|
|
|
aSource.width, aSource.height);
|
|
|
|
cairo_t* ctx = cairo_create(similar);
|
|
|
|
cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_set_source_surface(ctx, surf, -aSource.x, -aSource.y);
|
|
|
|
cairo_paint(ctx);
|
|
|
|
cairo_destroy(ctx);
|
|
|
|
|
|
|
|
source.x = 0;
|
|
|
|
source.y = 0;
|
|
|
|
surf = similar;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopySurfaceInternal(surf, source, aDest);
|
|
|
|
|
|
|
|
if (surf != mSurface) {
|
|
|
|
cairo_surface_destroy(surf);
|
|
|
|
}
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::ClearRect(const Rect& aRect)
|
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2013-08-20 23:56:27 +00:00
|
|
|
cairo_set_antialias(mContext, CAIRO_ANTIALIAS_NONE);
|
2011-06-24 17:41:16 +00:00
|
|
|
cairo_new_path(mContext);
|
2012-01-09 21:50:01 +00:00
|
|
|
cairo_set_operator(mContext, CAIRO_OPERATOR_CLEAR);
|
|
|
|
cairo_rectangle(mContext, aRect.X(), aRect.Y(),
|
|
|
|
aRect.Width(), aRect.Height());
|
|
|
|
cairo_fill(mContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::StrokeRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, aRect.x, aRect.y, aRect.Width(), aRect.Height());
|
|
|
|
|
|
|
|
DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::StrokeLine(const Point &aStart,
|
|
|
|
const Point &aEnd,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_move_to(mContext, aStart.x, aStart.y);
|
|
|
|
cairo_line_to(mContext, aEnd.x, aEnd.y);
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::Stroke(const Path *aPath,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext, aPath);
|
|
|
|
|
2014-01-10 19:06:16 +00:00
|
|
|
if (aPath->GetBackendType() != BackendType::CAIRO)
|
2012-01-09 22:15:10 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
|
2013-09-28 14:20:24 +00:00
|
|
|
path->SetPathOnContext(mContext);
|
2012-01-09 22:15:10 +00:00
|
|
|
|
|
|
|
DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::Fill(const Path *aPath,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext, aPath);
|
|
|
|
|
2014-01-10 19:06:16 +00:00
|
|
|
if (aPath->GetBackendType() != BackendType::CAIRO)
|
2012-01-09 22:15:10 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
|
2013-09-28 14:20:24 +00:00
|
|
|
path->SetPathOnContext(mContext);
|
2012-01-09 22:15:10 +00:00
|
|
|
|
|
|
|
DrawPattern(aPattern, StrokeOptions(), aOptions, DRAW_FILL);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 04:50:56 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::SetPermitSubpixelAA(bool aPermitSubpixelAA)
|
|
|
|
{
|
|
|
|
DrawTarget::SetPermitSubpixelAA(aPermitSubpixelAA);
|
2013-11-13 13:23:19 +00:00
|
|
|
#ifdef MOZ_TREE_CAIRO
|
2013-11-05 04:50:56 +00:00
|
|
|
cairo_surface_set_subpixel_antialiasing(mSurface,
|
|
|
|
aPermitSubpixelAA ? CAIRO_SUBPIXEL_ANTIALIASING_ENABLED : CAIRO_SUBPIXEL_ANTIALIASING_DISABLED);
|
2013-11-13 13:23:19 +00:00
|
|
|
#endif
|
2013-11-05 04:50:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::FillGlyphs(ScaledFont *aFont,
|
|
|
|
const GlyphBuffer &aBuffer,
|
|
|
|
const Pattern &aPattern,
|
2012-03-19 19:20:17 +00:00
|
|
|
const DrawOptions &aOptions,
|
|
|
|
const GlyphRenderingOptions*)
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2012-01-09 22:15:10 +00:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2013-08-20 23:52:20 +00:00
|
|
|
AutoClearDeviceOffset clear(aPattern);
|
2012-01-10 18:26:59 +00:00
|
|
|
|
2012-01-27 18:08:46 +00:00
|
|
|
ScaledFontBase* scaledFont = static_cast<ScaledFontBase*>(aFont);
|
2012-01-10 18:26:59 +00:00
|
|
|
cairo_set_scaled_font(mContext, scaledFont->GetCairoScaledFont());
|
|
|
|
|
|
|
|
cairo_pattern_t* pat = GfxPatternToCairoPattern(aPattern, aOptions.mAlpha);
|
2014-04-15 04:57:29 +00:00
|
|
|
if (!pat)
|
|
|
|
return;
|
|
|
|
|
2012-01-10 18:26:59 +00:00
|
|
|
cairo_set_source(mContext, pat);
|
|
|
|
cairo_pattern_destroy(pat);
|
|
|
|
|
2013-08-20 23:56:27 +00:00
|
|
|
cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
|
|
|
|
|
2012-01-10 18:26:59 +00:00
|
|
|
// Convert our GlyphBuffer into an array of Cairo glyphs.
|
|
|
|
std::vector<cairo_glyph_t> glyphs(aBuffer.mNumGlyphs);
|
|
|
|
for (uint32_t i = 0; i < aBuffer.mNumGlyphs; ++i) {
|
|
|
|
glyphs[i].index = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
glyphs[i].x = aBuffer.mGlyphs[i].mPosition.x;
|
|
|
|
glyphs[i].y = aBuffer.mGlyphs[i].mPosition.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_show_glyphs(mContext, &glyphs[0], aBuffer.mNumGlyphs);
|
2012-01-09 22:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::Mask(const Pattern &aSource,
|
|
|
|
const Pattern &aMask,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2013-08-20 23:52:20 +00:00
|
|
|
AutoClearDeviceOffset clearSource(aSource);
|
|
|
|
AutoClearDeviceOffset clearMask(aMask);
|
2012-10-03 00:14:38 +00:00
|
|
|
|
2013-08-20 23:56:27 +00:00
|
|
|
cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
|
|
|
|
|
2012-10-03 00:14:38 +00:00
|
|
|
cairo_pattern_t* source = GfxPatternToCairoPattern(aSource, aOptions.mAlpha);
|
2014-04-15 04:57:29 +00:00
|
|
|
if (!source)
|
|
|
|
return;
|
2012-10-03 00:14:38 +00:00
|
|
|
|
|
|
|
cairo_pattern_t* mask = GfxPatternToCairoPattern(aMask, aOptions.mAlpha);
|
2014-04-15 04:57:29 +00:00
|
|
|
if (!mask) {
|
|
|
|
cairo_pattern_destroy(source);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_set_source(mContext, source);
|
2012-10-03 00:14:38 +00:00
|
|
|
cairo_mask(mContext, mask);
|
|
|
|
|
|
|
|
cairo_pattern_destroy(mask);
|
|
|
|
cairo_pattern_destroy(source);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
2013-07-11 14:43:34 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::MaskSurface(const Pattern &aSource,
|
|
|
|
SourceSurface *aMask,
|
|
|
|
Point aOffset,
|
2013-06-13 03:57:51 +00:00
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2013-08-20 23:52:20 +00:00
|
|
|
AutoClearDeviceOffset clearSource(aSource);
|
|
|
|
AutoClearDeviceOffset clearMask(aMask);
|
2013-06-13 03:57:51 +00:00
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
if (!PatternIsCompatible(aSource)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-13 03:57:51 +00:00
|
|
|
|
2013-08-20 23:56:27 +00:00
|
|
|
cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
|
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
cairo_pattern_t* pat = GfxPatternToCairoPattern(aSource, aOptions.mAlpha);
|
2014-04-15 04:57:29 +00:00
|
|
|
if (!pat)
|
|
|
|
return;
|
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
cairo_set_source(mContext, pat);
|
2013-06-13 03:57:51 +00:00
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
if (NeedIntermediateSurface(aSource, aOptions)) {
|
|
|
|
cairo_push_group_with_content(mContext, CAIRO_CONTENT_COLOR_ALPHA);
|
2013-06-13 03:57:51 +00:00
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
// Don't want operators to be applied twice
|
|
|
|
cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);
|
2013-06-13 03:57:51 +00:00
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
// Now draw the content using the desired operator
|
|
|
|
cairo_paint_with_alpha(mContext, aOptions.mAlpha);
|
2013-06-13 03:57:51 +00:00
|
|
|
|
|
|
|
cairo_pop_group_to_source(mContext);
|
|
|
|
}
|
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aMask);
|
2014-04-09 09:15:19 +00:00
|
|
|
if (!surf) {
|
|
|
|
cairo_pattern_destroy(pat);
|
|
|
|
return;
|
|
|
|
}
|
2013-07-11 14:43:34 +00:00
|
|
|
cairo_pattern_t* mask = cairo_pattern_create_for_surface(surf);
|
|
|
|
cairo_matrix_t matrix;
|
2013-06-13 03:57:51 +00:00
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
cairo_matrix_init_translate (&matrix, -aOffset.x, -aOffset.y);
|
|
|
|
cairo_pattern_set_matrix (mask, &matrix);
|
2013-06-13 03:57:51 +00:00
|
|
|
|
2014-02-27 16:56:48 +00:00
|
|
|
cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
|
|
|
|
|
2013-07-11 14:43:34 +00:00
|
|
|
cairo_mask(mContext, mask);
|
|
|
|
|
|
|
|
cairo_surface_destroy(surf);
|
|
|
|
cairo_pattern_destroy(mask);
|
2013-06-13 03:57:51 +00:00
|
|
|
cairo_pattern_destroy(pat);
|
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::PushClip(const Path *aPath)
|
|
|
|
{
|
2014-01-10 19:06:16 +00:00
|
|
|
if (aPath->GetBackendType() != BackendType::CAIRO) {
|
2012-04-25 22:04:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WillChange(aPath);
|
|
|
|
cairo_save(mContext);
|
2012-05-17 21:54:22 +00:00
|
|
|
|
|
|
|
PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
|
2013-09-28 14:20:24 +00:00
|
|
|
path->SetPathOnContext(mContext);
|
2012-04-25 22:04:36 +00:00
|
|
|
cairo_clip_preserve(mContext);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::PushClipRect(const Rect& aRect)
|
|
|
|
{
|
2012-04-25 22:04:36 +00:00
|
|
|
WillChange();
|
|
|
|
cairo_save(mContext);
|
2012-05-17 21:54:22 +00:00
|
|
|
|
|
|
|
cairo_new_path(mContext);
|
2012-04-25 22:04:36 +00:00
|
|
|
cairo_rectangle(mContext, aRect.X(), aRect.Y(), aRect.Width(), aRect.Height());
|
|
|
|
cairo_clip_preserve(mContext);
|
2012-01-09 22:15:10 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::PopClip()
|
|
|
|
{
|
2012-05-17 21:54:22 +00:00
|
|
|
// save/restore does not affect the path, so no need to call WillChange()
|
2014-01-06 22:27:03 +00:00
|
|
|
|
|
|
|
// cairo_restore will restore the transform too and we don't want to do that
|
|
|
|
// so we'll save it now and restore it after the cairo_restore
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
cairo_get_matrix(mContext, &mat);
|
|
|
|
|
2012-04-25 22:04:36 +00:00
|
|
|
cairo_restore(mContext);
|
2014-01-06 22:27:03 +00:00
|
|
|
|
|
|
|
cairo_set_matrix(mContext, &mat);
|
|
|
|
|
2014-04-15 04:57:29 +00:00
|
|
|
MOZ_ASSERT(cairo_status(mContext) || GetTransform() == Matrix(mat.xx, mat.yx, mat.xy, mat.yy, mat.x0, mat.y0),
|
2014-01-06 22:27:03 +00:00
|
|
|
"Transforms are out of sync");
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<PathBuilder>
|
2014-01-10 19:06:17 +00:00
|
|
|
DrawTargetCairo::CreatePathBuilder(FillRule aFillRule /* = FillRule::FILL_WINDING */) const
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2013-09-28 14:20:24 +00:00
|
|
|
RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
|
2012-01-09 22:15:10 +00:00
|
|
|
|
|
|
|
return builder;
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2012-07-24 10:18:38 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::ClearSurfaceForUnboundedSource(const CompositionOp &aOperator)
|
|
|
|
{
|
2014-01-10 19:06:17 +00:00
|
|
|
if (aOperator != CompositionOp::OP_SOURCE)
|
2012-07-24 10:18:38 +00:00
|
|
|
return;
|
|
|
|
cairo_set_operator(mContext, CAIRO_OPERATOR_CLEAR);
|
|
|
|
// It doesn't really matter what the source is here, since Paint
|
|
|
|
// isn't bounded by the source and the mask covers the entire clip
|
|
|
|
// region.
|
|
|
|
cairo_paint(mContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
TemporaryRef<GradientStops>
|
2012-10-10 10:32:36 +00:00
|
|
|
DrawTargetCairo::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops,
|
|
|
|
ExtendMode aExtendMode) const
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2012-10-10 10:32:36 +00:00
|
|
|
RefPtr<GradientStopsCairo> stops = new GradientStopsCairo(aStops, aNumStops,
|
|
|
|
aExtendMode);
|
2012-01-09 21:50:01 +00:00
|
|
|
return stops;
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:22:56 +00:00
|
|
|
TemporaryRef<FilterNode>
|
|
|
|
DrawTargetCairo::CreateFilter(FilterType aType)
|
|
|
|
{
|
|
|
|
return FilterNodeSoftware::Create(aType);
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:18:38 +00:00
|
|
|
/**
|
|
|
|
* Copies pixel data from aData into aSurface; aData must have the dimensions
|
|
|
|
* given in aSize, with a stride of aStride bytes and aPixelWidth bytes per pixel
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CopyDataToCairoSurface(cairo_surface_t* aSurface,
|
|
|
|
unsigned char *aData,
|
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
int32_t aPixelWidth)
|
|
|
|
{
|
|
|
|
unsigned char* surfData = cairo_image_surface_get_data(aSurface);
|
2013-09-27 15:21:09 +00:00
|
|
|
int surfStride = cairo_image_surface_get_stride(aSurface);
|
2013-01-17 13:38:38 +00:00
|
|
|
// In certain scenarios, requesting larger than 8k image fails. Bug 803568
|
|
|
|
// covers the details of how to run into it, but the full detailed
|
|
|
|
// investigation hasn't been done to determine the underlying cause. We
|
|
|
|
// will just handle the failure to allocate the surface to avoid a crash.
|
|
|
|
if (!surfData) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-24 10:18:38 +00:00
|
|
|
for (int32_t y = 0; y < aSize.height; ++y) {
|
2013-09-27 15:21:09 +00:00
|
|
|
memcpy(surfData + y * surfStride,
|
2012-07-24 10:18:38 +00:00
|
|
|
aData + y * aStride,
|
|
|
|
aSize.width * aPixelWidth);
|
|
|
|
}
|
|
|
|
cairo_surface_mark_dirty(aSurface);
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::CreateSourceSurfaceFromData(unsigned char *aData,
|
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat) const
|
|
|
|
{
|
2012-07-24 10:18:38 +00:00
|
|
|
cairo_surface_t* surf = cairo_image_surface_create(GfxFormatToCairoFormat(aFormat),
|
|
|
|
aSize.width,
|
|
|
|
aSize.height);
|
2013-01-17 13:38:38 +00:00
|
|
|
// In certain scenarios, requesting larger than 8k image fails. Bug 803568
|
|
|
|
// covers the details of how to run into it, but the full detailed
|
|
|
|
// investigation hasn't been done to determine the underlying cause. We
|
|
|
|
// will just handle the failure to allocate the surface to avoid a crash.
|
|
|
|
if (cairo_surface_status(surf)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:18:38 +00:00
|
|
|
CopyDataToCairoSurface(surf, aData, aSize, aStride, BytesPerPixel(aFormat));
|
2012-10-25 02:03:21 +00:00
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
RefPtr<SourceSurfaceCairo> source_surf = new SourceSurfaceCairo(surf, aSize, aFormat);
|
2011-06-24 17:41:16 +00:00
|
|
|
cairo_surface_destroy(surf);
|
2012-07-24 10:18:38 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
return source_surf;
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::OptimizeSourceSurface(SourceSurface *aSurface) const
|
|
|
|
{
|
2012-01-09 21:50:01 +00:00
|
|
|
return aSurface;
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
|
|
|
|
{
|
2014-01-10 19:06:16 +00:00
|
|
|
if (aSurface.mType == NativeSurfaceType::CAIRO_SURFACE) {
|
2012-01-09 21:50:01 +00:00
|
|
|
IntSize size;
|
|
|
|
cairo_surface_t* surf = static_cast<cairo_surface_t*>(aSurface.mSurface);
|
|
|
|
if (GetCairoSurfaceSize(surf, size)) {
|
2012-01-09 22:19:11 +00:00
|
|
|
RefPtr<SourceSurfaceCairo> source =
|
2012-01-09 21:50:01 +00:00
|
|
|
new SourceSurfaceCairo(surf, size, aSurface.mFormat);
|
2012-01-09 22:19:11 +00:00
|
|
|
return source;
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 18:06:12 +00:00
|
|
|
return nullptr;
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 21:25:40 +00:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::CreateSourceSurfaceForCairoSurface(cairo_surface_t *aSurface,
|
|
|
|
SurfaceFormat aFormat)
|
|
|
|
{
|
|
|
|
IntSize size;
|
|
|
|
if (GetCairoSurfaceSize(aSurface, size)) {
|
|
|
|
RefPtr<SourceSurfaceCairo> source =
|
|
|
|
new SourceSurfaceCairo(aSurface, size, aFormat);
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
TemporaryRef<DrawTarget>
|
|
|
|
DrawTargetCairo::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
|
|
|
|
{
|
|
|
|
cairo_surface_t* similar = cairo_surface_create_similar(cairo_get_target(mContext),
|
|
|
|
GfxFormatToCairoContent(aFormat),
|
|
|
|
aSize.width, aSize.height);
|
|
|
|
|
|
|
|
if (!cairo_surface_status(similar)) {
|
|
|
|
RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
|
2012-08-29 00:55:20 +00:00
|
|
|
target->InitAlreadyReferenced(similar, aSize);
|
2012-01-09 21:50:01 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2012-08-14 18:06:12 +00:00
|
|
|
return nullptr;
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-04-14 18:51:00 +00:00
|
|
|
DrawTargetCairo::InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
|
2011-06-24 17:41:16 +00:00
|
|
|
{
|
|
|
|
mContext = cairo_create(aSurface);
|
2012-07-24 10:18:38 +00:00
|
|
|
mSurface = aSurface;
|
2012-07-24 10:18:38 +00:00
|
|
|
mSize = aSize;
|
2014-04-14 18:51:00 +00:00
|
|
|
mFormat = aFormat ? *aFormat : CairoContentToGfxFormat(cairo_surface_get_content(aSurface));
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2014-01-10 19:06:16 +00:00
|
|
|
if (mFormat == SurfaceFormat::B8G8R8A8 ||
|
|
|
|
mFormat == SurfaceFormat::R8G8B8A8) {
|
2013-11-05 04:50:56 +00:00
|
|
|
SetPermitSubpixelAA(false);
|
|
|
|
} else {
|
|
|
|
SetPermitSubpixelAA(true);
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-02 23:07:06 +00:00
|
|
|
TemporaryRef<DrawTarget>
|
2012-09-02 23:07:06 +00:00
|
|
|
DrawTargetCairo::CreateShadowDrawTarget(const IntSize &aSize, SurfaceFormat aFormat,
|
|
|
|
float aSigma) const
|
2012-09-02 23:07:06 +00:00
|
|
|
{
|
|
|
|
cairo_surface_t* similar = cairo_surface_create_similar(cairo_get_target(mContext),
|
|
|
|
GfxFormatToCairoContent(aFormat),
|
|
|
|
aSize.width, aSize.height);
|
|
|
|
|
|
|
|
if (cairo_surface_status(similar)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-02 23:07:06 +00:00
|
|
|
// If we don't have a blur then we can use the RGBA mask and keep all the
|
|
|
|
// operations in graphics memory.
|
|
|
|
if (aSigma == 0.0F) {
|
|
|
|
RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
|
|
|
|
target->InitAlreadyReferenced(similar, aSize);
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2012-09-02 23:07:06 +00:00
|
|
|
cairo_surface_t* blursurf = cairo_image_surface_create(CAIRO_FORMAT_A8,
|
|
|
|
aSize.width,
|
|
|
|
aSize.height);
|
|
|
|
|
|
|
|
if (cairo_surface_status(blursurf)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t* tee = cairo_tee_surface_create(blursurf);
|
|
|
|
cairo_surface_destroy(blursurf);
|
|
|
|
if (cairo_surface_status(tee)) {
|
|
|
|
cairo_surface_destroy(similar);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_tee_surface_add(tee, similar);
|
|
|
|
cairo_surface_destroy(similar);
|
|
|
|
|
|
|
|
RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
|
|
|
|
target->InitAlreadyReferenced(tee, aSize);
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2012-08-23 23:50:59 +00:00
|
|
|
bool
|
2014-04-14 18:51:00 +00:00
|
|
|
DrawTargetCairo::Init(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
|
2012-08-23 23:50:59 +00:00
|
|
|
{
|
|
|
|
cairo_surface_reference(aSurface);
|
2014-04-14 18:51:00 +00:00
|
|
|
return InitAlreadyReferenced(aSurface, aSize, aFormat);
|
2012-08-23 23:50:59 +00:00
|
|
|
}
|
|
|
|
|
2013-11-03 23:57:36 +00:00
|
|
|
bool
|
|
|
|
DrawTargetCairo::Init(const IntSize& aSize, SurfaceFormat aFormat)
|
|
|
|
{
|
|
|
|
cairo_surface_t *surf = cairo_image_surface_create(GfxFormatToCairoFormat(aFormat), aSize.width, aSize.height);
|
|
|
|
return InitAlreadyReferenced(surf, aSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DrawTargetCairo::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat)
|
|
|
|
{
|
|
|
|
cairo_surface_t* surf =
|
|
|
|
cairo_image_surface_create_for_data(aData,
|
|
|
|
GfxFormatToCairoFormat(aFormat),
|
|
|
|
aSize.width,
|
|
|
|
aSize.height,
|
|
|
|
aStride);
|
|
|
|
return InitAlreadyReferenced(surf, aSize);
|
|
|
|
}
|
|
|
|
|
2012-01-09 21:50:01 +00:00
|
|
|
void *
|
|
|
|
DrawTargetCairo::GetNativeSurface(NativeSurfaceType aType)
|
|
|
|
{
|
2014-01-10 19:06:16 +00:00
|
|
|
if (aType == NativeSurfaceType::CAIRO_SURFACE) {
|
2012-01-09 21:50:01 +00:00
|
|
|
return cairo_get_target(mContext);
|
|
|
|
}
|
2014-01-10 19:06:16 +00:00
|
|
|
if (aType == NativeSurfaceType::CAIRO_CONTEXT) {
|
2013-08-23 04:53:53 +00:00
|
|
|
return mContext;
|
|
|
|
}
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2012-08-14 18:06:12 +00:00
|
|
|
return nullptr;
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
void
|
2012-09-06 04:07:53 +00:00
|
|
|
DrawTargetCairo::MarkSnapshotIndependent()
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2012-09-06 04:07:53 +00:00
|
|
|
if (mSnapshot) {
|
|
|
|
if (mSnapshot->refCount() > 1) {
|
|
|
|
// We only need to worry about snapshots that someone else knows about
|
|
|
|
mSnapshot->DrawTargetWillChange();
|
|
|
|
}
|
|
|
|
mSnapshot = nullptr;
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-14 18:06:12 +00:00
|
|
|
DrawTargetCairo::WillChange(const Path* aPath /* = nullptr */)
|
2012-01-09 21:50:01 +00:00
|
|
|
{
|
2012-09-06 04:07:53 +00:00
|
|
|
MarkSnapshotIndependent();
|
2013-10-11 20:47:47 +00:00
|
|
|
MOZ_ASSERT(!mLockedBits);
|
2012-01-09 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 22:15:10 +00:00
|
|
|
void
|
|
|
|
DrawTargetCairo::SetTransform(const Matrix& aTransform)
|
|
|
|
{
|
|
|
|
mTransform = aTransform;
|
|
|
|
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
GfxMatrixToCairoMatrix(mTransform, mat);
|
|
|
|
cairo_set_matrix(mContext, &mat);
|
|
|
|
}
|
2012-01-09 21:50:01 +00:00
|
|
|
|
2013-10-22 10:11:30 +00:00
|
|
|
Rect
|
|
|
|
DrawTargetCairo::GetUserSpaceClip()
|
|
|
|
{
|
|
|
|
double clipX1, clipY1, clipX2, clipY2;
|
|
|
|
cairo_clip_extents(mContext, &clipX1, &clipY1, &clipX2, &clipY2);
|
|
|
|
return Rect(clipX1, clipY1, clipX2 - clipX1, clipY2 - clipY1); // Narrowing of doubles to floats
|
|
|
|
}
|
|
|
|
|
2013-09-20 02:00:35 +00:00
|
|
|
cairo_t*
|
|
|
|
BorrowedCairoContext::BorrowCairoContextFromDrawTarget(DrawTarget* aDT)
|
|
|
|
{
|
2014-01-10 19:06:16 +00:00
|
|
|
if (aDT->GetType() != BackendType::CAIRO || aDT->IsDualDrawTarget()) {
|
2013-09-20 02:00:35 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
DrawTargetCairo* cairoDT = static_cast<DrawTargetCairo*>(aDT);
|
|
|
|
|
|
|
|
cairoDT->WillChange();
|
|
|
|
|
|
|
|
// save the state to make it easier for callers to avoid mucking with things
|
|
|
|
cairo_save(cairoDT->mContext);
|
|
|
|
|
|
|
|
// Neuter the DrawTarget while the context is being borrowed
|
|
|
|
cairo_t* cairo = cairoDT->mContext;
|
|
|
|
cairoDT->mContext = nullptr;
|
|
|
|
|
|
|
|
return cairo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BorrowedCairoContext::ReturnCairoContextToDrawTarget(DrawTarget* aDT,
|
|
|
|
cairo_t* aCairo)
|
|
|
|
{
|
2014-01-10 19:06:16 +00:00
|
|
|
if (aDT->GetType() != BackendType::CAIRO || aDT->IsDualDrawTarget()) {
|
2013-09-20 02:00:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
DrawTargetCairo* cairoDT = static_cast<DrawTargetCairo*>(aDT);
|
|
|
|
|
|
|
|
cairo_restore(aCairo);
|
|
|
|
cairoDT->mContext = aCairo;
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
}
|