Commit fc43a71f authored by noel's avatar noel Committed by Commit bot

ui/gfx/color_profile: Add GetDisplayColorProfile for an NSWindow

Add a GetDisplayColorProfile variant on Mac to return the display
color profile associated with a native window (NSWindow).

Add unit tests for on-screen, partially-on-screen, off-screen and
empty and null windows.

Note an sRGB color profile is a common case, especially on win32,
so GetDisplayColorProfile returns true and an empty color profile
in that case. Add a header file comment about that.

TEST=gfx_unittests --gtest_filter="ColorProfileTest*Window"
BUG=368694

Review URL: https://codereview.chromium.org/849523003

Cr-Commit-Position: refs/heads/master@{#313601}
parent c077b472
...@@ -8,8 +8,6 @@ namespace gfx { ...@@ -8,8 +8,6 @@ namespace gfx {
#if defined(OS_WIN) || defined(OS_MACOSX) #if defined(OS_WIN) || defined(OS_MACOSX)
void ReadColorProfile(std::vector<char>* profile); void ReadColorProfile(std::vector<char>* profile);
GFX_EXPORT bool GetDisplayColorProfile(const gfx::Rect& bounds,
std::vector<char>* profile);
#else #else
void ReadColorProfile(std::vector<char>* profile) { } void ReadColorProfile(std::vector<char>* profile) { }
GFX_EXPORT bool GetDisplayColorProfile(const gfx::Rect& bounds, GFX_EXPORT bool GetDisplayColorProfile(const gfx::Rect& bounds,
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/gfx/gfx_export.h" #include "ui/gfx/gfx_export.h"
#include "ui/gfx/native_widget_types.h"
namespace gfx { namespace gfx {
...@@ -42,6 +43,14 @@ inline bool InvalidColorProfileLength(size_t length) { ...@@ -42,6 +43,14 @@ inline bool InvalidColorProfileLength(size_t length) {
// standard sRGB color profile should be assumed. // standard sRGB color profile should be assumed.
GFX_EXPORT bool GetDisplayColorProfile(const gfx::Rect& bounds, GFX_EXPORT bool GetDisplayColorProfile(const gfx::Rect& bounds,
std::vector<char>* profile); std::vector<char>* profile);
#if defined(OS_MACOSX)
// Return the color profile of the native window. If the window is null, or has
// empty bounds, return false meaning there is no color profile associated with
// the window. Otherwise return true after storing the window color profile in
// |profile|, which will be empty if the sRGB color profile should be assumed.
GFX_EXPORT bool GetDisplayColorProfile(gfx::NativeWindow window,
std::vector<char>* profile);
#endif
} // namespace gfx } // namespace gfx
#endif // UI_GFX_COLOR_PROFILE_H_ #endif // UI_GFX_COLOR_PROFILE_H_
...@@ -54,6 +54,24 @@ bool GetDisplayColorProfile(const gfx::Rect& bounds, ...@@ -54,6 +54,24 @@ bool GetDisplayColorProfile(const gfx::Rect& bounds,
return true; return true;
} }
GFX_EXPORT bool GetDisplayColorProfile(gfx::NativeWindow window,
std::vector<char>* profile) {
DCHECK(profile->empty());
NSColorSpace* color_space = [window colorSpace];
if (!color_space || NSIsEmptyRect([window frame]))
return false;
if ([color_space isEqual:[NSColorSpace sRGBColorSpace]])
return true;
NSData* profile_data = [color_space ICCProfileData];
const char* data = static_cast<const char*>([profile_data bytes]);
size_t length = [profile_data length];
if (data && !gfx::InvalidColorProfileLength(length))
profile->assign(data, data + length);
return true;
}
void ReadColorProfile(std::vector<char>* profile) { void ReadColorProfile(std::vector<char>* profile) {
CGColorSpaceRef monitor_color_space(base::mac::GetSystemColorSpace()); CGColorSpaceRef monitor_color_space(base::mac::GetSystemColorSpace());
base::ScopedCFTypeRef<CFDataRef> icc_profile( base::ScopedCFTypeRef<CFDataRef> icc_profile(
......
...@@ -93,4 +93,49 @@ TEST_F(ColorProfileTest, GetDisplayColorProfileForEmptyOffScreenBounds) { ...@@ -93,4 +93,49 @@ TEST_F(ColorProfileTest, GetDisplayColorProfileForEmptyOffScreenBounds) {
EXPECT_FALSE(TestColorProfileForBounds(TestWindowBounds())); EXPECT_FALSE(TestColorProfileForBounds(TestWindowBounds()));
} }
bool TestColorProfileForWindow(NSWindow* window) {
std::vector<char> color_profile;
return gfx::GetDisplayColorProfile(window, &color_profile);
}
TEST_F(ColorProfileTest, GetDisplayColorProfileForOnScreenWindow) {
MoveTestWindowTo(gfx::Rect(10, 10, 100, 100));
EXPECT_FALSE(TestWindowBounds().IsEmpty());
EXPECT_TRUE(TestWindowContainedOnScreen());
EXPECT_TRUE(TestColorProfileForWindow(test_window()));
}
TEST_F(ColorProfileTest, GetDisplayColorProfileForPartiallyOnScreenWindow) {
MoveTestWindowTo(gfx::Rect(-50, -50, 80, 80));
EXPECT_FALSE(TestWindowBounds().IsEmpty());
EXPECT_TRUE(TestWindowOnScreen());
EXPECT_TRUE(TestColorProfileForWindow(test_window()));
}
TEST_F(ColorProfileTest, GetDisplayColorProfileForOffScreenWindow) {
MoveTestWindowTo(gfx::Rect(-100, -100, 10, 10));
EXPECT_FALSE(TestWindowBounds().IsEmpty());
EXPECT_FALSE(TestWindowOnScreen());
EXPECT_TRUE(TestColorProfileForWindow(test_window()));
}
TEST_F(ColorProfileTest, GetDisplayColorProfileForEmptyOnScreenWindow) {
MoveTestWindowTo(gfx::Rect(10, 10, 0, 0));
EXPECT_TRUE(TestWindowBounds().IsEmpty());
EXPECT_FALSE(TestWindowOnScreen());
EXPECT_FALSE(TestColorProfileForWindow(test_window()));
}
TEST_F(ColorProfileTest, GetDisplayColorProfileForEmptyOffScreenWindow) {
MoveTestWindowTo(gfx::Rect(-100, -100, 0, 0));
EXPECT_TRUE(TestWindowBounds().IsEmpty());
EXPECT_FALSE(TestWindowOnScreen());
EXPECT_FALSE(TestColorProfileForWindow(test_window()));
}
TEST_F(ColorProfileTest, GetDisplayColorProfileForNullWindow) {
EXPECT_FALSE(TestColorProfileForWindow(nullptr));
EXPECT_FALSE(TestColorProfileForWindow(nil));
}
} // namespace } // namespace
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