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

Revert "Replace deprecated and internal Skia EncodedImage calls with public versions" #41595

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions impeller/image/backends/skia/compressed_image_skia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
#include "impeller/base/validation.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkData.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageGenerator.h"
#include "third_party/skia/include/core/SkPixmap.h"
#include "third_party/skia/include/core/SkRefCnt.h"

namespace impeller {

Expand Down Expand Up @@ -47,12 +46,12 @@ DecompressedImage CompressedImageSkia::Decode() const {
},
src);

auto image = SkImages::DeferredFromEncodedData(sk_data);
if (!image) {
auto generator = SkImageGenerator::MakeFromEncoded(sk_data);
if (!generator) {
return {};
}

const auto dims = image->imageInfo().dimensions();
const auto dims = generator->getInfo().dimensions();
auto info = SkImageInfo::Make(dims.width(), dims.height(),
kRGBA_8888_SkColorType, kPremul_SkAlphaType);

Expand All @@ -62,7 +61,7 @@ DecompressedImage CompressedImageSkia::Decode() const {
return {};
}

if (!image->readPixels(nullptr, bitmap->pixmap(), 0, 0)) {
if (!generator->getPixels(bitmap->pixmap())) {
VALIDATION_LOG << "Could not decompress image into arena.";
return {};
}
Expand Down
31 changes: 11 additions & 20 deletions lib/ui/painting/image_decoder_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,7 @@ TEST(ImageDecoderTest, VerifySimpleDecoding) {
auto data = OpenFixtureAsSkData("Horizontal.jpg");
auto image = SkImages::DeferredFromEncodedData(data);
ASSERT_TRUE(image != nullptr);
ASSERT_EQ(600, image->width());
ASSERT_EQ(200, image->height());
ASSERT_EQ(SkISize::Make(600, 200), image->dimensions());

ImageGeneratorRegistry registry;
std::shared_ptr<ImageGenerator> generator =
Expand All @@ -848,25 +847,24 @@ TEST(ImageDecoderTest, VerifySimpleDecoding) {

auto descriptor = fml::MakeRefCounted<ImageDescriptor>(std::move(data),
std::move(generator));
auto compressed_image = ImageDecoderSkia::ImageFromCompressedData(
descriptor.get(), 6, 2, fml::tracing::TraceFlow(""));
ASSERT_EQ(compressed_image->width(), 6);
ASSERT_EQ(compressed_image->height(), 2);

ASSERT_EQ(ImageDecoderSkia::ImageFromCompressedData(
descriptor.get(), 6, 2, fml::tracing::TraceFlow(""))
->dimensions(),
SkISize::Make(6, 2));

#if IMPELLER_SUPPORTS_RENDERING
std::shared_ptr<impeller::Allocator> allocator =
std::make_shared<impeller::TestImpellerAllocator>();
auto result_1 = ImageDecoderImpeller::DecompressTexture(
descriptor.get(), SkISize::Make(6, 2), {100, 100},
/*supports_wide_gamut=*/false, allocator);
ASSERT_EQ(result_1->sk_bitmap->width(), 6);
ASSERT_EQ(result_1->sk_bitmap->height(), 2);
ASSERT_EQ(result_1->sk_bitmap->dimensions(), SkISize::Make(6, 2));

auto result_2 = ImageDecoderImpeller::DecompressTexture(
descriptor.get(), SkISize::Make(60, 20), {10, 10},
/*supports_wide_gamut=*/false, allocator);
ASSERT_EQ(result_2->sk_bitmap->width(), 10);
ASSERT_EQ(result_2->sk_bitmap->height(), 10);
ASSERT_EQ(result_2->sk_bitmap->dimensions(), SkISize::Make(10, 10));
#endif // IMPELLER_SUPPORTS_RENDERING
}

Expand All @@ -880,15 +878,9 @@ TEST(ImageDecoderTest, VerifySubpixelDecodingPreservesExifOrientation) {
auto descriptor =
fml::MakeRefCounted<ImageDescriptor>(data, std::move(generator));

// If Exif metadata is ignored, the height and width will be swapped because
// "Rotate 90 CW" is what is encoded there.
ASSERT_EQ(600, descriptor->width());
ASSERT_EQ(200, descriptor->height());

auto image = SkImages::DeferredFromEncodedData(data);
ASSERT_TRUE(image != nullptr);
ASSERT_EQ(600, image->width());
ASSERT_EQ(200, image->height());
ASSERT_EQ(SkISize::Make(600, 200), image->dimensions());

auto decode = [descriptor](uint32_t target_width, uint32_t target_height) {
return ImageDecoderSkia::ImageFromCompressedData(
Expand All @@ -902,9 +894,8 @@ TEST(ImageDecoderTest, VerifySubpixelDecodingPreservesExifOrientation) {

auto assert_image = [&](auto decoded_image) {
ASSERT_EQ(decoded_image->dimensions(), SkISize::Make(300, 100));
sk_sp<SkData> encoded =
SkPngEncoder::Encode(nullptr, decoded_image.get(), {});
ASSERT_TRUE(encoded->equals(expected_data.get()));
ASSERT_TRUE(SkPngEncoder::Encode(nullptr, decoded_image.get(), {})
->equals(expected_data.get()));
};

assert_image(decode(300, 100));
Expand Down
70 changes: 11 additions & 59 deletions lib/ui/painting/image_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <utility>

#include "flutter/fml/logging.h"
#include "third_party/skia/include/codec/SkEncodedOrigin.h"
#include "third_party/skia/include/codec/SkPixmapUtils.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkImage.h"

Expand Down Expand Up @@ -83,43 +81,34 @@ std::unique_ptr<ImageGenerator> BuiltinSkiaImageGenerator::MakeFromGenerator(

BuiltinSkiaCodecImageGenerator::~BuiltinSkiaCodecImageGenerator() = default;

static SkImageInfo getInfoIncludingExif(SkCodec* codec) {
SkImageInfo info = codec->getInfo();
if (SkEncodedOriginSwapsWidthHeight(codec->getOrigin())) {
info = SkPixmapUtils::SwapWidthHeight(info);
}
return info;
}

BuiltinSkiaCodecImageGenerator::BuiltinSkiaCodecImageGenerator(
std::unique_ptr<SkCodec> codec)
: codec_(std::move(codec)) {
image_info_ = getInfoIncludingExif(codec_.get());
}
: codec_generator_(static_cast<SkCodecImageGenerator*>(
SkCodecImageGenerator::MakeFromCodec(std::move(codec)).release())) {}

BuiltinSkiaCodecImageGenerator::BuiltinSkiaCodecImageGenerator(
sk_sp<SkData> buffer)
: codec_(SkCodec::MakeFromData(std::move(buffer)).release()) {
image_info_ = getInfoIncludingExif(codec_.get());
}
: codec_generator_(static_cast<SkCodecImageGenerator*>(
SkCodecImageGenerator::MakeFromEncodedCodec(std::move(buffer))
.release())) {}

const SkImageInfo& BuiltinSkiaCodecImageGenerator::GetInfo() {
return image_info_;
return codec_generator_->getInfo();
}

unsigned int BuiltinSkiaCodecImageGenerator::GetFrameCount() const {
return codec_->getFrameCount();
return codec_generator_->getFrameCount();
}

unsigned int BuiltinSkiaCodecImageGenerator::GetPlayCount() const {
auto repetition_count = codec_->getRepetitionCount();
auto repetition_count = codec_generator_->getRepetitionCount();
return repetition_count < 0 ? kInfinitePlayCount : repetition_count + 1;
}

const ImageGenerator::FrameInfo BuiltinSkiaCodecImageGenerator::GetFrameInfo(
unsigned int frame_index) {
SkCodec::FrameInfo info = {};
codec_->getFrameInfo(frame_index, &info);
codec_generator_->getFrameInfo(frame_index, &info);
return {
.required_frame = info.fRequiredFrame == SkCodec::kNoFrame
? std::nullopt
Expand All @@ -130,11 +119,7 @@ const ImageGenerator::FrameInfo BuiltinSkiaCodecImageGenerator::GetFrameInfo(

SkISize BuiltinSkiaCodecImageGenerator::GetScaledDimensions(
float desired_scale) {
SkISize size = codec_->getScaledDimensions(desired_scale);
if (SkEncodedOriginSwapsWidthHeight(codec_->getOrigin())) {
std::swap(size.fWidth, size.fHeight);
}
return size;
return codec_generator_->getScaledDimensions(desired_scale);
}

bool BuiltinSkiaCodecImageGenerator::GetPixels(
Expand All @@ -148,40 +133,7 @@ bool BuiltinSkiaCodecImageGenerator::GetPixels(
if (prior_frame.has_value()) {
options.fPriorFrame = prior_frame.value();
}
SkEncodedOrigin origin = codec_->getOrigin();

SkPixmap output_pixmap(info, pixels, row_bytes);
SkPixmap temp_pixmap;
SkBitmap temp_bitmap;
if (origin == kTopLeft_SkEncodedOrigin) {
// We can decode directly into the output buffer.
temp_pixmap = output_pixmap;
} else {
// We need to decode into a different buffer so we can re-orient
// the pixels later.
SkImageInfo temp_info = output_pixmap.info();
if (SkEncodedOriginSwapsWidthHeight(origin)) {
// We'll be decoding into a buffer that has height and width swapped.
temp_info = SkPixmapUtils::SwapWidthHeight(temp_info);
}
if (!temp_bitmap.tryAllocPixels(temp_info)) {
FML_DLOG(ERROR) << "Failed to allocate memory for bitmap of size "
<< temp_info.computeMinByteSize() << "B";
return false;
}
temp_pixmap = temp_bitmap.pixmap();
}

SkCodec::Result result = codec_->getPixels(temp_pixmap, &options);
if (result != SkCodec::kSuccess) {
FML_DLOG(WARNING) << "codec could not get pixels. "
<< SkCodec::ResultToString(result);
return false;
}
if (origin == kTopLeft_SkEncodedOrigin) {
return true;
}
return SkPixmapUtils::Orient(output_pixmap, temp_pixmap, origin);
return codec_generator_->getPixels(info, pixels, row_bytes, &options);
}

std::unique_ptr<ImageGenerator> BuiltinSkiaCodecImageGenerator::MakeFromData(
Expand Down
5 changes: 2 additions & 3 deletions lib/ui/painting/image_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "third_party/skia/include/codec/SkCodecAnimation.h"
#include "third_party/skia/include/core/SkData.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageGenerator.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkSize.h"
#include "third_party/skia/src/codec/SkCodecImageGenerator.h" // nogncheck

namespace flutter {

Expand Down Expand Up @@ -213,8 +213,7 @@ class BuiltinSkiaCodecImageGenerator : public ImageGenerator {

private:
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(BuiltinSkiaCodecImageGenerator);
std::unique_ptr<SkCodec> codec_;
SkImageInfo image_info_;
std::unique_ptr<SkCodecImageGenerator> codec_generator_;
};

} // namespace flutter
Expand Down