From 8513b51bf17dccea9a278c00db6bc6139153b539 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 8 Jul 2021 17:49:15 -0700 Subject: [PATCH] Update image class names. --- .../impeller/compositor/texture_descriptor.h | 2 +- .../impeller/compositor/texture_descriptor.mm | 14 +-- engine/src/flutter/impeller/image/BUILD.gn | 4 +- .../impeller/image/compressed_image.cc | 86 +++++++++++++++++ .../flutter/impeller/image/compressed_image.h | 30 ++++++ engine/src/flutter/impeller/image/image.cc | 93 +++++-------------- engine/src/flutter/impeller/image/image.h | 32 +++++-- .../flutter/impeller/image/image_result.cc | 39 -------- .../src/flutter/impeller/image/image_result.h | 50 ---------- .../primitives/primitives_unittests.mm | 4 +- 10 files changed, 179 insertions(+), 175 deletions(-) create mode 100644 engine/src/flutter/impeller/image/compressed_image.cc create mode 100644 engine/src/flutter/impeller/image/compressed_image.h delete mode 100644 engine/src/flutter/impeller/image/image_result.cc delete mode 100644 engine/src/flutter/impeller/image/image_result.h diff --git a/engine/src/flutter/impeller/compositor/texture_descriptor.h b/engine/src/flutter/impeller/compositor/texture_descriptor.h index c3b7f653fb..53b8f316a7 100644 --- a/engine/src/flutter/impeller/compositor/texture_descriptor.h +++ b/engine/src/flutter/impeller/compositor/texture_descriptor.h @@ -39,7 +39,7 @@ struct TextureDescriptor { } static std::optional MakeFromImageResult( - const ImageResult& result); + const Image& result); }; } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/texture_descriptor.mm b/engine/src/flutter/impeller/compositor/texture_descriptor.mm index 0822b10fe8..70120f615b 100644 --- a/engine/src/flutter/impeller/compositor/texture_descriptor.mm +++ b/engine/src/flutter/impeller/compositor/texture_descriptor.mm @@ -7,24 +7,24 @@ namespace impeller { std::optional FormatForImageResultComponents( - ImageResult::Components comp) { + Image::Components comp) { switch (comp) { - case ImageResult::Components::Invalid: + case Image::Components::Invalid: return std::nullopt; - case ImageResult::Components::Grey: + case Image::Components::Grey: return std::nullopt; - case ImageResult::Components::GreyAlpha: + case Image::Components::GreyAlpha: return std::nullopt; - case ImageResult::Components::RGB: + case Image::Components::RGB: return std::nullopt; - case ImageResult::Components::RGBA: + case Image::Components::RGBA: return PixelFormat::kPixelFormat_R8G8B8A8_UNormInt; } return std::nullopt; } std::optional TextureDescriptor::MakeFromImageResult( - const ImageResult& result) { + const Image& result) { if (!result.IsValid()) { return std::nullopt; } diff --git a/engine/src/flutter/impeller/image/BUILD.gn b/engine/src/flutter/impeller/image/BUILD.gn index a54fcb9733..f19ec4666f 100644 --- a/engine/src/flutter/impeller/image/BUILD.gn +++ b/engine/src/flutter/impeller/image/BUILD.gn @@ -6,10 +6,10 @@ import("//flutter/impeller/tools/impeller.gni") impeller_component("image") { sources = [ + "compressed_image.cc", + "compressed_image.h", "image.cc", "image.h", - "image_result.cc", - "image_result.h", ] deps = [ "../third_party/stb" ] diff --git a/engine/src/flutter/impeller/image/compressed_image.cc b/engine/src/flutter/impeller/image/compressed_image.cc new file mode 100644 index 0000000000..719ba6311a --- /dev/null +++ b/engine/src/flutter/impeller/image/compressed_image.cc @@ -0,0 +1,86 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/image/compressed_image.h" + +#include + +namespace impeller { + +CompressedImage::CompressedImage( + std::shared_ptr sourceAllocation) + : source_(std::move(sourceAllocation)) {} + +CompressedImage::~CompressedImage() = default; + +Image CompressedImage::Decode() const { + if (!source_) { + return {}; + } + + int width = 0; + int height = 0; + int comps = 0; + + stbi_uc* decoded = + ::stbi_load_from_memory(source_->GetMapping(), // Source Data + source_->GetSize(), // Source Data Size + &width, // Out: Width + &height, // Out: Height + &comps, // Out: Components + STBI_default); + + if (decoded == nullptr) { + FML_LOG(ERROR) << "Could not decode image from host memory."; + return {}; + } + + auto dest_allocation = std::make_shared( + decoded, // bytes + width * height * comps * sizeof(stbi_uc), // byte size + [](const uint8_t* data, size_t size) { + ::stbi_image_free(const_cast(data)); + } // release proc + ); + + /* + * Make sure we got a valid component set. + */ + auto components = Image::Components::Invalid; + + switch (comps) { + case STBI_grey: + components = Image::Components::Grey; + break; + case STBI_grey_alpha: + components = Image::Components::GreyAlpha; + break; + case STBI_rgb: + components = Image::Components::RGB; + break; + case STBI_rgb_alpha: + components = Image::Components::RGBA; + break; + default: + components = Image::Components::Invalid; + break; + } + + if (components == Image::Components::Invalid) { + FML_LOG(ERROR) << "Could not detect image components when decoding."; + return {}; + } + + return Image{ + ISize{width, height}, // size + components, // components + std::move(dest_allocation) // allocation + }; +} + +bool CompressedImage::IsValid() const { + return static_cast(source_); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/image/compressed_image.h b/engine/src/flutter/impeller/image/compressed_image.h new file mode 100644 index 0000000000..a30c32276b --- /dev/null +++ b/engine/src/flutter/impeller/image/compressed_image.h @@ -0,0 +1,30 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "flutter/fml/macros.h" +#include "flutter/fml/mapping.h" +#include "impeller/geometry/size.h" +#include "impeller/image/image.h" + +namespace impeller { + +class ImageSource; + +class CompressedImage { + public: + CompressedImage(std::shared_ptr sourceAllocation); + + ~CompressedImage(); + + [[nodiscard]] Image Decode() const; + + bool IsValid() const; + + private: + std::shared_ptr source_; +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/image/image.cc b/engine/src/flutter/impeller/image/image.cc index edcb4b3bf1..7ea8b9013a 100644 --- a/engine/src/flutter/impeller/image/image.cc +++ b/engine/src/flutter/impeller/image/image.cc @@ -2,83 +2,38 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "image.h" -#include +#include "impeller/image/image.h" namespace impeller { -Image::Image(std::shared_ptr sourceAllocation) - : source_(std::move(sourceAllocation)) {} +Image::Image() = default; + +Image::Image(ISize size, + Components components, + std::shared_ptr allocation) + : size_(size), components_(components), allocation_(std::move(allocation)) { + if (!allocation_ || !size.IsPositive()) { + return; + } + is_valid_ = true; +} Image::~Image() = default; -ImageResult Image::Decode() const { - if (!source_) { - return {}; - } - - int width = 0; - int height = 0; - int comps = 0; - - stbi_uc* decoded = - ::stbi_load_from_memory(source_->GetMapping(), // Source Data - source_->GetSize(), // Source Data Size - &width, // Out: Width - &height, // Out: Height - &comps, // Out: Components - STBI_default); - - if (decoded == nullptr) { - FML_LOG(ERROR) << "Could not decode image from host memory."; - return {}; - } - - auto dest_allocation = std::make_shared( - decoded, // bytes - width * height * comps * sizeof(stbi_uc), // byte size - [](const uint8_t* data, size_t size) { - ::stbi_image_free(const_cast(data)); - } // release proc - ); - - /* - * Make sure we got a valid component set. - */ - auto components = ImageResult::Components::Invalid; - - switch (comps) { - case STBI_grey: - components = ImageResult::Components::Grey; - break; - case STBI_grey_alpha: - components = ImageResult::Components::GreyAlpha; - break; - case STBI_rgb: - components = ImageResult::Components::RGB; - break; - case STBI_rgb_alpha: - components = ImageResult::Components::RGBA; - break; - default: - components = ImageResult::Components::Invalid; - break; - } - - if (components == ImageResult::Components::Invalid) { - FML_LOG(ERROR) << "Could not detect image components when decoding."; - return {}; - } - - return ImageResult{ - ISize{width, height}, // size - components, // components - std::move(dest_allocation) // allocation - }; +bool Image::IsValid() const { + return is_valid_; } -bool Image::IsValid() const { - return static_cast(source_); +const ISize& Image::GetSize() const { + return size_; +} + +Image::Components Image::GetComponents() const { + return components_; +} + +const std::shared_ptr& Image::GetAllocation() const { + return allocation_; } } // namespace impeller diff --git a/engine/src/flutter/impeller/image/image.h b/engine/src/flutter/impeller/image/image.h index 612e76763b..30013542a5 100644 --- a/engine/src/flutter/impeller/image/image.h +++ b/engine/src/flutter/impeller/image/image.h @@ -4,27 +4,47 @@ #pragma once +#include + #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" -#include "image_result.h" #include "impeller/geometry/size.h" namespace impeller { -class ImageSource; - class Image { public: - Image(std::shared_ptr sourceAllocation); + enum class Components { + Invalid, + Grey, + GreyAlpha, + RGB, + RGBA, + }; + + Image(); + + Image(ISize size, + Components components, + std::shared_ptr allocation); ~Image(); - [[nodiscard]] ImageResult Decode() const; + const ISize& GetSize() const; bool IsValid() const; + Components GetComponents() const; + + const std::shared_ptr& GetAllocation() const; + private: - std::shared_ptr source_; + ISize size_; + Components components_ = Components::Invalid; + std::shared_ptr allocation_; + bool is_valid_ = false; + + FML_DISALLOW_COPY_AND_ASSIGN(Image); }; } // namespace impeller diff --git a/engine/src/flutter/impeller/image/image_result.cc b/engine/src/flutter/impeller/image/image_result.cc deleted file mode 100644 index a778de0e38..0000000000 --- a/engine/src/flutter/impeller/image/image_result.cc +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "image_result.h" - -namespace impeller { - -ImageResult::ImageResult() = default; - -ImageResult::ImageResult(ISize size, - Components components, - std::shared_ptr allocation) - : size_(size), components_(components), allocation_(std::move(allocation)) { - if (!allocation_ || !size.IsPositive()) { - return; - } - is_valid_ = true; -} - -ImageResult::~ImageResult() = default; - -bool ImageResult::IsValid() const { - return is_valid_; -} - -const ISize& ImageResult::GetSize() const { - return size_; -} - -ImageResult::Components ImageResult::GetComponents() const { - return components_; -} - -const std::shared_ptr& ImageResult::GetAllocation() const { - return allocation_; -} - -} // namespace impeller diff --git a/engine/src/flutter/impeller/image/image_result.h b/engine/src/flutter/impeller/image/image_result.h deleted file mode 100644 index 7b0c23354a..0000000000 --- a/engine/src/flutter/impeller/image/image_result.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#pragma once - -#include - -#include "flutter/fml/macros.h" -#include "flutter/fml/mapping.h" -#include "impeller/geometry/size.h" - -namespace impeller { - -class ImageResult { - public: - enum class Components { - Invalid, - Grey, - GreyAlpha, - RGB, - RGBA, - }; - - ImageResult(); - - ImageResult(ISize size, - Components components, - std::shared_ptr allocation); - - ~ImageResult(); - - const ISize& GetSize() const; - - bool IsValid() const; - - Components GetComponents() const; - - const std::shared_ptr& GetAllocation() const; - - private: - ISize size_; - Components components_ = Components::Invalid; - std::shared_ptr allocation_; - bool is_valid_ = false; - - FML_DISALLOW_COPY_AND_ASSIGN(ImageResult); -}; - -} // namespace impeller diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index 378d33edcd..922c6af7c6 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -10,6 +10,7 @@ #include "impeller/compositor/sampler_descriptor.h" #include "impeller/compositor/surface.h" #include "impeller/compositor/vertex_buffer_builder.h" +#include "impeller/image/compressed_image.h" #include "impeller/image/image.h" #include "impeller/playground/playground.h" #include "impeller/primitives/box.frag.h" @@ -47,7 +48,8 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { ASSERT_TRUE(vertex_buffer); // Contents texture. - Image image(flutter::testing::OpenFixtureAsMapping("bay_bridge.jpg")); + CompressedImage image( + flutter::testing::OpenFixtureAsMapping("bay_bridge.jpg")); auto result = image.Decode(); ASSERT_TRUE(result.IsValid()); auto texture_descriptor = TextureDescriptor::MakeFromImageResult(result);