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/SkStream.h"
9 #include "include/core/SkTypeface.h"
10 
11 namespace flutter {
12 
13 sk_sp<SkData> SerializeTypefaceWithoutData(SkTypeface* typeface, void* ctx) {
14  return typeface->serialize(SkTypeface::SerializeBehavior::kDontIncludeData);
15 }
16 
17 sk_sp<SkData> SerializeTypefaceWithData(SkTypeface* typeface, void* ctx) {
18  return typeface->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
19 }
20 
21 struct ImageMetaData {
22  int32_t width;
23  int32_t height;
24  uint32_t color_type;
25  uint32_t alpha_type;
27 } __attribute__((packed));
28 
29 sk_sp<SkData> SerializeImageWithoutData(SkImage* image, void* ctx) {
30  auto info = image->imageInfo();
31  SkDynamicMemoryWStream stream;
32 
33  ImageMetaData metadata = {info.width(), info.height(),
34  static_cast<uint32_t>(info.colorType()),
35  static_cast<uint32_t>(info.alphaType()),
36  static_cast<bool>(info.colorSpace())};
37  stream.write(&metadata, sizeof(ImageMetaData));
38 
39  if (info.colorSpace()) {
40  auto color_space_data = info.colorSpace()->serialize();
41  FML_CHECK(color_space_data);
42  SkMemoryStream color_space_stream(color_space_data);
43  stream.writeStream(&color_space_stream, color_space_data->size());
44  }
45 
46  return stream.detachAsData();
47 };
48 
49 sk_sp<SkImage> DeserializeImageWithoutData(const void* data,
50  size_t length,
51  void* ctx) {
52  FML_CHECK(length >= sizeof(ImageMetaData));
53  auto metadata = static_cast<const ImageMetaData*>(data);
54  sk_sp<SkColorSpace> color_space = nullptr;
55  if (metadata->has_color_space) {
56  color_space = SkColorSpace::Deserialize(
57  static_cast<const uint8_t*>(data) + sizeof(ImageMetaData),
58  length - sizeof(ImageMetaData));
59  }
60 
61  auto image_size = SkISize::Make(metadata->width, metadata->height);
62  auto info = SkImageInfo::Make(
63  image_size, static_cast<SkColorType>(metadata->color_type),
64  static_cast<SkAlphaType>(metadata->alpha_type), color_space);
65  sk_sp<SkData> image_data =
66  SkData::MakeUninitialized(image_size.width() * image_size.height() * 4);
67  memset(image_data->writable_data(), 0x0f, image_data->size());
68  sk_sp<SkImage> image =
69  SkImage::MakeRasterData(info, image_data, image_size.width() * 4);
70 
71  return image;
72 };
73 
74 } // 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< SkData > SerializeTypefaceWithoutData(SkTypeface *typeface, void *ctx)
size_t length
enum flutter::DartErrorCode __attribute__
#define FML_CHECK(condition)
Definition: logging.h:68