2017-03-27 14:44:52 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
#include "gfxUtils.h"
|
2017-03-27 14:44:52 +03:00
|
|
|
#include "mozilla/Range.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
2017-04-26 23:31:59 +03:00
|
|
|
#include "mozilla/gfx/InlineTranslator.h"
|
|
|
|
#include "mozilla/gfx/RecordedEvent.h"
|
2017-03-27 14:44:52 +03:00
|
|
|
#include "WebRenderTypes.h"
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
#include <iostream>
|
|
|
|
|
2017-05-18 04:56:58 +03:00
|
|
|
#ifdef MOZ_ENABLE_FREETYPE
|
|
|
|
#include "mozilla/ThreadLocal.h"
|
|
|
|
#endif
|
|
|
|
|
2017-03-27 14:44:52 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace wr {
|
|
|
|
|
2017-05-18 04:56:58 +03:00
|
|
|
#ifdef MOZ_ENABLE_FREETYPE
|
|
|
|
static MOZ_THREAD_LOCAL(FT_Library) sFTLibrary;
|
|
|
|
#endif
|
|
|
|
|
2017-04-11 23:13:44 +03:00
|
|
|
static bool Moz2DRenderCallback(const Range<const uint8_t> aBlob,
|
2017-03-27 14:44:52 +03:00
|
|
|
gfx::IntSize aSize,
|
|
|
|
gfx::SurfaceFormat aFormat,
|
2017-04-26 23:31:59 +03:00
|
|
|
Range<uint8_t> aOutput)
|
2017-03-27 14:44:52 +03:00
|
|
|
{
|
2017-04-26 23:31:59 +03:00
|
|
|
MOZ_ASSERT(aSize.width > 0 && aSize.height > 0);
|
|
|
|
if (aSize.width <= 0 || aSize.height <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto stride = aSize.width * gfx::BytesPerPixel(aFormat);
|
|
|
|
|
|
|
|
if (aOutput.length() < static_cast<size_t>(aSize.height * stride)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:16:06 +03:00
|
|
|
void* fontContext = nullptr;
|
2017-05-18 04:56:58 +03:00
|
|
|
#ifdef MOZ_ENABLE_FREETYPE
|
|
|
|
if (!sFTLibrary.init()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!sFTLibrary.get()) {
|
2017-05-18 20:16:06 +03:00
|
|
|
FT_Library library = gfx::Factory::NewFTLibrary();
|
|
|
|
if (!library) {
|
2017-05-18 04:56:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sFTLibrary.set(library);
|
|
|
|
}
|
2017-05-18 20:16:06 +03:00
|
|
|
fontContext = sFTLibrary.get();
|
2017-05-18 04:56:58 +03:00
|
|
|
#endif
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
// In bindings.rs we allocate a buffer filled with opaque white.
|
|
|
|
bool uninitialized = false;
|
|
|
|
|
|
|
|
RefPtr<gfx::DrawTarget> dt = gfx::Factory::CreateDrawTargetForData(
|
|
|
|
gfx::BackendType::SKIA,
|
|
|
|
aOutput.begin().get(),
|
|
|
|
aSize,
|
|
|
|
stride,
|
|
|
|
aFormat,
|
|
|
|
uninitialized
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!dt) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:16:06 +03:00
|
|
|
gfx::InlineTranslator translator(dt, fontContext);
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2017-07-04 22:36:05 +03:00
|
|
|
auto ret = translator.TranslateRecording((char*)aBlob.begin().get(), aBlob.length());
|
2017-04-26 23:31:59 +03:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int i = 0;
|
|
|
|
char filename[40];
|
|
|
|
sprintf(filename, "out%d.png", i++);
|
|
|
|
gfxUtils::WriteAsPNG(dt, filename);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2017-03-27 14:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2017-06-28 02:20:36 +03:00
|
|
|
bool wr_moz2d_render_cb(const mozilla::wr::WrByteSlice blob,
|
2017-03-27 14:44:52 +03:00
|
|
|
uint32_t width, uint32_t height,
|
|
|
|
mozilla::wr::ImageFormat aFormat,
|
2017-06-28 02:20:36 +03:00
|
|
|
mozilla::wr::MutByteSlice output)
|
2017-03-27 14:44:52 +03:00
|
|
|
{
|
|
|
|
return mozilla::wr::Moz2DRenderCallback(mozilla::wr::ByteSliceToRange(blob),
|
|
|
|
mozilla::gfx::IntSize(width, height),
|
|
|
|
mozilla::wr::WrImageFormatToSurfaceFormat(aFormat),
|
2017-04-26 23:31:59 +03:00
|
|
|
mozilla::wr::MutByteSliceToRange(output));
|
2017-03-27 14:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // extern
|
|
|
|
|
|
|
|
|