Remove NSObjectRetain/Release
These shouldn't exist; smart pointers should be used. Given that they're only used in one place, remove them with some refactoring. Do some additional C++ modernization to gfx::Image while we're there. Bug: none Change-Id: I603a4c5ab3693c2ee7dfd4b5059f99cbc1baea15 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3434388 Reviewed-by: Mark Mentovai <mark@chromium.org> Reviewed-by: Robert Sesek <rsesek@chromium.org> Commit-Queue: Avi Drissman <avi@chromium.org> Cr-Commit-Position: refs/heads/main@{#966996}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
656c5e6f85
commit
cd841d36c0
@@ -138,10 +138,6 @@ TYPE_NAME_FOR_CF_TYPE_DECL(SecPolicy);
|
|||||||
|
|
||||||
#undef TYPE_NAME_FOR_CF_TYPE_DECL
|
#undef TYPE_NAME_FOR_CF_TYPE_DECL
|
||||||
|
|
||||||
// Retain/release calls for memory management in C++.
|
|
||||||
BASE_EXPORT void NSObjectRetain(void* obj);
|
|
||||||
BASE_EXPORT void NSObjectRelease(void* obj);
|
|
||||||
|
|
||||||
// Returns the base bundle ID, which can be set by SetBaseBundleID but
|
// Returns the base bundle ID, which can be set by SetBaseBundleID but
|
||||||
// defaults to a reasonable string. This never returns NULL. BaseBundleID
|
// defaults to a reasonable string. This never returns NULL. BaseBundleID
|
||||||
// returns a pointer to static storage that must not be freed.
|
// returns a pointer to static storage that must not be freed.
|
||||||
|
@@ -231,16 +231,6 @@ TYPE_NAME_FOR_CF_TYPE_DEFN(SecPolicy)
|
|||||||
|
|
||||||
#undef TYPE_NAME_FOR_CF_TYPE_DEFN
|
#undef TYPE_NAME_FOR_CF_TYPE_DEFN
|
||||||
|
|
||||||
void NSObjectRetain(void* obj) {
|
|
||||||
id<NSObject> nsobj = static_cast<id<NSObject> >(obj);
|
|
||||||
[nsobj retain];
|
|
||||||
}
|
|
||||||
|
|
||||||
void NSObjectRelease(void* obj) {
|
|
||||||
id<NSObject> nsobj = static_cast<id<NSObject> >(obj);
|
|
||||||
[nsobj release];
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char* base_bundle_id;
|
static const char* base_bundle_id;
|
||||||
|
|
||||||
const char* BaseBundleID() {
|
const char* BaseBundleID() {
|
||||||
|
@@ -93,18 +93,6 @@ TEST_F(MacUtilTest, TestGetAppBundlePath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(MacUtilTest, NSObjectRetainRelease) {
|
|
||||||
base::scoped_nsobject<NSArray> array(
|
|
||||||
[[NSArray alloc] initWithObjects:@"foo", nil]);
|
|
||||||
EXPECT_EQ(1U, [array retainCount]);
|
|
||||||
|
|
||||||
NSObjectRetain(array);
|
|
||||||
EXPECT_EQ(2U, [array retainCount]);
|
|
||||||
|
|
||||||
NSObjectRelease(array);
|
|
||||||
EXPECT_EQ(1U, [array retainCount]);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(MacUtilTest, IsOSEllipsis) {
|
TEST_F(MacUtilTest, IsOSEllipsis) {
|
||||||
int32_t major, minor, bugfix;
|
int32_t major, minor, bugfix;
|
||||||
base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
|
base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
|
||||||
|
@@ -95,6 +95,7 @@ component("gfx") {
|
|||||||
"image/image.h",
|
"image/image.h",
|
||||||
"image/image_family.cc",
|
"image/image_family.cc",
|
||||||
"image/image_family.h",
|
"image/image_family.h",
|
||||||
|
"image/image_internal.h",
|
||||||
"image/image_platform.h",
|
"image/image_platform.h",
|
||||||
"image/image_png_rep.cc",
|
"image/image_png_rep.cc",
|
||||||
"image/image_png_rep.h",
|
"image/image_png_rep.h",
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
#include "build/build_config.h"
|
#include "build/build_config.h"
|
||||||
#include "third_party/skia/include/core/SkBitmap.h"
|
#include "third_party/skia/include/core/SkBitmap.h"
|
||||||
#include "ui/gfx/geometry/size.h"
|
#include "ui/gfx/geometry/size.h"
|
||||||
|
#include "ui/gfx/image/image_internal.h"
|
||||||
#include "ui/gfx/image/image_platform.h"
|
#include "ui/gfx/image/image_platform.h"
|
||||||
#include "ui/gfx/image/image_png_rep.h"
|
#include "ui/gfx/image/image_png_rep.h"
|
||||||
#include "ui/gfx/image/image_skia.h"
|
#include "ui/gfx/image/image_skia.h"
|
||||||
@@ -30,82 +31,31 @@
|
|||||||
|
|
||||||
namespace gfx {
|
namespace gfx {
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
using RepresentationMap =
|
|
||||||
std::map<Image::RepresentationType, std::unique_ptr<internal::ImageRep>>;
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
class ImageRepPNG;
|
ImageRep::ImageRep(Image::RepresentationType rep) : type_(rep) {}
|
||||||
class ImageRepSkia;
|
|
||||||
class ImageRepCocoa;
|
|
||||||
class ImageRepCocoaTouch;
|
|
||||||
|
|
||||||
// An ImageRep is the object that holds the backing memory for an Image. Each
|
ImageRep::~ImageRep() = default;
|
||||||
// RepresentationType has an ImageRep subclass that is responsible for freeing
|
|
||||||
// the memory that the ImageRep holds. When an ImageRep is created, it expects
|
|
||||||
// to take ownership of the image, without having to retain it or increase its
|
|
||||||
// reference count.
|
|
||||||
class ImageRep {
|
|
||||||
public:
|
|
||||||
explicit ImageRep(Image::RepresentationType rep) : type_(rep) {}
|
|
||||||
|
|
||||||
// Deletes the associated pixels of an ImageRep.
|
const ImageRepPNG* ImageRep::AsImageRepPNG() const {
|
||||||
virtual ~ImageRep() {}
|
CHECK_EQ(type_, Image::kImageRepPNG);
|
||||||
|
return reinterpret_cast<const ImageRepPNG*>(this);
|
||||||
|
}
|
||||||
|
ImageRepPNG* ImageRep::AsImageRepPNG() {
|
||||||
|
return const_cast<ImageRepPNG*>(
|
||||||
|
static_cast<const ImageRep*>(this)->AsImageRepPNG());
|
||||||
|
}
|
||||||
|
|
||||||
// Cast helpers ("fake RTTI").
|
const ImageRepSkia* ImageRep::AsImageRepSkia() const {
|
||||||
const ImageRepPNG* AsImageRepPNG() const {
|
CHECK_EQ(type_, Image::kImageRepSkia);
|
||||||
CHECK_EQ(type_, Image::kImageRepPNG);
|
return reinterpret_cast<const ImageRepSkia*>(this);
|
||||||
return reinterpret_cast<const ImageRepPNG*>(this);
|
}
|
||||||
}
|
ImageRepSkia* ImageRep::AsImageRepSkia() {
|
||||||
ImageRepPNG* AsImageRepPNG() {
|
return const_cast<ImageRepSkia*>(
|
||||||
return const_cast<ImageRepPNG*>(
|
static_cast<const ImageRep*>(this)->AsImageRepSkia());
|
||||||
static_cast<const ImageRep*>(this)->AsImageRepPNG());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const ImageRepSkia* AsImageRepSkia() const {
|
class ImageRepPNG final : public ImageRep {
|
||||||
CHECK_EQ(type_, Image::kImageRepSkia);
|
|
||||||
return reinterpret_cast<const ImageRepSkia*>(this);
|
|
||||||
}
|
|
||||||
ImageRepSkia* AsImageRepSkia() {
|
|
||||||
return const_cast<ImageRepSkia*>(
|
|
||||||
static_cast<const ImageRep*>(this)->AsImageRepSkia());
|
|
||||||
}
|
|
||||||
|
|
||||||
#if BUILDFLAG(IS_IOS)
|
|
||||||
const ImageRepCocoaTouch* AsImageRepCocoaTouch() const {
|
|
||||||
CHECK_EQ(type_, Image::kImageRepCocoaTouch);
|
|
||||||
return reinterpret_cast<const ImageRepCocoaTouch*>(this);
|
|
||||||
}
|
|
||||||
ImageRepCocoaTouch* AsImageRepCocoaTouch() {
|
|
||||||
return const_cast<ImageRepCocoaTouch*>(
|
|
||||||
static_cast<const ImageRep*>(this)->AsImageRepCocoaTouch());
|
|
||||||
}
|
|
||||||
#elif BUILDFLAG(IS_MAC)
|
|
||||||
const ImageRepCocoa* AsImageRepCocoa() const {
|
|
||||||
CHECK_EQ(type_, Image::kImageRepCocoa);
|
|
||||||
return reinterpret_cast<const ImageRepCocoa*>(this);
|
|
||||||
}
|
|
||||||
ImageRepCocoa* AsImageRepCocoa() {
|
|
||||||
return const_cast<ImageRepCocoa*>(
|
|
||||||
static_cast<const ImageRep*>(this)->AsImageRepCocoa());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Image::RepresentationType type() const { return type_; }
|
|
||||||
|
|
||||||
virtual int Width() const = 0;
|
|
||||||
virtual int Height() const = 0;
|
|
||||||
virtual gfx::Size Size() const = 0;
|
|
||||||
|
|
||||||
private:
|
|
||||||
Image::RepresentationType type_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ImageRepPNG : public ImageRep {
|
|
||||||
public:
|
public:
|
||||||
ImageRepPNG() : ImageRep(Image::kImageRepPNG) {
|
ImageRepPNG() : ImageRep(Image::kImageRepPNG) {
|
||||||
}
|
}
|
||||||
@@ -116,7 +66,7 @@ class ImageRepPNG : public ImageRep {
|
|||||||
ImageRepPNG(const ImageRepPNG&) = delete;
|
ImageRepPNG(const ImageRepPNG&) = delete;
|
||||||
ImageRepPNG& operator=(const ImageRepPNG&) = delete;
|
ImageRepPNG& operator=(const ImageRepPNG&) = delete;
|
||||||
|
|
||||||
~ImageRepPNG() override {}
|
~ImageRepPNG() override = default;
|
||||||
|
|
||||||
int Width() const override { return Size().width(); }
|
int Width() const override { return Size().width(); }
|
||||||
|
|
||||||
@@ -125,9 +75,9 @@ class ImageRepPNG : public ImageRep {
|
|||||||
gfx::Size Size() const override {
|
gfx::Size Size() const override {
|
||||||
// Read the PNG data to get the image size, caching it.
|
// Read the PNG data to get the image size, caching it.
|
||||||
if (!size_cache_) {
|
if (!size_cache_) {
|
||||||
for (auto it = image_reps().begin(); it != image_reps().end(); ++it) {
|
for (const auto& it : image_reps()) {
|
||||||
if (it->scale == 1.0f) {
|
if (it.scale == 1.0f) {
|
||||||
size_cache_ = it->Size();
|
size_cache_ = it.Size();
|
||||||
return *size_cache_;
|
return *size_cache_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,7 +96,7 @@ class ImageRepPNG : public ImageRep {
|
|||||||
mutable absl::optional<gfx::Size> size_cache_;
|
mutable absl::optional<gfx::Size> size_cache_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ImageRepSkia : public ImageRep {
|
class ImageRepSkia final : public ImageRep {
|
||||||
public:
|
public:
|
||||||
explicit ImageRepSkia(ImageSkia image)
|
explicit ImageRepSkia(ImageSkia image)
|
||||||
: ImageRep(Image::kImageRepSkia), image_(image) {}
|
: ImageRep(Image::kImageRepSkia), image_(image) {}
|
||||||
@@ -154,7 +104,7 @@ class ImageRepSkia : public ImageRep {
|
|||||||
ImageRepSkia(const ImageRepSkia&) = delete;
|
ImageRepSkia(const ImageRepSkia&) = delete;
|
||||||
ImageRepSkia& operator=(const ImageRepSkia&) = delete;
|
ImageRepSkia& operator=(const ImageRepSkia&) = delete;
|
||||||
|
|
||||||
~ImageRepSkia() override {}
|
~ImageRepSkia() override = default;
|
||||||
|
|
||||||
int Width() const override { return image_.width(); }
|
int Width() const override { return image_.width(); }
|
||||||
|
|
||||||
@@ -169,171 +119,67 @@ class ImageRepSkia : public ImageRep {
|
|||||||
ImageSkia image_;
|
ImageSkia image_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#if BUILDFLAG(IS_IOS)
|
ImageStorage::ImageStorage(Image::RepresentationType default_type)
|
||||||
class ImageRepCocoaTouch : public ImageRep {
|
: default_representation_type_(default_type)
|
||||||
public:
|
|
||||||
explicit ImageRepCocoaTouch(UIImage* image)
|
|
||||||
: ImageRep(Image::kImageRepCocoaTouch),
|
|
||||||
image_(image) {
|
|
||||||
CHECK(image_);
|
|
||||||
base::mac::NSObjectRetain(image_);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageRepCocoaTouch(const ImageRepCocoaTouch&) = delete;
|
|
||||||
ImageRepCocoaTouch& operator=(const ImageRepCocoaTouch&) = delete;
|
|
||||||
|
|
||||||
~ImageRepCocoaTouch() override {
|
|
||||||
base::mac::NSObjectRelease(image_);
|
|
||||||
image_ = nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Width() const override { return Size().width(); }
|
|
||||||
|
|
||||||
int Height() const override { return Size().height(); }
|
|
||||||
|
|
||||||
gfx::Size Size() const override { return internal::UIImageSize(image_); }
|
|
||||||
|
|
||||||
UIImage* image() const { return image_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
UIImage* image_;
|
|
||||||
};
|
|
||||||
#elif BUILDFLAG(IS_MAC)
|
|
||||||
class ImageRepCocoa : public ImageRep {
|
|
||||||
public:
|
|
||||||
explicit ImageRepCocoa(NSImage* image)
|
|
||||||
: ImageRep(Image::kImageRepCocoa),
|
|
||||||
image_(image) {
|
|
||||||
CHECK(image_);
|
|
||||||
base::mac::NSObjectRetain(image_);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageRepCocoa(const ImageRepCocoa&) = delete;
|
|
||||||
ImageRepCocoa& operator=(const ImageRepCocoa&) = delete;
|
|
||||||
|
|
||||||
~ImageRepCocoa() override {
|
|
||||||
base::mac::NSObjectRelease(image_);
|
|
||||||
image_ = nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Width() const override { return Size().width(); }
|
|
||||||
|
|
||||||
int Height() const override { return Size().height(); }
|
|
||||||
|
|
||||||
gfx::Size Size() const override { return internal::NSImageSize(image_); }
|
|
||||||
|
|
||||||
NSImage* image() const { return image_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
NSImage* image_;
|
|
||||||
};
|
|
||||||
#endif // BUILDFLAG(IS_MAC)
|
|
||||||
|
|
||||||
// The Storage class acts similarly to the pixels in a SkBitmap: the Image
|
|
||||||
// class holds a refptr instance of Storage, which in turn holds all the
|
|
||||||
// ImageReps. This way, the Image can be cheaply copied.
|
|
||||||
//
|
|
||||||
// This class is deliberately not RefCountedThreadSafe. Making it so does not
|
|
||||||
// solve threading issues, as gfx::Image and its internal classes are
|
|
||||||
// themselves not threadsafe.
|
|
||||||
class ImageStorage : public base::RefCounted<ImageStorage> {
|
|
||||||
public:
|
|
||||||
explicit ImageStorage(Image::RepresentationType default_type)
|
|
||||||
: default_representation_type_(default_type)
|
|
||||||
#if BUILDFLAG(IS_MAC)
|
#if BUILDFLAG(IS_MAC)
|
||||||
,
|
,
|
||||||
default_representation_color_space_(
|
default_representation_color_space_(base::mac::GetGenericRGBColorSpace())
|
||||||
base::mac::GetGenericRGBColorSpace())
|
|
||||||
#endif // BUILDFLAG(IS_MAC)
|
#endif // BUILDFLAG(IS_MAC)
|
||||||
{
|
{
|
||||||
|
}
|
||||||
|
ImageStorage::~ImageStorage() = default;
|
||||||
|
|
||||||
|
Image::RepresentationType ImageStorage::default_representation_type() const {
|
||||||
|
DCHECK(IsOnValidSequence());
|
||||||
|
return default_representation_type_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImageStorage::HasRepresentation(Image::RepresentationType type) const {
|
||||||
|
DCHECK(IsOnValidSequence());
|
||||||
|
return representations_.count(type) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ImageStorage::RepresentationCount() const {
|
||||||
|
DCHECK(IsOnValidSequence());
|
||||||
|
return representations_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const ImageRep* ImageStorage::GetRepresentation(
|
||||||
|
Image::RepresentationType rep_type,
|
||||||
|
bool must_exist) const {
|
||||||
|
DCHECK(IsOnValidSequence());
|
||||||
|
auto it = representations_.find(rep_type);
|
||||||
|
if (it == representations_.end()) {
|
||||||
|
CHECK(!must_exist);
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
return it->second.get();
|
||||||
|
}
|
||||||
|
|
||||||
ImageStorage(const ImageStorage&) = delete;
|
const ImageRep* ImageStorage::AddRepresentation(
|
||||||
ImageStorage& operator=(const ImageStorage&) = delete;
|
std::unique_ptr<ImageRep> rep) const {
|
||||||
|
DCHECK(IsOnValidSequence());
|
||||||
|
Image::RepresentationType type = rep->type();
|
||||||
|
auto result = representations_.emplace(type, std::move(rep));
|
||||||
|
|
||||||
Image::RepresentationType default_representation_type() const {
|
// insert should not fail (implies that there was already a representation
|
||||||
DCHECK(IsOnValidSequence());
|
// of that type in the map).
|
||||||
return default_representation_type_;
|
CHECK(result.second) << "type was already in map.";
|
||||||
}
|
|
||||||
|
|
||||||
bool HasRepresentation(Image::RepresentationType type) const {
|
return result.first->second.get();
|
||||||
DCHECK(IsOnValidSequence());
|
}
|
||||||
return representations_.count(type) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t RepresentationCount() const {
|
|
||||||
DCHECK(IsOnValidSequence());
|
|
||||||
return representations_.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
const ImageRep* GetRepresentation(Image::RepresentationType rep_type,
|
|
||||||
bool must_exist) const {
|
|
||||||
DCHECK(IsOnValidSequence());
|
|
||||||
RepresentationMap::const_iterator it = representations_.find(rep_type);
|
|
||||||
if (it == representations_.end()) {
|
|
||||||
CHECK(!must_exist);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return it->second.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
const ImageRep* AddRepresentation(std::unique_ptr<ImageRep> rep) const {
|
|
||||||
DCHECK(IsOnValidSequence());
|
|
||||||
Image::RepresentationType type = rep->type();
|
|
||||||
auto result = representations_.emplace(type, std::move(rep));
|
|
||||||
|
|
||||||
// insert should not fail (implies that there was already a representation
|
|
||||||
// of that type in the map).
|
|
||||||
CHECK(result.second) << "type was already in map.";
|
|
||||||
|
|
||||||
return result.first->second.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if BUILDFLAG(IS_MAC)
|
|
||||||
void set_default_representation_color_space(CGColorSpaceRef color_space) {
|
|
||||||
DCHECK(IsOnValidSequence());
|
|
||||||
default_representation_color_space_ = color_space;
|
|
||||||
}
|
|
||||||
CGColorSpaceRef default_representation_color_space() const {
|
|
||||||
DCHECK(IsOnValidSequence());
|
|
||||||
return default_representation_color_space_;
|
|
||||||
}
|
|
||||||
#endif // BUILDFLAG(IS_MAC)
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class base::RefCounted<ImageStorage>;
|
|
||||||
|
|
||||||
~ImageStorage() {}
|
|
||||||
|
|
||||||
// The type of image that was passed to the constructor. This key will always
|
|
||||||
// exist in the |representations_| map.
|
|
||||||
Image::RepresentationType default_representation_type_;
|
|
||||||
|
|
||||||
#if BUILDFLAG(IS_MAC)
|
|
||||||
// The default representation's colorspace. This is used for converting to
|
|
||||||
// NSImage. This field exists to compensate for PNGCodec not writing or
|
|
||||||
// reading colorspace ancillary chunks. (sRGB, iCCP).
|
|
||||||
// Not owned.
|
|
||||||
CGColorSpaceRef default_representation_color_space_;
|
|
||||||
#endif // BUILDFLAG(IS_MAC)
|
|
||||||
|
|
||||||
// All the representations of an Image. Size will always be at least one, with
|
|
||||||
// more for any converted representations.
|
|
||||||
mutable RepresentationMap representations_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
Image::Image() {
|
|
||||||
// |storage_| is null for empty Images.
|
// |storage_| is null for empty Images.
|
||||||
}
|
Image::Image() = default;
|
||||||
|
|
||||||
Image::Image(const std::vector<ImagePNGRep>& image_reps) {
|
Image::Image(const std::vector<ImagePNGRep>& image_reps) {
|
||||||
// Do not store obviously invalid ImagePNGReps.
|
// Do not store obviously invalid ImagePNGReps.
|
||||||
std::vector<ImagePNGRep> filtered;
|
std::vector<ImagePNGRep> filtered;
|
||||||
for (size_t i = 0; i < image_reps.size(); ++i) {
|
for (const auto& image_rep : image_reps) {
|
||||||
if (image_reps[i].raw_data.get() && image_reps[i].raw_data->size())
|
if (image_rep.raw_data.get() && image_rep.raw_data->size())
|
||||||
filtered.push_back(image_reps[i]);
|
filtered.push_back(image_rep);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filtered.empty())
|
if (filtered.empty())
|
||||||
@@ -350,22 +196,6 @@ Image::Image(const ImageSkia& image) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if BUILDFLAG(IS_IOS)
|
|
||||||
Image::Image(UIImage* image) {
|
|
||||||
if (image) {
|
|
||||||
storage_ = new internal::ImageStorage(Image::kImageRepCocoaTouch);
|
|
||||||
AddRepresentation(std::make_unique<internal::ImageRepCocoaTouch>(image));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#elif BUILDFLAG(IS_MAC)
|
|
||||||
Image::Image(NSImage* image) {
|
|
||||||
if (image) {
|
|
||||||
storage_ = new internal::ImageStorage(Image::kImageRepCocoa);
|
|
||||||
AddRepresentation(std::make_unique<internal::ImageRepCocoa>(image));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Image::Image(const Image& other) = default;
|
Image::Image(const Image& other) = default;
|
||||||
|
|
||||||
Image::Image(Image&& other) noexcept = default;
|
Image::Image(Image&& other) noexcept = default;
|
||||||
@@ -374,7 +204,7 @@ Image& Image::operator=(const Image& other) = default;
|
|||||||
|
|
||||||
Image& Image::operator=(Image&& other) noexcept = default;
|
Image& Image::operator=(Image&& other) noexcept = default;
|
||||||
|
|
||||||
Image::~Image() {}
|
Image::~Image() = default;
|
||||||
|
|
||||||
bool Image::operator==(const Image& other) const {
|
bool Image::operator==(const Image& other) const {
|
||||||
return storage_ == other.storage_;
|
return storage_ == other.storage_;
|
||||||
@@ -403,7 +233,7 @@ Image Image::CreateFrom1xPNGBytes(
|
|||||||
return Image();
|
return Image();
|
||||||
|
|
||||||
std::vector<ImagePNGRep> image_reps;
|
std::vector<ImagePNGRep> image_reps;
|
||||||
image_reps.push_back(ImagePNGRep(input, 1.0f));
|
image_reps.emplace_back(input, 1.0f);
|
||||||
return Image(image_reps);
|
return Image(image_reps);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,7 +260,7 @@ const ImageSkia* Image::ToImageSkia() const {
|
|||||||
GetRepresentation(kImageRepCocoaTouch, true)
|
GetRepresentation(kImageRepCocoaTouch, true)
|
||||||
->AsImageRepCocoaTouch();
|
->AsImageRepCocoaTouch();
|
||||||
scoped_rep = std::make_unique<internal::ImageRepSkia>(
|
scoped_rep = std::make_unique<internal::ImageRepSkia>(
|
||||||
ImageSkia(ImageSkiaFromUIImage(native_rep->image())));
|
ImageSkiaFromUIImage(UIImageOfImageRepCocoaTouch(native_rep)));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#elif BUILDFLAG(IS_MAC)
|
#elif BUILDFLAG(IS_MAC)
|
||||||
@@ -438,7 +268,7 @@ const ImageSkia* Image::ToImageSkia() const {
|
|||||||
const internal::ImageRepCocoa* native_rep =
|
const internal::ImageRepCocoa* native_rep =
|
||||||
GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa();
|
GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa();
|
||||||
scoped_rep = std::make_unique<internal::ImageRepSkia>(
|
scoped_rep = std::make_unique<internal::ImageRepSkia>(
|
||||||
ImageSkia(ImageSkiaFromNSImage(native_rep->image())));
|
ImageSkiaFromNSImage(NSImageOfImageRepCocoa(native_rep)));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -460,7 +290,7 @@ UIImage* Image::ToUIImage() const {
|
|||||||
case kImageRepPNG: {
|
case kImageRepPNG: {
|
||||||
const internal::ImageRepPNG* png_rep =
|
const internal::ImageRepPNG* png_rep =
|
||||||
GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
|
GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
|
||||||
scoped_rep = std::make_unique<internal::ImageRepCocoaTouch>(
|
scoped_rep = internal::MakeImageRepCocoaTouch(
|
||||||
internal::UIImageFromPNG(png_rep->image_reps()));
|
internal::UIImageFromPNG(png_rep->image_reps()));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -468,7 +298,7 @@ UIImage* Image::ToUIImage() const {
|
|||||||
const internal::ImageRepSkia* skia_rep =
|
const internal::ImageRepSkia* skia_rep =
|
||||||
GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
|
GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
|
||||||
UIImage* image = UIImageFromImageSkia(*skia_rep->image());
|
UIImage* image = UIImageFromImageSkia(*skia_rep->image());
|
||||||
scoped_rep = std::make_unique<internal::ImageRepCocoaTouch>(image);
|
scoped_rep = internal::MakeImageRepCocoaTouch(image);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -477,7 +307,7 @@ UIImage* Image::ToUIImage() const {
|
|||||||
CHECK(scoped_rep);
|
CHECK(scoped_rep);
|
||||||
rep = AddRepresentation(std::move(scoped_rep));
|
rep = AddRepresentation(std::move(scoped_rep));
|
||||||
}
|
}
|
||||||
return rep->AsImageRepCocoaTouch()->image();
|
return UIImageOfImageRepCocoaTouch(rep->AsImageRepCocoaTouch());
|
||||||
}
|
}
|
||||||
#elif BUILDFLAG(IS_MAC)
|
#elif BUILDFLAG(IS_MAC)
|
||||||
NSImage* Image::ToNSImage() const {
|
NSImage* Image::ToNSImage() const {
|
||||||
@@ -491,9 +321,8 @@ NSImage* Image::ToNSImage() const {
|
|||||||
case kImageRepPNG: {
|
case kImageRepPNG: {
|
||||||
const internal::ImageRepPNG* png_rep =
|
const internal::ImageRepPNG* png_rep =
|
||||||
GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
|
GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
|
||||||
scoped_rep =
|
scoped_rep = internal::MakeImageRepCocoa(internal::NSImageFromPNG(
|
||||||
std::make_unique<internal::ImageRepCocoa>(internal::NSImageFromPNG(
|
png_rep->image_reps(), default_representation_color_space));
|
||||||
png_rep->image_reps(), default_representation_color_space));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case kImageRepSkia: {
|
case kImageRepSkia: {
|
||||||
@@ -501,7 +330,7 @@ NSImage* Image::ToNSImage() const {
|
|||||||
GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
|
GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
|
||||||
NSImage* image = NSImageFromImageSkiaWithColorSpace(*skia_rep->image(),
|
NSImage* image = NSImageFromImageSkiaWithColorSpace(*skia_rep->image(),
|
||||||
default_representation_color_space);
|
default_representation_color_space);
|
||||||
scoped_rep = std::make_unique<internal::ImageRepCocoa>(image);
|
scoped_rep = internal::MakeImageRepCocoa(image);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -510,7 +339,7 @@ NSImage* Image::ToNSImage() const {
|
|||||||
CHECK(scoped_rep);
|
CHECK(scoped_rep);
|
||||||
rep = AddRepresentation(std::move(scoped_rep));
|
rep = AddRepresentation(std::move(scoped_rep));
|
||||||
}
|
}
|
||||||
return rep->AsImageRepCocoa()->image();
|
return NSImageOfImageRepCocoa(rep->AsImageRepCocoa());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -523,9 +352,9 @@ scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const {
|
|||||||
if (rep) {
|
if (rep) {
|
||||||
const std::vector<ImagePNGRep>& image_png_reps =
|
const std::vector<ImagePNGRep>& image_png_reps =
|
||||||
rep->AsImageRepPNG()->image_reps();
|
rep->AsImageRepPNG()->image_reps();
|
||||||
for (size_t i = 0; i < image_png_reps.size(); ++i) {
|
for (const auto& image_png_rep : image_png_reps) {
|
||||||
if (image_png_reps[i].scale == 1.0f)
|
if (image_png_rep.scale == 1.0f)
|
||||||
return image_png_reps[i].raw_data;
|
return image_png_rep.raw_data;
|
||||||
}
|
}
|
||||||
return new base::RefCountedBytes();
|
return new base::RefCountedBytes();
|
||||||
}
|
}
|
||||||
@@ -537,14 +366,15 @@ scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const {
|
|||||||
const internal::ImageRepCocoaTouch* cocoa_touch_rep =
|
const internal::ImageRepCocoaTouch* cocoa_touch_rep =
|
||||||
GetRepresentation(kImageRepCocoaTouch, true)->AsImageRepCocoaTouch();
|
GetRepresentation(kImageRepCocoaTouch, true)->AsImageRepCocoaTouch();
|
||||||
png_bytes = internal::Get1xPNGBytesFromUIImage(
|
png_bytes = internal::Get1xPNGBytesFromUIImage(
|
||||||
cocoa_touch_rep->image());
|
internal::UIImageOfImageRepCocoaTouch(cocoa_touch_rep));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#elif BUILDFLAG(IS_MAC)
|
#elif BUILDFLAG(IS_MAC)
|
||||||
case kImageRepCocoa: {
|
case kImageRepCocoa: {
|
||||||
const internal::ImageRepCocoa* cocoa_rep =
|
const internal::ImageRepCocoa* cocoa_rep =
|
||||||
GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa();
|
GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa();
|
||||||
png_bytes = internal::Get1xPNGBytesFromNSImage(cocoa_rep->image());
|
png_bytes = internal::Get1xPNGBytesFromNSImage(
|
||||||
|
internal::NSImageOfImageRepCocoa(cocoa_rep));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -571,7 +401,7 @@ scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const {
|
|||||||
// final type eg (converting from ImageRepSkia to ImageRepPNG to get an
|
// final type eg (converting from ImageRepSkia to ImageRepPNG to get an
|
||||||
// ImageRepCocoa).
|
// ImageRepCocoa).
|
||||||
std::vector<ImagePNGRep> image_png_reps;
|
std::vector<ImagePNGRep> image_png_reps;
|
||||||
image_png_reps.push_back(ImagePNGRep(png_bytes, 1.0f));
|
image_png_reps.emplace_back(png_bytes, 1.0f);
|
||||||
AddRepresentation(
|
AddRepresentation(
|
||||||
base::WrapUnique(new internal::ImageRepPNG(image_png_reps)));
|
base::WrapUnique(new internal::ImageRepPNG(image_png_reps)));
|
||||||
return png_bytes;
|
return png_bytes;
|
||||||
|
129
ui/gfx/image/image_internal.h
Normal file
129
ui/gfx/image/image_internal.h
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
// Copyright 2022 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
// This holds internal declarations for the machinery of gfx::Image. These are
|
||||||
|
// only for the internal use of gfx::Image; do not use them elsewhere.
|
||||||
|
|
||||||
|
#ifndef UI_GFX_IMAGE_IMAGE_INTERNAL_H_
|
||||||
|
#define UI_GFX_IMAGE_IMAGE_INTERNAL_H_
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "base/memory/ref_counted.h"
|
||||||
|
#include "build/build_config.h"
|
||||||
|
#include "ui/gfx/image/image.h"
|
||||||
|
|
||||||
|
#if BUILDFLAG(IS_MAC)
|
||||||
|
#include <CoreGraphics/CoreGraphics.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace gfx::internal {
|
||||||
|
|
||||||
|
class ImageRepPNG;
|
||||||
|
class ImageRepSkia;
|
||||||
|
class ImageRepCocoa;
|
||||||
|
class ImageRepCocoaTouch;
|
||||||
|
|
||||||
|
// An ImageRep is the object that holds the backing memory for an Image. Each
|
||||||
|
// RepresentationType has an ImageRep subclass that is responsible for freeing
|
||||||
|
// the memory that the ImageRep holds. When an ImageRep is created, it expects
|
||||||
|
// to take ownership of the image, without having to retain it or increase its
|
||||||
|
// reference count.
|
||||||
|
class ImageRep {
|
||||||
|
public:
|
||||||
|
ImageRep(const ImageRep&) = delete;
|
||||||
|
ImageRep& operator=(const ImageRep&) = delete;
|
||||||
|
|
||||||
|
// Deletes the associated pixels of an ImageRep.
|
||||||
|
virtual ~ImageRep();
|
||||||
|
|
||||||
|
// Cast helpers ("fake RTTI").
|
||||||
|
const ImageRepPNG* AsImageRepPNG() const;
|
||||||
|
ImageRepPNG* AsImageRepPNG();
|
||||||
|
|
||||||
|
const ImageRepSkia* AsImageRepSkia() const;
|
||||||
|
ImageRepSkia* AsImageRepSkia();
|
||||||
|
|
||||||
|
#if BUILDFLAG(IS_IOS)
|
||||||
|
const ImageRepCocoaTouch* AsImageRepCocoaTouch() const;
|
||||||
|
ImageRepCocoaTouch* AsImageRepCocoaTouch();
|
||||||
|
#elif BUILDFLAG(IS_MAC)
|
||||||
|
const ImageRepCocoa* AsImageRepCocoa() const;
|
||||||
|
ImageRepCocoa* AsImageRepCocoa();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Image::RepresentationType type() const { return type_; }
|
||||||
|
|
||||||
|
virtual int Width() const = 0;
|
||||||
|
virtual int Height() const = 0;
|
||||||
|
virtual gfx::Size Size() const = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
explicit ImageRep(Image::RepresentationType rep);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Image::RepresentationType type_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// The Storage class acts similarly to the pixels in a SkBitmap: the Image
|
||||||
|
// class holds a refptr instance of Storage, which in turn holds all the
|
||||||
|
// ImageReps. This way, the Image can be cheaply copied.
|
||||||
|
//
|
||||||
|
// This class is deliberately not RefCountedThreadSafe. Making it so does not
|
||||||
|
// solve threading issues, as gfx::Image and its internal classes are
|
||||||
|
// themselves not threadsafe.
|
||||||
|
class ImageStorage : public base::RefCounted<ImageStorage> {
|
||||||
|
public:
|
||||||
|
explicit ImageStorage(Image::RepresentationType default_type);
|
||||||
|
|
||||||
|
ImageStorage(const ImageStorage&) = delete;
|
||||||
|
ImageStorage& operator=(const ImageStorage&) = delete;
|
||||||
|
|
||||||
|
Image::RepresentationType default_representation_type() const;
|
||||||
|
bool HasRepresentation(Image::RepresentationType type) const;
|
||||||
|
size_t RepresentationCount() const;
|
||||||
|
|
||||||
|
const ImageRep* GetRepresentation(Image::RepresentationType rep_type,
|
||||||
|
bool must_exist) const;
|
||||||
|
const ImageRep* AddRepresentation(std::unique_ptr<ImageRep> rep) const;
|
||||||
|
|
||||||
|
#if BUILDFLAG(IS_MAC)
|
||||||
|
void set_default_representation_color_space(CGColorSpaceRef color_space) {
|
||||||
|
DCHECK(IsOnValidSequence());
|
||||||
|
default_representation_color_space_ = color_space;
|
||||||
|
}
|
||||||
|
CGColorSpaceRef default_representation_color_space() const {
|
||||||
|
DCHECK(IsOnValidSequence());
|
||||||
|
return default_representation_color_space_;
|
||||||
|
}
|
||||||
|
#endif // BUILDFLAG(IS_MAC)
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class base::RefCounted<ImageStorage>;
|
||||||
|
|
||||||
|
~ImageStorage();
|
||||||
|
|
||||||
|
// The type of image that was passed to the constructor. This key will always
|
||||||
|
// exist in the |representations_| map.
|
||||||
|
Image::RepresentationType default_representation_type_;
|
||||||
|
|
||||||
|
#if BUILDFLAG(IS_MAC)
|
||||||
|
// The default representation's colorspace. This is used for converting to
|
||||||
|
// NSImage. This field exists to compensate for PNGCodec not writing or
|
||||||
|
// reading colorspace ancillary chunks. (sRGB, iCCP).
|
||||||
|
// Not owned.
|
||||||
|
CGColorSpaceRef default_representation_color_space_;
|
||||||
|
#endif // BUILDFLAG(IS_MAC)
|
||||||
|
|
||||||
|
// All the representations of an Image. Size will always be at least one, with
|
||||||
|
// more for any converted representations.
|
||||||
|
mutable std::map<Image::RepresentationType,
|
||||||
|
std::unique_ptr<internal::ImageRep>>
|
||||||
|
representations_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace gfx::internal
|
||||||
|
|
||||||
|
#endif // UI_GFX_IMAGE_IMAGE_INTERNAL_H_
|
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
@@ -13,13 +14,11 @@
|
|||||||
#include "base/mac/scoped_cftyperef.h"
|
#include "base/mac/scoped_cftyperef.h"
|
||||||
#include "base/mac/scoped_nsobject.h"
|
#include "base/mac/scoped_nsobject.h"
|
||||||
#include "ui/gfx/geometry/size.h"
|
#include "ui/gfx/geometry/size.h"
|
||||||
|
#include "ui/gfx/image/image_internal.h"
|
||||||
#include "ui/gfx/image/image_png_rep.h"
|
#include "ui/gfx/image/image_png_rep.h"
|
||||||
#include "ui/gfx/image/image_skia.h"
|
#include "ui/gfx/image/image_skia.h"
|
||||||
#include "ui/gfx/image/image_skia_util_ios.h"
|
#include "ui/gfx/image/image_skia_util_ios.h"
|
||||||
|
|
||||||
namespace gfx {
|
|
||||||
namespace internal {
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// Returns a 16x16 red UIImage to visually show when a UIImage cannot be
|
// Returns a 16x16 red UIImage to visually show when a UIImage cannot be
|
||||||
@@ -57,8 +56,51 @@ UIImage* CreateUIImageFromImagePNGRep(const gfx::ImagePNGRep& image_png_rep) {
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
namespace gfx {
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
class ImageRepCocoaTouch final : public ImageRep {
|
||||||
|
public:
|
||||||
|
explicit ImageRepCocoaTouch(UIImage* image)
|
||||||
|
: ImageRep(Image::kImageRepCocoaTouch),
|
||||||
|
image_(image, base::scoped_policy::RETAIN) {
|
||||||
|
CHECK(image_);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageRepCocoaTouch(const ImageRepCocoaTouch&) = delete;
|
||||||
|
ImageRepCocoaTouch& operator=(const ImageRepCocoaTouch&) = delete;
|
||||||
|
|
||||||
|
~ImageRepCocoaTouch() override { image_.reset(); }
|
||||||
|
|
||||||
|
int Width() const override { return Size().width(); }
|
||||||
|
|
||||||
|
int Height() const override { return Size().height(); }
|
||||||
|
|
||||||
|
gfx::Size Size() const override {
|
||||||
|
int width = static_cast<int>(image_.get().size.width);
|
||||||
|
int height = static_cast<int>(image_.get().size.height);
|
||||||
|
return gfx::Size(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
UIImage* image() const { return image_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
base::scoped_nsobject<UIImage> image_;
|
||||||
|
};
|
||||||
|
|
||||||
|
const ImageRepCocoaTouch* ImageRep::AsImageRepCocoaTouch() const {
|
||||||
|
CHECK_EQ(type_, Image::kImageRepCocoaTouch);
|
||||||
|
return reinterpret_cast<const ImageRepCocoaTouch*>(this);
|
||||||
|
}
|
||||||
|
ImageRepCocoaTouch* ImageRep::AsImageRepCocoaTouch() {
|
||||||
|
return const_cast<ImageRepCocoaTouch*>(
|
||||||
|
static_cast<const ImageRep*>(this)->AsImageRepCocoaTouch());
|
||||||
|
}
|
||||||
|
|
||||||
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage(
|
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage(
|
||||||
UIImage* uiimage) {
|
UIImage* uiimage) {
|
||||||
|
DCHECK(uiimage);
|
||||||
NSData* data = UIImagePNGRepresentation(uiimage);
|
NSData* data = UIImagePNGRepresentation(uiimage);
|
||||||
|
|
||||||
if ([data length] == 0)
|
if ([data length] == 0)
|
||||||
@@ -113,22 +155,32 @@ ImageSkia ImageSkiaFromPNG(
|
|||||||
// iOS does not expose libpng, so conversion from PNG to ImageSkia must go
|
// iOS does not expose libpng, so conversion from PNG to ImageSkia must go
|
||||||
// through UIImage.
|
// through UIImage.
|
||||||
ImageSkia image_skia;
|
ImageSkia image_skia;
|
||||||
for (size_t i = 0; i < image_png_reps.size(); ++i) {
|
for (const auto& image_png_rep : image_png_reps) {
|
||||||
base::scoped_nsobject<UIImage> uiimage(
|
base::scoped_nsobject<UIImage> uiimage(
|
||||||
CreateUIImageFromImagePNGRep(image_png_reps[i]));
|
CreateUIImageFromImagePNGRep(image_png_rep));
|
||||||
gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFromUIImage(
|
gfx::ImageSkiaRep image_skia_rep =
|
||||||
uiimage, image_png_reps[i].scale);
|
ImageSkiaRepOfScaleFromUIImage(uiimage, image_png_rep.scale);
|
||||||
if (!image_skia_rep.is_null())
|
if (!image_skia_rep.is_null())
|
||||||
image_skia.AddRepresentation(image_skia_rep);
|
image_skia.AddRepresentation(image_skia_rep);
|
||||||
}
|
}
|
||||||
return image_skia;
|
return image_skia;
|
||||||
}
|
}
|
||||||
|
|
||||||
gfx::Size UIImageSize(UIImage* image) {
|
UIImage* UIImageOfImageRepCocoaTouch(const ImageRepCocoaTouch* image_rep) {
|
||||||
int width = static_cast<int>(image.size.width);
|
return image_rep->image();
|
||||||
int height = static_cast<int>(image.size.height);
|
|
||||||
return gfx::Size(width, height);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
std::unique_ptr<ImageRep> MakeImageRepCocoaTouch(UIImage* image) {
|
||||||
} // namespace gfx
|
return std::make_unique<internal::ImageRepCocoaTouch>(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
Image::Image(UIImage* image) {
|
||||||
|
if (image) {
|
||||||
|
storage_ = new internal::ImageStorage(Image::kImageRepCocoaTouch);
|
||||||
|
AddRepresentation(std::make_unique<internal::ImageRepCocoaTouch>(image));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace gfx
|
||||||
|
@@ -11,11 +11,9 @@
|
|||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
#include "base/mac/scoped_nsobject.h"
|
#include "base/mac/scoped_nsobject.h"
|
||||||
#include "ui/gfx/geometry/size.h"
|
#include "ui/gfx/geometry/size.h"
|
||||||
|
#include "ui/gfx/image/image_internal.h"
|
||||||
#include "ui/gfx/image/image_png_rep.h"
|
#include "ui/gfx/image/image_png_rep.h"
|
||||||
|
|
||||||
namespace gfx {
|
|
||||||
namespace internal {
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// Returns a 16x16 red NSImage to visually show when a NSImage cannot be
|
// Returns a 16x16 red NSImage to visually show when a NSImage cannot be
|
||||||
@@ -33,10 +31,52 @@ NSImage* GetErrorNSImage() {
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
namespace gfx {
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
class ImageRepCocoa final : public ImageRep {
|
||||||
|
public:
|
||||||
|
explicit ImageRepCocoa(NSImage* image)
|
||||||
|
: ImageRep(Image::kImageRepCocoa),
|
||||||
|
image_(image, base::scoped_policy::RETAIN) {
|
||||||
|
CHECK(image_);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageRepCocoa(const ImageRepCocoa&) = delete;
|
||||||
|
ImageRepCocoa& operator=(const ImageRepCocoa&) = delete;
|
||||||
|
|
||||||
|
~ImageRepCocoa() override { image_.reset(); }
|
||||||
|
|
||||||
|
int Width() const override { return Size().width(); }
|
||||||
|
|
||||||
|
int Height() const override { return Size().height(); }
|
||||||
|
|
||||||
|
gfx::Size Size() const override {
|
||||||
|
int width = static_cast<int>(image_.get().size.width);
|
||||||
|
int height = static_cast<int>(image_.get().size.height);
|
||||||
|
return gfx::Size(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
NSImage* image() const { return image_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
base::scoped_nsobject<NSImage> image_;
|
||||||
|
};
|
||||||
|
|
||||||
|
const ImageRepCocoa* ImageRep::AsImageRepCocoa() const {
|
||||||
|
CHECK_EQ(type_, Image::kImageRepCocoa);
|
||||||
|
return reinterpret_cast<const ImageRepCocoa*>(this);
|
||||||
|
}
|
||||||
|
ImageRepCocoa* ImageRep::AsImageRepCocoa() {
|
||||||
|
return const_cast<ImageRepCocoa*>(
|
||||||
|
static_cast<const ImageRep*>(this)->AsImageRepCocoa());
|
||||||
|
}
|
||||||
|
|
||||||
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage(
|
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage(
|
||||||
NSImage* nsimage) {
|
NSImage* nsimage) {
|
||||||
DCHECK(nsimage);
|
DCHECK(nsimage);
|
||||||
CGImageRef cg_image = [nsimage CGImageForProposedRect:NULL
|
CGImageRef cg_image = [nsimage CGImageForProposedRect:nullptr
|
||||||
context:nil
|
context:nil
|
||||||
hints:nil];
|
hints:nil];
|
||||||
if (!cg_image) {
|
if (!cg_image) {
|
||||||
@@ -64,17 +104,15 @@ NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps,
|
|||||||
}
|
}
|
||||||
|
|
||||||
base::scoped_nsobject<NSImage> image;
|
base::scoped_nsobject<NSImage> image;
|
||||||
for (size_t i = 0; i < image_png_reps.size(); ++i) {
|
for (const auto& image_png_rep : image_png_reps) {
|
||||||
scoped_refptr<base::RefCountedMemory> png = image_png_reps[i].raw_data;
|
scoped_refptr<base::RefCountedMemory> png = image_png_rep.raw_data;
|
||||||
CHECK(png.get());
|
CHECK(png.get());
|
||||||
base::scoped_nsobject<NSData> ns_data(
|
base::scoped_nsobject<NSData> ns_data(
|
||||||
[[NSData alloc] initWithBytes:png->front() length:png->size()]);
|
[[NSData alloc] initWithBytes:png->front() length:png->size()]);
|
||||||
base::scoped_nsobject<NSBitmapImageRep> ns_image_rep(
|
base::scoped_nsobject<NSBitmapImageRep> ns_image_rep(
|
||||||
[[NSBitmapImageRep alloc] initWithData:ns_data]);
|
[[NSBitmapImageRep alloc] initWithData:ns_data]);
|
||||||
if (!ns_image_rep) {
|
if (!ns_image_rep) {
|
||||||
LOG(ERROR) << "Unable to decode PNG at "
|
LOG(ERROR) << "Unable to decode PNG at " << image_png_rep.scale << ".";
|
||||||
<< image_png_reps[i].scale
|
|
||||||
<< ".";
|
|
||||||
return GetErrorNSImage();
|
return GetErrorNSImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +134,7 @@ NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!image.get()) {
|
if (!image.get()) {
|
||||||
float scale = image_png_reps[i].scale;
|
float scale = image_png_rep.scale;
|
||||||
NSSize image_size = NSMakeSize([ns_image_rep pixelsWide] / scale,
|
NSSize image_size = NSMakeSize([ns_image_rep pixelsWide] / scale,
|
||||||
[ns_image_rep pixelsHigh] / scale);
|
[ns_image_rep pixelsHigh] / scale);
|
||||||
image.reset([[NSImage alloc] initWithSize:image_size]);
|
image.reset([[NSImage alloc] initWithSize:image_size]);
|
||||||
@@ -107,13 +145,21 @@ NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps,
|
|||||||
return image.autorelease();
|
return image.autorelease();
|
||||||
}
|
}
|
||||||
|
|
||||||
gfx::Size NSImageSize(NSImage* image) {
|
NSImage* NSImageOfImageRepCocoa(const ImageRepCocoa* image_rep) {
|
||||||
NSSize size = [image size];
|
return image_rep->image();
|
||||||
int width = static_cast<int>(size.width);
|
|
||||||
int height = static_cast<int>(size.height);
|
|
||||||
return gfx::Size(width, height);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
std::unique_ptr<ImageRep> MakeImageRepCocoa(NSImage* image) {
|
||||||
} // namespace gfx
|
return std::make_unique<internal::ImageRepCocoa>(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
Image::Image(NSImage* image) {
|
||||||
|
if (image) {
|
||||||
|
storage_ = new internal::ImageStorage(Image::kImageRepCocoa);
|
||||||
|
AddRepresentation(std::make_unique<internal::ImageRepCocoa>(image));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace gfx
|
||||||
|
@@ -30,27 +30,33 @@
|
|||||||
#include "ui/gfx/image/image_skia_util_mac.h"
|
#include "ui/gfx/image/image_skia_util_mac.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace gfx {
|
namespace gfx::internal {
|
||||||
namespace internal {
|
|
||||||
|
class ImageRep;
|
||||||
|
class ImageRepCocoa;
|
||||||
|
class ImageRepCocoaTouch;
|
||||||
|
|
||||||
#if BUILDFLAG(IS_IOS)
|
#if BUILDFLAG(IS_IOS)
|
||||||
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage(
|
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage(
|
||||||
UIImage* uiimage);
|
UIImage* uiimage);
|
||||||
UIImage* UIImageFromPNG(const std::vector<ImagePNGRep>& image_png_reps);
|
UIImage* UIImageFromPNG(const std::vector<ImagePNGRep>& image_png_reps);
|
||||||
gfx::Size UIImageSize(UIImage* image);
|
|
||||||
|
UIImage* UIImageOfImageRepCocoaTouch(const ImageRepCocoaTouch* image_rep);
|
||||||
|
std::unique_ptr<ImageRep> MakeImageRepCocoaTouch(UIImage* image);
|
||||||
#elif BUILDFLAG(IS_MAC)
|
#elif BUILDFLAG(IS_MAC)
|
||||||
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage(
|
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage(
|
||||||
NSImage* nsimage);
|
NSImage* nsimage);
|
||||||
NSImage* NSImageFromPNG(const std::vector<ImagePNGRep>& image_png_reps,
|
NSImage* NSImageFromPNG(const std::vector<ImagePNGRep>& image_png_reps,
|
||||||
CGColorSpaceRef color_space);
|
CGColorSpaceRef color_space);
|
||||||
gfx::Size NSImageSize(NSImage* image);
|
|
||||||
|
NSImage* NSImageOfImageRepCocoa(const ImageRepCocoa* image_rep);
|
||||||
|
std::unique_ptr<ImageRep> MakeImageRepCocoa(NSImage* image);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ImageSkia ImageSkiaFromPNG(const std::vector<ImagePNGRep>& image_png_reps);
|
ImageSkia ImageSkiaFromPNG(const std::vector<ImagePNGRep>& image_png_reps);
|
||||||
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia(
|
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia(
|
||||||
const ImageSkia* image_skia);
|
const ImageSkia* image_skia);
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace gfx::internal
|
||||||
} // namespace gfx
|
|
||||||
|
|
||||||
#endif // UI_GFX_IMAGE_IMAGE_PLATFORM_H_
|
#endif // UI_GFX_IMAGE_IMAGE_PLATFORM_H_
|
||||||
|
Reference in New Issue
Block a user