Line data Source code
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 "gfxUtils.h"
8 : #include "mozilla/Range.h"
9 : #include "mozilla/gfx/2D.h"
10 : #include "mozilla/gfx/InlineTranslator.h"
11 : #include "mozilla/gfx/RecordedEvent.h"
12 : #include "WebRenderTypes.h"
13 :
14 : #include <iostream>
15 :
16 : #ifdef MOZ_ENABLE_FREETYPE
17 : #include "mozilla/ThreadLocal.h"
18 : #endif
19 :
20 : namespace mozilla {
21 : namespace wr {
22 :
23 : #ifdef MOZ_ENABLE_FREETYPE
24 : static MOZ_THREAD_LOCAL(FT_Library) sFTLibrary;
25 : #endif
26 :
27 0 : static bool Moz2DRenderCallback(const Range<const uint8_t> aBlob,
28 : gfx::IntSize aSize,
29 : gfx::SurfaceFormat aFormat,
30 : Range<uint8_t> aOutput)
31 : {
32 0 : MOZ_ASSERT(aSize.width > 0 && aSize.height > 0);
33 0 : if (aSize.width <= 0 || aSize.height <= 0) {
34 0 : return false;
35 : }
36 :
37 0 : auto stride = aSize.width * gfx::BytesPerPixel(aFormat);
38 :
39 0 : if (aOutput.length() < static_cast<size_t>(aSize.height * stride)) {
40 0 : return false;
41 : }
42 :
43 0 : void* fontContext = nullptr;
44 : #ifdef MOZ_ENABLE_FREETYPE
45 0 : if (!sFTLibrary.init()) {
46 0 : return false;
47 : }
48 0 : if (!sFTLibrary.get()) {
49 0 : FT_Library library = gfx::Factory::NewFTLibrary();
50 0 : if (!library) {
51 0 : return false;
52 : }
53 0 : sFTLibrary.set(library);
54 : }
55 0 : fontContext = sFTLibrary.get();
56 : #endif
57 :
58 : // In bindings.rs we allocate a buffer filled with opaque white.
59 0 : bool uninitialized = false;
60 :
61 0 : RefPtr<gfx::DrawTarget> dt = gfx::Factory::CreateDrawTargetForData(
62 : gfx::BackendType::SKIA,
63 0 : aOutput.begin().get(),
64 : aSize,
65 : stride,
66 : aFormat,
67 : uninitialized
68 0 : );
69 :
70 0 : if (!dt) {
71 0 : return false;
72 : }
73 :
74 0 : gfx::InlineTranslator translator(dt, fontContext);
75 :
76 0 : auto ret = translator.TranslateRecording((char*)aBlob.begin().get(), aBlob.length());
77 :
78 : #if 0
79 : static int i = 0;
80 : char filename[40];
81 : sprintf(filename, "out%d.png", i++);
82 : gfxUtils::WriteAsPNG(dt, filename);
83 : #endif
84 :
85 0 : return ret;
86 : }
87 :
88 : } // namespace
89 : } // namespace
90 :
91 : extern "C" {
92 :
93 0 : bool wr_moz2d_render_cb(const WrByteSlice blob,
94 : uint32_t width, uint32_t height,
95 : mozilla::wr::ImageFormat aFormat,
96 : MutByteSlice output)
97 : {
98 0 : return mozilla::wr::Moz2DRenderCallback(mozilla::wr::ByteSliceToRange(blob),
99 : mozilla::gfx::IntSize(width, height),
100 : mozilla::wr::WrImageFormatToSurfaceFormat(aFormat),
101 0 : mozilla::wr::MutByteSliceToRange(output));
102 : }
103 :
104 : } // extern
105 :
106 :
|