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-08-14 22:28:37 +03:00
|
|
|
#include "webrender_ffi.h"
|
2017-03-27 14:44:52 +03:00
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
#include <iostream>
|
2017-10-28 01:21:27 +03:00
|
|
|
#include <unordered_map>
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2017-11-07 04:21:25 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include "mozilla/gfx/UnscaledFontMac.h"
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
#include "mozilla/gfx/UnscaledFontDWrite.h"
|
2017-11-07 22:10:31 +03:00
|
|
|
#else
|
2017-11-07 04:21:25 +03:00
|
|
|
#include "mozilla/gfx/UnscaledFontFreeType.h"
|
2017-05-18 04:56:58 +03:00
|
|
|
#endif
|
|
|
|
|
2017-10-28 01:21:27 +03:00
|
|
|
namespace std {
|
|
|
|
template <>
|
|
|
|
struct hash<mozilla::wr::FontKey>{
|
|
|
|
public :
|
|
|
|
size_t operator()(const mozilla::wr::FontKey &key ) const
|
|
|
|
{
|
|
|
|
return hash<size_t>()(mozilla::wr::AsUint64(key));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-27 14:44:52 +03:00
|
|
|
namespace mozilla {
|
2017-10-28 01:21:27 +03:00
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
2017-03-27 14:44:52 +03:00
|
|
|
namespace wr {
|
|
|
|
|
2017-10-28 01:21:27 +03:00
|
|
|
struct FontTemplate {
|
2017-10-29 01:32:24 +03:00
|
|
|
const uint8_t *mData;
|
2017-10-28 01:21:27 +03:00
|
|
|
size_t mSize;
|
2017-10-29 01:32:24 +03:00
|
|
|
uint32_t mIndex;
|
2017-10-28 01:21:27 +03:00
|
|
|
const VecU8 *mVec;
|
2017-10-28 05:08:47 +03:00
|
|
|
RefPtr<UnscaledFont> mUnscaledFont;
|
2017-10-28 01:21:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
std::unordered_map<FontKey, FontTemplate> sFontDataTable;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
void
|
2017-10-29 01:32:24 +03:00
|
|
|
AddFontData(WrFontKey aKey, const uint8_t *aData, size_t aSize, uint32_t aIndex, const ArcVecU8 *aVec) {
|
2017-10-28 01:21:27 +03:00
|
|
|
auto i = sFontDataTable.find(aKey);
|
|
|
|
if (i == sFontDataTable.end()) {
|
|
|
|
FontTemplate font;
|
|
|
|
font.mData = aData;
|
|
|
|
font.mSize = aSize;
|
|
|
|
font.mIndex = aIndex;
|
|
|
|
font.mVec = wr_add_ref_arc(aVec);
|
|
|
|
sFontDataTable[aKey] = font;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 04:21:25 +03:00
|
|
|
void
|
|
|
|
AddNativeFontHandle(WrFontKey aKey, void* aHandle, uint32_t aIndex) {
|
|
|
|
auto i = sFontDataTable.find(aKey);
|
|
|
|
if (i == sFontDataTable.end()) {
|
|
|
|
FontTemplate font;
|
|
|
|
font.mData = nullptr;
|
|
|
|
font.mSize = 0;
|
|
|
|
font.mIndex = 0;
|
|
|
|
font.mVec = nullptr;
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
font.mUnscaledFont = new UnscaledFontMac(reinterpret_cast<CGFontRef>(aHandle), true);
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
font.mUnscaledFont = new UnscaledFontDWrite(reinterpret_cast<IDWriteFontFace*>(aHandle), nullptr);
|
|
|
|
#elif defined(ANDROID)
|
|
|
|
font.mUnscaledFont = new UnscaledFontFreeType(reinterpret_cast<const char*>(aHandle), aIndex);
|
|
|
|
#else
|
|
|
|
font.mUnscaledFont = new UnscaledFontFontconfig(reinterpret_cast<const char*>(aHandle), aIndex);
|
|
|
|
#endif
|
|
|
|
sFontDataTable[aKey] = font;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 01:21:27 +03:00
|
|
|
void
|
2017-10-29 01:32:24 +03:00
|
|
|
DeleteFontData(WrFontKey aKey) {
|
2017-10-28 01:21:27 +03:00
|
|
|
auto i = sFontDataTable.find(aKey);
|
|
|
|
if (i != sFontDataTable.end()) {
|
2017-11-07 04:21:25 +03:00
|
|
|
if (i->second.mVec) {
|
|
|
|
wr_dec_ref_arc(i->second.mVec);
|
|
|
|
}
|
2017-10-31 05:35:40 +03:00
|
|
|
sFontDataTable.erase(i);
|
2017-10-28 01:21:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<UnscaledFont>
|
|
|
|
GetUnscaledFont(Translator *aTranslator, wr::FontKey key) {
|
|
|
|
MOZ_ASSERT(sFontDataTable.find(key) != sFontDataTable.end());
|
2017-10-28 05:08:47 +03:00
|
|
|
auto &data = sFontDataTable[key];
|
|
|
|
if (data.mUnscaledFont) {
|
|
|
|
return data.mUnscaledFont;
|
|
|
|
}
|
2017-11-07 04:21:25 +03:00
|
|
|
MOZ_ASSERT(data.mData);
|
2017-10-28 01:21:27 +03:00
|
|
|
FontType type =
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
FontType::MAC;
|
2017-11-07 04:21:25 +03:00
|
|
|
#elif defined(XP_WIN)
|
2017-10-28 01:21:27 +03:00
|
|
|
FontType::DWRITE;
|
2017-11-07 04:21:25 +03:00
|
|
|
#elif defined(ANDROID)
|
2017-10-28 01:21:27 +03:00
|
|
|
FontType::FREETYPE;
|
|
|
|
#else
|
|
|
|
FontType::FONTCONFIG;
|
|
|
|
#endif
|
|
|
|
// makes a copy of the data
|
|
|
|
RefPtr<NativeFontResource> fontResource = Factory::CreateNativeFontResource((uint8_t*)data.mData, data.mSize,
|
|
|
|
aTranslator->GetReferenceDrawTarget()->GetBackendType(),
|
|
|
|
type,
|
|
|
|
aTranslator->GetFontContext());
|
|
|
|
RefPtr<UnscaledFont> unscaledFont;
|
|
|
|
if (fontResource) {
|
|
|
|
// Instance data is only needed for GDI fonts which webrender does not
|
|
|
|
// support.
|
|
|
|
unscaledFont = fontResource->CreateUnscaledFont(data.mIndex, nullptr, 0);
|
|
|
|
}
|
2017-10-28 05:08:47 +03:00
|
|
|
data.mUnscaledFont = unscaledFont;
|
2017-10-28 01:21:27 +03:00
|
|
|
return unscaledFont;
|
|
|
|
}
|
|
|
|
|
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-08-14 22:28:37 +03:00
|
|
|
const uint16_t *aTileSize,
|
|
|
|
const mozilla::wr::TileOffset *aTileOffset,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-14 22:28:37 +03:00
|
|
|
if (aTileOffset) {
|
|
|
|
// It's overkill to use a TiledDrawTarget for a single tile
|
|
|
|
// but it was the easiest way to get the offset handling working
|
|
|
|
gfx::TileSet tileset;
|
|
|
|
gfx::Tile tile;
|
|
|
|
tile.mDrawTarget = dt;
|
|
|
|
tile.mTileOrigin = gfx::IntPoint(aTileOffset->x * *aTileSize, aTileOffset->y * *aTileSize);
|
|
|
|
tileset.mTiles = &tile;
|
|
|
|
tileset.mTileCount = 1;
|
|
|
|
dt = gfx::Factory::CreateTiledDrawTarget(tileset);
|
|
|
|
}
|
|
|
|
|
2017-10-28 01:21:27 +03:00
|
|
|
struct Reader {
|
|
|
|
const uint8_t *buf;
|
|
|
|
size_t len;
|
|
|
|
size_t pos;
|
|
|
|
|
|
|
|
Reader(const uint8_t *buf, size_t len) : buf(buf), len(len), pos(0) {}
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2017-10-28 01:21:27 +03:00
|
|
|
size_t ReadSize() {
|
|
|
|
size_t ret;
|
|
|
|
MOZ_RELEASE_ASSERT(pos + sizeof(ret) <= len);
|
|
|
|
memcpy(&ret, buf + pos, sizeof(ret));
|
|
|
|
pos += sizeof(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//XXX: Make safe
|
|
|
|
size_t indexOffset = *(size_t*)(aBlob.end().get()-sizeof(size_t));
|
|
|
|
Reader reader(aBlob.begin().get()+indexOffset, aBlob.length()-sizeof(size_t)-indexOffset);
|
|
|
|
|
|
|
|
bool ret;
|
|
|
|
size_t offset = 0;
|
|
|
|
while (reader.pos < reader.len) {
|
|
|
|
size_t end = reader.ReadSize();
|
|
|
|
size_t extra_end = reader.ReadSize();
|
|
|
|
|
2017-11-07 22:10:31 +03:00
|
|
|
gfx::InlineTranslator translator(dt);
|
2017-10-28 01:21:27 +03:00
|
|
|
|
|
|
|
size_t count = *(size_t*)(aBlob.begin().get() + end);
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
wr::FontKey key = *(wr::FontKey*)(aBlob.begin() + end + sizeof(count) + sizeof(wr::FontKey)*i).get();
|
|
|
|
RefPtr<UnscaledFont> font = GetUnscaledFont(&translator, key);
|
|
|
|
translator.AddUnscaledFont(0, font);
|
|
|
|
}
|
|
|
|
Range<const uint8_t> blob(aBlob.begin() + offset, aBlob.begin() + end);
|
|
|
|
ret = translator.TranslateRecording((char*)blob.begin().get(), blob.length());
|
|
|
|
offset = extra_end;
|
|
|
|
}
|
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-07-19 01:35:52 +03:00
|
|
|
bool wr_moz2d_render_cb(const mozilla::wr::ByteSlice blob,
|
2017-03-27 14:44:52 +03:00
|
|
|
uint32_t width, uint32_t height,
|
|
|
|
mozilla::wr::ImageFormat aFormat,
|
2017-08-14 22:28:37 +03:00
|
|
|
const uint16_t *aTileSize,
|
|
|
|
const mozilla::wr::TileOffset *aTileOffset,
|
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),
|
2017-07-19 10:28:58 +03:00
|
|
|
mozilla::wr::ImageFormatToSurfaceFormat(aFormat),
|
2017-08-14 22:28:37 +03:00
|
|
|
aTileSize,
|
|
|
|
aTileOffset,
|
2017-04-26 23:31:59 +03:00
|
|
|
mozilla::wr::MutByteSliceToRange(output));
|
2017-03-27 14:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // extern
|
|
|
|
|
|
|
|
|