tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PrintTargetRecording.cpp (3953B)


      1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "PrintTargetRecording.h"
      7 
      8 #include "cairo.h"
      9 #include "mozilla/gfx/2D.h"
     10 #include "mozilla/gfx/Logging.h"
     11 
     12 namespace mozilla {
     13 namespace gfx {
     14 
     15 PrintTargetRecording::PrintTargetRecording(cairo_surface_t* aCairoSurface,
     16                                           const IntSize& aSize)
     17    : PrintTarget(aCairoSurface, aSize) {}
     18 
     19 /* static */
     20 already_AddRefed<PrintTargetRecording> PrintTargetRecording::CreateOrNull(
     21    const IntSize& aSize) {
     22  if (!Factory::CheckSurfaceSize(aSize)) {
     23    return nullptr;
     24  }
     25 
     26  // Perhaps surprisingly, this surface is never actually drawn to.  This class
     27  // creates a DrawTargetRecording using CreateRecordingDrawTarget, and
     28  // that needs another DrawTarget to be passed to it.  You might expect the
     29  // type of the DrawTarget that is passed to matter because it would seem
     30  // logical to encoded its type in the recording, and on replaying the
     31  // recording a DrawTarget of the same type would be created.  However, the
     32  // passed DrawTarget's type doesn't seem to be encoded any more accurately
     33  // than just "BackendType::CAIRO".  Even if it were, the code that replays the
     34  // recording is PrintTranslator::TranslateRecording which (indirectly) calls
     35  // MakePrintTarget on the type of nsIDeviceContextSpecProxy that is created
     36  // for the platform that we're running on, and the type of DrawTarget that
     37  // that returns is hardcoded.
     38  //
     39  // The only reason that we use cairo_recording_surface_create here is:
     40  //
     41  //   * It's pretty much the only cairo_*_surface_create methods that's both
     42  //     available on all platforms and doesn't require allocating a
     43  //     potentially large surface.
     44  //
     45  //   * Since we need a DrawTarget to pass to CreateRecordingDrawTarget we
     46  //     might as well leverage our base class's machinery to create a
     47  //     DrawTarget (it's as good a way as any other that will work), and to do
     48  //     that we need a cairo_surface_t.
     49  //
     50  // So the fact that this is a "recording" PrintTarget and the function that
     51  // we call here is cairo_recording_surface_create is simply a coincidence. We
     52  // could use any cairo_*_surface_create method and this class would still
     53  // work.
     54  //
     55  cairo_surface_t* surface =
     56      cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, nullptr);
     57 
     58  if (cairo_surface_status(surface)) {
     59    return nullptr;
     60  }
     61 
     62  // The new object takes ownership of our surface reference.
     63  RefPtr<PrintTargetRecording> target =
     64      new PrintTargetRecording(surface, aSize);
     65 
     66  return target.forget();
     67 }
     68 
     69 already_AddRefed<DrawTarget> PrintTargetRecording::MakeDrawTarget(
     70    const IntSize& aSize, DrawEventRecorder* aRecorder) {
     71  MOZ_ASSERT(aRecorder, "A DrawEventRecorder is required");
     72 
     73  if (!aRecorder) {
     74    return nullptr;
     75  }
     76 
     77  RefPtr<DrawTarget> dt = PrintTarget::MakeDrawTarget(aSize, nullptr);
     78  if (dt) {
     79    dt = CreateRecordingDrawTarget(aRecorder, dt);
     80    if (!dt || !dt->IsValid()) {
     81      return nullptr;
     82    }
     83  }
     84 
     85  return dt.forget();
     86 }
     87 
     88 already_AddRefed<DrawTarget> PrintTargetRecording::CreateRecordingDrawTarget(
     89    DrawEventRecorder* aRecorder, DrawTarget* aDrawTarget) {
     90  MOZ_ASSERT(aRecorder);
     91  MOZ_ASSERT(aDrawTarget);
     92 
     93  RefPtr<DrawTarget> dt;
     94 
     95  if (aRecorder) {
     96    // It doesn't really matter what we pass as the DrawTarget here.
     97    dt = gfx::Factory::CreateRecordingDrawTarget(aRecorder, aDrawTarget,
     98                                                 aDrawTarget->GetRect());
     99  }
    100 
    101  if (!dt || !dt->IsValid()) {
    102    gfxCriticalNote
    103        << "Failed to create a recording DrawTarget for PrintTarget";
    104    return nullptr;
    105  }
    106 
    107  return dt.forget();
    108 }
    109 
    110 }  // namespace gfx
    111 }  // namespace mozilla