PrintTranslator.cpp (2632B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "PrintTranslator.h" 8 9 #include "InlineTranslator.h" 10 #include "gfxContext.h" 11 #include "mozilla/ProfilerMarkers.h" 12 #include "mozilla/UniquePtr.h" 13 #include "mozilla/gfx/RecordedEvent.h" 14 #include "mozilla/gfx/RecordingTypes.h" 15 #include "nsDeviceContext.h" 16 17 using namespace mozilla::gfx; 18 19 namespace mozilla { 20 namespace layout { 21 22 PrintTranslator::PrintTranslator(nsDeviceContext* aDeviceContext) 23 : mDeviceContext(aDeviceContext) { 24 UniquePtr<gfxContext> context = 25 mDeviceContext->CreateReferenceRenderingContext(); 26 mBaseDT = context->GetDrawTarget(); 27 } 28 29 bool PrintTranslator::TranslateRecording(PRFileDescStream& aRecording) { 30 AUTO_PROFILER_MARKER_TEXT("PrintTranslator", LAYOUT_Printing, {}, 31 "PrintTranslator::TranslateRecording"_ns); 32 33 uint32_t magicInt; 34 ReadElement(aRecording, magicInt); 35 if (magicInt != mozilla::gfx::kMagicInt) { 36 return false; 37 } 38 39 uint16_t majorRevision; 40 ReadElement(aRecording, majorRevision); 41 if (majorRevision != kMajorRevision) { 42 return false; 43 } 44 45 uint16_t minorRevision; 46 ReadElement(aRecording, minorRevision); 47 if (minorRevision > kMinorRevision) { 48 return false; 49 } 50 51 uint8_t eventType = RecordedEvent::EventType::INVALID; 52 ReadElement(aRecording, eventType); 53 while (aRecording.good()) { 54 bool success = RecordedEvent::DoWithEventFromStream( 55 aRecording, static_cast<RecordedEvent::EventType>(eventType), 56 [&](RecordedEvent* recordedEvent) -> bool { 57 // Make sure that the whole event was read from the stream. 58 if (!aRecording.good()) { 59 return false; 60 } 61 62 return recordedEvent->PlayEvent(this); 63 }); 64 65 if (!success) { 66 return false; 67 } 68 69 ReadElement(aRecording, eventType); 70 } 71 72 return true; 73 } 74 75 already_AddRefed<DrawTarget> PrintTranslator::CreateDrawTarget( 76 ReferencePtr aRefPtr, const gfx::IntSize& aSize, 77 gfx::SurfaceFormat aFormat) { 78 UniquePtr<gfxContext> context = mDeviceContext->CreateRenderingContext(); 79 if (!context) { 80 NS_WARNING("Failed to create rendering context for print."); 81 return nullptr; 82 } 83 84 RefPtr<DrawTarget> drawTarget = context->GetDrawTarget(); 85 AddDrawTarget(aRefPtr, drawTarget); 86 return drawTarget.forget(); 87 } 88 89 } // namespace layout 90 } // namespace mozilla