1 /*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/private/chromium/Slug.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "src/core/SkReadBuffer.h"
12 #include "src/core/SkWriteBuffer.h"
13
14 #include <atomic>
15
16 namespace sktext::gpu {
17 class Slug;
18 sk_sp<Slug> SkMakeSlugFromBuffer(SkReadBuffer& buffer, const SkStrikeClient* client);
19
ConvertBlob(SkCanvas * canvas,const SkTextBlob & blob,SkPoint origin,const SkPaint & paint)20 sk_sp<Slug> Slug::ConvertBlob(
21 SkCanvas* canvas, const SkTextBlob& blob, SkPoint origin, const SkPaint& paint) {
22 return canvas->convertBlobToSlug(blob, origin, paint);
23 }
24
serialize() const25 sk_sp<SkData> Slug::serialize() const {
26 SkBinaryWriteBuffer buffer;
27 this->doFlatten(buffer);
28 return buffer.snapshotAsData();
29 }
30
serialize(void * buffer,size_t size) const31 size_t Slug::serialize(void* buffer, size_t size) const {
32 SkBinaryWriteBuffer writeBuffer{buffer, size};
33 this->doFlatten(writeBuffer);
34
35 // If we overflow the given buffer, then SkWriteBuffer allocates a new larger buffer. Check
36 // to see if an additional buffer was allocated, if it wasn't then everything fit, else
37 // return 0 signaling the buffer overflowed.
38 // N.B. This is the idiom from SkTextBlob.
39 return writeBuffer.usingInitialStorage() ? writeBuffer.bytesWritten() : 0u;
40 }
41
MakeFromBuffer(SkReadBuffer & buffer)42 sk_sp<Slug> Slug::MakeFromBuffer(SkReadBuffer& buffer) {
43 return SkMakeSlugFromBuffer(buffer, nullptr);
44 }
45
Deserialize(const void * data,size_t size,const SkStrikeClient * client)46 sk_sp<Slug> Slug::Deserialize(const void* data, size_t size, const SkStrikeClient* client) {
47 SkReadBuffer buffer{data, size};
48 return SkMakeSlugFromBuffer(buffer, client);
49 }
50
draw(SkCanvas * canvas) const51 void Slug::draw(SkCanvas* canvas) const {
52 canvas->drawSlug(this);
53 }
54
NextUniqueID()55 uint32_t Slug::NextUniqueID() {
56 static std::atomic<uint32_t> nextUnique = 1;
57 return nextUnique++;
58 }
59
60 // Most of Slug's implementation is in TextBlob.cpp to share common code.
61
62 } // namespace sktext::gpu
63
64