Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a0f0b99

Browse files
[Impeller] Implement 'ui.decodeImageFromPixels' (#37866)
1 parent d4fe02a commit a0f0b99

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

lib/ui/painting/image_decoder_impeller.cc

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "impeller/base/strings.h"
1919
#include "impeller/geometry/size.h"
2020
#include "include/core/SkSize.h"
21+
#include "third_party/skia/include/core/SkMallocPixelRef.h"
2122
#include "third_party/skia/include/core/SkPixmap.h"
2223

2324
namespace flutter {
@@ -66,23 +67,21 @@ std::shared_ptr<SkBitmap> ImageDecoderImpeller::DecompressTexture(
6667
return nullptr;
6768
}
6869

69-
if (!descriptor->is_compressed()) {
70-
FML_DLOG(ERROR)
71-
<< "Uncompressed images are not implemented in Impeller yet.";
72-
return nullptr;
73-
}
7470
target_size.set(std::min(static_cast<int32_t>(max_texture_size.width),
7571
target_size.width()),
7672
std::min(static_cast<int32_t>(max_texture_size.height),
7773
target_size.height()));
7874

7975
const SkISize source_size = descriptor->image_info().dimensions();
80-
auto decode_size = descriptor->get_scaled_dimensions(std::max(
81-
static_cast<double>(target_size.width()) / source_size.width(),
82-
static_cast<double>(target_size.height()) / source_size.height()));
76+
auto decode_size = source_size;
77+
if (descriptor->is_compressed()) {
78+
decode_size = descriptor->get_scaled_dimensions(std::max(
79+
static_cast<double>(target_size.width()) / source_size.width(),
80+
static_cast<double>(target_size.height()) / source_size.height()));
81+
}
8382

8483
//----------------------------------------------------------------------------
85-
/// 1. Decode the image into the image generator's closest supported size.
84+
/// 1. Decode the image.
8685
///
8786

8887
const auto base_image_info = descriptor->image_info();
@@ -99,18 +98,26 @@ std::shared_ptr<SkBitmap> ImageDecoderImpeller::DecompressTexture(
9998
}
10099

101100
auto bitmap = std::make_shared<SkBitmap>();
102-
if (!bitmap->tryAllocPixels(image_info)) {
103-
FML_DLOG(ERROR)
104-
<< "Could not allocate intermediate for image decompression.";
105-
return nullptr;
106-
}
107-
108-
if (!descriptor->get_pixels(bitmap->pixmap())) {
109-
FML_DLOG(ERROR) << "Could not decompress image.";
110-
return nullptr;
101+
if (descriptor->is_compressed()) {
102+
if (!bitmap->tryAllocPixels(image_info)) {
103+
FML_DLOG(ERROR)
104+
<< "Could not allocate intermediate for image decompression.";
105+
return nullptr;
106+
}
107+
// Decode the image into the image generator's closest supported size.
108+
if (!descriptor->get_pixels(bitmap->pixmap())) {
109+
FML_DLOG(ERROR) << "Could not decompress image.";
110+
return nullptr;
111+
}
112+
} else {
113+
bitmap->setInfo(image_info);
114+
auto pixel_ref = SkMallocPixelRef::MakeWithData(
115+
image_info, descriptor->row_bytes(), descriptor->data());
116+
bitmap->setPixelRef(pixel_ref, 0, 0);
117+
bitmap->setImmutable();
111118
}
112119

113-
if (decode_size == target_size) {
120+
if (bitmap->dimensions() == target_size) {
114121
return bitmap;
115122
}
116123

0 commit comments

Comments
 (0)