| 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/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
27 CompressedImageSkia::CompressedImageSkia(
28 std::shared_ptr<const fml::Mapping> allocation)
29 : CompressedImage(std::move(allocation)) {}
30
31 CompressedImageSkia::~CompressedImageSkia() = default;
32
33 // |CompressedImage|
34 DecompressedImage CompressedImageSkia::Decode() const {
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
81 DecompressedImage::Format::kRGBA, // format
82 mapping // allocation
83 };
84 }
85
86 } // 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:27
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:18
impeller::CompressedImageSkia::Decode
DecompressedImage Decode() const override
Definition: compressed_image_skia.cc:34
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:68
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91