Commit 92294e78 authored by ccameron's avatar ccameron Committed by Commit bot

Color: Don't duplicate ICC profile data

Make gfx::ColorSpace have an internal pointer to a globally-unique
structure for the color space, where its ICC profile can be stored.

BUG=622133

Review-Url: https://codereview.chromium.org/2140803002
Cr-Commit-Position: refs/heads/master@{#405211}
parent 4843d09c
......@@ -2,15 +2,89 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "build/build_config.h"
#include "ui/gfx/color_space.h"
#include <map>
#include "base/synchronization/lock.h"
namespace gfx {
namespace {
static const size_t kMinProfileLength = 128;
static const size_t kMaxProfileLength = 4 * 1024 * 1024;
}
} // namespace
// The structure used to look up GlobalData structures.
struct ColorSpace::Key {
Key(ColorSpace::Type type, const std::vector<char>& icc_profile)
: type(type), icc_profile(icc_profile) {}
bool operator<(const Key& other) const {
if (type < other.type)
return true;
if (type > other.type)
return false;
if (type != Type::ICC_PROFILE)
return false;
if (icc_profile.size() < other.icc_profile.size())
return true;
if (icc_profile.size() > other.icc_profile.size())
return false;
for (size_t i = 0; i < icc_profile.size(); ++i) {
if (icc_profile[i] < other.icc_profile[i])
return true;
if (icc_profile[i] > other.icc_profile[i])
return false;
}
return false;
}
ColorSpace::Type type;
const std::vector<char> icc_profile;
};
// Because this structure is shared across gfx::ColorSpace objects on
// different threads, it needs to be thread-safe.
class ColorSpace::GlobalData
: public base::RefCountedThreadSafe<ColorSpace::GlobalData> {
public:
static void Get(const Key& key, scoped_refptr<GlobalData>* value) {
base::AutoLock lock(map_lock_);
auto insert_result = map_.insert(std::make_pair(key, nullptr));
if (insert_result.second)
insert_result.first->second = new GlobalData(key, insert_result.first);
*value = make_scoped_refptr(insert_result.first->second);
}
const std::vector<char>& GetICCProfile() const { return icc_profile_; }
private:
friend class base::RefCountedThreadSafe<GlobalData>;
GlobalData(const Key& key, std::map<Key, GlobalData*>::iterator iterator)
: iterator_(iterator) {
// TODO: Compute the ICC profile for named color spaces.
if (key.type == Type::ICC_PROFILE)
icc_profile_ = key.icc_profile;
}
~GlobalData() {
base::AutoLock lock(map_lock_);
map_.erase(iterator_);
}
std::vector<char> icc_profile_;
// In order to remove |this| from |map_| when its last reference goes away,
// keep in |iterator_| the corresponding iterator in |map_|.
std::map<Key, GlobalData*>::iterator iterator_;
static std::map<Key, GlobalData*> map_;
static base::Lock map_lock_;
};
std::map<ColorSpace::Key, ColorSpace::GlobalData*> ColorSpace::GlobalData::map_;
base::Lock ColorSpace::GlobalData::map_lock_;
ColorSpace::ColorSpace() = default;
ColorSpace::ColorSpace(ColorSpace&& other) = default;
......@@ -19,13 +93,26 @@ ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default;
ColorSpace::~ColorSpace() = default;
bool ColorSpace::operator==(const ColorSpace& other) const {
return icc_profile_ == other.icc_profile_;
if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE)
return global_data_ == other.global_data_;
return type_ == other.type_;
}
bool ColorSpace::operator<(const ColorSpace& other) const {
// Note that this does a pointer-based comparision.
if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE)
return global_data_.get() < other.global_data_.get();
return type_ < other.type_;
}
// static
ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) {
ColorSpace color_space;
if (IsValidProfileLength(icc_profile.size()))
color_space.icc_profile_ = icc_profile;
if (IsValidProfileLength(icc_profile.size())) {
color_space.type_ = Type::ICC_PROFILE;
Key key(Type::ICC_PROFILE, icc_profile);
GlobalData::Get(key, &color_space.global_data_);
}
return color_space;
}
......@@ -36,6 +123,14 @@ ColorSpace ColorSpace::FromBestMonitor() {
}
#endif
const std::vector<char>& ColorSpace::GetICCProfile() const {
if (!global_data_) {
Key key(type_, std::vector<char>());
GlobalData::Get(key, &global_data_);
}
return global_data_->GetICCProfile();
}
// static
bool ColorSpace::IsValidProfileLength(size_t length) {
return length >= kMinProfileLength && length <= kMaxProfileLength;
......
......@@ -8,6 +8,7 @@
#include <vector>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "ui/gfx/gfx_export.h"
......@@ -25,6 +26,7 @@ class GFX_EXPORT ColorSpace {
ColorSpace& operator=(const ColorSpace& other);
~ColorSpace();
bool operator==(const ColorSpace& other) const;
bool operator<(const ColorSpace& other) const;
// Returns the color profile of the monitor that can best represent color.
// This profile should be used for creating content that does not know on
......@@ -35,7 +37,7 @@ class GFX_EXPORT ColorSpace {
static ColorSpace FromCGColorSpace(CGColorSpaceRef cg_color_space);
#endif
const std::vector<char>& GetICCProfile() const { return icc_profile_; }
const std::vector<char>& GetICCProfile() const;
#if defined(OS_WIN)
// This will read monitor ICC profiles from disk and cache the results for the
......@@ -47,7 +49,20 @@ class GFX_EXPORT ColorSpace {
static bool IsValidProfileLength(size_t length);
private:
std::vector<char> icc_profile_;
struct Key;
class GlobalData;
friend struct Key;
friend class GlobalData;
enum class Type {
UNDEFINED,
ICC_PROFILE,
};
Type type_ = Type::UNDEFINED;
// GlobalData stores large or expensive-to-compute data about a color space
// (e.g, ICC profile). This structure is shared by all identical ColorSpace
// objects in the process. It is lazily initialized for named color spaces.
mutable scoped_refptr<GlobalData> global_data_;
};
} // namespace gfx
......
......@@ -59,9 +59,10 @@ bool ColorSpace::CachedProfilesNeedUpdate() {
void ColorSpace::UpdateCachedProfilesOnBackgroundThread() {
std::vector<char> icc_profile;
ReadBestMonitorICCProfile(&icc_profile);
gfx::ColorSpace color_space = FromICCProfile(icc_profile);
base::AutoLock lock(g_best_monitor_color_space_lock.Get());
g_best_monitor_color_space.Get().icc_profile_ = icc_profile;
g_best_monitor_color_space.Get() = color_space;
g_has_initialized_best_monitor_color_space = true;
}
......
......@@ -17,7 +17,6 @@ namespace gfx {
// static
ColorSpace ColorSpace::FromBestMonitor() {
ColorSpace color_space;
Atom property = XInternAtom(GetXDisplay(), "_ICC_PROFILE", true);
if (property != None) {
Atom prop_type = None;
......@@ -30,11 +29,13 @@ ColorSpace ColorSpace::FromBestMonitor() {
0x1FFFFFFF /* MAXINT32 / 4 */, False, AnyPropertyType, &prop_type,
&prop_format, &nitems, &nbytes,
reinterpret_cast<unsigned char**>(&property_data)) == Success) {
color_space.icc_profile_.assign(property_data, property_data + nitems);
std::vector<char> icc_profile;
icc_profile.assign(property_data, property_data + nitems);
XFree(property_data);
return FromICCProfile(icc_profile);
}
}
return color_space;
return ColorSpace();
}
} // namespace gfx
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment