Flutter Impeller
compressed_image_skia.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 
6 
7 #include <memory>
8 
10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "third_party/skia/include/core/SkData.h"
12 #include "third_party/skia/include/core/SkImage.h"
13 #include "third_party/skia/include/core/SkPixmap.h"
14 #include "third_party/skia/include/core/SkRefCnt.h"
15 
16 namespace impeller {
17 
18 std::shared_ptr<CompressedImage> CompressedImageSkia::Create(
19  std::shared_ptr<const fml::Mapping> allocation) {
20  // There is only one backend today.
21  if (!allocation) {
22  return nullptr;
23  }
24  return std::make_shared<CompressedImageSkia>(std::move(allocation));
25 }
26 
28  std::shared_ptr<const fml::Mapping> allocation)
29  : CompressedImage(std::move(allocation)) {}
30 
32 
33 // |CompressedImage|
35  if (!IsValid()) {
36  return {};
37  }
38  if (source_->GetSize() == 0u) {
39  return {};
40  }
41 
42  auto src = new std::shared_ptr<const fml::Mapping>(source_);
43  auto sk_data = SkData::MakeWithProc(
44  source_->GetMapping(), source_->GetSize(),
45  [](const void* ptr, void* context) {
46  delete reinterpret_cast<decltype(src)>(context);
47  },
48  src);
49 
50  auto image = SkImages::DeferredFromEncodedData(sk_data);
51  if (!image) {
52  return {};
53  }
54 
55  const auto dims = image->imageInfo().dimensions();
56  auto info = SkImageInfo::Make(dims.width(), dims.height(),
57  kRGBA_8888_SkColorType, kPremul_SkAlphaType);
58 
59  auto bitmap = std::make_shared<SkBitmap>();
60  if (!bitmap->tryAllocPixels(info)) {
61  VALIDATION_LOG << "Could not allocate arena for decompressing image.";
62  return {};
63  }
64 
65  if (!image->readPixels(nullptr, bitmap->pixmap(), 0, 0)) {
66  VALIDATION_LOG << "Could not decompress image into arena.";
67  return {};
68  }
69 
70  auto mapping = std::make_shared<fml::NonOwnedMapping>(
71  reinterpret_cast<const uint8_t*>(bitmap->pixmap().addr()), // data
72  bitmap->pixmap().rowBytes() * bitmap->pixmap().height(), // size
73  [bitmap](const uint8_t* data, size_t size) mutable {
74  bitmap.reset();
75  } // proc
76  );
77 
78  return {
79  {bitmap->pixmap().dimensions().fWidth,
80  bitmap->pixmap().dimensions().fHeight}, // size
82  mapping // allocation
83  };
84 }
85 
86 } // namespace impeller
const std::shared_ptr< const fml::Mapping > source_
CompressedImageSkia(std::shared_ptr< const fml::Mapping > allocation)
static std::shared_ptr< CompressedImage > Create(std::shared_ptr< const fml::Mapping > allocation)
DecompressedImage Decode() const override
Definition: comparable.h:95
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68
#define VALIDATION_LOG
Definition: validation.h:91