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