use serialprocs for typefaces
Bug: skia: Change-Id: Ibf59a0fdcf68e8555bd4241e9473e733f6a30993 Reviewed-on: https://skia-review.googlesource.com/81840 Reviewed-by: Florin Malita <fmalita@chromium.org> Reviewed-by: Mike Klein <mtklein@chromium.org> Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
parent
75d55d3d45
commit
b681972e79
@ -17,6 +17,9 @@
|
|||||||
class SkReadBuffer;
|
class SkReadBuffer;
|
||||||
class SkWriteBuffer;
|
class SkWriteBuffer;
|
||||||
|
|
||||||
|
struct SkSerialProcs;
|
||||||
|
struct SkDeserialProcs;
|
||||||
|
|
||||||
typedef void (*SkTypefaceCatalogerProc)(SkTypeface*, void* ctx);
|
typedef void (*SkTypefaceCatalogerProc)(SkTypeface*, void* ctx);
|
||||||
typedef sk_sp<SkTypeface> (*SkTypefaceResolverProc)(uint32_t id, void* ctx);
|
typedef sk_sp<SkTypeface> (*SkTypefaceResolverProc)(uint32_t id, void* ctx);
|
||||||
|
|
||||||
@ -71,6 +74,9 @@ public:
|
|||||||
static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
|
static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
|
||||||
SkTypefaceResolverProc, void* ctx);
|
SkTypefaceResolverProc, void* ctx);
|
||||||
|
|
||||||
|
sk_sp<SkData> serialize(const SkSerialProcs&) const;
|
||||||
|
static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size, const SkDeserialProcs&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class SkNVRefCnt<SkTextBlob>;
|
friend class SkNVRefCnt<SkTextBlob>;
|
||||||
class RunRecord;
|
class RunRecord;
|
||||||
|
@ -374,12 +374,26 @@ sk_sp<SkTypeface> SkReadBuffer::readTypeface() {
|
|||||||
return sk_ref_sp(fInflator->getTypeface(this->read32()));
|
return sk_ref_sp(fInflator->getTypeface(this->read32()));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t index = this->readUInt();
|
// Read 32 bits (signed)
|
||||||
if (0 == index || index > (unsigned)fTFCount) {
|
// 0 -- failure
|
||||||
|
// >0 -- index
|
||||||
|
// <0 -- custom (serial procs) : negative size in bytes
|
||||||
|
|
||||||
|
int32_t index = this->readUInt();
|
||||||
|
if (index == 0) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
} else {
|
} else if (index > 0) {
|
||||||
SkASSERT(fTFArray);
|
if (index > fTFCount) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
return sk_ref_sp(fTFArray[index - 1]);
|
return sk_ref_sp(fTFArray[index - 1]);
|
||||||
|
} else { // custom
|
||||||
|
size_t size = -index;
|
||||||
|
const void* data = this->skip(size);
|
||||||
|
if (!data) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return fProcs.fTypefaceProc(data, size, fProcs.fTypefaceCtx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +185,7 @@ public:
|
|||||||
// be created (e.g. it was not originally encoded) then this returns an image that doesn't
|
// be created (e.g. it was not originally encoded) then this returns an image that doesn't
|
||||||
// draw.
|
// draw.
|
||||||
sk_sp<SkImage> readImage();
|
sk_sp<SkImage> readImage();
|
||||||
virtual sk_sp<SkTypeface> readTypeface();
|
sk_sp<SkTypeface> readTypeface();
|
||||||
|
|
||||||
void setTypefaceArray(SkTypeface* array[], int count) {
|
void setTypefaceArray(SkTypeface* array[], int count) {
|
||||||
fTFArray = array;
|
fTFArray = array;
|
||||||
|
@ -868,25 +868,9 @@ sk_sp<SkTextBlob> SkTextBlob::MakeFromBuffer(SkReadBuffer& reader) {
|
|||||||
return blobBuilder.make();
|
return blobBuilder.make();
|
||||||
}
|
}
|
||||||
|
|
||||||
class SkTypefaceCatalogerWriteBuffer : public SkBinaryWriteBuffer {
|
sk_sp<SkData> SkTextBlob::serialize(const SkSerialProcs& procs) const {
|
||||||
public:
|
SkBinaryWriteBuffer buffer;
|
||||||
SkTypefaceCatalogerWriteBuffer(SkTypefaceCatalogerProc proc, void* ctx)
|
buffer.setSerialProcs(procs);
|
||||||
: SkBinaryWriteBuffer(SkBinaryWriteBuffer::kCrossProcess_Flag)
|
|
||||||
, fCatalogerProc(proc)
|
|
||||||
, fCatalogerCtx(ctx)
|
|
||||||
{}
|
|
||||||
|
|
||||||
void writeTypeface(SkTypeface* typeface) override {
|
|
||||||
fCatalogerProc(typeface, fCatalogerCtx);
|
|
||||||
this->write32(typeface ? typeface->uniqueID() : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
SkTypefaceCatalogerProc fCatalogerProc;
|
|
||||||
void* fCatalogerCtx;
|
|
||||||
};
|
|
||||||
|
|
||||||
sk_sp<SkData> SkTextBlob::serialize(SkTypefaceCatalogerProc proc, void* ctx) const {
|
|
||||||
SkTypefaceCatalogerWriteBuffer buffer(proc, ctx);
|
|
||||||
this->flatten(buffer);
|
this->flatten(buffer);
|
||||||
|
|
||||||
size_t total = buffer.bytesWritten();
|
size_t total = buffer.bytesWritten();
|
||||||
@ -895,26 +879,61 @@ sk_sp<SkData> SkTextBlob::serialize(SkTypefaceCatalogerProc proc, void* ctx) con
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
class SkTypefaceResolverReadBuffer : public SkReadBuffer {
|
sk_sp<SkTextBlob> SkTextBlob::Deserialize(const void* data, size_t length,
|
||||||
public:
|
const SkDeserialProcs& procs) {
|
||||||
SkTypefaceResolverReadBuffer(const void* data, size_t size, SkTypefaceResolverProc proc,
|
SkReadBuffer buffer(data, length);
|
||||||
void* ctx)
|
buffer.setDeserialProcs(procs);
|
||||||
: SkReadBuffer(data, size)
|
return SkTextBlob::MakeFromBuffer(buffer);
|
||||||
, fResolverProc(proc)
|
}
|
||||||
, fResolverCtx(ctx)
|
|
||||||
{}
|
|
||||||
|
|
||||||
sk_sp<SkTypeface> readTypeface() override {
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
auto id = this->readUInt();
|
|
||||||
return this->isValid() ? fResolverProc(id, fResolverCtx) : nullptr;
|
namespace {
|
||||||
|
struct CatalogState {
|
||||||
|
SkTypefaceCatalogerProc fProc;
|
||||||
|
void* fCtx;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool catalog_typeface_proc(SkTypeface* face, SkWStream* stream, void* ctx) {
|
||||||
|
CatalogState* state = static_cast<CatalogState*>(ctx);
|
||||||
|
state->fProc(face, state->fCtx);
|
||||||
|
stream->write32(face->uniqueID());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SkTypefaceResolverProc fResolverProc;
|
sk_sp<SkData> SkTextBlob::serialize(SkTypefaceCatalogerProc proc, void* ctx) const {
|
||||||
void* fResolverCtx;
|
CatalogState state = { proc, ctx };
|
||||||
};
|
SkSerialProcs procs;
|
||||||
|
procs.fTypefaceProc = catalog_typeface_proc;
|
||||||
|
procs.fTypefaceCtx = &state;
|
||||||
|
return this->serialize(procs);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
struct ResolverState {
|
||||||
|
SkTypefaceResolverProc fProc;
|
||||||
|
void* fCtx;
|
||||||
|
};
|
||||||
|
|
||||||
|
sk_sp<SkTypeface> resolver_typeface_proc(const void* data, size_t length, void* ctx) {
|
||||||
|
SkASSERT(length == 4);
|
||||||
|
if (length != 4) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResolverState* state = static_cast<ResolverState*>(ctx);
|
||||||
|
uint32_t id;
|
||||||
|
memcpy(&id, data, length);
|
||||||
|
return state->fProc(id, state->fCtx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sk_sp<SkTextBlob> SkTextBlob::Deserialize(const void* data, size_t length,
|
sk_sp<SkTextBlob> SkTextBlob::Deserialize(const void* data, size_t length,
|
||||||
SkTypefaceResolverProc proc, void* ctx) {
|
SkTypefaceResolverProc proc, void* ctx) {
|
||||||
SkTypefaceResolverReadBuffer buffer(data, length, proc, ctx);
|
ResolverState state = { proc, ctx };
|
||||||
return SkTextBlob::MakeFromBuffer(buffer);
|
SkDeserialProcs procs;
|
||||||
|
procs.fTypefaceProc = resolver_typeface_proc;
|
||||||
|
procs.fTypefaceCtx = &state;
|
||||||
|
return Deserialize(data, length, procs);
|
||||||
}
|
}
|
||||||
|
@ -175,11 +175,31 @@ void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nullptr == obj || nullptr == fTFSet) {
|
// Write 32 bits (signed)
|
||||||
|
// 0 -- failure
|
||||||
|
// >0 -- index
|
||||||
|
// <0 -- custom (serial procs)
|
||||||
|
|
||||||
|
if (obj == nullptr) {
|
||||||
fWriter.write32(0);
|
fWriter.write32(0);
|
||||||
} else {
|
} else if (fProcs.fTypefaceProc) {
|
||||||
fWriter.write32(fTFSet->add(obj));
|
SkDynamicMemoryWStream stream;
|
||||||
|
if (fProcs.fTypefaceProc(obj, &stream, fProcs.fTypefaceCtx)) {
|
||||||
|
auto data = stream.detachAsData();
|
||||||
|
size_t size = data->size();
|
||||||
|
if (!sk_64_isS32(size)) {
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
int32_t ssize = SkToS32(size);
|
||||||
|
fWriter.write32(-ssize); // negative to signal custom
|
||||||
|
if (size) {
|
||||||
|
this->writePad32(data->data(), size);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// if the proc returned false, we fall through for std behavior
|
||||||
}
|
}
|
||||||
|
fWriter.write32(fTFSet ? fTFSet->add(obj) : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) {
|
void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) {
|
||||||
|
Loading…
Reference in New Issue
Block a user