| 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
5 #include "impeller/playground/image/backends/skia/compressed_image_skia.h"
6
7 #include <memory>
8
9 #include "impeller/base/validation.h"
10 #include "third_party/skia/include/codec/SkBmpDecoder.h"
11 #include "third_party/skia/include/codec/SkCodec.h"
12 #include "third_party/skia/include/codec/SkGifDecoder.h"
13 #include "third_party/skia/include/codec/SkIcoDecoder.h"
14 #include "third_party/skia/include/codec/SkJpegDecoder.h"
15 #include "third_party/skia/include/codec/SkPngDecoder.h"
16 #include "third_party/skia/include/codec/SkWbmpDecoder.h"
17 #include "third_party/skia/include/codec/SkWebpDecoder.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "third_party/skia/include/core/SkData.h"
20 #include "third_party/skia/include/core/SkImage.h"
21 #include "third_party/skia/include/core/SkPixmap.h"
22 #include "third_party/skia/include/core/SkRefCnt.h"
23
24 namespace impeller {
25
26 std::shared_ptr<CompressedImage> CompressedImageSkia::Create(
27 std::shared_ptr<const fml::Mapping> allocation) {
28 // There is only one backend today.
29 if (!allocation) {
30 return nullptr;
31 }
32 return std::make_shared<CompressedImageSkia>(std::move(allocation));
33 }
34
35 CompressedImageSkia::CompressedImageSkia(
36 std::shared_ptr<const fml::Mapping> allocation)
37 : CompressedImage(std::move(allocation)) {}
38
39 CompressedImageSkia::~CompressedImageSkia() = default;
40
41 // |CompressedImage|
42 DecompressedImage CompressedImageSkia::Decode() const {
43 if (!IsValid()) {
44 return {};
45 }
46 if (source_->GetSize() == 0u) {
47 return {};
48 }
49
50 auto src = new std::shared_ptr<const fml::Mapping>(source_);
51 auto sk_data = SkData::MakeWithProc(
52 source_->GetMapping(), source_->GetSize(),
53 [](const void* ptr, void* context) {
54 delete reinterpret_cast<decltype(src)>(context);
55 },
56 src);
57
58 std::unique_ptr<SkCodec> codec;
59 if (SkBmpDecoder::IsBmp(sk_data->bytes(), sk_data->size())) {
60 codec = SkBmpDecoder::Decode(sk_data, nullptr);
61 } else if (SkGifDecoder::IsGif(sk_data->bytes(), sk_data->size())) {
62 codec = SkGifDecoder::Decode(sk_data, nullptr);
63 } else if (SkIcoDecoder::IsIco(sk_data->bytes(), sk_data->size())) {
64 codec = SkIcoDecoder::Decode(sk_data, nullptr);
65 } else if (SkJpegDecoder::IsJpeg(sk_data->bytes(), sk_data->size())) {
66 codec = SkJpegDecoder::Decode(sk_data, nullptr);
67 } else if (SkPngDecoder::IsPng(sk_data->bytes(), sk_data->size())) {
68 codec = SkPngDecoder::Decode(sk_data, nullptr);
69 } else if (SkWebpDecoder::IsWebp(sk_data->bytes(), sk_data->size())) {
70 codec = SkWebpDecoder::Decode(sk_data, nullptr);
71 } else if (SkWbmpDecoder::IsWbmp(sk_data->bytes(), sk_data->size())) {
72 codec = SkWbmpDecoder::Decode(sk_data, nullptr);
73 }
74 if (!codec) {
75 VALIDATION_LOG << "Image data was not valid.";
76 return {};
77 }
78
79 const auto dims = codec->dimensions();
80 auto info = SkImageInfo::Make(dims.width(), dims.height(),
81 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
82
83 auto bitmap = std::make_shared<SkBitmap>();
84 if (!bitmap->tryAllocPixels(info)) {
85 VALIDATION_LOG << "Could not allocate arena for decompressing image.";
86 return {};
87 }
88
89 // We extract this to an SkImage first because not all codecs can directly
90 // swizzle via getPixels.
91 auto image = SkCodecs::DeferredImage(std::move(codec));
92 if (!image) {
93 VALIDATION_LOG << "Could not extract image from compressed data.";
94 return {};
95 }
96
97 if (!image->readPixels(nullptr, bitmap->pixmap(), 0, 0)) {
98 VALIDATION_LOG << "Could not resize image into arena.";
99 return {};
100 }
101
102 auto mapping = std::make_shared<fml::NonOwnedMapping>(
103 reinterpret_cast<const uint8_t*>(bitmap->pixmap().addr()), // data
104 bitmap->pixmap().rowBytes() * bitmap->pixmap().height(), // size
105 [bitmap](const uint8_t* data, size_t size) mutable {
106 bitmap.reset();
107 } // proc
108 );
109
110 return {
111 {bitmap->pixmap().dimensions().fWidth,
112 bitmap->pixmap().dimensions().fHeight}, // size
113 DecompressedImage::Format::kRGBA, // format
114 mapping // allocation
115 };
116 }
117
118 } // namespace impeller
impeller::CompressedImage
Definition: compressed_image.h:17
impeller::CompressedImage::source_
const std::shared_ptr< const fml::Mapping > source_
Definition: compressed_image.h:26
impeller::CompressedImage::IsValid
bool IsValid() const
Definition: compressed_image.cc:14
impeller::CompressedImageSkia::CompressedImageSkia
CompressedImageSkia(std::shared_ptr< const fml::Mapping > allocation)
Definition: compressed_image_skia.cc:35
impeller::CompressedImageSkia::~CompressedImageSkia
~CompressedImageSkia() override
impeller::CompressedImageSkia::Create
static std::shared_ptr< CompressedImage > Create(std::shared_ptr< const fml::Mapping > allocation)
Definition: compressed_image_skia.cc:26
impeller::CompressedImageSkia::Decode
DecompressedImage Decode() const override
Definition: compressed_image_skia.cc:42
impeller::DecompressedImage
Definition: decompressed_image.h:15
impeller::DecompressedImage::Format::kRGBA
@ kRGBA
compressed_image_skia.h
impeller
Definition: allocation.cc:12
std
Definition: comparable.h:95
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:69
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91