Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this
3 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #include "mozilla/ipc/DocumentRendererChild.h"
6 :
7 : #include "base/basictypes.h"
8 :
9 : #include "gfx2DGlue.h"
10 : #include "gfxContext.h"
11 : #include "gfxPattern.h"
12 : #include "mozilla/gfx/2D.h"
13 : #include "mozilla/RefPtr.h"
14 : #include "nsPIDOMWindow.h"
15 : #include "nsIDOMWindow.h"
16 : #include "nsIDocShell.h"
17 : #include "nsIInterfaceRequestorUtils.h"
18 : #include "nsComponentManagerUtils.h"
19 : #include "nsCSSParser.h"
20 : #include "nsPresContext.h"
21 : #include "nsCOMPtr.h"
22 : #include "nsColor.h"
23 : #include "nsLayoutUtils.h"
24 : #include "nsContentUtils.h"
25 : #include "nsCSSValue.h"
26 : #include "nsRuleNode.h"
27 : #include "mozilla/gfx/Matrix.h"
28 :
29 : using namespace mozilla;
30 : using namespace mozilla::gfx;
31 : using namespace mozilla::ipc;
32 :
33 0 : DocumentRendererChild::DocumentRendererChild()
34 0 : {}
35 :
36 0 : DocumentRendererChild::~DocumentRendererChild()
37 0 : {}
38 :
39 : bool
40 0 : DocumentRendererChild::RenderDocument(nsPIDOMWindowOuter* window,
41 : const nsRect& documentRect,
42 : const mozilla::gfx::Matrix& transform,
43 : const nsString& aBGColor,
44 : uint32_t renderFlags,
45 : bool flushLayout,
46 : const nsIntSize& renderSize,
47 : nsCString& data)
48 : {
49 0 : if (flushLayout)
50 0 : nsContentUtils::FlushLayoutForTree(window);
51 :
52 0 : RefPtr<nsPresContext> presContext;
53 0 : if (window) {
54 0 : nsIDocShell* docshell = window->GetDocShell();
55 0 : if (docshell) {
56 0 : docshell->GetPresContext(getter_AddRefs(presContext));
57 : }
58 : }
59 0 : if (!presContext)
60 0 : return false;
61 :
62 0 : nsCSSParser parser;
63 0 : nsCSSValue bgColorValue;
64 0 : if (!parser.ParseColorString(aBGColor, nullptr, 0, bgColorValue)) {
65 0 : return false;
66 : }
67 :
68 : nscolor bgColor;
69 0 : if (!nsRuleNode::ComputeColor(bgColorValue, presContext, nullptr, bgColor)) {
70 0 : return false;
71 : }
72 :
73 : // Draw directly into the output array.
74 0 : data.SetLength(renderSize.width * renderSize.height * 4);
75 :
76 : RefPtr<DrawTarget> dt =
77 0 : Factory::CreateDrawTargetForData(gfxPlatform::GetPlatform()->GetSoftwareBackend(),
78 0 : reinterpret_cast<uint8_t*>(data.BeginWriting()),
79 0 : IntSize(renderSize.width, renderSize.height),
80 0 : 4 * renderSize.width,
81 0 : SurfaceFormat::B8G8R8A8);
82 0 : if (!dt || !dt->IsValid()) {
83 0 : gfxWarning() << "DocumentRendererChild::RenderDocument failed to Factory::CreateDrawTargetForData";
84 0 : return false;
85 : }
86 0 : RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(dt);
87 0 : MOZ_ASSERT(ctx); // already checked the draw target above
88 0 : ctx->SetMatrix(mozilla::gfx::ThebesMatrix(transform));
89 :
90 0 : nsCOMPtr<nsIPresShell> shell = presContext->PresShell();
91 0 : shell->RenderDocument(documentRect, renderFlags, bgColor, ctx);
92 :
93 0 : return true;
94 : }
|