Flutter Engine
 
Loading...
Searching...
No Matches
serialization_callbacks.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
6#include "third_party/skia/include/core/SkColorSpace.h"
7#include "third_party/skia/include/core/SkImage.h"
8#include "third_party/skia/include/core/SkImageInfo.h"
9#include "third_party/skia/include/core/SkStream.h"
10#include "third_party/skia/include/core/SkTypeface.h"
11
12namespace flutter {
13
14sk_sp<SkData> SerializeTypefaceWithoutData(SkTypeface* typeface, void* ctx) {
15 return SkData::MakeEmpty();
16}
17
18sk_sp<SkData> SerializeTypefaceWithData(SkTypeface* typeface, void* ctx) {
19 return typeface->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
20}
21
22sk_sp<SkTypeface> DeserializeTypefaceWithoutData(const void* data,
23 size_t length,
24 void* ctx) {
25 return nullptr;
26}
27
29 int32_t width;
30 int32_t height;
31 uint32_t color_type;
32 uint32_t alpha_type;
34} __attribute__((packed));
35
36sk_sp<SkData> SerializeImageWithoutData(SkImage* image, void* ctx) {
37 const auto& info = image->imageInfo();
38 SkDynamicMemoryWStream stream;
39
40 ImageMetaData metadata = {info.width(), info.height(),
41 static_cast<uint32_t>(info.colorType()),
42 static_cast<uint32_t>(info.alphaType()),
43 static_cast<bool>(info.colorSpace())};
44 stream.write(&metadata, sizeof(ImageMetaData));
45
46 if (info.colorSpace()) {
47 auto color_space_data = info.colorSpace()->serialize();
48 FML_CHECK(color_space_data);
49 SkMemoryStream color_space_stream(color_space_data);
50 stream.writeStream(&color_space_stream, color_space_data->size());
51 }
52
53 return stream.detachAsData();
54};
55
56sk_sp<SkImage> DeserializeImageWithoutData(const void* data,
57 size_t length,
58 void* ctx) {
59 FML_CHECK(length >= sizeof(ImageMetaData));
60 auto metadata = static_cast<const ImageMetaData*>(data);
61 sk_sp<SkColorSpace> color_space = nullptr;
62 if (metadata->has_color_space) {
63 color_space = SkColorSpace::Deserialize(
64 static_cast<const uint8_t*>(data) + sizeof(ImageMetaData),
65 length - sizeof(ImageMetaData));
66 }
67
68 auto image_size = SkISize::Make(metadata->width, metadata->height);
69 auto info = SkImageInfo::Make(
70 image_size, static_cast<SkColorType>(metadata->color_type),
71 static_cast<SkAlphaType>(metadata->alpha_type), color_space);
72 sk_sp<SkData> image_data =
73 SkData::MakeUninitialized(image_size.width() * image_size.height() * 4);
74 memset(image_data->writable_data(), 0x0f, image_data->size());
75 sk_sp<SkImage> image =
76 SkImages::RasterFromData(info, image_data, image_size.width() * 4);
77
78 return image;
79};
80
81} // namespace flutter
FlutterVulkanImage * image
#define FML_CHECK(condition)
Definition logging.h:104
size_t length
sk_sp< SkData > SerializeTypefaceWithData(SkTypeface *typeface, void *ctx)
sk_sp< SkImage > DeserializeImageWithoutData(const void *data, size_t length, void *ctx)
sk_sp< SkTypeface > DeserializeTypefaceWithoutData(const void *data, size_t length, void *ctx)
sk_sp< SkData > SerializeTypefaceWithoutData(SkTypeface *typeface, void *ctx)
struct flutter::ImageMetaData __attribute__((packed))
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
sk_sp< SkData > SerializeImageWithoutData(SkImage *image, void *ctx)