author Mark Banner <>
Wed, 07 Feb 2018 16:01:29 +0000
changeset 455301 098f795537450afd8e7a3040247613cf570a859b
parent 412305 94dda3aac54c8b95c338e9879f8de3592bedce6a
child 505383 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1436389 - Upgrade ESLint to version 4.17.0. r=mossop MozReview-Commit-ID: I4KBELxhBlM

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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 */

#include "PrintTargetRecording.h"

#include "cairo.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Logging.h"

namespace mozilla {
namespace gfx {

PrintTargetRecording::PrintTargetRecording(cairo_surface_t* aCairoSurface,
                                           const IntSize& aSize)
  : PrintTarget(aCairoSurface, aSize)

/* static */ already_AddRefed<PrintTargetRecording>
PrintTargetRecording::CreateOrNull(const IntSize& aSize)
  if (!Factory::CheckSurfaceSize(aSize)) {
    return nullptr;

  // Perhaps surprisingly, this surface is never actually drawn to.  This class
  // creates a DrawTargetWrapAndRecord using CreateWrapAndRecordDrawTarget, and that
  // needs another DrawTarget to be passed to it.  You might expect the type of
  // the DrawTarget that is passed to matter because it would seem logical to
  // encoded its type in the recording, and on replaying the recording a
  // DrawTarget of the same type would be created.  However, the passed
  // DrawTarget's type doesn't seem to be encoded any more accurately than just
  // "BackendType::CAIRO".  Even if it were, the code that replays the
  // recording is PrintTranslator::TranslateRecording which (indirectly) calls
  // MakePrintTarget on the type of nsIDeviceContextSpecProxy that is created
  // for the platform that we're running on, and the type of DrawTarget that
  // that returns is hardcoded.
  // The only reason that we use cairo_recording_surface_create here is:
  //   * It's pretty much the only cairo_*_surface_create methods that's both
  //     available on all platforms and doesn't require allocating a
  //     potentially large surface.
  //   * Since we need a DrawTarget to pass to CreateWrapAndRecordDrawTarget we
  //     might as well leverage our base class's machinery to create a
  //     DrawTarget (it's as good a way as any other that will work), and to do
  //     that we need a cairo_surface_t.
  // So the fact that this is a "recording" PrintTarget and the function that
  // we call here is cairo_recording_surface_create is simply a coincidence. We
  // could use any cairo_*_surface_create method and this class would still
  // work.
  cairo_surface_t* surface =
    cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, nullptr);

  if (cairo_surface_status(surface)) {
    return nullptr;

  // The new object takes ownership of our surface reference.
  RefPtr<PrintTargetRecording> target =
    new PrintTargetRecording(surface, aSize);

  return target.forget();

PrintTargetRecording::MakeDrawTarget(const IntSize& aSize,
                                     DrawEventRecorder* aRecorder)
  MOZ_ASSERT(aRecorder, "A DrawEventRecorder is required");

  if (!aRecorder) {
    return nullptr;

  RefPtr<DrawTarget> dt = PrintTarget::MakeDrawTarget(aSize, nullptr);
  if (dt) {
    dt = CreateWrapAndRecordDrawTarget(aRecorder, dt);
    if (!dt || !dt->IsValid()) {
      return nullptr;

  return dt.forget();

PrintTargetRecording::CreateWrapAndRecordDrawTarget(DrawEventRecorder* aRecorder,
                                                    DrawTarget* aDrawTarget)

  RefPtr<DrawTarget> dt;

  if (aRecorder) {
    // It doesn't really matter what we pass as the DrawTarget here.
    dt = gfx::Factory::CreateWrapAndRecordDrawTarget(aRecorder, aDrawTarget);

  if (!dt || !dt->IsValid()) {
      << "Failed to create a recording DrawTarget for PrintTarget";
    return nullptr;

  return dt.forget();

} // namespace gfx
} // namespace mozilla