From c73b643cce916f9a83822a7dee05a4292ae1c928 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Sat, 1 May 2021 17:37:33 -0700 Subject: [PATCH] Import STB for rl::image::Image. --- engine/src/flutter/impeller/impeller/BUILD.gn | 3 +- .../impeller/impeller/entity/Entity.cc | 8 -- .../flutter/impeller/impeller/entity/Entity.h | 6 +- .../flutter/impeller/impeller/image/Image.cc | 62 +++++-------- .../flutter/impeller/impeller/image/Image.h | 14 +-- .../impeller/impeller/image/ImageEncoder.cc | 86 ------------------- .../impeller/impeller/image/ImageEncoder.h | 41 --------- .../impeller/impeller/image/ImageResult.cc | 27 ++---- .../impeller/impeller/image/ImageResult.h | 17 ++-- 9 files changed, 36 insertions(+), 228 deletions(-) delete mode 100644 engine/src/flutter/impeller/impeller/image/ImageEncoder.cc delete mode 100644 engine/src/flutter/impeller/impeller/image/ImageEncoder.h diff --git a/engine/src/flutter/impeller/impeller/BUILD.gn b/engine/src/flutter/impeller/impeller/BUILD.gn index f10f70df6b..306f295025 100644 --- a/engine/src/flutter/impeller/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/impeller/BUILD.gn @@ -47,8 +47,6 @@ source_set("impeller") { image_sources = [ "image/Image.cc", "image/Image.h", - "image/ImageEncoder.cc", - "image/ImageEncoder.h", "image/ImageResult.cc", "image/ImageResult.h", ] @@ -57,6 +55,7 @@ source_set("impeller") { deps = [ "//flutter/fml", + "//flutter/impeller/third_party/stb", "//flutter/runtime:libdart", # For tracing, seems to be pulled in by FML. ] } diff --git a/engine/src/flutter/impeller/impeller/entity/Entity.cc b/engine/src/flutter/impeller/impeller/entity/Entity.cc index 6920d12034..772ceef394 100644 --- a/engine/src/flutter/impeller/impeller/entity/Entity.cc +++ b/engine/src/flutter/impeller/impeller/entity/Entity.cc @@ -121,14 +121,6 @@ void Entity::setStrokeSize(double strokeSize) { _strokeSize = strokeSize; } -const image::Image& Entity::contents() const { - return _contents; -} - -void Entity::setContents(image::Image image) { - _contents = std::move(image); -} - const geom::Path& Entity::path() const { return _path; } diff --git a/engine/src/flutter/impeller/impeller/entity/Entity.h b/engine/src/flutter/impeller/impeller/entity/Entity.h index fe0717f720..c1e9e4d557 100644 --- a/engine/src/flutter/impeller/impeller/entity/Entity.h +++ b/engine/src/flutter/impeller/impeller/entity/Entity.h @@ -180,10 +180,6 @@ class Entity { void setStrokeSize(double strokeSize); - const image::Image& contents() const; - - void setContents(image::Image image); - const geom::Path& path() const; void setPath(geom::Path path); @@ -194,7 +190,7 @@ class Entity { geom::Point _anchorPoint; geom::Matrix _transformation; Color _backgroundColor; - image::Image _contents; + geom::Path _path; double _opacity; Color _strokeColor; diff --git a/engine/src/flutter/impeller/impeller/image/Image.cc b/engine/src/flutter/impeller/impeller/image/Image.cc index 953d2f0a1a..bf0bf186cc 100644 --- a/engine/src/flutter/impeller/impeller/image/Image.cc +++ b/engine/src/flutter/impeller/impeller/image/Image.cc @@ -5,30 +5,17 @@ #include "Image.h" #include -#include "ImageSource.h" namespace rl { namespace image { -Image::Image() = default; - -Image::Image(fml::RefPtr sourceAllocation) - : _source(ImageSource::Create(std::move(sourceAllocation))) {} - -Image::Image(core::FileHandle sourceFile) - : _source(ImageSource::Create(std::move(sourceFile))) {} +Image::Image(std::shared_ptr sourceAllocation) + : _source(std::move(sourceAllocation)) {} Image::~Image() = default; ImageResult Image::decode() const { - if (_source == nullptr) { - return {}; - } - - _source->prepareForUse(); - - if (_source->sourceDataSize() == 0) { - RL_LOG("Source data for image decoding was zero sized."); + if (!_source) { return {}; } @@ -37,27 +24,26 @@ ImageResult Image::decode() const { int comps = 0; stbi_uc* decoded = - stbi_load_from_memory(_source->sourceData(), // Source Data - _source->sourceDataSize(), // Source Data Size - &width, // Out: Width - &height, // Out: Height - &comps, // Out: Components + stbi_load_from_memory(_source->GetMapping(), // Source Data + _source->GetSize(), // Source Data Size + &width, // Out: Width + &height, // Out: Height + &comps, // Out: Components STBI_default); - auto destinationAllocation = fml::RefPtr{ - decoded, width * height * comps * sizeof(stbi_uc)}; - - /* - * If either the decoded allocation is null or the size works out to be zero, - * the allocation will mark itself as not ready and we know that the decode - * job failed. - */ - - if (!destinationAllocation.isReady()) { - RL_LOG("Destination allocation for image decoding was null."); + if (decoded == nullptr) { + FML_LOG(ERROR) << "Could not decode image from host memory."; return {}; } + auto destinationAllocation = 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. */ @@ -82,7 +68,7 @@ ImageResult Image::decode() const { } if (components == ImageResult::Components::Invalid) { - RL_LOG("Could not detect image components when decoding."); + FML_LOG(ERROR) << "Could not detect image components when decoding."; return {}; } @@ -95,15 +81,7 @@ ImageResult Image::decode() const { } bool Image::isValid() const { - return _source != nullptr; -} - -std::size_t Image::Hash::operator()(const Image& key) const { - return std::hash()(key._source); -} - -bool Image::Equal::operator()(const Image& lhs, const Image& rhs) const { - return lhs._source == rhs._source; + return static_cast(_source); } } // namespace image diff --git a/engine/src/flutter/impeller/impeller/image/Image.h b/engine/src/flutter/impeller/impeller/image/Image.h index 8b163b6960..40b5471cef 100644 --- a/engine/src/flutter/impeller/impeller/image/Image.h +++ b/engine/src/flutter/impeller/impeller/image/Image.h @@ -17,9 +17,7 @@ class ImageSource; class Image { public: - Image(); - - Image(fml::RefPtr sourceAllocation); + Image(std::shared_ptr sourceAllocation); ~Image(); @@ -27,16 +25,8 @@ class Image { bool isValid() const; - struct Hash { - std::size_t operator()(const Image& key) const; - }; - - struct Equal { - bool operator()(const Image& lhs, const Image& rhs) const; - }; - private: - std::shared_ptr _source; + std::shared_ptr _source; }; } // namespace image diff --git a/engine/src/flutter/impeller/impeller/image/ImageEncoder.cc b/engine/src/flutter/impeller/impeller/image/ImageEncoder.cc deleted file mode 100644 index 939e9e07f6..0000000000 --- a/engine/src/flutter/impeller/impeller/image/ImageEncoder.cc +++ /dev/null @@ -1,86 +0,0 @@ -/* - * This source file is part of the Radar project. - * Licensed under the MIT License. See LICENSE file for details. - */ - -#include "ImageEncoder.h" -#include - -namespace rl { -namespace image { - -ImageEncoder::ImageEncoder(Type type, core::FileHandle handle) - : _isReady(false), _type(type), _adapter(std::move(handle)) { - if (!_adapter.isValid()) { - return; - } - - _isReady = true; -} - -ImageEncoder::~ImageEncoder() = default; - -bool ImageEncoder::isReady() const { - return _isReady; -} - -bool ImageEncoder::encode(ImageResult image) { - if (!_isReady) { - return false; - } - - if (!image.wasSuccessful()) { - return false; - } - - switch (_type) { - case Type::PNG: - return encodePNG(std::move(image)); - } - - return false; -} - -int ComponentsToSize(ImageResult::Components components) { - switch (components) { - case ImageResult::Components::Invalid: - return 0; - case ImageResult::Components::Grey: - return 1; - case ImageResult::Components::GreyAlpha: - return 2; - case ImageResult::Components::RGB: - return 3; - case ImageResult::Components::RGBA: - return 4; - } - - return 0; -} - -bool ImageEncoder::encodePNG(ImageResult image) { - auto size = image.size(); - - int componentSize = ComponentsToSize(image.components()); - - auto callback = [](void* context, void* data, int size) { - reinterpret_cast(context)->write( - reinterpret_cast(data), size); - }; - - return stbi_write_png_to_func(callback, // - this, // - size.width, // - size.height, // - componentSize, // - image.allocation().data(), // - componentSize // - ) == 1; -} - -void ImageEncoder::write(const uint8_t* data, size_t size) { - RL_UNUSED(_adapter.write(data, size)); -} - -} // namespace image -} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageEncoder.h b/engine/src/flutter/impeller/impeller/image/ImageEncoder.h deleted file mode 100644 index ba49a5bc2a..0000000000 --- a/engine/src/flutter/impeller/impeller/image/ImageEncoder.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * This source file is part of the Radar project. - * Licensed under the MIT License. See LICENSE file for details. - */ - -#pragma once - -#include "ImageResult.h" -#include "Size.h" - -namespace rl { -namespace image { - -class ImageEncoder { - public: - enum class Type { - PNG, - }; - - ImageEncoder(Type type, core::FileHandle handle); - - ~ImageEncoder(); - - bool isReady() const; - - bool encode(ImageResult image); - - private: - bool _isReady; - Type _type; - core::FileIOAdapter _adapter; - - bool encodePNG(ImageResult image); - - void write(const uint8_t* data, size_t size); - - FML_DISALLOW_COPY_AND_ASSIGN(ImageEncoder); -}; - -} // namespace image -} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageResult.cc b/engine/src/flutter/impeller/impeller/image/ImageResult.cc index f9dbbb5922..09c4ea10eb 100644 --- a/engine/src/flutter/impeller/impeller/image/ImageResult.cc +++ b/engine/src/flutter/impeller/impeller/image/ImageResult.cc @@ -8,32 +8,17 @@ namespace rl { namespace image { +ImageResult::ImageResult() = default; + ImageResult::ImageResult(geom::Size size, Components components, - fml::RefPtr allocation) + std::shared_ptr allocation) : _success(true), _size(size), _components(components), _allocation(std::move(allocation)) {} -ImageResult::ImageResult() : _success(false) {} - -ImageResult::ImageResult(ImageResult&&) = default; - -ImageResult& ImageResult::operator=(ImageResult&& other) { - _success = other._success; - other._success = false; - - _size = other._size; - other._size = geom::Size{}; - - _components = other._components; - other._components = Components::Invalid; - - _allocation = std::move(other._allocation); - - return *this; -} +ImageResult::~ImageResult() = default; bool ImageResult::wasSuccessful() const { return _success; @@ -47,11 +32,9 @@ ImageResult::Components ImageResult::components() const { return _components; } -const fml::RefPtr& ImageResult::allocation() const { +const std::shared_ptr& ImageResult::allocation() const { return _allocation; } -ImageResult::~ImageResult() = default; - } // namespace image } // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageResult.h b/engine/src/flutter/impeller/impeller/image/ImageResult.h index 4ee85619b7..8943dc02d7 100644 --- a/engine/src/flutter/impeller/impeller/image/ImageResult.h +++ b/engine/src/flutter/impeller/impeller/image/ImageResult.h @@ -5,10 +5,11 @@ #pragma once +#include + #include "Size.h" #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" -#include "flutter/fml/memory/ref_counted.h" namespace rl { namespace image { @@ -27,11 +28,7 @@ class ImageResult { ImageResult(geom::Size size, Components components, - fml::RefPtr allocation); - - ImageResult(ImageResult&&); - - ImageResult& operator=(ImageResult&&); + std::shared_ptr allocation); ~ImageResult(); @@ -41,13 +38,13 @@ class ImageResult { Components components() const; - const fml::RefPtr& allocation() const; + const std::shared_ptr& allocation() const; private: - bool _success; + bool _success = false; geom::Size _size; - Components _components; - fml::RefPtr _allocation; + Components _components = Components::Invalid; + std::shared_ptr _allocation; FML_DISALLOW_COPY_AND_ASSIGN(ImageResult); };