gfx/2d/DrawTargetCairo.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/DrawTargetCairo.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1298 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "DrawTargetCairo.h"
    1.10 +
    1.11 +#include "SourceSurfaceCairo.h"
    1.12 +#include "PathCairo.h"
    1.13 +#include "HelpersCairo.h"
    1.14 +#include "ScaledFontBase.h"
    1.15 +#include "BorrowedContext.h"
    1.16 +#include "FilterNodeSoftware.h"
    1.17 +
    1.18 +#include "cairo.h"
    1.19 +#include "cairo-tee.h"
    1.20 +#include <string.h>
    1.21 +
    1.22 +#include "Blur.h"
    1.23 +#include "Logging.h"
    1.24 +#include "Tools.h"
    1.25 +
    1.26 +#ifdef CAIRO_HAS_QUARTZ_SURFACE
    1.27 +#include "cairo-quartz.h"
    1.28 +#include <ApplicationServices/ApplicationServices.h>
    1.29 +#endif
    1.30 +
    1.31 +#ifdef CAIRO_HAS_XLIB_SURFACE
    1.32 +#include "cairo-xlib.h"
    1.33 +#endif
    1.34 +
    1.35 +#ifdef CAIRO_HAS_WIN32_SURFACE
    1.36 +#include "cairo-win32.h"
    1.37 +#endif
    1.38 +
    1.39 +#include <algorithm>
    1.40 +
    1.41 +namespace mozilla {
    1.42 +namespace gfx {
    1.43 +
    1.44 +cairo_surface_t *DrawTargetCairo::mDummySurface;
    1.45 +
    1.46 +namespace {
    1.47 +
    1.48 +// An RAII class to prepare to draw a context and optional path. Saves and
    1.49 +// restores the context on construction/destruction.
    1.50 +class AutoPrepareForDrawing
    1.51 +{
    1.52 +public:
    1.53 +  AutoPrepareForDrawing(DrawTargetCairo* dt, cairo_t* ctx)
    1.54 +    : mCtx(ctx)
    1.55 +  {
    1.56 +    dt->PrepareForDrawing(ctx);
    1.57 +    cairo_save(mCtx);
    1.58 +    MOZ_ASSERT(cairo_status(mCtx) || dt->GetTransform() == GetTransform());
    1.59 +  }
    1.60 +
    1.61 +  AutoPrepareForDrawing(DrawTargetCairo* dt, cairo_t* ctx, const Path* path)
    1.62 +    : mCtx(ctx)
    1.63 +  {
    1.64 +    dt->PrepareForDrawing(ctx, path);
    1.65 +    cairo_save(mCtx);
    1.66 +    MOZ_ASSERT(cairo_status(mCtx) || dt->GetTransform() == GetTransform());
    1.67 +  }
    1.68 +
    1.69 +  ~AutoPrepareForDrawing() { cairo_restore(mCtx); }
    1.70 +
    1.71 +private:
    1.72 +#ifdef DEBUG
    1.73 +  Matrix GetTransform()
    1.74 +  {
    1.75 +    cairo_matrix_t mat;
    1.76 +    cairo_get_matrix(mCtx, &mat);
    1.77 +    return Matrix(mat.xx, mat.yx, mat.xy, mat.yy, mat.x0, mat.y0);
    1.78 +  }
    1.79 +#endif
    1.80 +
    1.81 +  cairo_t* mCtx;
    1.82 +};
    1.83 +
    1.84 +
    1.85 +} // end anonymous namespace
    1.86 +
    1.87 +static bool
    1.88 +SupportsSelfCopy(cairo_surface_t* surface)
    1.89 +{
    1.90 +  switch (cairo_surface_get_type(surface))
    1.91 +  {
    1.92 +#ifdef CAIRO_HAS_QUARTZ_SURFACE
    1.93 +    case CAIRO_SURFACE_TYPE_QUARTZ:
    1.94 +      return true;
    1.95 +#endif
    1.96 +#ifdef CAIRO_HAS_WIN32_SURFACE
    1.97 +    case CAIRO_SURFACE_TYPE_WIN32:
    1.98 +    case CAIRO_SURFACE_TYPE_WIN32_PRINTING:
    1.99 +      return true;
   1.100 +#endif
   1.101 +    default:
   1.102 +      return false;
   1.103 +  }
   1.104 +}
   1.105 +
   1.106 +static bool
   1.107 +PatternIsCompatible(const Pattern& aPattern)
   1.108 +{
   1.109 +  switch (aPattern.GetType())
   1.110 +  {
   1.111 +    case PatternType::LINEAR_GRADIENT:
   1.112 +    {
   1.113 +      const LinearGradientPattern& pattern = static_cast<const LinearGradientPattern&>(aPattern);
   1.114 +      return pattern.mStops->GetBackendType() == BackendType::CAIRO;
   1.115 +    }
   1.116 +    case PatternType::RADIAL_GRADIENT:
   1.117 +    {
   1.118 +      const RadialGradientPattern& pattern = static_cast<const RadialGradientPattern&>(aPattern);
   1.119 +      return pattern.mStops->GetBackendType() == BackendType::CAIRO;
   1.120 +    }
   1.121 +    default:
   1.122 +      return true;
   1.123 +  }
   1.124 +}
   1.125 +
   1.126 +static cairo_user_data_key_t surfaceDataKey;
   1.127 +
   1.128 +void
   1.129 +ReleaseData(void* aData)
   1.130 +{
   1.131 +  static_cast<DataSourceSurface*>(aData)->Release();
   1.132 +}
   1.133 +
   1.134 +/**
   1.135 + * Returns cairo surface for the given SourceSurface.
   1.136 + * If possible, it will use the cairo_surface associated with aSurface,
   1.137 + * otherwise, it will create a new cairo_surface.
   1.138 + * In either case, the caller must call cairo_surface_destroy on the
   1.139 + * result when it is done with it.
   1.140 + */
   1.141 +cairo_surface_t*
   1.142 +GetCairoSurfaceForSourceSurface(SourceSurface *aSurface, bool aExistingOnly = false)
   1.143 +{
   1.144 +  if (aSurface->GetType() == SurfaceType::CAIRO) {
   1.145 +    cairo_surface_t* surf = static_cast<SourceSurfaceCairo*>(aSurface)->GetSurface();
   1.146 +    cairo_surface_reference(surf);
   1.147 +    return surf;
   1.148 +  }
   1.149 +
   1.150 +  if (aSurface->GetType() == SurfaceType::CAIRO_IMAGE) {
   1.151 +    cairo_surface_t* surf =
   1.152 +      static_cast<const DataSourceSurfaceCairo*>(aSurface)->GetSurface();
   1.153 +    cairo_surface_reference(surf);
   1.154 +    return surf;
   1.155 +  }
   1.156 +
   1.157 +  if (aExistingOnly) {
   1.158 +    return nullptr;
   1.159 +  }
   1.160 +
   1.161 +  RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
   1.162 +  if (!data) {
   1.163 +    return nullptr;
   1.164 +  }
   1.165 +
   1.166 +  cairo_surface_t* surf =
   1.167 +    cairo_image_surface_create_for_data(data->GetData(),
   1.168 +                                        GfxFormatToCairoFormat(data->GetFormat()),
   1.169 +                                        data->GetSize().width,
   1.170 +                                        data->GetSize().height,
   1.171 +                                        data->Stride());
   1.172 +
   1.173 +  // In certain scenarios, requesting larger than 8k image fails.  Bug 803568
   1.174 +  // covers the details of how to run into it, but the full detailed
   1.175 +  // investigation hasn't been done to determine the underlying cause.  We
   1.176 +  // will just handle the failure to allocate the surface to avoid a crash.
   1.177 +  if (cairo_surface_status(surf)) {
   1.178 +    return nullptr;
   1.179 +  }
   1.180 +
   1.181 +  cairo_surface_set_user_data(surf,
   1.182 + 				                      &surfaceDataKey,
   1.183 + 				                      data.forget().drop(),
   1.184 + 				                      ReleaseData);
   1.185 +  return surf;
   1.186 +}
   1.187 +
   1.188 +// An RAII class to temporarily clear any device offset set
   1.189 +// on a surface. Note that this does not take a reference to the
   1.190 +// surface.
   1.191 +class AutoClearDeviceOffset
   1.192 +{
   1.193 +public:
   1.194 +  AutoClearDeviceOffset(SourceSurface* aSurface)
   1.195 +    : mSurface(nullptr)
   1.196 +  {
   1.197 +    Init(aSurface);
   1.198 +  }
   1.199 +
   1.200 +  AutoClearDeviceOffset(const Pattern& aPattern)
   1.201 +    : mSurface(nullptr)
   1.202 +  {
   1.203 +    if (aPattern.GetType() == PatternType::SURFACE) {
   1.204 +      const SurfacePattern& pattern = static_cast<const SurfacePattern&>(aPattern);
   1.205 +      Init(pattern.mSurface);
   1.206 +    }
   1.207 +  }
   1.208 +
   1.209 +  ~AutoClearDeviceOffset()
   1.210 +  {
   1.211 +    if (mSurface) {
   1.212 +      cairo_surface_set_device_offset(mSurface, mX, mY);
   1.213 +    }
   1.214 +  }
   1.215 +
   1.216 +private:
   1.217 +  void Init(SourceSurface* aSurface)
   1.218 +  {
   1.219 +    cairo_surface_t* surface = GetCairoSurfaceForSourceSurface(aSurface, true);
   1.220 +    if (surface) {
   1.221 +      Init(surface);
   1.222 +      cairo_surface_destroy(surface);
   1.223 +    }
   1.224 +  }
   1.225 +
   1.226 +  void Init(cairo_surface_t *aSurface)
   1.227 +  {
   1.228 +    mSurface = aSurface;
   1.229 +    cairo_surface_get_device_offset(mSurface, &mX, &mY);
   1.230 +    cairo_surface_set_device_offset(mSurface, 0, 0);
   1.231 +  }
   1.232 +
   1.233 +  cairo_surface_t* mSurface;
   1.234 +  double mX;
   1.235 +  double mY;
   1.236 +};
   1.237 +
   1.238 +// Never returns nullptr. As such, you must always pass in Cairo-compatible
   1.239 +// patterns, most notably gradients with a GradientStopCairo.
   1.240 +// The pattern returned must have cairo_pattern_destroy() called on it by the
   1.241 +// caller.
   1.242 +// As the cairo_pattern_t returned may depend on the Pattern passed in, the
   1.243 +// lifetime of the cairo_pattern_t returned must not exceed the lifetime of the
   1.244 +// Pattern passed in.
   1.245 +static cairo_pattern_t*
   1.246 +GfxPatternToCairoPattern(const Pattern& aPattern, Float aAlpha)
   1.247 +{
   1.248 +  cairo_pattern_t* pat;
   1.249 +  const Matrix* matrix = nullptr;
   1.250 +
   1.251 +  switch (aPattern.GetType())
   1.252 +  {
   1.253 +    case PatternType::COLOR:
   1.254 +    {
   1.255 +      Color color = static_cast<const ColorPattern&>(aPattern).mColor;
   1.256 +      pat = cairo_pattern_create_rgba(color.r, color.g, color.b, color.a * aAlpha);
   1.257 +      break;
   1.258 +    }
   1.259 +
   1.260 +    case PatternType::SURFACE:
   1.261 +    {
   1.262 +      const SurfacePattern& pattern = static_cast<const SurfacePattern&>(aPattern);
   1.263 +      cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(pattern.mSurface);
   1.264 +      if (!surf)
   1.265 +        return nullptr;
   1.266 +
   1.267 +      pat = cairo_pattern_create_for_surface(surf);
   1.268 +
   1.269 +      matrix = &pattern.mMatrix;
   1.270 +
   1.271 +      cairo_pattern_set_filter(pat, GfxFilterToCairoFilter(pattern.mFilter));
   1.272 +      cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(pattern.mExtendMode));
   1.273 +
   1.274 +      cairo_surface_destroy(surf);
   1.275 +      break;
   1.276 +    }
   1.277 +    case PatternType::LINEAR_GRADIENT:
   1.278 +    {
   1.279 +      const LinearGradientPattern& pattern = static_cast<const LinearGradientPattern&>(aPattern);
   1.280 +
   1.281 +      pat = cairo_pattern_create_linear(pattern.mBegin.x, pattern.mBegin.y,
   1.282 +                                        pattern.mEnd.x, pattern.mEnd.y);
   1.283 +
   1.284 +      MOZ_ASSERT(pattern.mStops->GetBackendType() == BackendType::CAIRO);
   1.285 +      GradientStopsCairo* cairoStops = static_cast<GradientStopsCairo*>(pattern.mStops.get());
   1.286 +      cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(cairoStops->GetExtendMode()));
   1.287 +
   1.288 +      matrix = &pattern.mMatrix;
   1.289 +
   1.290 +      const std::vector<GradientStop>& stops = cairoStops->GetStops();
   1.291 +      for (size_t i = 0; i < stops.size(); ++i) {
   1.292 +        const GradientStop& stop = stops[i];
   1.293 +        cairo_pattern_add_color_stop_rgba(pat, stop.offset, stop.color.r,
   1.294 +                                          stop.color.g, stop.color.b,
   1.295 +                                          stop.color.a);
   1.296 +      }
   1.297 +
   1.298 +      break;
   1.299 +    }
   1.300 +    case PatternType::RADIAL_GRADIENT:
   1.301 +    {
   1.302 +      const RadialGradientPattern& pattern = static_cast<const RadialGradientPattern&>(aPattern);
   1.303 +
   1.304 +      pat = cairo_pattern_create_radial(pattern.mCenter1.x, pattern.mCenter1.y, pattern.mRadius1,
   1.305 +                                        pattern.mCenter2.x, pattern.mCenter2.y, pattern.mRadius2);
   1.306 +
   1.307 +      MOZ_ASSERT(pattern.mStops->GetBackendType() == BackendType::CAIRO);
   1.308 +      GradientStopsCairo* cairoStops = static_cast<GradientStopsCairo*>(pattern.mStops.get());
   1.309 +      cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(cairoStops->GetExtendMode()));
   1.310 +
   1.311 +      matrix = &pattern.mMatrix;
   1.312 +
   1.313 +      const std::vector<GradientStop>& stops = cairoStops->GetStops();
   1.314 +      for (size_t i = 0; i < stops.size(); ++i) {
   1.315 +        const GradientStop& stop = stops[i];
   1.316 +        cairo_pattern_add_color_stop_rgba(pat, stop.offset, stop.color.r,
   1.317 +                                          stop.color.g, stop.color.b,
   1.318 +                                          stop.color.a);
   1.319 +      }
   1.320 +
   1.321 +      break;
   1.322 +    }
   1.323 +    default:
   1.324 +    {
   1.325 +      // We should support all pattern types!
   1.326 +      MOZ_ASSERT(false);
   1.327 +    }
   1.328 +  }
   1.329 +
   1.330 +  // The pattern matrix is a matrix that transforms the pattern into user
   1.331 +  // space. Cairo takes a matrix that converts from user space to pattern
   1.332 +  // space. Cairo therefore needs the inverse.
   1.333 +  if (matrix) {
   1.334 +    cairo_matrix_t mat;
   1.335 +    GfxMatrixToCairoMatrix(*matrix, mat);
   1.336 +    cairo_matrix_invert(&mat);
   1.337 +    cairo_pattern_set_matrix(pat, &mat);
   1.338 +  }
   1.339 +
   1.340 +  return pat;
   1.341 +}
   1.342 +
   1.343 +static bool
   1.344 +NeedIntermediateSurface(const Pattern& aPattern, const DrawOptions& aOptions)
   1.345 +{
   1.346 +  // We pre-multiply colours' alpha by the global alpha, so we don't need to
   1.347 +  // use an intermediate surface for them.
   1.348 +  if (aPattern.GetType() == PatternType::COLOR)
   1.349 +    return false;
   1.350 +
   1.351 +  if (aOptions.mAlpha == 1.0)
   1.352 +    return false;
   1.353 +
   1.354 +  return true;
   1.355 +}
   1.356 +
   1.357 +DrawTargetCairo::DrawTargetCairo()
   1.358 +  : mContext(nullptr)
   1.359 +  , mSurface(nullptr)
   1.360 +  , mLockedBits(nullptr)
   1.361 +{
   1.362 +}
   1.363 +
   1.364 +DrawTargetCairo::~DrawTargetCairo()
   1.365 +{
   1.366 +  cairo_destroy(mContext);
   1.367 +  if (mSurface) {
   1.368 +    cairo_surface_destroy(mSurface);
   1.369 +  }
   1.370 +  MOZ_ASSERT(!mLockedBits);
   1.371 +}
   1.372 +
   1.373 +IntSize
   1.374 +DrawTargetCairo::GetSize()
   1.375 +{
   1.376 +  return mSize;
   1.377 +}
   1.378 +
   1.379 +TemporaryRef<SourceSurface>
   1.380 +DrawTargetCairo::Snapshot()
   1.381 +{
   1.382 +  if (mSnapshot) {
   1.383 +    return mSnapshot;
   1.384 +  }
   1.385 +
   1.386 +  IntSize size = GetSize();
   1.387 +
   1.388 +  cairo_content_t content = cairo_surface_get_content(mSurface);
   1.389 +  mSnapshot = new SourceSurfaceCairo(mSurface,
   1.390 +                                     size,
   1.391 +                                     CairoContentToGfxFormat(content),
   1.392 +                                     this);
   1.393 +  return mSnapshot;
   1.394 +}
   1.395 +
   1.396 +bool
   1.397 +DrawTargetCairo::LockBits(uint8_t** aData, IntSize* aSize,
   1.398 +                          int32_t* aStride, SurfaceFormat* aFormat)
   1.399 +{
   1.400 +  if (cairo_surface_get_type(mSurface) == CAIRO_SURFACE_TYPE_IMAGE) {
   1.401 +    WillChange();
   1.402 +
   1.403 +    mLockedBits = cairo_image_surface_get_data(mSurface);
   1.404 +    *aData = mLockedBits;
   1.405 +    *aSize = GetSize();
   1.406 +    *aStride = cairo_image_surface_get_stride(mSurface);
   1.407 +    *aFormat = GetFormat();
   1.408 +    return true;
   1.409 +  }
   1.410 +
   1.411 +  return false;
   1.412 +}
   1.413 +
   1.414 +void
   1.415 +DrawTargetCairo::ReleaseBits(uint8_t* aData)
   1.416 +{
   1.417 +  MOZ_ASSERT(mLockedBits == aData);
   1.418 +  mLockedBits = nullptr;
   1.419 +}
   1.420 +
   1.421 +void
   1.422 +DrawTargetCairo::Flush()
   1.423 +{
   1.424 +  cairo_surface_t* surf = cairo_get_target(mContext);
   1.425 +  cairo_surface_flush(surf);
   1.426 +}
   1.427 +
   1.428 +void
   1.429 +DrawTargetCairo::PrepareForDrawing(cairo_t* aContext, const Path* aPath /* = nullptr */)
   1.430 +{
   1.431 +  WillChange(aPath);
   1.432 +}
   1.433 +
   1.434 +cairo_surface_t*
   1.435 +DrawTargetCairo::GetDummySurface()
   1.436 +{
   1.437 +  if (mDummySurface) {
   1.438 +    return mDummySurface;
   1.439 +  }
   1.440 +
   1.441 +  mDummySurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
   1.442 +
   1.443 +  return mDummySurface;
   1.444 +}
   1.445 +
   1.446 +void
   1.447 +DrawTargetCairo::DrawSurface(SourceSurface *aSurface,
   1.448 +                             const Rect &aDest,
   1.449 +                             const Rect &aSource,
   1.450 +                             const DrawSurfaceOptions &aSurfOptions,
   1.451 +                             const DrawOptions &aOptions)
   1.452 +{
   1.453 +  AutoPrepareForDrawing prep(this, mContext);
   1.454 +  AutoClearDeviceOffset clear(aSurface);
   1.455 +
   1.456 +  float sx = aSource.Width() / aDest.Width();
   1.457 +  float sy = aSource.Height() / aDest.Height();
   1.458 +
   1.459 +  cairo_matrix_t src_mat;
   1.460 +  cairo_matrix_init_translate(&src_mat, aSource.X(), aSource.Y());
   1.461 +  cairo_matrix_scale(&src_mat, sx, sy);
   1.462 +
   1.463 +  cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aSurface);
   1.464 +  cairo_pattern_t* pat = cairo_pattern_create_for_surface(surf);
   1.465 +  cairo_surface_destroy(surf);
   1.466 +
   1.467 +  cairo_pattern_set_matrix(pat, &src_mat);
   1.468 +  cairo_pattern_set_filter(pat, GfxFilterToCairoFilter(aSurfOptions.mFilter));
   1.469 +  cairo_pattern_set_extend(pat, CAIRO_EXTEND_PAD);
   1.470 +
   1.471 +  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
   1.472 +
   1.473 +  // If the destination rect covers the entire clipped area, then unbounded and bounded
   1.474 +  // operations are identical, and we don't need to push a group.
   1.475 +  bool needsGroup = !IsOperatorBoundByMask(aOptions.mCompositionOp) &&
   1.476 +                    !aDest.Contains(GetUserSpaceClip());
   1.477 +
   1.478 +  cairo_translate(mContext, aDest.X(), aDest.Y());
   1.479 +
   1.480 +  if (needsGroup) {
   1.481 +    cairo_push_group(mContext);
   1.482 +      cairo_new_path(mContext);
   1.483 +      cairo_rectangle(mContext, 0, 0, aDest.Width(), aDest.Height());
   1.484 +      cairo_set_source(mContext, pat);
   1.485 +      cairo_fill(mContext);
   1.486 +    cairo_pop_group_to_source(mContext);
   1.487 +  } else {
   1.488 +    cairo_new_path(mContext);
   1.489 +    cairo_rectangle(mContext, 0, 0, aDest.Width(), aDest.Height());
   1.490 +    cairo_clip(mContext);
   1.491 +    cairo_set_source(mContext, pat);
   1.492 +  }
   1.493 +
   1.494 +  cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
   1.495 +
   1.496 +  cairo_paint_with_alpha(mContext, aOptions.mAlpha);
   1.497 +
   1.498 +  cairo_pattern_destroy(pat);
   1.499 +}
   1.500 +
   1.501 +void
   1.502 +DrawTargetCairo::DrawFilter(FilterNode *aNode,
   1.503 +                            const Rect &aSourceRect,
   1.504 +                            const Point &aDestPoint,
   1.505 +                            const DrawOptions &aOptions)
   1.506 +{
   1.507 +  FilterNodeSoftware* filter = static_cast<FilterNodeSoftware*>(aNode);
   1.508 +  filter->Draw(this, aSourceRect, aDestPoint, aOptions);
   1.509 +}
   1.510 +
   1.511 +void
   1.512 +DrawTargetCairo::DrawSurfaceWithShadow(SourceSurface *aSurface,
   1.513 +                                       const Point &aDest,
   1.514 +                                       const Color &aColor,
   1.515 +                                       const Point &aOffset,
   1.516 +                                       Float aSigma,
   1.517 +                                       CompositionOp aOperator)
   1.518 +{
   1.519 +  if (aSurface->GetType() != SurfaceType::CAIRO) {
   1.520 +    return;
   1.521 +  }
   1.522 +
   1.523 +  AutoClearDeviceOffset clear(aSurface);
   1.524 +
   1.525 +  Float width = Float(aSurface->GetSize().width);
   1.526 +  Float height = Float(aSurface->GetSize().height);
   1.527 +
   1.528 +  SourceSurfaceCairo* source = static_cast<SourceSurfaceCairo*>(aSurface);
   1.529 +  cairo_surface_t* sourcesurf = source->GetSurface();
   1.530 +  cairo_surface_t* blursurf;
   1.531 +  cairo_surface_t* surf;
   1.532 +
   1.533 +  // We only use the A8 surface for blurred shadows. Unblurred shadows can just
   1.534 +  // use the RGBA surface directly.
   1.535 +  if (cairo_surface_get_type(sourcesurf) == CAIRO_SURFACE_TYPE_TEE) {
   1.536 +    blursurf = cairo_tee_surface_index(sourcesurf, 0);
   1.537 +    surf = cairo_tee_surface_index(sourcesurf, 1);
   1.538 +
   1.539 +    MOZ_ASSERT(cairo_surface_get_type(blursurf) == CAIRO_SURFACE_TYPE_IMAGE);
   1.540 +    Rect extents(0, 0, width, height);
   1.541 +    AlphaBoxBlur blur(extents,
   1.542 +                      cairo_image_surface_get_stride(blursurf),
   1.543 +                      aSigma, aSigma);
   1.544 +    blur.Blur(cairo_image_surface_get_data(blursurf));
   1.545 +  } else {
   1.546 +    blursurf = sourcesurf;
   1.547 +    surf = sourcesurf;
   1.548 +  }
   1.549 +
   1.550 +  WillChange();
   1.551 +  ClearSurfaceForUnboundedSource(aOperator);
   1.552 +
   1.553 +  cairo_save(mContext);
   1.554 +  cairo_set_operator(mContext, GfxOpToCairoOp(aOperator));
   1.555 +  cairo_identity_matrix(mContext);
   1.556 +  cairo_translate(mContext, aDest.x, aDest.y);
   1.557 +
   1.558 +  if (IsOperatorBoundByMask(aOperator)){
   1.559 +    cairo_set_source_rgba(mContext, aColor.r, aColor.g, aColor.b, aColor.a);
   1.560 +    cairo_mask_surface(mContext, blursurf, aOffset.x, aOffset.y);
   1.561 +
   1.562 +    // Now that the shadow has been drawn, we can draw the surface on top.
   1.563 +    cairo_set_source_surface(mContext, surf, 0, 0);
   1.564 +    cairo_new_path(mContext);
   1.565 +    cairo_rectangle(mContext, 0, 0, width, height);
   1.566 +    cairo_fill(mContext);
   1.567 +  } else {
   1.568 +    cairo_push_group(mContext);
   1.569 +      cairo_set_source_rgba(mContext, aColor.r, aColor.g, aColor.b, aColor.a);
   1.570 +      cairo_mask_surface(mContext, blursurf, aOffset.x, aOffset.y);
   1.571 +
   1.572 +      // Now that the shadow has been drawn, we can draw the surface on top.
   1.573 +      cairo_set_source_surface(mContext, surf, 0, 0);
   1.574 +      cairo_new_path(mContext);
   1.575 +      cairo_rectangle(mContext, 0, 0, width, height);
   1.576 +      cairo_fill(mContext);
   1.577 +    cairo_pop_group_to_source(mContext);
   1.578 +    cairo_paint(mContext);
   1.579 +  }
   1.580 +
   1.581 +  cairo_restore(mContext);
   1.582 +}
   1.583 +
   1.584 +void
   1.585 +DrawTargetCairo::DrawPattern(const Pattern& aPattern,
   1.586 +                             const StrokeOptions& aStrokeOptions,
   1.587 +                             const DrawOptions& aOptions,
   1.588 +                             DrawPatternType aDrawType,
   1.589 +                             bool aPathBoundsClip)
   1.590 +{
   1.591 +  if (!PatternIsCompatible(aPattern)) {
   1.592 +    return;
   1.593 +  }
   1.594 +
   1.595 +  AutoClearDeviceOffset clear(aPattern);
   1.596 +
   1.597 +  cairo_pattern_t* pat = GfxPatternToCairoPattern(aPattern, aOptions.mAlpha);
   1.598 +  if (!pat) {
   1.599 +    return;
   1.600 +  }
   1.601 +  if (cairo_pattern_status(pat)) {
   1.602 +    cairo_pattern_destroy(pat);
   1.603 +    gfxWarning() << "Invalid pattern";
   1.604 +    return;
   1.605 +  }
   1.606 +
   1.607 +  cairo_set_source(mContext, pat);
   1.608 +
   1.609 +  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
   1.610 +
   1.611 +  if (NeedIntermediateSurface(aPattern, aOptions) ||
   1.612 +      (!IsOperatorBoundByMask(aOptions.mCompositionOp) && !aPathBoundsClip)) {
   1.613 +    cairo_push_group_with_content(mContext, CAIRO_CONTENT_COLOR_ALPHA);
   1.614 +
   1.615 +    // Don't want operators to be applied twice
   1.616 +    cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);
   1.617 +
   1.618 +    if (aDrawType == DRAW_STROKE) {
   1.619 +      SetCairoStrokeOptions(mContext, aStrokeOptions);
   1.620 +      cairo_stroke_preserve(mContext);
   1.621 +    } else {
   1.622 +      cairo_fill_preserve(mContext);
   1.623 +    }
   1.624 +
   1.625 +    cairo_pop_group_to_source(mContext);
   1.626 +
   1.627 +    // Now draw the content using the desired operator
   1.628 +    cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
   1.629 +    cairo_paint_with_alpha(mContext, aOptions.mAlpha);
   1.630 +  } else {
   1.631 +    cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
   1.632 +
   1.633 +    if (aDrawType == DRAW_STROKE) {
   1.634 +      SetCairoStrokeOptions(mContext, aStrokeOptions);
   1.635 +      cairo_stroke_preserve(mContext);
   1.636 +    } else {
   1.637 +      cairo_fill_preserve(mContext);
   1.638 +    }
   1.639 +  }
   1.640 +
   1.641 +  cairo_pattern_destroy(pat);
   1.642 +}
   1.643 +
   1.644 +void
   1.645 +DrawTargetCairo::FillRect(const Rect &aRect,
   1.646 +                          const Pattern &aPattern,
   1.647 +                          const DrawOptions &aOptions)
   1.648 +{
   1.649 +  AutoPrepareForDrawing prep(this, mContext);
   1.650 +
   1.651 +  cairo_new_path(mContext);
   1.652 +  cairo_rectangle(mContext, aRect.x, aRect.y, aRect.Width(), aRect.Height());
   1.653 +
   1.654 +  bool pathBoundsClip = false;
   1.655 +
   1.656 +  if (aRect.Contains(GetUserSpaceClip())) {
   1.657 +    pathBoundsClip = true;
   1.658 +  }
   1.659 +
   1.660 +  DrawPattern(aPattern, StrokeOptions(), aOptions, DRAW_FILL, pathBoundsClip);
   1.661 +}
   1.662 +
   1.663 +void
   1.664 +DrawTargetCairo::CopySurfaceInternal(cairo_surface_t* aSurface,
   1.665 +                                     const IntRect &aSource,
   1.666 +                                     const IntPoint &aDest)
   1.667 +{
   1.668 +  if (cairo_surface_status(aSurface)) {
   1.669 +    gfxWarning() << "Invalid surface";
   1.670 +    return;
   1.671 +  }
   1.672 +
   1.673 +  cairo_identity_matrix(mContext);
   1.674 +
   1.675 +  cairo_set_source_surface(mContext, aSurface, aDest.x - aSource.x, aDest.y - aSource.y);
   1.676 +  cairo_set_operator(mContext, CAIRO_OPERATOR_SOURCE);
   1.677 +  cairo_set_antialias(mContext, CAIRO_ANTIALIAS_NONE);
   1.678 +
   1.679 +  cairo_reset_clip(mContext);
   1.680 +  cairo_new_path(mContext);
   1.681 +  cairo_rectangle(mContext, aDest.x, aDest.y, aSource.width, aSource.height);
   1.682 +  cairo_fill(mContext);
   1.683 +}
   1.684 +
   1.685 +void
   1.686 +DrawTargetCairo::CopySurface(SourceSurface *aSurface,
   1.687 +                             const IntRect &aSource,
   1.688 +                             const IntPoint &aDest)
   1.689 +{
   1.690 +  AutoPrepareForDrawing prep(this, mContext);
   1.691 +  AutoClearDeviceOffset clear(aSurface);
   1.692 +
   1.693 +  if (!aSurface) {
   1.694 +    gfxWarning() << "Unsupported surface type specified";
   1.695 +    return;
   1.696 +  }
   1.697 +
   1.698 +  cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aSurface);
   1.699 +  if (!surf) {
   1.700 +    gfxWarning() << "Unsupported surface type specified";
   1.701 +    return;
   1.702 +  }
   1.703 +
   1.704 +  CopySurfaceInternal(surf, aSource, aDest);
   1.705 +  cairo_surface_destroy(surf);
   1.706 +}
   1.707 +
   1.708 +void
   1.709 +DrawTargetCairo::CopyRect(const IntRect &aSource,
   1.710 +                          const IntPoint &aDest)
   1.711 +{
   1.712 +  AutoPrepareForDrawing prep(this, mContext);
   1.713 +
   1.714 +  IntRect source = aSource;
   1.715 +  cairo_surface_t* surf = mSurface;
   1.716 +
   1.717 +  if (!SupportsSelfCopy(mSurface) &&
   1.718 +      aDest.y >= aSource.y &&
   1.719 +      aDest.y < aSource.YMost()) {
   1.720 +    cairo_surface_t* similar = cairo_surface_create_similar(mSurface,
   1.721 +                                                            GfxFormatToCairoContent(GetFormat()),
   1.722 +                                                            aSource.width, aSource.height);
   1.723 +    cairo_t* ctx = cairo_create(similar);
   1.724 +    cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
   1.725 +    cairo_set_source_surface(ctx, surf, -aSource.x, -aSource.y);
   1.726 +    cairo_paint(ctx);
   1.727 +    cairo_destroy(ctx);
   1.728 +
   1.729 +    source.x = 0;
   1.730 +    source.y = 0;
   1.731 +    surf = similar;
   1.732 +  }
   1.733 +
   1.734 +  CopySurfaceInternal(surf, source, aDest);
   1.735 +
   1.736 +  if (surf != mSurface) {
   1.737 +    cairo_surface_destroy(surf);
   1.738 +  }
   1.739 +}
   1.740 +
   1.741 +void
   1.742 +DrawTargetCairo::ClearRect(const Rect& aRect)
   1.743 +{
   1.744 +  AutoPrepareForDrawing prep(this, mContext);
   1.745 +
   1.746 +  cairo_set_antialias(mContext, CAIRO_ANTIALIAS_NONE);
   1.747 +  cairo_new_path(mContext);
   1.748 +  cairo_set_operator(mContext, CAIRO_OPERATOR_CLEAR);
   1.749 +  cairo_rectangle(mContext, aRect.X(), aRect.Y(),
   1.750 +                  aRect.Width(), aRect.Height());
   1.751 +  cairo_fill(mContext);
   1.752 +}
   1.753 +
   1.754 +void
   1.755 +DrawTargetCairo::StrokeRect(const Rect &aRect,
   1.756 +                            const Pattern &aPattern,
   1.757 +                            const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
   1.758 +                            const DrawOptions &aOptions /* = DrawOptions() */)
   1.759 +{
   1.760 +  AutoPrepareForDrawing prep(this, mContext);
   1.761 +
   1.762 +  cairo_new_path(mContext);
   1.763 +  cairo_rectangle(mContext, aRect.x, aRect.y, aRect.Width(), aRect.Height());
   1.764 +
   1.765 +  DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
   1.766 +}
   1.767 +
   1.768 +void
   1.769 +DrawTargetCairo::StrokeLine(const Point &aStart,
   1.770 +                            const Point &aEnd,
   1.771 +                            const Pattern &aPattern,
   1.772 +                            const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
   1.773 +                            const DrawOptions &aOptions /* = DrawOptions() */)
   1.774 +{
   1.775 +  AutoPrepareForDrawing prep(this, mContext);
   1.776 +
   1.777 +  cairo_new_path(mContext);
   1.778 +  cairo_move_to(mContext, aStart.x, aStart.y);
   1.779 +  cairo_line_to(mContext, aEnd.x, aEnd.y);
   1.780 +
   1.781 +  DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
   1.782 +}
   1.783 +
   1.784 +void
   1.785 +DrawTargetCairo::Stroke(const Path *aPath,
   1.786 +                        const Pattern &aPattern,
   1.787 +                        const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
   1.788 +                        const DrawOptions &aOptions /* = DrawOptions() */)
   1.789 +{
   1.790 +  AutoPrepareForDrawing prep(this, mContext, aPath);
   1.791 +
   1.792 +  if (aPath->GetBackendType() != BackendType::CAIRO)
   1.793 +    return;
   1.794 +
   1.795 +  PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
   1.796 +  path->SetPathOnContext(mContext);
   1.797 +
   1.798 +  DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
   1.799 +}
   1.800 +
   1.801 +void
   1.802 +DrawTargetCairo::Fill(const Path *aPath,
   1.803 +                      const Pattern &aPattern,
   1.804 +                      const DrawOptions &aOptions /* = DrawOptions() */)
   1.805 +{
   1.806 +  AutoPrepareForDrawing prep(this, mContext, aPath);
   1.807 +
   1.808 +  if (aPath->GetBackendType() != BackendType::CAIRO)
   1.809 +    return;
   1.810 +
   1.811 +  PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
   1.812 +  path->SetPathOnContext(mContext);
   1.813 +
   1.814 +  DrawPattern(aPattern, StrokeOptions(), aOptions, DRAW_FILL);
   1.815 +}
   1.816 +
   1.817 +void
   1.818 +DrawTargetCairo::SetPermitSubpixelAA(bool aPermitSubpixelAA)
   1.819 +{
   1.820 +  DrawTarget::SetPermitSubpixelAA(aPermitSubpixelAA);
   1.821 +#ifdef MOZ_TREE_CAIRO
   1.822 +  cairo_surface_set_subpixel_antialiasing(mSurface,
   1.823 +    aPermitSubpixelAA ? CAIRO_SUBPIXEL_ANTIALIASING_ENABLED : CAIRO_SUBPIXEL_ANTIALIASING_DISABLED);
   1.824 +#endif
   1.825 +}
   1.826 +
   1.827 +void
   1.828 +DrawTargetCairo::FillGlyphs(ScaledFont *aFont,
   1.829 +                            const GlyphBuffer &aBuffer,
   1.830 +                            const Pattern &aPattern,
   1.831 +                            const DrawOptions &aOptions,
   1.832 +                            const GlyphRenderingOptions*)
   1.833 +{
   1.834 +  AutoPrepareForDrawing prep(this, mContext);
   1.835 +  AutoClearDeviceOffset clear(aPattern);
   1.836 +
   1.837 +  ScaledFontBase* scaledFont = static_cast<ScaledFontBase*>(aFont);
   1.838 +  cairo_set_scaled_font(mContext, scaledFont->GetCairoScaledFont());
   1.839 +
   1.840 +  cairo_pattern_t* pat = GfxPatternToCairoPattern(aPattern, aOptions.mAlpha);
   1.841 +  if (!pat)
   1.842 +    return;
   1.843 +
   1.844 +  cairo_set_source(mContext, pat);
   1.845 +  cairo_pattern_destroy(pat);
   1.846 +
   1.847 +  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
   1.848 +
   1.849 +  // Convert our GlyphBuffer into an array of Cairo glyphs.
   1.850 +  std::vector<cairo_glyph_t> glyphs(aBuffer.mNumGlyphs);
   1.851 +  for (uint32_t i = 0; i < aBuffer.mNumGlyphs; ++i) {
   1.852 +    glyphs[i].index = aBuffer.mGlyphs[i].mIndex;
   1.853 +    glyphs[i].x = aBuffer.mGlyphs[i].mPosition.x;
   1.854 +    glyphs[i].y = aBuffer.mGlyphs[i].mPosition.y;
   1.855 +  }
   1.856 +
   1.857 +  cairo_show_glyphs(mContext, &glyphs[0], aBuffer.mNumGlyphs);
   1.858 +}
   1.859 +
   1.860 +void
   1.861 +DrawTargetCairo::Mask(const Pattern &aSource,
   1.862 +                      const Pattern &aMask,
   1.863 +                      const DrawOptions &aOptions /* = DrawOptions() */)
   1.864 +{
   1.865 +  AutoPrepareForDrawing prep(this, mContext);
   1.866 +  AutoClearDeviceOffset clearSource(aSource);
   1.867 +  AutoClearDeviceOffset clearMask(aMask);
   1.868 +
   1.869 +  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
   1.870 +
   1.871 +  cairo_pattern_t* source = GfxPatternToCairoPattern(aSource, aOptions.mAlpha);
   1.872 +  if (!source) {
   1.873 +    return;
   1.874 +  }
   1.875 +
   1.876 +  cairo_pattern_t* mask = GfxPatternToCairoPattern(aMask, aOptions.mAlpha);
   1.877 +  if (!mask) {
   1.878 +    cairo_pattern_destroy(source);
   1.879 +    return;
   1.880 +  }
   1.881 +
   1.882 +  if (cairo_pattern_status(source) || cairo_pattern_status(mask)) {
   1.883 +    cairo_pattern_destroy(source);
   1.884 +    cairo_pattern_destroy(mask);
   1.885 +    gfxWarning() << "Invalid pattern";
   1.886 +    return;
   1.887 +  }
   1.888 +
   1.889 +  cairo_set_source(mContext, source);
   1.890 +  cairo_mask(mContext, mask);
   1.891 +
   1.892 +  cairo_pattern_destroy(mask);
   1.893 +  cairo_pattern_destroy(source);
   1.894 +}
   1.895 +
   1.896 +void
   1.897 +DrawTargetCairo::MaskSurface(const Pattern &aSource,
   1.898 +                             SourceSurface *aMask,
   1.899 +                             Point aOffset,
   1.900 +                             const DrawOptions &aOptions)
   1.901 +{
   1.902 +  AutoPrepareForDrawing prep(this, mContext);
   1.903 +  AutoClearDeviceOffset clearSource(aSource);
   1.904 +  AutoClearDeviceOffset clearMask(aMask);
   1.905 +
   1.906 +  if (!PatternIsCompatible(aSource)) {
   1.907 +    return;
   1.908 +  }
   1.909 +
   1.910 +  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));
   1.911 +
   1.912 +  cairo_pattern_t* pat = GfxPatternToCairoPattern(aSource, aOptions.mAlpha);
   1.913 +  if (!pat) {
   1.914 +    return;
   1.915 +  }
   1.916 +
   1.917 +  if (cairo_pattern_status(pat)) {
   1.918 +    cairo_pattern_destroy(pat);
   1.919 +    gfxWarning() << "Invalid pattern";
   1.920 +    return;
   1.921 +  }
   1.922 +
   1.923 +  cairo_set_source(mContext, pat);
   1.924 +
   1.925 +  if (NeedIntermediateSurface(aSource, aOptions)) {
   1.926 +    cairo_push_group_with_content(mContext, CAIRO_CONTENT_COLOR_ALPHA);
   1.927 +
   1.928 +    // Don't want operators to be applied twice
   1.929 +    cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);
   1.930 +
   1.931 +    // Now draw the content using the desired operator
   1.932 +    cairo_paint_with_alpha(mContext, aOptions.mAlpha);
   1.933 +
   1.934 +    cairo_pop_group_to_source(mContext);
   1.935 +  }
   1.936 +
   1.937 +  cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aMask);
   1.938 +  if (!surf) {
   1.939 +    cairo_pattern_destroy(pat);
   1.940 +    return;
   1.941 +  }
   1.942 +  cairo_pattern_t* mask = cairo_pattern_create_for_surface(surf);
   1.943 +  cairo_matrix_t matrix;
   1.944 +
   1.945 +  cairo_matrix_init_translate (&matrix, -aOffset.x, -aOffset.y);
   1.946 +  cairo_pattern_set_matrix (mask, &matrix);
   1.947 +
   1.948 +  cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
   1.949 +
   1.950 +  cairo_mask(mContext, mask);
   1.951 +
   1.952 +  cairo_surface_destroy(surf);
   1.953 +  cairo_pattern_destroy(mask);
   1.954 +  cairo_pattern_destroy(pat);
   1.955 +}
   1.956 +
   1.957 +void
   1.958 +DrawTargetCairo::PushClip(const Path *aPath)
   1.959 +{
   1.960 +  if (aPath->GetBackendType() != BackendType::CAIRO) {
   1.961 +    return;
   1.962 +  }
   1.963 +
   1.964 +  WillChange(aPath);
   1.965 +  cairo_save(mContext);
   1.966 +
   1.967 +  PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
   1.968 +  path->SetPathOnContext(mContext);
   1.969 +  cairo_clip_preserve(mContext);
   1.970 +}
   1.971 +
   1.972 +void
   1.973 +DrawTargetCairo::PushClipRect(const Rect& aRect)
   1.974 +{
   1.975 +  WillChange();
   1.976 +  cairo_save(mContext);
   1.977 +
   1.978 +  cairo_new_path(mContext);
   1.979 +  cairo_rectangle(mContext, aRect.X(), aRect.Y(), aRect.Width(), aRect.Height());
   1.980 +  cairo_clip_preserve(mContext);
   1.981 +}
   1.982 +
   1.983 +void
   1.984 +DrawTargetCairo::PopClip()
   1.985 +{
   1.986 +  // save/restore does not affect the path, so no need to call WillChange()
   1.987 +
   1.988 +  // cairo_restore will restore the transform too and we don't want to do that
   1.989 +  // so we'll save it now and restore it after the cairo_restore
   1.990 +  cairo_matrix_t mat;
   1.991 +  cairo_get_matrix(mContext, &mat);
   1.992 +
   1.993 +  cairo_restore(mContext);
   1.994 +
   1.995 +  cairo_set_matrix(mContext, &mat);
   1.996 +
   1.997 +  MOZ_ASSERT(cairo_status(mContext) || GetTransform() == Matrix(mat.xx, mat.yx, mat.xy, mat.yy, mat.x0, mat.y0),
   1.998 +             "Transforms are out of sync");
   1.999 +}
  1.1000 +
  1.1001 +TemporaryRef<PathBuilder>
  1.1002 +DrawTargetCairo::CreatePathBuilder(FillRule aFillRule /* = FillRule::FILL_WINDING */) const
  1.1003 +{
  1.1004 +  RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
  1.1005 +
  1.1006 +  return builder;
  1.1007 +}
  1.1008 +
  1.1009 +void
  1.1010 +DrawTargetCairo::ClearSurfaceForUnboundedSource(const CompositionOp &aOperator)
  1.1011 +{
  1.1012 +  if (aOperator != CompositionOp::OP_SOURCE)
  1.1013 +    return;
  1.1014 +  cairo_set_operator(mContext, CAIRO_OPERATOR_CLEAR);
  1.1015 +  // It doesn't really matter what the source is here, since Paint
  1.1016 +  // isn't bounded by the source and the mask covers the entire clip
  1.1017 +  // region.
  1.1018 +  cairo_paint(mContext);
  1.1019 +}
  1.1020 +
  1.1021 +
  1.1022 +TemporaryRef<GradientStops>
  1.1023 +DrawTargetCairo::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops,
  1.1024 +                                     ExtendMode aExtendMode) const
  1.1025 +{
  1.1026 +  RefPtr<GradientStopsCairo> stops = new GradientStopsCairo(aStops, aNumStops,
  1.1027 +                                                            aExtendMode);
  1.1028 +  return stops;
  1.1029 +}
  1.1030 +
  1.1031 +TemporaryRef<FilterNode>
  1.1032 +DrawTargetCairo::CreateFilter(FilterType aType)
  1.1033 +{
  1.1034 +  return FilterNodeSoftware::Create(aType);
  1.1035 +}
  1.1036 +
  1.1037 +/**
  1.1038 + * Copies pixel data from aData into aSurface; aData must have the dimensions
  1.1039 + * given in aSize, with a stride of aStride bytes and aPixelWidth bytes per pixel
  1.1040 + */
  1.1041 +static void
  1.1042 +CopyDataToCairoSurface(cairo_surface_t* aSurface,
  1.1043 +                       unsigned char *aData,
  1.1044 +                       const IntSize &aSize,
  1.1045 +                       int32_t aStride,
  1.1046 +                       int32_t aPixelWidth)
  1.1047 +{
  1.1048 +  unsigned char* surfData = cairo_image_surface_get_data(aSurface);
  1.1049 +  int surfStride = cairo_image_surface_get_stride(aSurface);
  1.1050 +  // In certain scenarios, requesting larger than 8k image fails.  Bug 803568
  1.1051 +  // covers the details of how to run into it, but the full detailed
  1.1052 +  // investigation hasn't been done to determine the underlying cause.  We
  1.1053 +  // will just handle the failure to allocate the surface to avoid a crash.
  1.1054 +  if (!surfData) {
  1.1055 +    return;
  1.1056 +  }
  1.1057 +  for (int32_t y = 0; y < aSize.height; ++y) {
  1.1058 +    memcpy(surfData + y * surfStride,
  1.1059 +           aData + y * aStride,
  1.1060 +           aSize.width * aPixelWidth);
  1.1061 +  }
  1.1062 +  cairo_surface_mark_dirty(aSurface);
  1.1063 +}
  1.1064 +
  1.1065 +TemporaryRef<SourceSurface>
  1.1066 +DrawTargetCairo::CreateSourceSurfaceFromData(unsigned char *aData,
  1.1067 +                                             const IntSize &aSize,
  1.1068 +                                             int32_t aStride,
  1.1069 +                                             SurfaceFormat aFormat) const
  1.1070 +{
  1.1071 +  cairo_surface_t* surf = cairo_image_surface_create(GfxFormatToCairoFormat(aFormat),
  1.1072 +                                                     aSize.width,
  1.1073 +                                                     aSize.height);
  1.1074 +  // In certain scenarios, requesting larger than 8k image fails.  Bug 803568
  1.1075 +  // covers the details of how to run into it, but the full detailed
  1.1076 +  // investigation hasn't been done to determine the underlying cause.  We
  1.1077 +  // will just handle the failure to allocate the surface to avoid a crash.
  1.1078 +  if (cairo_surface_status(surf)) {
  1.1079 +    return nullptr;
  1.1080 +  }
  1.1081 +
  1.1082 +  CopyDataToCairoSurface(surf, aData, aSize, aStride, BytesPerPixel(aFormat));
  1.1083 +
  1.1084 +  RefPtr<SourceSurfaceCairo> source_surf = new SourceSurfaceCairo(surf, aSize, aFormat);
  1.1085 +  cairo_surface_destroy(surf);
  1.1086 +
  1.1087 +  return source_surf;
  1.1088 +}
  1.1089 +
  1.1090 +TemporaryRef<SourceSurface>
  1.1091 +DrawTargetCairo::OptimizeSourceSurface(SourceSurface *aSurface) const
  1.1092 +{
  1.1093 +  return aSurface;
  1.1094 +}
  1.1095 +
  1.1096 +TemporaryRef<SourceSurface>
  1.1097 +DrawTargetCairo::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
  1.1098 +{
  1.1099 +  if (aSurface.mType == NativeSurfaceType::CAIRO_SURFACE) {
  1.1100 +    if (aSurface.mSize.width <= 0 ||
  1.1101 +        aSurface.mSize.height <= 0) {
  1.1102 +      gfxWarning() << "Can't create a SourceSurface without a valid size";
  1.1103 +      return nullptr;
  1.1104 +    }
  1.1105 +    cairo_surface_t* surf = static_cast<cairo_surface_t*>(aSurface.mSurface);
  1.1106 +    RefPtr<SourceSurfaceCairo> source =
  1.1107 +      new SourceSurfaceCairo(surf, aSurface.mSize, aSurface.mFormat);
  1.1108 +    return source;
  1.1109 +  }
  1.1110 +
  1.1111 +  return nullptr;
  1.1112 +}
  1.1113 +
  1.1114 +TemporaryRef<DrawTarget>
  1.1115 +DrawTargetCairo::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
  1.1116 +{
  1.1117 +  cairo_surface_t* similar = cairo_surface_create_similar(cairo_get_target(mContext),
  1.1118 +                                                          GfxFormatToCairoContent(aFormat),
  1.1119 +                                                          aSize.width, aSize.height);
  1.1120 +
  1.1121 +  if (!cairo_surface_status(similar)) {
  1.1122 +    RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
  1.1123 +    target->InitAlreadyReferenced(similar, aSize);
  1.1124 +    return target;
  1.1125 +  }
  1.1126 +
  1.1127 +  return nullptr;
  1.1128 +}
  1.1129 +
  1.1130 +bool
  1.1131 +DrawTargetCairo::InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
  1.1132 +{
  1.1133 +  mContext = cairo_create(aSurface);
  1.1134 +  mSurface = aSurface;
  1.1135 +  mSize = aSize;
  1.1136 +  mFormat = aFormat ? *aFormat : CairoContentToGfxFormat(cairo_surface_get_content(aSurface));
  1.1137 +
  1.1138 +  if (mFormat == SurfaceFormat::B8G8R8A8 ||
  1.1139 +      mFormat == SurfaceFormat::R8G8B8A8) {
  1.1140 +    SetPermitSubpixelAA(false);
  1.1141 +  } else {
  1.1142 +    SetPermitSubpixelAA(true);
  1.1143 +  }
  1.1144 +
  1.1145 +  return true;
  1.1146 +}
  1.1147 +
  1.1148 +TemporaryRef<DrawTarget>
  1.1149 +DrawTargetCairo::CreateShadowDrawTarget(const IntSize &aSize, SurfaceFormat aFormat,
  1.1150 +                                        float aSigma) const
  1.1151 +{
  1.1152 +  cairo_surface_t* similar = cairo_surface_create_similar(cairo_get_target(mContext),
  1.1153 +                                                          GfxFormatToCairoContent(aFormat),
  1.1154 +                                                          aSize.width, aSize.height);
  1.1155 +
  1.1156 +  if (cairo_surface_status(similar)) {
  1.1157 +    return nullptr;
  1.1158 +  }
  1.1159 +
  1.1160 +  // If we don't have a blur then we can use the RGBA mask and keep all the
  1.1161 +  // operations in graphics memory.
  1.1162 +  if (aSigma == 0.0F) {
  1.1163 +    RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
  1.1164 +    target->InitAlreadyReferenced(similar, aSize);
  1.1165 +    return target;
  1.1166 +  }
  1.1167 +
  1.1168 +  cairo_surface_t* blursurf = cairo_image_surface_create(CAIRO_FORMAT_A8,
  1.1169 +                                                         aSize.width,
  1.1170 +                                                         aSize.height);
  1.1171 +
  1.1172 +  if (cairo_surface_status(blursurf)) {
  1.1173 +    return nullptr;
  1.1174 +  }
  1.1175 +
  1.1176 +  cairo_surface_t* tee = cairo_tee_surface_create(blursurf);
  1.1177 +  cairo_surface_destroy(blursurf);
  1.1178 +  if (cairo_surface_status(tee)) {
  1.1179 +    cairo_surface_destroy(similar);
  1.1180 +    return nullptr;
  1.1181 +  }
  1.1182 +
  1.1183 +  cairo_tee_surface_add(tee, similar);
  1.1184 +  cairo_surface_destroy(similar);
  1.1185 +
  1.1186 +  RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
  1.1187 +  target->InitAlreadyReferenced(tee, aSize);
  1.1188 +  return target;
  1.1189 +}
  1.1190 +
  1.1191 +bool
  1.1192 +DrawTargetCairo::Init(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
  1.1193 +{
  1.1194 +  cairo_surface_reference(aSurface);
  1.1195 +  return InitAlreadyReferenced(aSurface, aSize, aFormat);
  1.1196 +}
  1.1197 +
  1.1198 +bool
  1.1199 +DrawTargetCairo::Init(const IntSize& aSize, SurfaceFormat aFormat)
  1.1200 +{
  1.1201 +  cairo_surface_t *surf = cairo_image_surface_create(GfxFormatToCairoFormat(aFormat), aSize.width, aSize.height);
  1.1202 +  return InitAlreadyReferenced(surf, aSize);
  1.1203 +}
  1.1204 +
  1.1205 +bool
  1.1206 +DrawTargetCairo::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat)
  1.1207 +{
  1.1208 +  cairo_surface_t* surf =
  1.1209 +    cairo_image_surface_create_for_data(aData,
  1.1210 +                                        GfxFormatToCairoFormat(aFormat),
  1.1211 +                                        aSize.width,
  1.1212 +                                        aSize.height,
  1.1213 +                                        aStride);
  1.1214 +  return InitAlreadyReferenced(surf, aSize);
  1.1215 +}
  1.1216 +
  1.1217 +void *
  1.1218 +DrawTargetCairo::GetNativeSurface(NativeSurfaceType aType)
  1.1219 +{
  1.1220 +  if (aType == NativeSurfaceType::CAIRO_SURFACE) {
  1.1221 +    return cairo_get_target(mContext);
  1.1222 +  }
  1.1223 +  if (aType == NativeSurfaceType::CAIRO_CONTEXT) {
  1.1224 +    return mContext;
  1.1225 +  }
  1.1226 +
  1.1227 +  return nullptr;
  1.1228 +}
  1.1229 +
  1.1230 +void
  1.1231 +DrawTargetCairo::MarkSnapshotIndependent()
  1.1232 +{
  1.1233 +  if (mSnapshot) {
  1.1234 +    if (mSnapshot->refCount() > 1) {
  1.1235 +      // We only need to worry about snapshots that someone else knows about
  1.1236 +      mSnapshot->DrawTargetWillChange();
  1.1237 +    }
  1.1238 +    mSnapshot = nullptr;
  1.1239 +  }
  1.1240 +}
  1.1241 +
  1.1242 +void
  1.1243 +DrawTargetCairo::WillChange(const Path* aPath /* = nullptr */)
  1.1244 +{
  1.1245 +  MarkSnapshotIndependent();
  1.1246 +  MOZ_ASSERT(!mLockedBits);
  1.1247 +}
  1.1248 +
  1.1249 +void
  1.1250 +DrawTargetCairo::SetTransform(const Matrix& aTransform)
  1.1251 +{
  1.1252 +  mTransform = aTransform;
  1.1253 +
  1.1254 +  cairo_matrix_t mat;
  1.1255 +  GfxMatrixToCairoMatrix(mTransform, mat);
  1.1256 +  cairo_set_matrix(mContext, &mat);
  1.1257 +}
  1.1258 +
  1.1259 +Rect
  1.1260 +DrawTargetCairo::GetUserSpaceClip()
  1.1261 +{
  1.1262 +  double clipX1, clipY1, clipX2, clipY2;
  1.1263 +  cairo_clip_extents(mContext, &clipX1, &clipY1, &clipX2, &clipY2);
  1.1264 +  return Rect(clipX1, clipY1, clipX2 - clipX1, clipY2 - clipY1); // Narrowing of doubles to floats
  1.1265 +}
  1.1266 +
  1.1267 +cairo_t*
  1.1268 +BorrowedCairoContext::BorrowCairoContextFromDrawTarget(DrawTarget* aDT)
  1.1269 +{
  1.1270 +  if (aDT->GetType() != BackendType::CAIRO || aDT->IsDualDrawTarget()) {
  1.1271 +    return nullptr;
  1.1272 +  }
  1.1273 +  DrawTargetCairo* cairoDT = static_cast<DrawTargetCairo*>(aDT);
  1.1274 +
  1.1275 +  cairoDT->WillChange();
  1.1276 +
  1.1277 +  // save the state to make it easier for callers to avoid mucking with things
  1.1278 +  cairo_save(cairoDT->mContext);
  1.1279 +
  1.1280 +  // Neuter the DrawTarget while the context is being borrowed
  1.1281 +  cairo_t* cairo = cairoDT->mContext;
  1.1282 +  cairoDT->mContext = nullptr;
  1.1283 +
  1.1284 +  return cairo;
  1.1285 +}
  1.1286 +
  1.1287 +void
  1.1288 +BorrowedCairoContext::ReturnCairoContextToDrawTarget(DrawTarget* aDT,
  1.1289 +                                                     cairo_t* aCairo)
  1.1290 +{
  1.1291 +  if (aDT->GetType() != BackendType::CAIRO || aDT->IsDualDrawTarget()) {
  1.1292 +    return;
  1.1293 +  }
  1.1294 +  DrawTargetCairo* cairoDT = static_cast<DrawTargetCairo*>(aDT);
  1.1295 +
  1.1296 +  cairo_restore(aCairo);
  1.1297 +  cairoDT->mContext = aCairo;
  1.1298 +}
  1.1299 +
  1.1300 +}
  1.1301 +}

mercurial