Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
15
16namespace impeller {
17
18std::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
51 if (!image) {
52 return {};
53 }
54
55 const auto dims = image->imageInfo().dimensions();
56 auto info = SkImageInfo::Make(dims.width(), dims.height(),
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
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Definition SkData.cpp:128
const SkImageInfo & imageInfo() const
Definition SkImage.h:279
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Definition SkImage.cpp:42
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
const std::shared_ptr< const fml::Mapping > source_
sk_sp< SkImage > image
Definition examples.cpp:29
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
Definition ref_ptr.h:256
SkISize dimensions() const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
#define VALIDATION_LOG
Definition validation.h:73