Update image class names.

This commit is contained in:
Chinmay Garde
2021-07-08 17:49:15 -07:00
committed by Dan Field
parent e9f5771fcf
commit 8513b51bf1
10 changed files with 179 additions and 175 deletions

View File

@@ -39,7 +39,7 @@ struct TextureDescriptor {
}
static std::optional<TextureDescriptor> MakeFromImageResult(
const ImageResult& result);
const Image& result);
};
} // namespace impeller

View File

@@ -7,24 +7,24 @@
namespace impeller {
std::optional<PixelFormat> 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> TextureDescriptor::MakeFromImageResult(
const ImageResult& result) {
const Image& result) {
if (!result.IsValid()) {
return std::nullopt;
}

View File

@@ -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" ]

View File

@@ -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 <stb_image.h>
namespace impeller {
CompressedImage::CompressedImage(
std::shared_ptr<const fml::Mapping> 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<const fml::NonOwnedMapping>(
decoded, // bytes
width * height * comps * sizeof(stbi_uc), // byte size
[](const uint8_t* data, size_t size) {
::stbi_image_free(const_cast<uint8_t*>(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<bool>(source_);
}
} // namespace impeller

View File

@@ -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<const fml::Mapping> sourceAllocation);
~CompressedImage();
[[nodiscard]] Image Decode() const;
bool IsValid() const;
private:
std::shared_ptr<const fml::Mapping> source_;
};
} // namespace impeller

View File

@@ -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 <stb_image.h>
#include "impeller/image/image.h"
namespace impeller {
Image::Image(std::shared_ptr<const fml::Mapping> sourceAllocation)
: source_(std::move(sourceAllocation)) {}
Image::Image() = default;
Image::Image(ISize size,
Components components,
std::shared_ptr<const fml::Mapping> 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<const fml::NonOwnedMapping>(
decoded, // bytes
width * height * comps * sizeof(stbi_uc), // byte size
[](const uint8_t* data, size_t size) {
::stbi_image_free(const_cast<uint8_t*>(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<bool>(source_);
const ISize& Image::GetSize() const {
return size_;
}
Image::Components Image::GetComponents() const {
return components_;
}
const std::shared_ptr<const fml::Mapping>& Image::GetAllocation() const {
return allocation_;
}
} // namespace impeller

View File

@@ -4,27 +4,47 @@
#pragma once
#include <memory>
#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<const fml::Mapping> sourceAllocation);
enum class Components {
Invalid,
Grey,
GreyAlpha,
RGB,
RGBA,
};
Image();
Image(ISize size,
Components components,
std::shared_ptr<const fml::Mapping> allocation);
~Image();
[[nodiscard]] ImageResult Decode() const;
const ISize& GetSize() const;
bool IsValid() const;
Components GetComponents() const;
const std::shared_ptr<const fml::Mapping>& GetAllocation() const;
private:
std::shared_ptr<const fml::Mapping> source_;
ISize size_;
Components components_ = Components::Invalid;
std::shared_ptr<const fml::Mapping> allocation_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(Image);
};
} // namespace impeller

View File

@@ -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<const fml::Mapping> 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<const fml::Mapping>& ImageResult::GetAllocation() const {
return allocation_;
}
} // namespace impeller

View File

@@ -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 <memory>
#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<const fml::Mapping> allocation);
~ImageResult();
const ISize& GetSize() const;
bool IsValid() const;
Components GetComponents() const;
const std::shared_ptr<const fml::Mapping>& GetAllocation() const;
private:
ISize size_;
Components components_ = Components::Invalid;
std::shared_ptr<const fml::Mapping> allocation_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(ImageResult);
};
} // namespace impeller

View File

@@ -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);