2011-06-24 10:41:16 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07: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 10:41:16 -07:00
|
|
|
|
|
|
|
#include "DrawTargetCairo.h"
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
#include "SourceSurfaceCairo.h"
|
2012-01-09 14:15:10 -08:00
|
|
|
#include "PathCairo.h"
|
|
|
|
#include "HelpersCairo.h"
|
2012-01-27 10:08:46 -08:00
|
|
|
#include "ScaledFontBase.h"
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
#include "cairo.h"
|
2012-07-27 14:51:53 -07:00
|
|
|
#include <string.h>
|
2012-01-09 13:50:01 -08:00
|
|
|
|
|
|
|
#include "Blur.h"
|
2012-07-24 03:18:37 -07:00
|
|
|
#include "Logging.h"
|
2012-07-24 03:18:38 -07:00
|
|
|
#include "Tools.h"
|
2012-01-09 13:50:01 -08: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
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
namespace {
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-01-09 14:15:10 -08: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 10:41:16 -07:00
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
public:
|
|
|
|
AutoPrepareForDrawing(DrawTargetCairo* dt, cairo_t* ctx)
|
|
|
|
: mCtx(ctx)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
dt->PrepareForDrawing(ctx);
|
|
|
|
cairo_save(mCtx);
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing(DrawTargetCairo* dt, cairo_t* ctx, const Path* path)
|
|
|
|
: mCtx(ctx)
|
2012-01-09 13:50:01 -08:00
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
dt->PrepareForDrawing(ctx, path);
|
|
|
|
cairo_save(mCtx);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
~AutoPrepareForDrawing() { cairo_restore(mCtx); }
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
private:
|
|
|
|
cairo_t* mCtx;
|
|
|
|
};
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
} // end anonymous namespace
|
2012-01-09 13:50:01 -08: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);
|
|
|
|
size.height = CGBitmapContextGetWidth(cgc);
|
2012-01-27 10:09:20 -08:00
|
|
|
return true;
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 14:19:11 -08:00
|
|
|
static bool
|
|
|
|
PatternIsCompatible(const Pattern& aPattern)
|
|
|
|
{
|
|
|
|
switch (aPattern.GetType())
|
|
|
|
{
|
|
|
|
case PATTERN_LINEAR_GRADIENT:
|
|
|
|
{
|
|
|
|
const LinearGradientPattern& pattern = static_cast<const LinearGradientPattern&>(aPattern);
|
|
|
|
return pattern.mStops->GetBackendType() == BACKEND_CAIRO;
|
|
|
|
}
|
|
|
|
case PATTERN_RADIAL_GRADIENT:
|
|
|
|
{
|
|
|
|
const RadialGradientPattern& pattern = static_cast<const RadialGradientPattern&>(aPattern);
|
|
|
|
return pattern.mStops->GetBackendType() == BACKEND_CAIRO;
|
|
|
|
}
|
2012-02-04 04:22:47 -08:00
|
|
|
default:
|
|
|
|
return true;
|
2012-01-09 14:19:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 03:18:40 -07: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*
|
|
|
|
GetCairoSurfaceForSourceSurface(SourceSurface *aSurface)
|
|
|
|
{
|
|
|
|
if (aSurface->GetType() == SURFACE_CAIRO) {
|
|
|
|
cairo_surface_t* surf = static_cast<SourceSurfaceCairo*>(aSurface)->GetSurface();
|
|
|
|
cairo_surface_reference(surf);
|
|
|
|
return surf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSurface->GetType() == SURFACE_CAIRO_IMAGE) {
|
|
|
|
cairo_surface_t* surf =
|
|
|
|
static_cast<const DataSourceSurfaceCairo*>(aSurface)->GetSurface();
|
|
|
|
cairo_surface_reference(surf);
|
|
|
|
return surf;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
|
2012-08-22 13:56:03 -07:00
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-24 03:18:40 -07:00
|
|
|
cairo_surface_t* surf =
|
|
|
|
cairo_image_surface_create_for_data(data->GetData(),
|
|
|
|
GfxFormatToCairoFormat(data->GetFormat()),
|
|
|
|
data->GetSize().width,
|
|
|
|
data->GetSize().height,
|
|
|
|
data->Stride());
|
|
|
|
cairo_surface_set_user_data(surf,
|
|
|
|
&surfaceDataKey,
|
|
|
|
data.forget().drop(),
|
|
|
|
ReleaseData);
|
|
|
|
return surf;
|
|
|
|
}
|
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
// Never returns nullptr. As such, you must always pass in Cairo-compatible
|
2012-01-09 14:19:11 -08: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 14:15:10 -08:00
|
|
|
static cairo_pattern_t*
|
2012-01-09 13:50:01 -08:00
|
|
|
GfxPatternToCairoPattern(const Pattern& aPattern, Float aAlpha)
|
|
|
|
{
|
2012-01-09 14:19:11 -08:00
|
|
|
cairo_pattern_t* pat;
|
2012-01-09 13:50:01 -08:00
|
|
|
|
|
|
|
switch (aPattern.GetType())
|
|
|
|
{
|
|
|
|
case PATTERN_COLOR:
|
|
|
|
{
|
|
|
|
Color color = static_cast<const ColorPattern&>(aPattern).mColor;
|
|
|
|
pat = cairo_pattern_create_rgba(color.r, color.g, color.b, color.a * aAlpha);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PATTERN_SURFACE:
|
|
|
|
{
|
|
|
|
const SurfacePattern& pattern = static_cast<const SurfacePattern&>(aPattern);
|
2012-07-24 03:18:40 -07:00
|
|
|
cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(pattern.mSurface);
|
2012-01-09 13:50:01 -08:00
|
|
|
|
|
|
|
pat = cairo_pattern_create_for_surface(surf);
|
|
|
|
cairo_pattern_set_filter(pat, GfxFilterToCairoFilter(pattern.mFilter));
|
|
|
|
cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(pattern.mExtendMode));
|
|
|
|
|
|
|
|
cairo_surface_destroy(surf);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PATTERN_LINEAR_GRADIENT:
|
|
|
|
{
|
|
|
|
const LinearGradientPattern& pattern = static_cast<const LinearGradientPattern&>(aPattern);
|
2012-01-09 14:19:11 -08:00
|
|
|
|
|
|
|
pat = cairo_pattern_create_linear(pattern.mBegin.x, pattern.mBegin.y,
|
|
|
|
pattern.mEnd.x, pattern.mEnd.y);
|
|
|
|
|
|
|
|
MOZ_ASSERT(pattern.mStops->GetBackendType() == BACKEND_CAIRO);
|
|
|
|
const std::vector<GradientStop>& stops =
|
|
|
|
static_cast<GradientStopsCairo*>(pattern.mStops.get())->GetStops();
|
|
|
|
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 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PATTERN_RADIAL_GRADIENT:
|
|
|
|
{
|
|
|
|
const RadialGradientPattern& pattern = static_cast<const RadialGradientPattern&>(aPattern);
|
2012-01-09 14:19:11 -08:00
|
|
|
|
|
|
|
pat = cairo_pattern_create_radial(pattern.mCenter1.x, pattern.mCenter1.y, pattern.mRadius1,
|
|
|
|
pattern.mCenter2.x, pattern.mCenter2.y, pattern.mRadius2);
|
|
|
|
|
|
|
|
const std::vector<GradientStop>& stops =
|
|
|
|
static_cast<GradientStopsCairo*>(pattern.mStops.get())->GetStops();
|
|
|
|
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 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-01-09 14:19:11 -08:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
// We should support all pattern types!
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pat;
|
|
|
|
}
|
|
|
|
|
2012-07-24 03:18:38 -07:00
|
|
|
/**
|
|
|
|
* Returns true iff the the given operator should affect areas of the
|
|
|
|
* destination where the source is transparent. Among other things, this
|
|
|
|
* implies that a fully transparent source would still affect the canvas.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
OperatorAffectsUncoveredAreas(CompositionOp op)
|
|
|
|
{
|
|
|
|
return op == OP_IN ||
|
|
|
|
op == OP_OUT ||
|
|
|
|
op == OP_DEST_IN ||
|
|
|
|
op == OP_DEST_ATOP ||
|
|
|
|
op == OP_DEST_OUT;
|
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
static bool
|
2012-01-09 13:50:01 -08: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.
|
|
|
|
if (aPattern.GetType() == PATTERN_COLOR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (aOptions.mAlpha == 1.0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
DrawTargetCairo::DrawTargetCairo()
|
2012-08-14 11:06:12 -07:00
|
|
|
: mContext(nullptr)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawTargetCairo::~DrawTargetCairo()
|
|
|
|
{
|
2012-01-09 13:50:01 -08:00
|
|
|
MarkSnapshotsIndependent();
|
2012-01-09 14:15:10 -08:00
|
|
|
if (mPathObserver) {
|
|
|
|
mPathObserver->ForgetDrawTarget();
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
cairo_destroy(mContext);
|
2012-07-24 03:18:38 -07:00
|
|
|
if (mSurface) {
|
|
|
|
cairo_surface_destroy(mSurface);
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
IntSize
|
|
|
|
DrawTargetCairo::GetSize()
|
|
|
|
{
|
2012-07-24 03:18:38 -07:00
|
|
|
return mSize;
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::Snapshot()
|
|
|
|
{
|
2012-07-24 03:18:38 -07:00
|
|
|
IntSize size = GetSize();
|
|
|
|
|
|
|
|
cairo_content_t content = cairo_surface_get_content(mSurface);
|
|
|
|
RefPtr<SourceSurfaceCairo> surf = new SourceSurfaceCairo(mSurface, size,
|
|
|
|
CairoContentToGfxFormat(content),
|
|
|
|
this);
|
|
|
|
AppendSnapshot(surf);
|
|
|
|
return surf;
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::Flush()
|
|
|
|
{
|
|
|
|
cairo_surface_t* surf = cairo_get_target(mContext);
|
|
|
|
cairo_surface_flush(surf);
|
|
|
|
}
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
void
|
2012-08-14 11:06:12 -07:00
|
|
|
DrawTargetCairo::PrepareForDrawing(cairo_t* aContext, const Path* aPath /* = nullptr */)
|
2012-01-09 13:50:01 -08:00
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
WillChange(aPath);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
void
|
|
|
|
DrawTargetCairo::DrawSurface(SourceSurface *aSurface,
|
|
|
|
const Rect &aDest,
|
|
|
|
const Rect &aSource,
|
|
|
|
const DrawSurfaceOptions &aSurfOptions,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
float sx = aSource.Width() / aDest.Width();
|
|
|
|
float sy = aSource.Height() / aDest.Height();
|
|
|
|
|
|
|
|
cairo_matrix_t src_mat;
|
2012-07-24 03:18:38 -07:00
|
|
|
cairo_matrix_init_translate(&src_mat, aSource.X(), aSource.Y());
|
|
|
|
cairo_matrix_scale(&src_mat, sx, sy);
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-07-24 03:18:39 -07:00
|
|
|
cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aSurface);
|
2012-07-25 23:48:24 -07:00
|
|
|
cairo_pattern_t* pat = cairo_pattern_create_for_surface(surf);
|
2012-07-24 03:18:39 -07:00
|
|
|
cairo_surface_destroy(surf);
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
cairo_pattern_set_matrix(pat, &src_mat);
|
|
|
|
cairo_pattern_set_filter(pat, GfxFilterToCairoFilter(aSurfOptions.mFilter));
|
2012-07-24 03:18:38 -07:00
|
|
|
cairo_pattern_set_extend(pat, CAIRO_EXTEND_PAD);
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-07-25 23:48:24 -07:00
|
|
|
cairo_translate(mContext, aDest.X(), aDest.Y());
|
|
|
|
|
2012-07-24 03:18:38 -07:00
|
|
|
if (OperatorAffectsUncoveredAreas(aOptions.mCompositionOp) ||
|
|
|
|
aOptions.mCompositionOp == OP_SOURCE) {
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, 0, 0, aDest.Width(), aDest.Height());
|
|
|
|
cairo_clip(mContext);
|
|
|
|
cairo_set_source(mContext, pat);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
|
2012-07-25 23:48:24 -07:00
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
cairo_paint_with_alpha(mContext, aOptions.mAlpha);
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
cairo_pattern_destroy(pat);
|
|
|
|
}
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
void
|
|
|
|
DrawTargetCairo::DrawSurfaceWithShadow(SourceSurface *aSurface,
|
|
|
|
const Point &aDest,
|
|
|
|
const Color &aColor,
|
|
|
|
const Point &aOffset,
|
|
|
|
Float aSigma,
|
|
|
|
CompositionOp aOperator)
|
|
|
|
{
|
|
|
|
if (aSurface->GetType() != SURFACE_CAIRO) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-09 14:19:11 -08:00
|
|
|
Float width = aSurface->GetSize().width;
|
|
|
|
Float height = aSurface->GetSize().height;
|
2012-09-02 16:07:05 -07:00
|
|
|
|
2012-08-27 03:27:40 -07:00
|
|
|
SourceSurfaceCairo* source = static_cast<SourceSurfaceCairo*>(aSurface);
|
|
|
|
cairo_surface_t* surf = source->GetSurface();
|
2012-09-02 16:07:05 -07:00
|
|
|
|
|
|
|
cairo_surface_t* blursurf = cairo_image_surface_create(CAIRO_FORMAT_A8,
|
|
|
|
width,
|
|
|
|
height);
|
2012-08-27 03:27:40 -07:00
|
|
|
|
|
|
|
cairo_t* ctx = cairo_create(blursurf);
|
2012-09-02 16:07:05 -07:00
|
|
|
cairo_set_source_surface(ctx, surf, 0, 0);
|
2012-08-27 03:27:40 -07:00
|
|
|
cairo_new_path(ctx);
|
2012-09-02 16:07:05 -07:00
|
|
|
cairo_rectangle(ctx, 0, 0, width, height);
|
2012-09-02 16:07:05 -07:00
|
|
|
cairo_fill(ctx);
|
2012-08-27 03:27:40 -07:00
|
|
|
cairo_destroy(ctx);
|
2012-09-02 16:07:05 -07:00
|
|
|
|
|
|
|
Rect extents(0, 0, width, height);
|
|
|
|
AlphaBoxBlur blur(cairo_image_surface_get_data(blursurf),
|
|
|
|
extents,
|
|
|
|
cairo_image_surface_get_stride(blursurf),
|
|
|
|
aSigma);
|
2012-08-27 03:27:40 -07:00
|
|
|
blur.Blur();
|
2012-09-02 16:07:05 -07:00
|
|
|
|
|
|
|
WillChange();
|
|
|
|
ClearSurfaceForUnboundedSource(aOperator);
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
cairo_save(mContext);
|
2012-07-24 03:18:38 -07:00
|
|
|
cairo_set_operator(mContext, GfxOpToCairoOp(aOperator));
|
2012-01-09 13:50:01 -08:00
|
|
|
cairo_identity_matrix(mContext);
|
|
|
|
cairo_translate(mContext, aDest.x, aDest.y);
|
|
|
|
|
2012-07-24 03:18:38 -07:00
|
|
|
if (OperatorAffectsUncoveredAreas(aOperator) ||
|
|
|
|
aOperator == OP_SOURCE){
|
|
|
|
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 16:07:05 -07: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 03:18:38 -07:00
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, 0, 0, width, height);
|
|
|
|
cairo_fill(mContext);
|
|
|
|
cairo_pop_group_to_source(mContext);
|
2012-09-02 16:07:05 -07:00
|
|
|
cairo_paint(mContext);
|
2012-07-24 03:18:38 -07:00
|
|
|
} else {
|
|
|
|
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.
|
2012-09-02 16:07:05 -07:00
|
|
|
cairo_set_source_surface(mContext, surf, 0, 0);
|
2012-07-24 03:18:38 -07:00
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, 0, 0, width, height);
|
2012-09-02 16:07:05 -07:00
|
|
|
cairo_fill(mContext);
|
2012-07-24 03:18:38 -07:00
|
|
|
}
|
2012-01-09 13:50:01 -08:00
|
|
|
|
|
|
|
cairo_restore(mContext);
|
2012-08-27 03:27:40 -07:00
|
|
|
cairo_surface_destroy(blursurf);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-09 14:15:10 -08:00
|
|
|
DrawTargetCairo::DrawPattern(const Pattern& aPattern,
|
2012-01-09 13:50:01 -08:00
|
|
|
const StrokeOptions& aStrokeOptions,
|
|
|
|
const DrawOptions& aOptions,
|
|
|
|
DrawPatternType aDrawType)
|
|
|
|
{
|
2012-01-09 14:19:11 -08:00
|
|
|
if (!PatternIsCompatible(aPattern)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
cairo_pattern_t* pat = GfxPatternToCairoPattern(aPattern, aOptions.mAlpha);
|
|
|
|
cairo_set_source(mContext, pat);
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2012-07-24 03:18:38 -07:00
|
|
|
if (NeedIntermediateSurface(aPattern, aOptions) ||
|
|
|
|
OperatorAffectsUncoveredAreas(aOptions.mCompositionOp)) {
|
2012-01-09 13:50:01 -08:00
|
|
|
cairo_push_group_with_content(mContext, CAIRO_CONTENT_COLOR_ALPHA);
|
|
|
|
|
2012-07-24 03:18:38 -07:00
|
|
|
ClearSurfaceForUnboundedSource(aOptions.mCompositionOp);
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
// Don't want operators to be applied twice
|
2012-01-09 14:15:10 -08:00
|
|
|
cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);
|
2012-01-09 13:50:01 -08:00
|
|
|
|
|
|
|
if (aDrawType == DRAW_STROKE) {
|
2012-01-09 14:15:10 -08:00
|
|
|
SetCairoStrokeOptions(mContext, aStrokeOptions);
|
|
|
|
cairo_stroke_preserve(mContext);
|
2012-01-09 13:50:01 -08:00
|
|
|
} else {
|
2012-01-09 14:15:10 -08:00
|
|
|
cairo_fill_preserve(mContext);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
cairo_pop_group_to_source(mContext);
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
// Now draw the content using the desired operator
|
|
|
|
cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
|
2012-01-09 13:50:01 -08:00
|
|
|
cairo_paint_with_alpha(mContext, aOptions.mAlpha);
|
2012-01-09 14:15:10 -08:00
|
|
|
} else {
|
2012-07-24 03:18:38 -07:00
|
|
|
ClearSurfaceForUnboundedSource(aOptions.mCompositionOp);
|
2012-01-09 14:15:10 -08:00
|
|
|
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 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
cairo_pattern_destroy(pat);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
void
|
|
|
|
DrawTargetCairo::FillRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
|
|
|
|
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_rectangle(mContext, aRect.x, aRect.y, aRect.Width(), aRect.Height());
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
DrawPattern(aPattern, StrokeOptions(), aOptions, DRAW_FILL);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::CopySurface(SourceSurface *aSurface,
|
2012-07-24 03:18:37 -07:00
|
|
|
const IntRect &aSource,
|
|
|
|
const IntPoint &aDest)
|
2012-01-09 13:50:01 -08:00
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-07-24 03:18:37 -07:00
|
|
|
|
|
|
|
if (!aSurface || aSurface->GetType() != SURFACE_CAIRO) {
|
|
|
|
gfxWarning() << "Unsupported surface type specified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t* surf = static_cast<SourceSurfaceCairo*>(aSurface)->GetSurface();
|
|
|
|
|
|
|
|
cairo_identity_matrix(mContext);
|
|
|
|
|
|
|
|
cairo_set_source_surface(mContext, surf, aDest.x - aSource.x, aDest.y - aSource.y);
|
|
|
|
cairo_set_operator(mContext, CAIRO_OPERATOR_SOURCE);
|
|
|
|
|
|
|
|
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 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::ClearRect(const Rect& aRect)
|
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
cairo_new_path(mContext);
|
2012-01-09 13:50:01 -08: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 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2012-01-09 14:15:10 -08: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 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::StrokeLine(const Point &aStart,
|
|
|
|
const Point &aEnd,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
cairo_new_path(mContext);
|
|
|
|
cairo_move_to(mContext, aStart.x, aStart.y);
|
|
|
|
cairo_line_to(mContext, aEnd.x, aEnd.y);
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::Stroke(const Path *aPath,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext, aPath);
|
|
|
|
|
|
|
|
if (aPath->GetBackendType() != BACKEND_CAIRO)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
|
|
|
|
path->CopyPathTo(mContext, this);
|
|
|
|
|
|
|
|
DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::Fill(const Path *aPath,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext, aPath);
|
|
|
|
|
|
|
|
if (aPath->GetBackendType() != BACKEND_CAIRO)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
|
|
|
|
path->CopyPathTo(mContext, this);
|
|
|
|
|
|
|
|
DrawPattern(aPattern, StrokeOptions(), aOptions, DRAW_FILL);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::FillGlyphs(ScaledFont *aFont,
|
|
|
|
const GlyphBuffer &aBuffer,
|
|
|
|
const Pattern &aPattern,
|
2012-03-19 12:20:17 -07:00
|
|
|
const DrawOptions &aOptions,
|
|
|
|
const GlyphRenderingOptions*)
|
2012-01-09 13:50:01 -08:00
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-01-10 10:26:59 -08:00
|
|
|
|
2012-01-27 10:08:46 -08:00
|
|
|
ScaledFontBase* scaledFont = static_cast<ScaledFontBase*>(aFont);
|
2012-01-10 10:26:59 -08:00
|
|
|
cairo_set_scaled_font(mContext, scaledFont->GetCairoScaledFont());
|
|
|
|
|
|
|
|
cairo_pattern_t* pat = GfxPatternToCairoPattern(aPattern, aOptions.mAlpha);
|
|
|
|
cairo_set_source(mContext, pat);
|
|
|
|
cairo_pattern_destroy(pat);
|
|
|
|
|
|
|
|
// 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 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::Mask(const Pattern &aSource,
|
|
|
|
const Pattern &aMask,
|
|
|
|
const DrawOptions &aOptions /* = DrawOptions() */)
|
|
|
|
{
|
|
|
|
AutoPrepareForDrawing prep(this, mContext);
|
2012-04-25 15:04:36 -07:00
|
|
|
// TODO
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::PushClip(const Path *aPath)
|
|
|
|
{
|
2012-04-25 15:04:36 -07:00
|
|
|
if (aPath->GetBackendType() != BACKEND_CAIRO) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WillChange(aPath);
|
|
|
|
cairo_save(mContext);
|
2012-05-17 14:54:22 -07:00
|
|
|
|
|
|
|
PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
|
2012-04-25 15:04:36 -07:00
|
|
|
path->CopyPathTo(mContext, this);
|
|
|
|
cairo_clip_preserve(mContext);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
void
|
|
|
|
DrawTargetCairo::PushClipRect(const Rect& aRect)
|
|
|
|
{
|
2012-04-25 15:04:36 -07:00
|
|
|
WillChange();
|
|
|
|
cairo_save(mContext);
|
2012-05-17 14:54:22 -07:00
|
|
|
|
|
|
|
cairo_new_path(mContext);
|
2012-04-25 15:04:36 -07:00
|
|
|
cairo_rectangle(mContext, aRect.X(), aRect.Y(), aRect.Width(), aRect.Height());
|
|
|
|
cairo_clip_preserve(mContext);
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
void
|
|
|
|
DrawTargetCairo::PopClip()
|
|
|
|
{
|
2012-05-17 14:54:22 -07:00
|
|
|
// save/restore does not affect the path, so no need to call WillChange()
|
2012-04-25 15:04:36 -07:00
|
|
|
cairo_restore(mContext);
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<PathBuilder>
|
|
|
|
DrawTargetCairo::CreatePathBuilder(FillRule aFillRule /* = FILL_WINDING */) const
|
|
|
|
{
|
2012-01-09 14:15:10 -08:00
|
|
|
RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(mContext,
|
|
|
|
const_cast<DrawTargetCairo*>(this),
|
|
|
|
aFillRule);
|
|
|
|
|
|
|
|
// Creating a PathBuilder implicitly resets our mPathObserver, as it calls
|
|
|
|
// SetPathObserver() on us. Since this guarantees our old path is saved off,
|
|
|
|
// it's safe to reset the path here.
|
|
|
|
cairo_new_path(mContext);
|
|
|
|
|
|
|
|
return builder;
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2012-07-24 03:18:38 -07:00
|
|
|
void
|
|
|
|
DrawTargetCairo::ClearSurfaceForUnboundedSource(const CompositionOp &aOperator)
|
|
|
|
{
|
|
|
|
if (aOperator != OP_SOURCE)
|
|
|
|
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 13:50:01 -08:00
|
|
|
TemporaryRef<GradientStops>
|
|
|
|
DrawTargetCairo::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops, ExtendMode aExtendMode) const
|
|
|
|
{
|
|
|
|
RefPtr<GradientStopsCairo> stops = new GradientStopsCairo(aStops, aNumStops);
|
|
|
|
return stops;
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-07-24 03:18:38 -07: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);
|
|
|
|
for (int32_t y = 0; y < aSize.height; ++y) {
|
|
|
|
memcpy(surfData + y * aSize.width * aPixelWidth,
|
|
|
|
aData + y * aStride,
|
|
|
|
aSize.width * aPixelWidth);
|
|
|
|
}
|
|
|
|
cairo_surface_mark_dirty(aSurface);
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::CreateSourceSurfaceFromData(unsigned char *aData,
|
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat) const
|
|
|
|
{
|
2012-07-24 03:18:38 -07:00
|
|
|
cairo_surface_t* surf = cairo_image_surface_create(GfxFormatToCairoFormat(aFormat),
|
|
|
|
aSize.width,
|
|
|
|
aSize.height);
|
|
|
|
CopyDataToCairoSurface(surf, aData, aSize, aStride, BytesPerPixel(aFormat));
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
RefPtr<SourceSurfaceCairo> source_surf = new SourceSurfaceCairo(surf, aSize, aFormat);
|
2011-06-24 10:41:16 -07:00
|
|
|
cairo_surface_destroy(surf);
|
2012-07-24 03:18:38 -07:00
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
return source_surf;
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::OptimizeSourceSurface(SourceSurface *aSurface) const
|
|
|
|
{
|
2012-01-09 13:50:01 -08:00
|
|
|
return aSurface;
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCairo::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
|
|
|
|
{
|
2012-01-09 13:50:01 -08:00
|
|
|
if (aSurface.mType == NATIVE_SURFACE_CAIRO_SURFACE) {
|
|
|
|
IntSize size;
|
|
|
|
cairo_surface_t* surf = static_cast<cairo_surface_t*>(aSurface.mSurface);
|
|
|
|
if (GetCairoSurfaceSize(surf, size)) {
|
2012-01-09 14:19:11 -08:00
|
|
|
RefPtr<SourceSurfaceCairo> source =
|
2012-01-09 13:50:01 -08:00
|
|
|
new SourceSurfaceCairo(surf, size, aSurface.mFormat);
|
2012-01-09 14:19:11 -08:00
|
|
|
return source;
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2012-01-09 13:50:01 -08: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-28 17:55:20 -07:00
|
|
|
target->InitAlreadyReferenced(similar, aSize);
|
2012-01-09 13:50:01 -08:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-23 16:50:59 -07:00
|
|
|
DrawTargetCairo::InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
|
|
|
mContext = cairo_create(aSurface);
|
2012-07-24 03:18:38 -07:00
|
|
|
mSurface = aSurface;
|
2012-07-24 03:18:38 -07:00
|
|
|
mSize = aSize;
|
2012-07-24 03:18:39 -07:00
|
|
|
mFormat = CairoContentToGfxFormat(cairo_surface_get_content(aSurface));
|
2011-06-24 10:41:16 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-23 16:50:59 -07:00
|
|
|
bool
|
|
|
|
DrawTargetCairo::Init(cairo_surface_t* aSurface, const IntSize& aSize)
|
|
|
|
{
|
|
|
|
cairo_surface_reference(aSurface);
|
|
|
|
return InitAlreadyReferenced(aSurface, aSize);
|
|
|
|
}
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
void *
|
|
|
|
DrawTargetCairo::GetNativeSurface(NativeSurfaceType aType)
|
|
|
|
{
|
|
|
|
if (aType == NATIVE_SURFACE_CAIRO_SURFACE) {
|
|
|
|
return cairo_get_target(mContext);
|
|
|
|
}
|
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
void
|
2012-01-09 13:50:01 -08:00
|
|
|
DrawTargetCairo::MarkSnapshotsIndependent()
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
2012-01-09 13:50:01 -08:00
|
|
|
// Make a copy of the vector, since MarkIndependent implicitly modifies mSnapshots.
|
|
|
|
std::vector<SourceSurfaceCairo*> snapshots = mSnapshots;
|
|
|
|
for (std::vector<SourceSurfaceCairo*>::iterator iter = snapshots.begin();
|
|
|
|
iter != snapshots.end();
|
|
|
|
++iter) {
|
|
|
|
(*iter)->MarkIndependent();
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 13:50:01 -08:00
|
|
|
void
|
|
|
|
DrawTargetCairo::AppendSnapshot(SourceSurfaceCairo* aSnapshot)
|
|
|
|
{
|
|
|
|
mSnapshots.push_back(aSnapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::RemoveSnapshot(SourceSurfaceCairo* aSnapshot)
|
|
|
|
{
|
|
|
|
std::vector<SourceSurfaceCairo*>::iterator iter = std::find(mSnapshots.begin(),
|
|
|
|
mSnapshots.end(),
|
|
|
|
aSnapshot);
|
|
|
|
if (iter != mSnapshots.end()) {
|
|
|
|
mSnapshots.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-14 11:06:12 -07:00
|
|
|
DrawTargetCairo::WillChange(const Path* aPath /* = nullptr */)
|
2012-01-09 13:50:01 -08:00
|
|
|
{
|
|
|
|
if (!mSnapshots.empty()) {
|
|
|
|
for (std::vector<SourceSurfaceCairo*>::iterator iter = mSnapshots.begin();
|
|
|
|
iter != mSnapshots.end(); ++iter) {
|
|
|
|
(*iter)->DrawTargetWillChange();
|
|
|
|
}
|
|
|
|
// All snapshots will now have copied data.
|
|
|
|
mSnapshots.clear();
|
|
|
|
}
|
2012-01-09 14:15:10 -08:00
|
|
|
|
2012-04-25 15:04:36 -07:00
|
|
|
if (mPathObserver &&
|
|
|
|
(!aPath || !mPathObserver->ContainsPath(aPath))) {
|
2012-01-09 14:15:10 -08:00
|
|
|
mPathObserver->PathWillChange();
|
2012-08-14 11:06:12 -07:00
|
|
|
mPathObserver = nullptr;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCairo::SetPathObserver(CairoPathContext* aPathObserver)
|
|
|
|
{
|
|
|
|
if (mPathObserver && mPathObserver != aPathObserver) {
|
|
|
|
mPathObserver->PathWillChange();
|
|
|
|
}
|
|
|
|
mPathObserver = aPathObserver;
|
2012-01-09 13:50:01 -08:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
void
|
|
|
|
DrawTargetCairo::SetTransform(const Matrix& aTransform)
|
|
|
|
{
|
|
|
|
// We're about to logically change our transformation. Our current path will
|
|
|
|
// need to change, because Cairo stores paths in device space.
|
|
|
|
if (mPathObserver) {
|
|
|
|
mPathObserver->MatrixWillChange(aTransform);
|
|
|
|
}
|
|
|
|
|
|
|
|
mTransform = aTransform;
|
|
|
|
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
GfxMatrixToCairoMatrix(mTransform, mat);
|
|
|
|
cairo_set_matrix(mContext, &mat);
|
|
|
|
}
|
2012-01-09 13:50:01 -08:00
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
}
|