Flutter Engine
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.
4 
5 #include "flutter/fml/logging.h"
6 #include "include/core/SkImage.h"
7 #include "include/core/SkPicture.h"
8 #include "include/core/SkSerialProcs.h"
9 #include "include/core/SkStream.h"
10 #include "include/core/SkTypeface.h"
11 
12 namespace flutter {
13 
14 sk_sp<SkData> SerializeTypefaceWithoutData(SkTypeface* typeface, void* ctx) {
15  return SkData::MakeEmpty();
16 }
17 
18 sk_sp<SkData> SerializeTypefaceWithData(SkTypeface* typeface, void* ctx) {
19  return typeface->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
20 }
21 
22 sk_sp<SkTypeface> DeserializeTypefaceWithoutData(const void* data,
23  size_t length,
24  void* ctx) {
25  return SkTypeface::MakeDefault();
26 }
27 
28 struct ImageMetaData {
29  int32_t width;
30  int32_t height;
31  uint32_t color_type;
32  uint32_t alpha_type;
34 } __attribute__((packed));
35 
36 sk_sp<SkData> SerializeImageWithoutData(SkImage* image, void* ctx) {
37  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 
56 sk_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  SkImage::MakeRasterData(info, image_data, image_size.width() * 4);
77 
78  return image;
79 };
80 
81 } // namespace flutter
sk_sp< SkImage > DeserializeImageWithoutData(const void *data, size_t length, void *ctx)
sk_sp< SkData > SerializeImageWithoutData(SkImage *image, void *ctx)
sk_sp< SkData > SerializeTypefaceWithData(SkTypeface *typeface, void *ctx)
sk_sp< SkTypeface > DeserializeTypefaceWithoutData(const void *data, size_t length, void *ctx)
sk_sp< SkData > SerializeTypefaceWithoutData(SkTypeface *typeface, void *ctx)
size_t length
enum flutter::DartErrorCode __attribute__
#define FML_CHECK(condition)
Definition: logging.h:68