0

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:
Avi Drissman
2022-02-03 23:36:25 +00:00
committed by Chromium LUCI CQ
parent 656c5e6f85
commit cd841d36c0
9 changed files with 361 additions and 323 deletions

@ -138,10 +138,6 @@ TYPE_NAME_FOR_CF_TYPE_DECL(SecPolicy);
#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
// defaults to a reasonable string. This never returns NULL. BaseBundleID
// 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
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;
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) {
int32_t major, minor, bugfix;
base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);

@ -95,6 +95,7 @@ component("gfx") {
"image/image.h",
"image/image_family.cc",
"image/image_family.h",
"image/image_internal.h",
"image/image_platform.h",
"image/image_png_rep.cc",
"image/image_png_rep.h",

@ -15,6 +15,7 @@
#include "build/build_config.h"
#include "third_party/skia/include/core/SkBitmap.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_png_rep.h"
#include "ui/gfx/image/image_skia.h"
@ -30,82 +31,31 @@
namespace gfx {
namespace {
using RepresentationMap =
std::map<Image::RepresentationType, std::unique_ptr<internal::ImageRep>>;
} // namespace
namespace internal {
class ImageRepPNG;
class ImageRepSkia;
class ImageRepCocoa;
class ImageRepCocoaTouch;
ImageRep::ImageRep(Image::RepresentationType rep) : type_(rep) {}
// 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:
explicit ImageRep(Image::RepresentationType rep) : type_(rep) {}
ImageRep::~ImageRep() = default;
// Deletes the associated pixels of an ImageRep.
virtual ~ImageRep() {}
const ImageRepPNG* ImageRep::AsImageRepPNG() const {
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 ImageRepPNG* AsImageRepPNG() const {
CHECK_EQ(type_, Image::kImageRepPNG);
return reinterpret_cast<const ImageRepPNG*>(this);
}
ImageRepPNG* AsImageRepPNG() {
return const_cast<ImageRepPNG*>(
static_cast<const ImageRep*>(this)->AsImageRepPNG());
}
const ImageRepSkia* ImageRep::AsImageRepSkia() const {
CHECK_EQ(type_, Image::kImageRepSkia);
return reinterpret_cast<const ImageRepSkia*>(this);
}
ImageRepSkia* ImageRep::AsImageRepSkia() {
return const_cast<ImageRepSkia*>(
static_cast<const ImageRep*>(this)->AsImageRepSkia());
}
const ImageRepSkia* AsImageRepSkia() const {
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 {
class ImageRepPNG final : public ImageRep {
public:
ImageRepPNG() : ImageRep(Image::kImageRepPNG) {
}
@ -116,7 +66,7 @@ class ImageRepPNG : public ImageRep {
ImageRepPNG(const ImageRepPNG&) = delete;
ImageRepPNG& operator=(const ImageRepPNG&) = delete;
~ImageRepPNG() override {}
~ImageRepPNG() override = default;
int Width() const override { return Size().width(); }
@ -125,9 +75,9 @@ class ImageRepPNG : public ImageRep {
gfx::Size Size() const override {
// Read the PNG data to get the image size, caching it.
if (!size_cache_) {
for (auto it = image_reps().begin(); it != image_reps().end(); ++it) {
if (it->scale == 1.0f) {
size_cache_ = it->Size();
for (const auto& it : image_reps()) {
if (it.scale == 1.0f) {
size_cache_ = it.Size();
return *size_cache_;
}
}
@ -146,7 +96,7 @@ class ImageRepPNG : public ImageRep {
mutable absl::optional<gfx::Size> size_cache_;
};
class ImageRepSkia : public ImageRep {
class ImageRepSkia final : public ImageRep {
public:
explicit ImageRepSkia(ImageSkia image)
: ImageRep(Image::kImageRepSkia), image_(image) {}
@ -154,7 +104,7 @@ class ImageRepSkia : public ImageRep {
ImageRepSkia(const ImageRepSkia&) = delete;
ImageRepSkia& operator=(const ImageRepSkia&) = delete;
~ImageRepSkia() override {}
~ImageRepSkia() override = default;
int Width() const override { return image_.width(); }
@ -169,171 +119,67 @@ class ImageRepSkia : public ImageRep {
ImageSkia image_;
};
#if BUILDFLAG(IS_IOS)
class ImageRepCocoaTouch : public ImageRep {
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)
ImageStorage::ImageStorage(Image::RepresentationType default_type)
: default_representation_type_(default_type)
#if BUILDFLAG(IS_MAC)
,
default_representation_color_space_(
base::mac::GetGenericRGBColorSpace())
,
default_representation_color_space_(base::mac::GetGenericRGBColorSpace())
#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;
ImageStorage& operator=(const ImageStorage&) = delete;
const ImageRep* ImageStorage::AddRepresentation(
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 {
DCHECK(IsOnValidSequence());
return default_representation_type_;
}
// 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.";
bool HasRepresentation(Image::RepresentationType type) const {
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_;
};
return result.first->second.get();
}
} // namespace internal
Image::Image() {
// |storage_| is null for empty Images.
}
Image::Image() = default;
Image::Image(const std::vector<ImagePNGRep>& image_reps) {
// Do not store obviously invalid ImagePNGReps.
std::vector<ImagePNGRep> filtered;
for (size_t i = 0; i < image_reps.size(); ++i) {
if (image_reps[i].raw_data.get() && image_reps[i].raw_data->size())
filtered.push_back(image_reps[i]);
for (const auto& image_rep : image_reps) {
if (image_rep.raw_data.get() && image_rep.raw_data->size())
filtered.push_back(image_rep);
}
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(Image&& other) noexcept = default;
@ -374,7 +204,7 @@ Image& Image::operator=(const Image& other) = default;
Image& Image::operator=(Image&& other) noexcept = default;
Image::~Image() {}
Image::~Image() = default;
bool Image::operator==(const Image& other) const {
return storage_ == other.storage_;
@ -403,7 +233,7 @@ Image Image::CreateFrom1xPNGBytes(
return Image();
std::vector<ImagePNGRep> image_reps;
image_reps.push_back(ImagePNGRep(input, 1.0f));
image_reps.emplace_back(input, 1.0f);
return Image(image_reps);
}
@ -430,7 +260,7 @@ const ImageSkia* Image::ToImageSkia() const {
GetRepresentation(kImageRepCocoaTouch, true)
->AsImageRepCocoaTouch();
scoped_rep = std::make_unique<internal::ImageRepSkia>(
ImageSkia(ImageSkiaFromUIImage(native_rep->image())));
ImageSkiaFromUIImage(UIImageOfImageRepCocoaTouch(native_rep)));
break;
}
#elif BUILDFLAG(IS_MAC)
@ -438,7 +268,7 @@ const ImageSkia* Image::ToImageSkia() const {
const internal::ImageRepCocoa* native_rep =
GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa();
scoped_rep = std::make_unique<internal::ImageRepSkia>(
ImageSkia(ImageSkiaFromNSImage(native_rep->image())));
ImageSkiaFromNSImage(NSImageOfImageRepCocoa(native_rep)));
break;
}
#endif
@ -460,7 +290,7 @@ UIImage* Image::ToUIImage() const {
case kImageRepPNG: {
const internal::ImageRepPNG* png_rep =
GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
scoped_rep = std::make_unique<internal::ImageRepCocoaTouch>(
scoped_rep = internal::MakeImageRepCocoaTouch(
internal::UIImageFromPNG(png_rep->image_reps()));
break;
}
@ -468,7 +298,7 @@ UIImage* Image::ToUIImage() const {
const internal::ImageRepSkia* skia_rep =
GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
UIImage* image = UIImageFromImageSkia(*skia_rep->image());
scoped_rep = std::make_unique<internal::ImageRepCocoaTouch>(image);
scoped_rep = internal::MakeImageRepCocoaTouch(image);
break;
}
default:
@ -477,7 +307,7 @@ UIImage* Image::ToUIImage() const {
CHECK(scoped_rep);
rep = AddRepresentation(std::move(scoped_rep));
}
return rep->AsImageRepCocoaTouch()->image();
return UIImageOfImageRepCocoaTouch(rep->AsImageRepCocoaTouch());
}
#elif BUILDFLAG(IS_MAC)
NSImage* Image::ToNSImage() const {
@ -491,9 +321,8 @@ NSImage* Image::ToNSImage() const {
case kImageRepPNG: {
const internal::ImageRepPNG* png_rep =
GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
scoped_rep =
std::make_unique<internal::ImageRepCocoa>(internal::NSImageFromPNG(
png_rep->image_reps(), default_representation_color_space));
scoped_rep = internal::MakeImageRepCocoa(internal::NSImageFromPNG(
png_rep->image_reps(), default_representation_color_space));
break;
}
case kImageRepSkia: {
@ -501,7 +330,7 @@ NSImage* Image::ToNSImage() const {
GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
NSImage* image = NSImageFromImageSkiaWithColorSpace(*skia_rep->image(),
default_representation_color_space);
scoped_rep = std::make_unique<internal::ImageRepCocoa>(image);
scoped_rep = internal::MakeImageRepCocoa(image);
break;
}
default:
@ -510,7 +339,7 @@ NSImage* Image::ToNSImage() const {
CHECK(scoped_rep);
rep = AddRepresentation(std::move(scoped_rep));
}
return rep->AsImageRepCocoa()->image();
return NSImageOfImageRepCocoa(rep->AsImageRepCocoa());
}
#endif
@ -523,9 +352,9 @@ scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const {
if (rep) {
const std::vector<ImagePNGRep>& image_png_reps =
rep->AsImageRepPNG()->image_reps();
for (size_t i = 0; i < image_png_reps.size(); ++i) {
if (image_png_reps[i].scale == 1.0f)
return image_png_reps[i].raw_data;
for (const auto& image_png_rep : image_png_reps) {
if (image_png_rep.scale == 1.0f)
return image_png_rep.raw_data;
}
return new base::RefCountedBytes();
}
@ -537,14 +366,15 @@ scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const {
const internal::ImageRepCocoaTouch* cocoa_touch_rep =
GetRepresentation(kImageRepCocoaTouch, true)->AsImageRepCocoaTouch();
png_bytes = internal::Get1xPNGBytesFromUIImage(
cocoa_touch_rep->image());
internal::UIImageOfImageRepCocoaTouch(cocoa_touch_rep));
break;
}
#elif BUILDFLAG(IS_MAC)
case kImageRepCocoa: {
const internal::ImageRepCocoa* cocoa_rep =
GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa();
png_bytes = internal::Get1xPNGBytesFromNSImage(cocoa_rep->image());
png_bytes = internal::Get1xPNGBytesFromNSImage(
internal::NSImageOfImageRepCocoa(cocoa_rep));
break;
}
#endif
@ -571,7 +401,7 @@ scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const {
// final type eg (converting from ImageRepSkia to ImageRepPNG to get an
// ImageRepCocoa).
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(
base::WrapUnique(new internal::ImageRepPNG(image_png_reps)));
return png_bytes;

@ -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>
#import <UIKit/UIKit.h>
#include <cmath>
#include <limits>
@ -13,13 +14,11 @@
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_nsobject.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_skia.h"
#include "ui/gfx/image/image_skia_util_ios.h"
namespace gfx {
namespace internal {
namespace {
// 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 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(
UIImage* uiimage) {
DCHECK(uiimage);
NSData* data = UIImagePNGRepresentation(uiimage);
if ([data length] == 0)
@ -113,22 +155,32 @@ ImageSkia ImageSkiaFromPNG(
// iOS does not expose libpng, so conversion from PNG to ImageSkia must go
// through UIImage.
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(
CreateUIImageFromImagePNGRep(image_png_reps[i]));
gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFromUIImage(
uiimage, image_png_reps[i].scale);
CreateUIImageFromImagePNGRep(image_png_rep));
gfx::ImageSkiaRep image_skia_rep =
ImageSkiaRepOfScaleFromUIImage(uiimage, image_png_rep.scale);
if (!image_skia_rep.is_null())
image_skia.AddRepresentation(image_skia_rep);
}
return image_skia;
}
gfx::Size UIImageSize(UIImage* image) {
int width = static_cast<int>(image.size.width);
int height = static_cast<int>(image.size.height);
return gfx::Size(width, height);
UIImage* UIImageOfImageRepCocoaTouch(const ImageRepCocoaTouch* image_rep) {
return image_rep->image();
}
} // namespace internal
} // namespace gfx
std::unique_ptr<ImageRep> MakeImageRepCocoaTouch(UIImage* image) {
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/mac/scoped_nsobject.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_internal.h"
#include "ui/gfx/image/image_png_rep.h"
namespace gfx {
namespace internal {
namespace {
// Returns a 16x16 red NSImage to visually show when a NSImage cannot be
@ -33,10 +31,52 @@ NSImage* GetErrorNSImage() {
} // 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(
NSImage* nsimage) {
DCHECK(nsimage);
CGImageRef cg_image = [nsimage CGImageForProposedRect:NULL
CGImageRef cg_image = [nsimage CGImageForProposedRect:nullptr
context:nil
hints:nil];
if (!cg_image) {
@ -64,17 +104,15 @@ NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps,
}
base::scoped_nsobject<NSImage> image;
for (size_t i = 0; i < image_png_reps.size(); ++i) {
scoped_refptr<base::RefCountedMemory> png = image_png_reps[i].raw_data;
for (const auto& image_png_rep : image_png_reps) {
scoped_refptr<base::RefCountedMemory> png = image_png_rep.raw_data;
CHECK(png.get());
base::scoped_nsobject<NSData> ns_data(
[[NSData alloc] initWithBytes:png->front() length:png->size()]);
base::scoped_nsobject<NSBitmapImageRep> ns_image_rep(
[[NSBitmapImageRep alloc] initWithData:ns_data]);
if (!ns_image_rep) {
LOG(ERROR) << "Unable to decode PNG at "
<< image_png_reps[i].scale
<< ".";
LOG(ERROR) << "Unable to decode PNG at " << image_png_rep.scale << ".";
return GetErrorNSImage();
}
@ -96,7 +134,7 @@ NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps,
}
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,
[ns_image_rep pixelsHigh] / scale);
image.reset([[NSImage alloc] initWithSize:image_size]);
@ -107,13 +145,21 @@ NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps,
return image.autorelease();
}
gfx::Size NSImageSize(NSImage* image) {
NSSize size = [image size];
int width = static_cast<int>(size.width);
int height = static_cast<int>(size.height);
return gfx::Size(width, height);
NSImage* NSImageOfImageRepCocoa(const ImageRepCocoa* image_rep) {
return image_rep->image();
}
} // namespace internal
} // namespace gfx
std::unique_ptr<ImageRep> MakeImageRepCocoa(NSImage* image) {
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"
#endif
namespace gfx {
namespace internal {
namespace gfx::internal {
class ImageRep;
class ImageRepCocoa;
class ImageRepCocoaTouch;
#if BUILDFLAG(IS_IOS)
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage(
UIImage* uiimage);
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)
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage(
NSImage* nsimage);
NSImage* NSImageFromPNG(const std::vector<ImagePNGRep>& image_png_reps,
CGColorSpaceRef color_space);
gfx::Size NSImageSize(NSImage* image);
NSImage* NSImageOfImageRepCocoa(const ImageRepCocoa* image_rep);
std::unique_ptr<ImageRep> MakeImageRepCocoa(NSImage* image);
#endif
ImageSkia ImageSkiaFromPNG(const std::vector<ImagePNGRep>& image_png_reps);
scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia(
const ImageSkia* image_skia);
} // namespace internal
} // namespace gfx
} // namespace gfx::internal
#endif // UI_GFX_IMAGE_IMAGE_PLATFORM_H_