summaryrefslogtreecommitdiffstats
path: root/gfx
diff options
context:
space:
mode:
Diffstat (limited to 'gfx')
-rw-r--r--gfx/color_utils.cc285
-rw-r--r--gfx/color_utils.h77
-rw-r--r--gfx/color_utils_unittest.cc49
-rw-r--r--gfx/favicon_size.h33
-rw-r--r--gfx/gdi_util.cc79
-rw-r--r--gfx/gdi_util.h37
-rw-r--r--gfx/gfx.gyp18
-rw-r--r--gfx/native_theme_win.cc710
-rw-r--r--gfx/native_theme_win.h296
-rw-r--r--gfx/native_theme_win_unittest.cc11
-rw-r--r--gfx/skbitmap_operations.cc356
-rw-r--r--gfx/skbitmap_operations.h93
-rw-r--r--gfx/skbitmap_operations_unittest.cc383
-rw-r--r--gfx/skia_util.cc42
-rw-r--r--gfx/skia_util.h34
-rw-r--r--gfx/skia_utils_gtk.cc32
-rw-r--r--gfx/skia_utils_gtk.h22
17 files changed, 2556 insertions, 1 deletions
diff --git a/gfx/color_utils.cc b/gfx/color_utils.cc
new file mode 100644
index 0000000..268f556
--- /dev/null
+++ b/gfx/color_utils.cc
@@ -0,0 +1,285 @@
+// Copyright (c) 2009 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.
+
+#include "gfx/color_utils.h"
+
+#include <math.h>
+#if defined(OS_WIN)
+#include <windows.h>
+#endif
+
+#include <algorithm>
+
+#include "base/basictypes.h"
+#include "base/logging.h"
+#include "build/build_config.h"
+#if defined(OS_WIN)
+#include "skia/ext/skia_utils_win.h"
+#endif
+#include "third_party/skia/include/core/SkBitmap.h"
+
+namespace color_utils {
+
+// Helper functions -----------------------------------------------------------
+
+namespace {
+
+double calcHue(double temp1, double temp2, double hue) {
+ if (hue < 0.0)
+ ++hue;
+ else if (hue > 1.0)
+ --hue;
+
+ if (hue * 6.0 < 1.0)
+ return temp1 + (temp2 - temp1) * hue * 6.0;
+ if (hue * 2.0 < 1.0)
+ return temp2;
+ if (hue * 3.0 < 2.0)
+ return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hue) * 6.0;
+
+ return temp1;
+}
+
+int GetLumaForColor(SkColor* color) {
+ int luma = static_cast<int>((0.3 * SkColorGetR(*color)) +
+ (0.59 * SkColorGetG(*color)) +
+ (0.11 * SkColorGetB(*color)));
+ return std::max(std::min(luma, 255), 0);
+}
+
+// Next two functions' formulas from:
+// http://www.w3.org/TR/WCAG20/#relativeluminancedef
+// http://www.w3.org/TR/WCAG20/#contrast-ratiodef
+
+double ConvertSRGB(double eight_bit_component) {
+ const double component = eight_bit_component / 255.0;
+ return (component <= 0.03928) ?
+ (component / 12.92) : pow((component + 0.055) / 1.055, 2.4);
+}
+
+SkColor LumaInvertColor(const SkColor& color) {
+ HSL hsl;
+ SkColorToHSL(color, &hsl);
+ hsl.l = 1.0 - hsl.l;
+ return HSLToSkColor(hsl, 255);
+}
+
+double ContrastRatio(double foreground_luminance, double background_luminance) {
+ // NOTE: Only pass in numbers obtained from RelativeLuminance(), since those
+ // are guaranteed to be > 0 and thus not cause a divide-by-zero error here.
+ return (foreground_luminance > background_luminance) ?
+ (foreground_luminance / background_luminance) :
+ (background_luminance / foreground_luminance);
+}
+
+} // namespace
+
+// ----------------------------------------------------------------------------
+
+double RelativeLuminance(SkColor color) {
+ return (0.2126 * ConvertSRGB(SkColorGetR(color))) +
+ (0.7152 * ConvertSRGB(SkColorGetG(color))) +
+ (0.0722 * ConvertSRGB(SkColorGetB(color))) + 0.05;
+}
+
+void SkColorToHSL(SkColor c, HSL* hsl) {
+ double r = static_cast<double>(SkColorGetR(c)) / 255.0;
+ double g = static_cast<double>(SkColorGetG(c)) / 255.0;
+ double b = static_cast<double>(SkColorGetB(c)) / 255.0;
+ double vmax = std::max(std::max(r, g), b);
+ double vmin = std::min(std::min(r, g), b);
+ double delta = vmax - vmin;
+ hsl->l = (vmax + vmin) / 2;
+ if (delta) {
+ double dr = (((vmax - r) / 6.0) + (delta / 2.0)) / delta;
+ double dg = (((vmax - g) / 6.0) + (delta / 2.0)) / delta;
+ double db = (((vmax - b) / 6.0) + (delta / 2.0)) / delta;
+ // We need to compare for the max value because comparing vmax to r,
+ // g or b can sometimes result in values overflowing registers.
+ if (r >= g && r >= b)
+ hsl->h = db - dg;
+ else if (g >= r && g >= b)
+ hsl->h = (1.0 / 3.0) + dr - db;
+ else // (b >= r && b >= g)
+ hsl->h = (2.0 / 3.0) + dg - dr;
+
+ if (hsl->h < 0.0)
+ ++hsl->h;
+ else if (hsl->h > 1.0)
+ --hsl->h;
+
+ hsl->s = delta / ((hsl->l < 0.5) ? (vmax + vmin) : (2 - vmax - vmin));
+ } else {
+ hsl->h = hsl->s = 0;
+ }
+}
+
+SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha) {
+ double hue = hsl.h;
+ double saturation = hsl.s;
+ double lightness = hsl.l;
+
+ // If there's no color, we don't care about hue and can do everything based
+ // on brightness.
+ if (!saturation) {
+ uint8 light;
+
+ if (lightness < 0)
+ light = 0;
+ else if (lightness >= 1.0)
+ light = 255;
+ else
+ light = SkDoubleToFixed(lightness) >> 8;
+
+ return SkColorSetARGB(alpha, light, light, light);
+ }
+
+ double temp2 = (lightness < 0.5) ?
+ (lightness * (1.0 + saturation)) :
+ (lightness + saturation - (lightness * saturation));
+ double temp1 = 2.0 * lightness - temp2;
+ return SkColorSetARGB(alpha,
+ static_cast<int>(calcHue(temp1, temp2, hue + 1.0 / 3.0) * 255),
+ static_cast<int>(calcHue(temp1, temp2, hue) * 255),
+ static_cast<int>(calcHue(temp1, temp2, hue - 1.0 / 3.0) * 255));
+}
+
+SkColor HSLShift(SkColor color, const HSL& shift) {
+ HSL hsl;
+ int alpha = SkColorGetA(color);
+ SkColorToHSL(color, &hsl);
+
+ // Replace the hue with the tint's hue.
+ if (shift.h >= 0)
+ hsl.h = shift.h;
+
+ // Change the saturation.
+ if (shift.s >= 0) {
+ if (shift.s <= 0.5)
+ hsl.s *= shift.s * 2.0;
+ else
+ hsl.s += (1.0 - hsl.s) * ((shift.s - 0.5) * 2.0);
+ }
+
+ SkColor result = HSLToSkColor(hsl, alpha);
+
+ if (shift.l < 0)
+ return result;
+
+ // Lightness shifts in the style of popular image editors aren't
+ // actually represented in HSL - the L value does have some effect
+ // on saturation.
+ double r = static_cast<double>(SkColorGetR(result));
+ double g = static_cast<double>(SkColorGetG(result));
+ double b = static_cast<double>(SkColorGetB(result));
+ if (shift.l <= 0.5) {
+ r *= (shift.l * 2.0);
+ g *= (shift.l * 2.0);
+ b *= (shift.l * 2.0);
+ } else {
+ r += (255.0 - r) * ((shift.l - 0.5) * 2.0);
+ g += (255.0 - g) * ((shift.l - 0.5) * 2.0);
+ b += (255.0 - b) * ((shift.l - 0.5) * 2.0);
+ }
+ return SkColorSetARGB(alpha,
+ static_cast<int>(r),
+ static_cast<int>(g),
+ static_cast<int>(b));
+}
+
+bool IsColorCloseToTransparent(SkAlpha alpha) {
+ const int kCloseToBoundary = 64;
+ return alpha < kCloseToBoundary;
+}
+
+bool IsColorCloseToGrey(int r, int g, int b) {
+ const int kAverageBoundary = 15;
+ int average = (r + g + b) / 3;
+ return (abs(r - average) < kAverageBoundary) &&
+ (abs(g - average) < kAverageBoundary) &&
+ (abs(b - average) < kAverageBoundary);
+}
+
+SkColor GetAverageColorOfFavicon(SkBitmap* favicon, SkAlpha alpha) {
+ int r = 0, g = 0, b = 0;
+
+ SkAutoLockPixels favicon_lock(*favicon);
+ SkColor* pixels = static_cast<SkColor*>(favicon->getPixels());
+ // Assume ARGB_8888 format.
+ DCHECK(favicon->getConfig() == SkBitmap::kARGB_8888_Config);
+ SkColor* current_color = pixels;
+
+ DCHECK(favicon->width() <= 16 && favicon->height() <= 16);
+
+ int pixel_count = favicon->width() * favicon->height();
+ int color_count = 0;
+ for (int i = 0; i < pixel_count; ++i, ++current_color) {
+ // Disregard this color if it is close to black, close to white, or close
+ // to transparent since any of those pixels do not contribute much to the
+ // color makeup of this icon.
+ int cr = SkColorGetR(*current_color);
+ int cg = SkColorGetG(*current_color);
+ int cb = SkColorGetB(*current_color);
+
+ if (IsColorCloseToTransparent(SkColorGetA(*current_color)) ||
+ IsColorCloseToGrey(cr, cg, cb))
+ continue;
+
+ r += cr;
+ g += cg;
+ b += cb;
+ ++color_count;
+ }
+
+ return color_count ?
+ SkColorSetARGB(alpha, r / color_count, g / color_count, b / color_count) :
+ SkColorSetARGB(alpha, 0, 0, 0);
+}
+
+void BuildLumaHistogram(SkBitmap* bitmap, int histogram[256]) {
+ SkAutoLockPixels bitmap_lock(*bitmap);
+ // Assume ARGB_8888 format.
+ DCHECK(bitmap->getConfig() == SkBitmap::kARGB_8888_Config);
+
+ int pixel_width = bitmap->width();
+ int pixel_height = bitmap->height();
+ for (int y = 0; y < pixel_height; ++y) {
+ SkColor* current_color = static_cast<uint32_t*>(bitmap->getAddr32(0, y));
+ for (int x = 0; x < pixel_width; ++x, ++current_color)
+ histogram[GetLumaForColor(current_color)]++;
+ }
+}
+
+SkColor AlphaBlend(SkColor foreground, SkColor background, SkAlpha alpha) {
+ if (alpha == 0)
+ return background;
+ if (alpha == 255)
+ return foreground;
+ return SkColorSetRGB(
+ ((SkColorGetR(foreground) * alpha) +
+ (SkColorGetR(background) * (255 - alpha))) / 255,
+ ((SkColorGetG(foreground) * alpha) +
+ (SkColorGetG(background) * (255 - alpha))) / 255,
+ ((SkColorGetB(foreground) * alpha) +
+ (SkColorGetB(background) * (255 - alpha))) / 255);
+}
+
+SkColor GetReadableColor(SkColor foreground, SkColor background) {
+ const SkColor foreground2 = LumaInvertColor(foreground);
+ const double background_luminance = RelativeLuminance(background);
+ return (ContrastRatio(RelativeLuminance(foreground), background_luminance) >=
+ ContrastRatio(RelativeLuminance(foreground2), background_luminance)) ?
+ foreground : foreground2;
+}
+
+SkColor GetSysSkColor(int which) {
+#if defined(OS_WIN)
+ return skia::COLORREFToSkColor(GetSysColor(which));
+#else
+ NOTIMPLEMENTED();
+ return SK_ColorLTGRAY;
+#endif
+}
+
+} // namespace color_utils
diff --git a/gfx/color_utils.h b/gfx/color_utils.h
new file mode 100644
index 0000000..3e2f8bf
--- /dev/null
+++ b/gfx/color_utils.h
@@ -0,0 +1,77 @@
+// Copyright (c) 2009 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.
+
+#ifndef APP_GFX_COLOR_UTILS_H_
+#define APP_GFX_COLOR_UTILS_H_
+
+#include "third_party/skia/include/core/SkColor.h"
+
+class SkBitmap;
+
+namespace color_utils {
+
+// Represents an HSL color.
+struct HSL {
+ double h;
+ double s;
+ double l;
+};
+
+// Calculated according to http://www.w3.org/TR/WCAG20/#relativeluminancedef
+double RelativeLuminance(SkColor color);
+
+// Note: these transformations assume sRGB as the source color space
+void SkColorToHSL(SkColor c, HSL* hsl);
+SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha);
+
+// HSL-Shift an SkColor. The shift values are in the range of 0-1, with the
+// option to specify -1 for 'no change'. The shift values are defined as:
+// hsl_shift[0] (hue): The absolute hue value - 0 and 1 map
+// to 0 and 360 on the hue color wheel (red).
+// hsl_shift[1] (saturation): A saturation shift, with the
+// following key values:
+// 0 = remove all color.
+// 0.5 = leave unchanged.
+// 1 = fully saturate the image.
+// hsl_shift[2] (lightness): A lightness shift, with the
+// following key values:
+// 0 = remove all lightness (make all pixels black).
+// 0.5 = leave unchanged.
+// 1 = full lightness (make all pixels white).
+SkColor HSLShift(SkColor color, const HSL& shift);
+
+// Determine if a given alpha value is nearly completely transparent.
+bool IsColorCloseToTransparent(SkAlpha alpha);
+
+// Determine if a color is near grey.
+bool IsColorCloseToGrey(int r, int g, int b);
+
+// Gets a color representing a bitmap. The definition of "representing" is the
+// average color in the bitmap. The color returned is modified to have the
+// specified alpha.
+SkColor GetAverageColorOfFavicon(SkBitmap* bitmap, SkAlpha alpha);
+
+// Builds a histogram based on the Y' of the Y'UV representation of
+// this image.
+void BuildLumaHistogram(SkBitmap* bitmap, int histogram[256]);
+
+// Returns a blend of the supplied colors, ranging from |background| (for
+// |alpha| == 0) to |foreground| (for |alpha| == 255).
+SkColor AlphaBlend(SkColor foreground, SkColor background, SkAlpha alpha);
+
+// Given a foreground and background color, try to return a foreground color
+// that is "readable" over the background color by luma-inverting the foreground
+// color and then picking whichever foreground color has higher contrast against
+// the background color.
+//
+// NOTE: This won't do anything but waste time if the supplied foreground color
+// has a luma value close to the midpoint (0.5 in the HSL representation).
+SkColor GetReadableColor(SkColor foreground, SkColor background);
+
+// Gets a Windows system color as a SkColor
+SkColor GetSysSkColor(int which);
+
+} // namespace color_utils
+
+#endif // APP_GFX_COLOR_UTILS_H_
diff --git a/gfx/color_utils_unittest.cc b/gfx/color_utils_unittest.cc
new file mode 100644
index 0000000..363d700
--- /dev/null
+++ b/gfx/color_utils_unittest.cc
@@ -0,0 +1,49 @@
+// Copyright (c) 2006-2008 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.
+
+#include <stdlib.h>
+
+#include "gfx/color_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkColorPriv.h"
+
+TEST(ColorUtils, SkColorToHSLRed) {
+ color_utils::HSL hsl = { 0, 0, 0 };
+ color_utils::SkColorToHSL(SK_ColorRED, &hsl);
+ EXPECT_EQ(hsl.h, 0);
+ EXPECT_EQ(hsl.s, 1);
+ EXPECT_EQ(hsl.l, 0.5);
+}
+
+TEST(ColorUtils, SkColorToHSLGrey) {
+ color_utils::HSL hsl = { 0, 0, 0 };
+ color_utils::SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl);
+ EXPECT_EQ(hsl.h, 0);
+ EXPECT_EQ(hsl.s, 0);
+ EXPECT_EQ(static_cast<int>(hsl.l * 100),
+ static_cast<int>(0.5 * 100)); // Accurate to two decimal places.
+}
+
+TEST(ColorUtils, HSLToSkColorWithAlpha) {
+ SkColor red = SkColorSetARGB(128, 255, 0, 0);
+ color_utils::HSL hsl = { 0, 1, 0.5 };
+ SkColor result = color_utils::HSLToSkColor(hsl, 128);
+ EXPECT_EQ(SkColorGetA(red), SkColorGetA(result));
+ EXPECT_EQ(SkColorGetR(red), SkColorGetR(result));
+ EXPECT_EQ(SkColorGetG(red), SkColorGetG(result));
+ EXPECT_EQ(SkColorGetB(red), SkColorGetB(result));
+}
+
+TEST(ColorUtils, ColorToHSLRegisterSpill) {
+ // In a opt build on Linux, this was causing a register spill on my laptop
+ // (Pentium M) when converting from SkColor to HSL.
+ SkColor input = SkColorSetARGB(255, 206, 154, 89);
+ color_utils::HSL hsl = { -1, -1, -1 };
+ SkColor result = color_utils::HSLShift(input, hsl);
+ EXPECT_EQ(255U, SkColorGetA(result));
+ EXPECT_EQ(206U, SkColorGetR(result));
+ EXPECT_EQ(153U, SkColorGetG(result));
+ EXPECT_EQ(88U, SkColorGetB(result));
+}
diff --git a/gfx/favicon_size.h b/gfx/favicon_size.h
new file mode 100644
index 0000000..3fe9cd8
--- /dev/null
+++ b/gfx/favicon_size.h
@@ -0,0 +1,33 @@
+// Copyright (c) 2006-2008 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.
+
+#ifndef APP_GFX_FAVICON_SIZE_H_
+#define APP_GFX_FAVICON_SIZE_H_
+
+#include "base/compiler_specific.h"
+
+// Size (along each axis) of the favicon.
+const int kFavIconSize = 16;
+
+// If the width or height is bigger than the favicon size, a new width/height
+// is calculated and returned in width/height that maintains the aspect
+// ratio of the supplied values.
+static void calc_favicon_target_size(int* width, int* height) ALLOW_UNUSED;
+
+// static
+void calc_favicon_target_size(int* width, int* height) {
+ if (*width > kFavIconSize || *height > kFavIconSize) {
+ // Too big, resize it maintaining the aspect ratio.
+ float aspect_ratio = static_cast<float>(*width) /
+ static_cast<float>(*height);
+ *height = kFavIconSize;
+ *width = static_cast<int>(aspect_ratio * *height);
+ if (*width > kFavIconSize) {
+ *width = kFavIconSize;
+ *height = static_cast<int>(*width / aspect_ratio);
+ }
+ }
+}
+
+#endif // APP_GFX_FAVICON_SIZE_H_
diff --git a/gfx/gdi_util.cc b/gfx/gdi_util.cc
new file mode 100644
index 0000000..5dbb5b5
--- /dev/null
+++ b/gfx/gdi_util.cc
@@ -0,0 +1,79 @@
+// Copyright (c) 2009 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.
+
+#include "gfx/gdi_util.h"
+
+namespace gfx {
+
+void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) {
+ CreateBitmapHeaderWithColorDepth(width, height, 32, hdr);
+}
+
+void CreateBitmapHeaderWithColorDepth(int width, int height, int color_depth,
+ BITMAPINFOHEADER* hdr) {
+ // These values are shared with gfx::PlatformDevice
+ hdr->biSize = sizeof(BITMAPINFOHEADER);
+ hdr->biWidth = width;
+ hdr->biHeight = -height; // minus means top-down bitmap
+ hdr->biPlanes = 1;
+ hdr->biBitCount = color_depth;
+ hdr->biCompression = BI_RGB; // no compression
+ hdr->biSizeImage = 0;
+ hdr->biXPelsPerMeter = 1;
+ hdr->biYPelsPerMeter = 1;
+ hdr->biClrUsed = 0;
+ hdr->biClrImportant = 0;
+}
+
+
+void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr) {
+ // Because bmp v4 header is just an extension, we just create a v3 header and
+ // copy the bits over to the v4 header.
+ BITMAPINFOHEADER header_v3;
+ CreateBitmapHeader(width, height, &header_v3);
+ memset(hdr, 0, sizeof(BITMAPV4HEADER));
+ memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER));
+
+ // Correct the size of the header and fill in the mask values.
+ hdr->bV4Size = sizeof(BITMAPV4HEADER);
+ hdr->bV4RedMask = 0x00ff0000;
+ hdr->bV4GreenMask = 0x0000ff00;
+ hdr->bV4BlueMask = 0x000000ff;
+ hdr->bV4AlphaMask = 0xff000000;
+}
+
+// Creates a monochrome bitmap header.
+void CreateMonochromeBitmapHeader(int width,
+ int height,
+ BITMAPINFOHEADER* hdr) {
+ hdr->biSize = sizeof(BITMAPINFOHEADER);
+ hdr->biWidth = width;
+ hdr->biHeight = -height;
+ hdr->biPlanes = 1;
+ hdr->biBitCount = 1;
+ hdr->biCompression = BI_RGB;
+ hdr->biSizeImage = 0;
+ hdr->biXPelsPerMeter = 1;
+ hdr->biYPelsPerMeter = 1;
+ hdr->biClrUsed = 0;
+ hdr->biClrImportant = 0;
+}
+
+void SubtractRectanglesFromRegion(HRGN hrgn,
+ const std::vector<gfx::Rect>& cutouts) {
+ if (cutouts.size()) {
+ HRGN cutout = ::CreateRectRgn(0, 0, 0, 0);
+ for (size_t i = 0; i < cutouts.size(); i++) {
+ ::SetRectRgn(cutout,
+ cutouts[i].x(),
+ cutouts[i].y(),
+ cutouts[i].right(),
+ cutouts[i].bottom());
+ ::CombineRgn(hrgn, hrgn, cutout, RGN_DIFF);
+ }
+ ::DeleteObject(cutout);
+ }
+}
+
+} // namespace gfx
diff --git a/gfx/gdi_util.h b/gfx/gdi_util.h
new file mode 100644
index 0000000..7da4926
--- /dev/null
+++ b/gfx/gdi_util.h
@@ -0,0 +1,37 @@
+// Copyright (c) 2009 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.
+
+#ifndef APP_GFX_GDI_UTIL_H_
+#define APP_GFX_GDI_UTIL_H_
+
+#include <vector>
+#include <windows.h>
+
+#include "gfx/rect.h"
+
+namespace gfx {
+
+// Creates a BITMAPINFOHEADER structure given the bitmap's size.
+void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr);
+
+// Creates a BITMAPINFOHEADER structure given the bitmap's size and
+// color depth in bits per pixel.
+void CreateBitmapHeaderWithColorDepth(int width, int height, int color_depth,
+ BITMAPINFOHEADER* hdr);
+
+// Creates a BITMAPV4HEADER structure given the bitmap's size. You probably
+// only need to use BMP V4 if you need transparency (alpha channel). This
+// function sets the AlphaMask to 0xff000000.
+void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr);
+
+// Creates a monochrome bitmap header.
+void CreateMonochromeBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr);
+
+// Modify the given hrgn by subtracting the given rectangles.
+void SubtractRectanglesFromRegion(HRGN hrgn,
+ const std::vector<gfx::Rect>& cutouts);
+
+} // namespace gfx
+
+#endif // APP_GFX_GDI_UTIL_H_
diff --git a/gfx/gfx.gyp b/gfx/gfx.gyp
index 0aabe26..8a33525 100644
--- a/gfx/gfx.gyp
+++ b/gfx/gfx.gyp
@@ -19,9 +19,11 @@
'sources': [
'codec/jpeg_codec_unittest.cc',
'codec/png_codec_unittest.cc',
+ 'color_utils_unittest.cc',
'insets_unittest.cc',
'rect_unittest.cc',
'run_all_unittests.cc',
+ 'skbitmap_operations_unittest.cc',
'test_suite.h',
],
'include_dirs': [
@@ -31,6 +33,7 @@
['OS=="win"', {
'sources': [
'icon_util_unittest.cc',
+ 'native_theme_win_unittest.cc',
],
}],
['OS=="linux" or OS=="freebsd" or OS=="openbsd"', {
@@ -62,12 +65,14 @@
'codec/jpeg_codec.h',
'codec/png_codec.cc',
'codec/png_codec.h',
+ 'color_utils.cc',
+ 'color_utils.h',
+ 'favicon_size.h',
'gfx_paths.cc',
'gfx_paths.h',
'insets.cc',
'insets.h',
'native_widget_types.h',
- 'native_widget_types_gtk.cc',
'path.cc',
'path.h',
'path_gtk.cc',
@@ -80,12 +85,22 @@
'scrollbar_size.h',
'size.cc',
'size.h',
+ 'skbitmap_operations.cc',
+ 'skbitmap_operations.h',
+ 'skia_util.cc',
+ 'skia_util.h',
+ 'skia_utils_gtk.cc',
+ 'skia_utils_gtk.h',
],
'conditions': [
['OS=="win"', {
'sources': [
+ 'gdi_util.cc',
+ 'gdi_util.h',
'icon_util.cc',
'icon_util.h',
+ 'native_theme_win.cc',
+ 'native_theme_win.h',
],
}],
['OS=="linux" or OS=="freebsd" or OS=="openbsd"', {
@@ -94,6 +109,7 @@
'gtk_native_view_id_manager.h',
'gtk_util.cc',
'gtk_util.h',
+ 'native_widget_types_gtk.cc',
],
}],
],
diff --git a/gfx/native_theme_win.cc b/gfx/native_theme_win.cc
new file mode 100644
index 0000000..87024d3
--- /dev/null
+++ b/gfx/native_theme_win.cc
@@ -0,0 +1,710 @@
+// Copyright (c) 2009 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.
+
+#include "gfx/native_theme_win.h"
+
+#include <windows.h>
+#include <uxtheme.h>
+#include <vsstyle.h>
+#include <vssym32.h>
+
+#include "base/logging.h"
+#include "base/scoped_handle.h"
+#include "gfx/gdi_util.h"
+#include "gfx/rect.h"
+#include "skia/ext/platform_canvas.h"
+#include "skia/ext/skia_utils_win.h"
+#include "third_party/skia/include/core/SkShader.h"
+
+namespace {
+
+void SetCheckerboardShader(SkPaint* paint, const RECT& align_rect) {
+ // Create a 2x2 checkerboard pattern using the 3D face and highlight colors.
+ SkColor face = skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE));
+ SkColor highlight = skia::COLORREFToSkColor(GetSysColor(COLOR_3DHILIGHT));
+ SkColor buffer[] = { face, highlight, highlight, face };
+ // Confusing bit: we first create a temporary bitmap with our desired pattern,
+ // then copy it to another bitmap. The temporary bitmap doesn't take
+ // ownership of the pixel data, and so will point to garbage when this
+ // function returns. The copy will copy the pixel data into a place owned by
+ // the bitmap, which is in turn owned by the shader, etc., so it will live
+ // until we're done using it.
+ SkBitmap temp_bitmap;
+ temp_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
+ temp_bitmap.setPixels(buffer);
+ SkBitmap bitmap;
+ temp_bitmap.copyTo(&bitmap, temp_bitmap.config());
+ SkShader* shader = SkShader::CreateBitmapShader(bitmap,
+ SkShader::kRepeat_TileMode,
+ SkShader::kRepeat_TileMode);
+
+ // Align the pattern with the upper corner of |align_rect|.
+ SkMatrix matrix;
+ matrix.setTranslate(SkIntToScalar(align_rect.left),
+ SkIntToScalar(align_rect.top));
+ shader->setLocalMatrix(matrix);
+ paint->setShader(shader)->safeUnref();
+}
+
+} // namespace
+
+namespace gfx {
+
+/* static */
+const NativeTheme* NativeTheme::instance() {
+ // The global NativeTheme instance.
+ static const NativeTheme s_native_theme;
+ return &s_native_theme;
+}
+
+NativeTheme::NativeTheme()
+ : theme_dll_(LoadLibrary(L"uxtheme.dll")),
+ draw_theme_(NULL),
+ draw_theme_ex_(NULL),
+ get_theme_color_(NULL),
+ get_theme_content_rect_(NULL),
+ get_theme_part_size_(NULL),
+ open_theme_(NULL),
+ close_theme_(NULL),
+ set_theme_properties_(NULL),
+ is_theme_active_(NULL),
+ get_theme_int_(NULL) {
+ if (theme_dll_) {
+ draw_theme_ = reinterpret_cast<DrawThemeBackgroundPtr>(
+ GetProcAddress(theme_dll_, "DrawThemeBackground"));
+ draw_theme_ex_ = reinterpret_cast<DrawThemeBackgroundExPtr>(
+ GetProcAddress(theme_dll_, "DrawThemeBackgroundEx"));
+ get_theme_color_ = reinterpret_cast<GetThemeColorPtr>(
+ GetProcAddress(theme_dll_, "GetThemeColor"));
+ get_theme_content_rect_ = reinterpret_cast<GetThemeContentRectPtr>(
+ GetProcAddress(theme_dll_, "GetThemeBackgroundContentRect"));
+ get_theme_part_size_ = reinterpret_cast<GetThemePartSizePtr>(
+ GetProcAddress(theme_dll_, "GetThemePartSize"));
+ open_theme_ = reinterpret_cast<OpenThemeDataPtr>(
+ GetProcAddress(theme_dll_, "OpenThemeData"));
+ close_theme_ = reinterpret_cast<CloseThemeDataPtr>(
+ GetProcAddress(theme_dll_, "CloseThemeData"));
+ set_theme_properties_ = reinterpret_cast<SetThemeAppPropertiesPtr>(
+ GetProcAddress(theme_dll_, "SetThemeAppProperties"));
+ is_theme_active_ = reinterpret_cast<IsThemeActivePtr>(
+ GetProcAddress(theme_dll_, "IsThemeActive"));
+ get_theme_int_ = reinterpret_cast<GetThemeIntPtr>(
+ GetProcAddress(theme_dll_, "GetThemeInt"));
+ }
+ memset(theme_handles_, 0, sizeof(theme_handles_));
+}
+
+NativeTheme::~NativeTheme() {
+ if (theme_dll_) {
+ // todo (cpu): fix this soon.
+ // CloseHandles();
+ FreeLibrary(theme_dll_);
+ }
+}
+
+HRESULT NativeTheme::PaintButton(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(BUTTON);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+
+ // Draw it manually.
+ // All pressed states have both low bits set, and no other states do.
+ const bool focused = ((state_id & ETS_FOCUSED) == ETS_FOCUSED);
+ const bool pressed = ((state_id & PBS_PRESSED) == PBS_PRESSED);
+ if ((BP_PUSHBUTTON == part_id) && (pressed || focused)) {
+ // BP_PUSHBUTTON has a focus rect drawn around the outer edge, and the
+ // button itself is shrunk by 1 pixel.
+ HBRUSH brush = GetSysColorBrush(COLOR_3DDKSHADOW);
+ if (brush) {
+ FrameRect(hdc, rect, brush);
+ InflateRect(rect, -1, -1);
+ }
+ }
+ DrawFrameControl(hdc, rect, DFC_BUTTON, classic_state);
+
+ // Draw the focus rectangle (the dotted line box) only on buttons. For radio
+ // and checkboxes, we let webkit draw the focus rectangle (orange glow).
+ if ((BP_PUSHBUTTON == part_id) && focused) {
+ // The focus rect is inside the button. The exact number of pixels depends
+ // on whether we're in classic mode or using uxtheme.
+ if (handle && get_theme_content_rect_) {
+ get_theme_content_rect_(handle, hdc, part_id, state_id, rect, rect);
+ } else {
+ InflateRect(rect, -GetSystemMetrics(SM_CXEDGE),
+ -GetSystemMetrics(SM_CYEDGE));
+ }
+ DrawFocusRect(hdc, rect);
+ }
+
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintDialogBackground(HDC hdc, bool active,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(WINDOW);
+ if (handle && draw_theme_) {
+ return draw_theme_(handle, hdc, WP_DIALOG,
+ active ? FS_ACTIVE : FS_INACTIVE, rect, NULL);
+ }
+
+ // Classic just renders a flat color background.
+ FillRect(hdc, rect, reinterpret_cast<HBRUSH>(COLOR_3DFACE + 1));
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintListBackground(HDC hdc,
+ bool enabled,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(LIST);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, 1, TS_NORMAL, rect, NULL);
+
+ // Draw it manually.
+ HBRUSH bg_brush = GetSysColorBrush(COLOR_WINDOW);
+ FillRect(hdc, rect, bg_brush);
+ DrawEdge(hdc, rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintMenuArrow(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect,
+ MenuArrowDirection arrow_direction,
+ bool is_highlighted) const {
+ HANDLE handle = GetThemeHandle(MENU);
+ if (handle && draw_theme_) {
+ if (arrow_direction == RIGHT_POINTING_ARROW) {
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+ } else {
+ // There is no way to tell the uxtheme API to draw a left pointing arrow;
+ // it doesn't have a flag equivalent to DFCS_MENUARROWRIGHT. But they
+ // are needed for RTL locales on Vista. So use a memory DC and mirror
+ // the region with GDI's StretchBlt.
+ Rect r(*rect);
+ ScopedHDC mem_dc(CreateCompatibleDC(hdc));
+ ScopedBitmap mem_bitmap(CreateCompatibleBitmap(hdc, r.width(),
+ r.height()));
+ HGDIOBJ old_bitmap = SelectObject(mem_dc, mem_bitmap);
+ // Copy and horizontally mirror the background from hdc into mem_dc. Use
+ // a negative-width source rect, starting at the rightmost pixel.
+ StretchBlt(mem_dc, 0, 0, r.width(), r.height(),
+ hdc, r.right()-1, r.y(), -r.width(), r.height(), SRCCOPY);
+ // Draw the arrow.
+ RECT theme_rect = {0, 0, r.width(), r.height()};
+ HRESULT result = draw_theme_(handle, mem_dc, part_id,
+ state_id, &theme_rect, NULL);
+ // Copy and mirror the result back into mem_dc.
+ StretchBlt(hdc, r.x(), r.y(), r.width(), r.height(),
+ mem_dc, r.width()-1, 0, -r.width(), r.height(), SRCCOPY);
+ SelectObject(mem_dc, old_bitmap);
+ return result;
+ }
+ }
+
+ // For some reason, Windows uses the name DFCS_MENUARROWRIGHT to indicate a
+ // left pointing arrow. This makes the following 'if' statement slightly
+ // counterintuitive.
+ UINT state;
+ if (arrow_direction == RIGHT_POINTING_ARROW)
+ state = DFCS_MENUARROW;
+ else
+ state = DFCS_MENUARROWRIGHT;
+ return PaintFrameControl(hdc, rect, DFC_MENU, state, is_highlighted);
+}
+
+HRESULT NativeTheme::PaintMenuBackground(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(MENU);
+ if (handle && draw_theme_) {
+ HRESULT result = draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+ FrameRect(hdc, rect, GetSysColorBrush(COLOR_3DSHADOW));
+ return result;
+ }
+
+ FillRect(hdc, rect, GetSysColorBrush(COLOR_MENU));
+ DrawEdge(hdc, rect, EDGE_RAISED, BF_RECT);
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintMenuCheckBackground(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(MENU);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+ // Nothing to do for background.
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintMenuCheck(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect,
+ bool is_highlighted) const {
+ HANDLE handle = GetThemeHandle(MENU);
+ if (handle && draw_theme_) {
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+ }
+ return PaintFrameControl(hdc, rect, DFC_MENU, DFCS_MENUCHECK, is_highlighted);
+}
+
+HRESULT NativeTheme::PaintMenuGutter(HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(MENU);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+ return E_NOTIMPL;
+}
+
+HRESULT NativeTheme::PaintMenuItemBackground(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ bool selected,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(MENU);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+ if (selected)
+ FillRect(hdc, rect, GetSysColorBrush(COLOR_HIGHLIGHT));
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintMenuList(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(MENULIST);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+
+ // Draw it manually.
+ DrawFrameControl(hdc, rect, DFC_SCROLL, DFCS_SCROLLCOMBOBOX | classic_state);
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintMenuSeparator(HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(MENU);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+ DrawEdge(hdc, rect, EDGE_ETCHED, BF_TOP);
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintScrollbarArrow(HDC hdc,
+ int state_id,
+ int classic_state,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(SCROLLBAR);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, SBP_ARROWBTN, state_id, rect, NULL);
+
+ // Draw it manually.
+ DrawFrameControl(hdc, rect, DFC_SCROLL, classic_state);
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintScrollbarTrack(
+ HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* target_rect,
+ RECT* align_rect,
+ skia::PlatformCanvas* canvas) const {
+ HANDLE handle = GetThemeHandle(SCROLLBAR);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, target_rect, NULL);
+
+ // Draw it manually.
+ const DWORD colorScrollbar = GetSysColor(COLOR_SCROLLBAR);
+ const DWORD color3DFace = GetSysColor(COLOR_3DFACE);
+ if ((colorScrollbar != color3DFace) &&
+ (colorScrollbar != GetSysColor(COLOR_WINDOW))) {
+ FillRect(hdc, target_rect, reinterpret_cast<HBRUSH>(COLOR_SCROLLBAR + 1));
+ } else {
+ SkPaint paint;
+ SetCheckerboardShader(&paint, *align_rect);
+ canvas->drawIRect(skia::RECTToSkIRect(*target_rect), paint);
+ }
+ if (classic_state & DFCS_PUSHED)
+ InvertRect(hdc, target_rect);
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintScrollbarThumb(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(SCROLLBAR);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+
+ // Draw it manually.
+ if ((part_id == SBP_THUMBBTNHORZ) || (part_id == SBP_THUMBBTNVERT))
+ DrawEdge(hdc, rect, EDGE_RAISED, BF_RECT | BF_MIDDLE);
+ // Classic mode doesn't have a gripper.
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintStatusGripper(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect) const {
+ HANDLE handle = GetThemeHandle(STATUS);
+ if (handle && draw_theme_) {
+ // Paint the status bar gripper. There doesn't seem to be a
+ // standard gripper in Windows for the space between
+ // scrollbars. This is pretty close, but it's supposed to be
+ // painted over a status bar.
+ return draw_theme_(handle, hdc, SP_GRIPPER, 0, rect, NULL);
+ }
+
+ // Draw a windows classic scrollbar gripper.
+ DrawFrameControl(hdc, rect, DFC_SCROLL, DFCS_SCROLLSIZEGRIP);
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintTabPanelBackground(HDC hdc, RECT* rect) const {
+ HANDLE handle = GetThemeHandle(TAB);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, TABP_BODY, 0, rect, NULL);
+
+ // Classic just renders a flat color background.
+ FillRect(hdc, rect, reinterpret_cast<HBRUSH>(COLOR_3DFACE + 1));
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintTrackbar(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect,
+ skia::PlatformCanvas* canvas) const {
+ // Make the channel be 4 px thick in the center of the supplied rect. (4 px
+ // matches what XP does in various menus; GetThemePartSize() doesn't seem to
+ // return good values here.)
+ RECT channel_rect = *rect;
+ const int channel_thickness = 4;
+ if (part_id == TKP_TRACK) {
+ channel_rect.top +=
+ ((channel_rect.bottom - channel_rect.top - channel_thickness) / 2);
+ channel_rect.bottom = channel_rect.top + channel_thickness;
+ } else if (part_id == TKP_TRACKVERT) {
+ channel_rect.left +=
+ ((channel_rect.right - channel_rect.left - channel_thickness) / 2);
+ channel_rect.right = channel_rect.left + channel_thickness;
+ } // else this isn't actually a channel, so |channel_rect| == |rect|.
+
+ HANDLE handle = GetThemeHandle(TRACKBAR);
+ if (handle && draw_theme_)
+ return draw_theme_(handle, hdc, part_id, state_id, &channel_rect, NULL);
+
+ // Classic mode, draw it manually.
+ if ((part_id == TKP_TRACK) || (part_id == TKP_TRACKVERT)) {
+ DrawEdge(hdc, &channel_rect, EDGE_SUNKEN, BF_RECT);
+ } else if (part_id == TKP_THUMBVERT) {
+ DrawEdge(hdc, rect, EDGE_RAISED, BF_RECT | BF_SOFT | BF_MIDDLE);
+ } else {
+ // Split rect into top and bottom pieces.
+ RECT top_section = *rect;
+ RECT bottom_section = *rect;
+ top_section.bottom -= ((bottom_section.right - bottom_section.left) / 2);
+ bottom_section.top = top_section.bottom;
+ DrawEdge(hdc, &top_section, EDGE_RAISED,
+ BF_LEFT | BF_TOP | BF_RIGHT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
+
+ // Split triangular piece into two diagonals.
+ RECT& left_half = bottom_section;
+ RECT right_half = bottom_section;
+ right_half.left += ((bottom_section.right - bottom_section.left) / 2);
+ left_half.right = right_half.left;
+ DrawEdge(hdc, &left_half, EDGE_RAISED,
+ BF_DIAGONAL_ENDTOPLEFT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
+ DrawEdge(hdc, &right_half, EDGE_RAISED,
+ BF_DIAGONAL_ENDBOTTOMLEFT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
+
+ // If the button is pressed, draw hatching.
+ if (classic_state & DFCS_PUSHED) {
+ SkPaint paint;
+ SetCheckerboardShader(&paint, *rect);
+
+ // Fill all three pieces with the pattern.
+ canvas->drawIRect(skia::RECTToSkIRect(top_section), paint);
+
+ SkScalar left_triangle_top = SkIntToScalar(left_half.top);
+ SkScalar left_triangle_right = SkIntToScalar(left_half.right);
+ SkPath left_triangle;
+ left_triangle.moveTo(SkIntToScalar(left_half.left), left_triangle_top);
+ left_triangle.lineTo(left_triangle_right, left_triangle_top);
+ left_triangle.lineTo(left_triangle_right,
+ SkIntToScalar(left_half.bottom));
+ left_triangle.close();
+ canvas->drawPath(left_triangle, paint);
+
+ SkScalar right_triangle_left = SkIntToScalar(right_half.left);
+ SkScalar right_triangle_top = SkIntToScalar(right_half.top);
+ SkPath right_triangle;
+ right_triangle.moveTo(right_triangle_left, right_triangle_top);
+ right_triangle.lineTo(SkIntToScalar(right_half.right),
+ right_triangle_top);
+ right_triangle.lineTo(right_triangle_left,
+ SkIntToScalar(right_half.bottom));
+ right_triangle.close();
+ canvas->drawPath(right_triangle, paint);
+ }
+ }
+ return S_OK;
+}
+
+HRESULT NativeTheme::PaintTextField(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect,
+ COLORREF color,
+ bool fill_content_area,
+ bool draw_edges) const {
+ // TODO(ojan): http://b/1210017 Figure out how to give the ability to
+ // exclude individual edges from being drawn.
+
+ HANDLE handle = GetThemeHandle(TEXTFIELD);
+ // TODO(mpcomplete): can we detect if the color is specified by the user,
+ // and if not, just use the system color?
+ // CreateSolidBrush() accepts a RGB value but alpha must be 0.
+ HBRUSH bg_brush = CreateSolidBrush(color);
+ HRESULT hr;
+ // DrawThemeBackgroundEx was introduced in XP SP2, so that it's possible
+ // draw_theme_ex_ is NULL and draw_theme_ is non-null.
+ if (handle && (draw_theme_ex_ || (draw_theme_ && draw_edges))) {
+ if (draw_theme_ex_) {
+ static DTBGOPTS omit_border_options = {
+ sizeof(DTBGOPTS),
+ DTBG_OMITBORDER,
+ {0,0,0,0}
+ };
+ DTBGOPTS* draw_opts = draw_edges ? NULL : &omit_border_options;
+ hr = draw_theme_ex_(handle, hdc, part_id, state_id, rect, draw_opts);
+ } else {
+ hr = draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
+ }
+
+ // TODO(maruel): Need to be fixed if get_theme_content_rect_ is NULL.
+ if (fill_content_area && get_theme_content_rect_) {
+ RECT content_rect;
+ hr = get_theme_content_rect_(handle, hdc, part_id, state_id, rect,
+ &content_rect);
+ FillRect(hdc, &content_rect, bg_brush);
+ }
+ } else {
+ // Draw it manually.
+ if (draw_edges)
+ DrawEdge(hdc, rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
+
+ if (fill_content_area) {
+ FillRect(hdc, rect, (classic_state & DFCS_INACTIVE) ?
+ reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1) : bg_brush);
+ }
+ hr = S_OK;
+ }
+ DeleteObject(bg_brush);
+ return hr;
+}
+
+bool NativeTheme::IsThemingActive() const {
+ if (is_theme_active_)
+ return !!is_theme_active_();
+ return false;
+}
+
+HRESULT NativeTheme::GetThemePartSize(ThemeName theme_name,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect,
+ int ts,
+ SIZE* size) const {
+ HANDLE handle = GetThemeHandle(theme_name);
+ if (handle && get_theme_part_size_)
+ return get_theme_part_size_(handle, hdc, part_id, state_id, rect, ts, size);
+
+ return E_NOTIMPL;
+}
+
+HRESULT NativeTheme::GetThemeColor(ThemeName theme,
+ int part_id,
+ int state_id,
+ int prop_id,
+ SkColor* color) const {
+ HANDLE handle = GetThemeHandle(theme);
+ if (handle && get_theme_color_) {
+ COLORREF color_ref;
+ if (get_theme_color_(handle, part_id, state_id, prop_id, &color_ref) ==
+ S_OK) {
+ *color = skia::COLORREFToSkColor(color_ref);
+ return S_OK;
+ }
+ }
+ return E_NOTIMPL;
+}
+
+SkColor NativeTheme::GetThemeColorWithDefault(ThemeName theme,
+ int part_id,
+ int state_id,
+ int prop_id,
+ int default_sys_color) const {
+ SkColor color;
+ if (GetThemeColor(theme, part_id, state_id, prop_id, &color) != S_OK)
+ color = skia::COLORREFToSkColor(GetSysColor(default_sys_color));
+ return color;
+}
+
+HRESULT NativeTheme::GetThemeInt(ThemeName theme,
+ int part_id,
+ int state_id,
+ int prop_id,
+ int *value) const {
+ HANDLE handle = GetThemeHandle(theme);
+ if (handle && get_theme_int_)
+ return get_theme_int_(handle, part_id, state_id, prop_id, value);
+ return E_NOTIMPL;
+}
+
+Size NativeTheme::GetThemeBorderSize(ThemeName theme) const {
+ // For simplicity use the wildcard state==0, part==0, since it works
+ // for the cases we currently depend on.
+ int border;
+ if (GetThemeInt(theme, 0, 0, TMT_BORDERSIZE, &border) == S_OK)
+ return Size(border, border);
+ else
+ return Size(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE));
+}
+
+
+void NativeTheme::DisableTheming() const {
+ if (!set_theme_properties_)
+ return;
+ set_theme_properties_(0);
+}
+
+HRESULT NativeTheme::PaintFrameControl(HDC hdc,
+ RECT* rect,
+ UINT type,
+ UINT state,
+ bool is_highlighted) const {
+ const int width = rect->right - rect->left;
+ const int height = rect->bottom - rect->top;
+
+ // DrawFrameControl for menu arrow/check wants a monochrome bitmap.
+ ScopedBitmap mask_bitmap(CreateBitmap(width, height, 1, 1, NULL));
+
+ if (mask_bitmap == NULL)
+ return E_OUTOFMEMORY;
+
+ ScopedHDC bitmap_dc(CreateCompatibleDC(NULL));
+ HGDIOBJ org_bitmap = SelectObject(bitmap_dc, mask_bitmap);
+ RECT local_rect = { 0, 0, width, height };
+ DrawFrameControl(bitmap_dc, &local_rect, type, state);
+
+ // We're going to use BitBlt with a b&w mask. This results in using the dest
+ // dc's text color for the black bits in the mask, and the dest dc's
+ // background color for the white bits in the mask. DrawFrameControl draws the
+ // check in black, and the background in white.
+ COLORREF old_bg_color =
+ SetBkColor(hdc,
+ GetSysColor(is_highlighted ? COLOR_HIGHLIGHT : COLOR_MENU));
+ COLORREF old_text_color =
+ SetTextColor(hdc,
+ GetSysColor(is_highlighted ? COLOR_HIGHLIGHTTEXT :
+ COLOR_MENUTEXT));
+ BitBlt(hdc, rect->left, rect->top, width, height, bitmap_dc, 0, 0, SRCCOPY);
+ SetBkColor(hdc, old_bg_color);
+ SetTextColor(hdc, old_text_color);
+
+ SelectObject(bitmap_dc, org_bitmap);
+
+ return S_OK;
+}
+
+void NativeTheme::CloseHandles() const
+{
+ if (!close_theme_)
+ return;
+
+ for (int i = 0; i < LAST; ++i) {
+ if (theme_handles_[i])
+ close_theme_(theme_handles_[i]);
+ theme_handles_[i] = NULL;
+ }
+}
+
+HANDLE NativeTheme::GetThemeHandle(ThemeName theme_name) const
+{
+ if (!open_theme_ || theme_name < 0 || theme_name >= LAST)
+ return 0;
+
+ if (theme_handles_[theme_name])
+ return theme_handles_[theme_name];
+
+ // Not found, try to load it.
+ HANDLE handle = 0;
+ switch (theme_name) {
+ case BUTTON:
+ handle = open_theme_(NULL, L"Button");
+ break;
+ case LIST:
+ handle = open_theme_(NULL, L"Listview");
+ break;
+ case MENU:
+ handle = open_theme_(NULL, L"Menu");
+ break;
+ case MENULIST:
+ handle = open_theme_(NULL, L"Combobox");
+ break;
+ case SCROLLBAR:
+ handle = open_theme_(NULL, L"Scrollbar");
+ break;
+ case STATUS:
+ handle = open_theme_(NULL, L"Status");
+ break;
+ case TAB:
+ handle = open_theme_(NULL, L"Tab");
+ break;
+ case TEXTFIELD:
+ handle = open_theme_(NULL, L"Edit");
+ break;
+ case TRACKBAR:
+ handle = open_theme_(NULL, L"Trackbar");
+ break;
+ case WINDOW:
+ handle = open_theme_(NULL, L"Window");
+ break;
+ default:
+ NOTREACHED();
+ }
+ theme_handles_[theme_name] = handle;
+ return handle;
+}
+
+} // namespace gfx
diff --git a/gfx/native_theme_win.h b/gfx/native_theme_win.h
new file mode 100644
index 0000000..504ce46
--- /dev/null
+++ b/gfx/native_theme_win.h
@@ -0,0 +1,296 @@
+// Copyright (c) 2009 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.
+//
+// A wrapper class for working with custom XP/Vista themes provided in
+// uxtheme.dll. This is a singleton class that can be grabbed using
+// NativeTheme::instance().
+// For more information on visual style parts and states, see:
+// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/userex/topics/partsandstates.asp
+
+#ifndef APP_GFX_NATIVE_THEME_WIN_H_
+#define APP_GFX_NATIVE_THEME_WIN_H_
+
+#include <windows.h>
+#include <uxtheme.h>
+#include "base/basictypes.h"
+#include "gfx/size.h"
+#include "third_party/skia/include/core/SkColor.h"
+
+namespace skia {
+class PlatformCanvas;
+} // namespace skia
+
+namespace gfx {
+
+// TODO: Define class member enums to replace part_id and state_id parameters
+// that are currently defined in <vssym32.h>. Afterward, classic_state should
+// be removed and class users wouldn't need to include <vssym32.h> anymore.
+// This would enable HOT state on non-themed UI (like when RDP'ing) and would
+// simplify usage.
+// TODO: This class should probably be changed to be platform independent at
+// the same time.
+class NativeTheme {
+ public:
+ enum ThemeName {
+ BUTTON,
+ LIST,
+ MENU,
+ MENULIST,
+ SCROLLBAR,
+ STATUS,
+ TAB,
+ TEXTFIELD,
+ TRACKBAR,
+ WINDOW,
+ LAST
+ };
+
+ // This enumeration is used within PaintMenuArrow in order to indicate the
+ // direction the menu arrow should point to.
+ enum MenuArrowDirection {
+ LEFT_POINTING_ARROW,
+ RIGHT_POINTING_ARROW
+ };
+
+ typedef HRESULT (WINAPI* DrawThemeBackgroundPtr)(HANDLE theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ const RECT* rect,
+ const RECT* clip_rect);
+ typedef HRESULT (WINAPI* DrawThemeBackgroundExPtr)(HANDLE theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ const RECT* rect,
+ const DTBGOPTS* opts);
+ typedef HRESULT (WINAPI* GetThemeColorPtr)(HANDLE hTheme,
+ int part_id,
+ int state_id,
+ int prop_id,
+ COLORREF* color);
+ typedef HRESULT (WINAPI* GetThemeContentRectPtr)(HANDLE hTheme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ const RECT* rect,
+ RECT* content_rect);
+ typedef HRESULT (WINAPI* GetThemePartSizePtr)(HANDLE hTheme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect,
+ int ts,
+ SIZE* size);
+ typedef HANDLE (WINAPI* OpenThemeDataPtr)(HWND window,
+ LPCWSTR class_list);
+ typedef HRESULT (WINAPI* CloseThemeDataPtr)(HANDLE theme);
+
+ typedef void (WINAPI* SetThemeAppPropertiesPtr) (DWORD flags);
+ typedef BOOL (WINAPI* IsThemeActivePtr)();
+ typedef HRESULT (WINAPI* GetThemeIntPtr)(HANDLE hTheme,
+ int part_id,
+ int state_id,
+ int prop_id,
+ int *value);
+
+ HRESULT PaintButton(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect) const;
+
+ HRESULT PaintDialogBackground(HDC dc, bool active, RECT* rect) const;
+
+ HRESULT PaintListBackground(HDC dc, bool enabled, RECT* rect) const;
+
+ // |arrow_direction| determines whether the arrow is pointing to the left or
+ // to the right. In RTL locales, sub-menus open from right to left and
+ // therefore the menu arrow should point to the left and not to the right.
+ HRESULT PaintMenuArrow(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect,
+ MenuArrowDirection arrow_direction,
+ bool is_highlighted) const;
+
+ HRESULT PaintMenuBackground(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect) const;
+
+ HRESULT PaintMenuCheck(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect,
+ bool is_highlighted) const;
+
+ HRESULT PaintMenuCheckBackground(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect) const;
+
+ HRESULT PaintMenuGutter(HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect) const;
+
+ HRESULT PaintMenuItemBackground(ThemeName theme,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ bool selected,
+ RECT* rect) const;
+
+ HRESULT PaintMenuList(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect) const;
+
+ HRESULT PaintMenuSeparator(HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect) const;
+
+ // Paints a scrollbar arrow. |classic_state| should have the appropriate
+ // classic part number ORed in already.
+ HRESULT PaintScrollbarArrow(HDC hdc,
+ int state_id,
+ int classic_state,
+ RECT* rect) const;
+
+ // Paints a scrollbar track section. |align_rect| is only used in classic
+ // mode, and makes sure the checkerboard pattern in |target_rect| is aligned
+ // with one presumed to be in |align_rect|.
+ HRESULT PaintScrollbarTrack(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* target_rect,
+ RECT* align_rect,
+ skia::PlatformCanvas* canvas) const;
+
+ // Paints a scrollbar thumb or gripper.
+ HRESULT PaintScrollbarThumb(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect) const;
+
+ HRESULT PaintStatusGripper(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect) const;
+
+ HRESULT PaintTabPanelBackground(HDC dc, RECT* rect) const;
+
+ HRESULT PaintTextField(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect,
+ COLORREF color,
+ bool fill_content_area,
+ bool draw_edges) const;
+
+ HRESULT PaintTrackbar(HDC hdc,
+ int part_id,
+ int state_id,
+ int classic_state,
+ RECT* rect,
+ skia::PlatformCanvas* canvas) const;
+
+ bool IsThemingActive() const;
+
+ HRESULT GetThemePartSize(ThemeName themeName,
+ HDC hdc,
+ int part_id,
+ int state_id,
+ RECT* rect,
+ int ts,
+ SIZE* size) const;
+
+ HRESULT GetThemeColor(ThemeName theme,
+ int part_id,
+ int state_id,
+ int prop_id,
+ SkColor* color) const;
+
+ // Get the theme color if theming is enabled. If theming is unsupported
+ // for this part, use Win32's GetSysColor to find the color specified
+ // by default_sys_color.
+ SkColor GetThemeColorWithDefault(ThemeName theme,
+ int part_id,
+ int state_id,
+ int prop_id,
+ int default_sys_color) const;
+
+ HRESULT GetThemeInt(ThemeName theme,
+ int part_id,
+ int state_id,
+ int prop_id,
+ int *result) const;
+
+ // Get the thickness of the border associated with the specified theme,
+ // defaulting to GetSystemMetrics edge size if themes are disabled.
+ // In Classic Windows, borders are typically 2px; on XP+, they are 1px.
+ Size GetThemeBorderSize(ThemeName theme) const;
+
+ // Disables all theming for top-level windows in the entire process, from
+ // when this method is called until the process exits. All the other
+ // methods in this class will continue to work, but their output will ignore
+ // the user's theme. This is meant for use when running tests that require
+ // consistent visual results.
+ void DisableTheming() const;
+
+ // Closes cached theme handles so we can unload the DLL or update our UI
+ // for a theme change.
+ void CloseHandles() const;
+
+ // Gets our singleton instance.
+ static const NativeTheme* instance();
+
+ private:
+ NativeTheme();
+ ~NativeTheme();
+
+ HRESULT PaintFrameControl(HDC hdc,
+ RECT* rect,
+ UINT type,
+ UINT state,
+ bool is_highlighted) const;
+
+ // Returns a handle to the theme data.
+ HANDLE GetThemeHandle(ThemeName theme_name) const;
+
+ // Function pointers into uxtheme.dll.
+ DrawThemeBackgroundPtr draw_theme_;
+ DrawThemeBackgroundExPtr draw_theme_ex_;
+ GetThemeColorPtr get_theme_color_;
+ GetThemeContentRectPtr get_theme_content_rect_;
+ GetThemePartSizePtr get_theme_part_size_;
+ OpenThemeDataPtr open_theme_;
+ CloseThemeDataPtr close_theme_;
+ SetThemeAppPropertiesPtr set_theme_properties_;
+ IsThemeActivePtr is_theme_active_;
+ GetThemeIntPtr get_theme_int_;
+
+ // Handle to uxtheme.dll.
+ HMODULE theme_dll_;
+
+ // A cache of open theme handles.
+ mutable HANDLE theme_handles_[LAST];
+
+ DISALLOW_COPY_AND_ASSIGN(NativeTheme);
+};
+
+} // namespace gfx
+
+#endif // APP_GFX_NATIVE_THEME_WIN_H_
diff --git a/gfx/native_theme_win_unittest.cc b/gfx/native_theme_win_unittest.cc
new file mode 100644
index 0000000..b087da6
--- /dev/null
+++ b/gfx/native_theme_win_unittest.cc
@@ -0,0 +1,11 @@
+// Copyright (c) 2009 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.
+
+#include "gfx/native_theme_win.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(NativeThemeTest, Init) {
+ ASSERT_TRUE(gfx::NativeTheme::instance() != NULL);
+}
diff --git a/gfx/skbitmap_operations.cc b/gfx/skbitmap_operations.cc
new file mode 100644
index 0000000..5ce8ebd
--- /dev/null
+++ b/gfx/skbitmap_operations.cc
@@ -0,0 +1,356 @@
+// Copyright (c) 2009 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.
+
+#include "gfx/skbitmap_operations.h"
+
+#include <algorithm>
+
+#include "base/logging.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkColorPriv.h"
+#include "third_party/skia/include/core/SkUnPreMultiply.h"
+
+// static
+SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) {
+ DCHECK(image.config() == SkBitmap::kARGB_8888_Config);
+
+ SkAutoLockPixels lock_image(image);
+
+ SkBitmap inverted;
+ inverted.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height(),
+ 0);
+ inverted.allocPixels();
+ inverted.eraseARGB(0, 0, 0, 0);
+
+ for (int y = 0; y < image.height(); ++y) {
+ uint32* image_row = image.getAddr32(0, y);
+ uint32* dst_row = inverted.getAddr32(0, y);
+
+ for (int x = 0; x < image.width(); ++x) {
+ uint32 image_pixel = image_row[x];
+ dst_row[x] = (image_pixel & 0xFF000000) |
+ (0x00FFFFFF - (image_pixel & 0x00FFFFFF));
+ }
+ }
+
+ return inverted;
+}
+
+// static
+SkBitmap SkBitmapOperations::CreateSuperimposedBitmap(const SkBitmap& first,
+ const SkBitmap& second) {
+ DCHECK(first.width() == second.width());
+ DCHECK(first.height() == second.height());
+ DCHECK(first.bytesPerPixel() == second.bytesPerPixel());
+ DCHECK(first.config() == SkBitmap::kARGB_8888_Config);
+
+ SkAutoLockPixels lock_first(first);
+ SkAutoLockPixels lock_second(second);
+
+ SkBitmap superimposed;
+ superimposed.setConfig(SkBitmap::kARGB_8888_Config,
+ first.width(), first.height());
+ superimposed.allocPixels();
+ superimposed.eraseARGB(0, 0, 0, 0);
+
+ SkCanvas canvas(superimposed);
+
+ SkRect rect;
+ rect.fLeft = 0;
+ rect.fTop = 0;
+ rect.fRight = SkIntToScalar(first.width());
+ rect.fBottom = SkIntToScalar(first.height());
+
+ canvas.drawBitmapRect(first, NULL, rect);
+ canvas.drawBitmapRect(second, NULL, rect);
+
+ return superimposed;
+}
+
+// static
+SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first,
+ const SkBitmap& second,
+ double alpha) {
+ DCHECK((alpha >= 0) && (alpha <= 1));
+ DCHECK(first.width() == second.width());
+ DCHECK(first.height() == second.height());
+ DCHECK(first.bytesPerPixel() == second.bytesPerPixel());
+ DCHECK(first.config() == SkBitmap::kARGB_8888_Config);
+
+ // Optimize for case where we won't need to blend anything.
+ static const double alpha_min = 1.0 / 255;
+ static const double alpha_max = 254.0 / 255;
+ if (alpha < alpha_min)
+ return first;
+ else if (alpha > alpha_max)
+ return second;
+
+ SkAutoLockPixels lock_first(first);
+ SkAutoLockPixels lock_second(second);
+
+ SkBitmap blended;
+ blended.setConfig(SkBitmap::kARGB_8888_Config, first.width(), first.height(),
+ 0);
+ blended.allocPixels();
+ blended.eraseARGB(0, 0, 0, 0);
+
+ double first_alpha = 1 - alpha;
+
+ for (int y = 0; y < first.height(); ++y) {
+ uint32* first_row = first.getAddr32(0, y);
+ uint32* second_row = second.getAddr32(0, y);
+ uint32* dst_row = blended.getAddr32(0, y);
+
+ for (int x = 0; x < first.width(); ++x) {
+ uint32 first_pixel = first_row[x];
+ uint32 second_pixel = second_row[x];
+
+ int a = static_cast<int>((SkColorGetA(first_pixel) * first_alpha) +
+ (SkColorGetA(second_pixel) * alpha));
+ int r = static_cast<int>((SkColorGetR(first_pixel) * first_alpha) +
+ (SkColorGetR(second_pixel) * alpha));
+ int g = static_cast<int>((SkColorGetG(first_pixel) * first_alpha) +
+ (SkColorGetG(second_pixel) * alpha));
+ int b = static_cast<int>((SkColorGetB(first_pixel) * first_alpha) +
+ (SkColorGetB(second_pixel) * alpha));
+
+ dst_row[x] = SkColorSetARGB(a, r, g, b);
+ }
+ }
+
+ return blended;
+}
+
+// static
+SkBitmap SkBitmapOperations::CreateMaskedBitmap(const SkBitmap& rgb,
+ const SkBitmap& alpha) {
+ DCHECK(rgb.width() == alpha.width());
+ DCHECK(rgb.height() == alpha.height());
+ DCHECK(rgb.bytesPerPixel() == alpha.bytesPerPixel());
+ DCHECK(rgb.config() == SkBitmap::kARGB_8888_Config);
+ DCHECK(alpha.config() == SkBitmap::kARGB_8888_Config);
+
+ SkBitmap masked;
+ masked.setConfig(SkBitmap::kARGB_8888_Config, rgb.width(), rgb.height(), 0);
+ masked.allocPixels();
+ masked.eraseARGB(0, 0, 0, 0);
+
+ SkAutoLockPixels lock_rgb(rgb);
+ SkAutoLockPixels lock_alpha(alpha);
+ SkAutoLockPixels lock_masked(masked);
+
+ for (int y = 0; y < masked.height(); ++y) {
+ uint32* rgb_row = rgb.getAddr32(0, y);
+ uint32* alpha_row = alpha.getAddr32(0, y);
+ uint32* dst_row = masked.getAddr32(0, y);
+
+ for (int x = 0; x < masked.width(); ++x) {
+ SkColor rgb_pixel = SkUnPreMultiply::PMColorToColor(rgb_row[x]);
+ int alpha = SkAlphaMul(SkColorGetA(rgb_pixel), SkColorGetA(alpha_row[x]));
+ dst_row[x] = SkColorSetARGB(alpha,
+ SkAlphaMul(SkColorGetR(rgb_pixel), alpha),
+ SkAlphaMul(SkColorGetG(rgb_pixel), alpha),
+ SkAlphaMul(SkColorGetB(rgb_pixel), alpha));
+ }
+ }
+
+ return masked;
+}
+
+// static
+SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color,
+ const SkBitmap& image,
+ const SkBitmap& mask) {
+ DCHECK(image.config() == SkBitmap::kARGB_8888_Config);
+ DCHECK(mask.config() == SkBitmap::kARGB_8888_Config);
+
+ SkBitmap background;
+ background.setConfig(
+ SkBitmap::kARGB_8888_Config, mask.width(), mask.height(), 0);
+ background.allocPixels();
+
+ double bg_a = SkColorGetA(color);
+ double bg_r = SkColorGetR(color);
+ double bg_g = SkColorGetG(color);
+ double bg_b = SkColorGetB(color);
+
+ SkAutoLockPixels lock_mask(mask);
+ SkAutoLockPixels lock_image(image);
+ SkAutoLockPixels lock_background(background);
+
+ for (int y = 0; y < mask.height(); ++y) {
+ uint32* dst_row = background.getAddr32(0, y);
+ uint32* image_row = image.getAddr32(0, y % image.height());
+ uint32* mask_row = mask.getAddr32(0, y);
+
+ for (int x = 0; x < mask.width(); ++x) {
+ uint32 image_pixel = image_row[x % image.width()];
+
+ double img_a = SkColorGetA(image_pixel);
+ double img_r = SkColorGetR(image_pixel);
+ double img_g = SkColorGetG(image_pixel);
+ double img_b = SkColorGetB(image_pixel);
+
+ double img_alpha = static_cast<double>(img_a) / 255.0;
+ double img_inv = 1 - img_alpha;
+
+ double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0;
+
+ dst_row[x] = SkColorSetARGB(
+ static_cast<int>(std::min(255.0, bg_a + img_a) * mask_a),
+ static_cast<int>(((bg_r * img_inv) + (img_r * img_alpha)) * mask_a),
+ static_cast<int>(((bg_g * img_inv) + (img_g * img_alpha)) * mask_a),
+ static_cast<int>(((bg_b * img_inv) + (img_b * img_alpha)) * mask_a));
+ }
+ }
+
+ return background;
+}
+
+
+// static
+SkBitmap SkBitmapOperations::CreateHSLShiftedBitmap(
+ const SkBitmap& bitmap,
+ color_utils::HSL hsl_shift) {
+ DCHECK(bitmap.empty() == false);
+ DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config);
+
+ SkBitmap shifted;
+ shifted.setConfig(SkBitmap::kARGB_8888_Config, bitmap.width(),
+ bitmap.height(), 0);
+ shifted.allocPixels();
+ shifted.eraseARGB(0, 0, 0, 0);
+ shifted.setIsOpaque(false);
+
+ SkAutoLockPixels lock_bitmap(bitmap);
+ SkAutoLockPixels lock_shifted(shifted);
+
+ // Loop through the pixels of the original bitmap.
+ for (int y = 0; y < bitmap.height(); ++y) {
+ SkPMColor* pixels = bitmap.getAddr32(0, y);
+ SkPMColor* tinted_pixels = shifted.getAddr32(0, y);
+
+ for (int x = 0; x < bitmap.width(); ++x) {
+ tinted_pixels[x] = SkPreMultiplyColor(color_utils::HSLShift(
+ SkUnPreMultiply::PMColorToColor(pixels[x]), hsl_shift));
+ }
+ }
+
+ return shifted;
+}
+
+// static
+SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source,
+ int src_x, int src_y,
+ int dst_w, int dst_h) {
+ DCHECK(source.getConfig() == SkBitmap::kARGB_8888_Config);
+
+ SkBitmap cropped;
+ cropped.setConfig(SkBitmap::kARGB_8888_Config, dst_w, dst_h, 0);
+ cropped.allocPixels();
+ cropped.eraseARGB(0, 0, 0, 0);
+
+ SkAutoLockPixels lock_source(source);
+ SkAutoLockPixels lock_cropped(cropped);
+
+ // Loop through the pixels of the original bitmap.
+ for (int y = 0; y < dst_h; ++y) {
+ int y_pix = (src_y + y) % source.height();
+ while (y_pix < 0)
+ y_pix += source.height();
+
+ uint32* source_row = source.getAddr32(0, y_pix);
+ uint32* dst_row = cropped.getAddr32(0, y);
+
+ for (int x = 0; x < dst_w; ++x) {
+ int x_pix = (src_x + x) % source.width();
+ while (x_pix < 0)
+ x_pix += source.width();
+
+ dst_row[x] = source_row[x_pix];
+ }
+ }
+
+ return cropped;
+}
+
+// static
+SkBitmap SkBitmapOperations::DownsampleByTwoUntilSize(const SkBitmap& bitmap,
+ int min_w, int min_h) {
+ if ((bitmap.width() <= min_w) || (bitmap.height() <= min_h) ||
+ (min_w < 0) || (min_h < 0))
+ return bitmap;
+
+ // Since bitmaps are refcounted, this copy will be fast.
+ SkBitmap current = bitmap;
+ while ((current.width() >= min_w * 2) && (current.height() >= min_h * 2) &&
+ (current.width() > 1) && (current.height() > 1))
+ current = DownsampleByTwo(current);
+ return current;
+}
+
+// static
+SkBitmap SkBitmapOperations::DownsampleByTwo(const SkBitmap& bitmap) {
+ // Handle the nop case.
+ if ((bitmap.width() <= 1) || (bitmap.height() <= 1))
+ return bitmap;
+
+ SkBitmap result;
+ result.setConfig(SkBitmap::kARGB_8888_Config,
+ (bitmap.width() + 1) / 2, (bitmap.height() + 1) / 2);
+ result.allocPixels();
+
+ SkAutoLockPixels lock(bitmap);
+ for (int dest_y = 0; dest_y < result.height(); ++dest_y) {
+ for (int dest_x = 0; dest_x < result.width(); ++dest_x) {
+ // This code is based on downsampleby2_proc32 in SkBitmap.cpp. It is very
+ // clever in that it does two channels at once: alpha and green ("ag")
+ // and red and blue ("rb"). Each channel gets averaged across 4 pixels
+ // to get the result.
+ int src_x = dest_x << 1;
+ int src_y = dest_y << 1;
+ const SkPMColor* cur_src = bitmap.getAddr32(src_x, src_y);
+ SkPMColor tmp, ag, rb;
+
+ // Top left pixel of the 2x2 block.
+ tmp = *cur_src;
+ ag = (tmp >> 8) & 0xFF00FF;
+ rb = tmp & 0xFF00FF;
+ if (src_x < (bitmap.width() - 1))
+ ++cur_src;
+
+ // Top right pixel of the 2x2 block.
+ tmp = *cur_src;
+ ag += (tmp >> 8) & 0xFF00FF;
+ rb += tmp & 0xFF00FF;
+ if (src_y < (bitmap.height() - 1))
+ cur_src = bitmap.getAddr32(src_x, src_y + 1);
+ else
+ cur_src = bitmap.getAddr32(src_x, src_y); // Move back to the first.
+
+ // Bottom left pixel of the 2x2 block.
+ tmp = *cur_src;
+ ag += (tmp >> 8) & 0xFF00FF;
+ rb += tmp & 0xFF00FF;
+ if (src_x < (bitmap.width() - 1))
+ ++cur_src;
+
+ // Bottom right pixel of the 2x2 block.
+ tmp = *cur_src;
+ ag += (tmp >> 8) & 0xFF00FF;
+ rb += tmp & 0xFF00FF;
+
+ // Put the channels back together, dividing each by 4 to get the average.
+ // |ag| has the alpha and green channels shifted right by 8 bits from
+ // there they should end up, so shifting left by 6 gives them in the
+ // correct position divided by 4.
+ *result.getAddr32(dest_x, dest_y) =
+ ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
+ }
+ }
+
+ return result;
+}
+
diff --git a/gfx/skbitmap_operations.h b/gfx/skbitmap_operations.h
new file mode 100644
index 0000000..71f6473
--- /dev/null
+++ b/gfx/skbitmap_operations.h
@@ -0,0 +1,93 @@
+// Copyright (c) 2009 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.
+
+#ifndef APP_GFX_SKBITMAP_OPERATIONS_H_
+#define APP_GFX_SKBITMAP_OPERATIONS_H_
+
+#include "gfx/color_utils.h"
+#include "testing/gtest/include/gtest/gtest_prod.h"
+
+class SkBitmap;
+
+class SkBitmapOperations {
+ public:
+ // Create a bitmap that is an inverted image of the passed in image.
+ // Each color becomes its inverse in the color wheel. So (255, 15, 0) becomes
+ // (0, 240, 255). The alpha value is not inverted.
+ static SkBitmap CreateInvertedBitmap(const SkBitmap& image);
+
+ // Create a bitmap that is a superimposition of the second bitmap on top of
+ // the first. The provided bitmaps must use have the kARGB_8888_Config config
+ // and be of equal dimensions.
+ static SkBitmap CreateSuperimposedBitmap(const SkBitmap& first,
+ const SkBitmap& second);
+
+ // Create a bitmap that is a blend of two others. The alpha argument
+ // specifies the opacity of the second bitmap. The provided bitmaps must
+ // use have the kARGB_8888_Config config and be of equal dimensions.
+ static SkBitmap CreateBlendedBitmap(const SkBitmap& first,
+ const SkBitmap& second,
+ double alpha);
+
+ // Create a bitmap that is the original bitmap masked out by the mask defined
+ // in the alpha bitmap. The images must use the kARGB_8888_Config config and
+ // be of equal dimensions.
+ static SkBitmap CreateMaskedBitmap(const SkBitmap& first,
+ const SkBitmap& alpha);
+
+ // We create a button background image by compositing the color and image
+ // together, then applying the mask. This is a highly specialized composite
+ // operation that is the equivalent of drawing a background in |color|,
+ // tiling |image| over the top, and then masking the result out with |mask|.
+ // The images must use kARGB_8888_Config config.
+ static SkBitmap CreateButtonBackground(SkColor color,
+ const SkBitmap& image,
+ const SkBitmap& mask);
+
+ // Shift a bitmap's HSL values. The shift values are in the range of 0-1,
+ // with the option to specify -1 for 'no change'. The shift values are
+ // defined as:
+ // hsl_shift[0] (hue): The absolute hue value for the image - 0 and 1 map
+ // to 0 and 360 on the hue color wheel (red).
+ // hsl_shift[1] (saturation): A saturation shift for the image, with the
+ // following key values:
+ // 0 = remove all color.
+ // 0.5 = leave unchanged.
+ // 1 = fully saturate the image.
+ // hsl_shift[2] (lightness): A lightness shift for the image, with the
+ // following key values:
+ // 0 = remove all lightness (make all pixels black).
+ // 0.5 = leave unchanged.
+ // 1 = full lightness (make all pixels white).
+ static SkBitmap CreateHSLShiftedBitmap(const SkBitmap& bitmap,
+ color_utils::HSL hsl_shift);
+
+ // Create a bitmap that is cropped from another bitmap. This is special
+ // because it tiles the original bitmap, so your coordinates can extend
+ // outside the bounds of the original image.
+ static SkBitmap CreateTiledBitmap(const SkBitmap& bitmap,
+ int src_x, int src_y,
+ int dst_w, int dst_h);
+
+ // Iteratively downsamples by 2 until the bitmap is no smaller than the
+ // input size. The normal use of this is to downsample the bitmap "close" to
+ // the final size, and then use traditional resampling on the result.
+ // Because the bitmap will be closer to the final size, it will be faster,
+ // and linear interpolation will generally work well as a second step.
+ static SkBitmap DownsampleByTwoUntilSize(const SkBitmap& bitmap,
+ int min_w, int min_h);
+
+ private:
+ SkBitmapOperations(); // Class for scoping only.
+
+ // Makes a bitmap half has large in each direction by averaging groups of
+ // 4 pixels. This is one step in generating a mipmap.
+ static SkBitmap DownsampleByTwo(const SkBitmap& bitmap);
+
+ FRIEND_TEST(SkBitmapOperationsTest, DownsampleByTwo);
+ FRIEND_TEST(SkBitmapOperationsTest, DownsampleByTwoSmall);
+};
+
+#endif // APP_GFX_SKBITMAP_OPERATIONS_H_
+
diff --git a/gfx/skbitmap_operations_unittest.cc b/gfx/skbitmap_operations_unittest.cc
new file mode 100644
index 0000000..4082b22
--- /dev/null
+++ b/gfx/skbitmap_operations_unittest.cc
@@ -0,0 +1,383 @@
+// Copyright (c) 2009 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.
+
+#include "gfx/skbitmap_operations.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkColorPriv.h"
+#include "third_party/skia/include/core/SkUnPreMultiply.h"
+
+namespace {
+
+// Returns true if each channel of the given two colors are "close." This is
+// used for comparing colors where rounding errors may cause off-by-one.
+bool ColorsClose(uint32_t a, uint32_t b) {
+ return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2;
+}
+
+void FillDataToBitmap(int w, int h, SkBitmap* bmp) {
+ bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
+ bmp->allocPixels();
+
+ unsigned char* src_data =
+ reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0));
+ for (int i = 0; i < w * h; i++) {
+ src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255);
+ }
+}
+
+} // namespace
+
+// Invert bitmap and verify the each pixel is inverted and the alpha value is
+// not changed.
+TEST(SkBitmapOperationsTest, CreateInvertedBitmap) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src;
+ src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src.allocPixels();
+
+ for (int y = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ int i = y * src_w + x;
+ *src.getAddr32(x, y) =
+ SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0);
+ }
+ }
+
+ SkBitmap inverted = SkBitmapOperations::CreateInvertedBitmap(src);
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels inverted_lock(inverted);
+
+ for (int y = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ int i = y * src_w + x;
+ EXPECT_EQ(static_cast<unsigned int>((255 - i) % 255),
+ SkColorGetA(*inverted.getAddr32(x, y)));
+ EXPECT_EQ(static_cast<unsigned int>(255 - (i % 255)),
+ SkColorGetR(*inverted.getAddr32(x, y)));
+ EXPECT_EQ(static_cast<unsigned int>(255 - (i * 4 % 255)),
+ SkColorGetG(*inverted.getAddr32(x, y)));
+ EXPECT_EQ(static_cast<unsigned int>(255),
+ SkColorGetB(*inverted.getAddr32(x, y)));
+ }
+ }
+}
+
+// Blend two bitmaps together at 50% alpha and verify that the result
+// is the middle-blend of the two.
+TEST(SkBitmapOperationsTest, CreateBlendedBitmap) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src_a;
+ src_a.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src_a.allocPixels();
+
+ SkBitmap src_b;
+ src_b.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src_b.allocPixels();
+
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ *src_a.getAddr32(x, y) = SkColorSetARGB(255, 0, i * 2 % 255, i % 255);
+ *src_b.getAddr32(x, y) =
+ SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0);
+ i++;
+ }
+ }
+
+ // Shift to red.
+ SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap(
+ src_a, src_b, 0.5);
+ SkAutoLockPixels srca_lock(src_a);
+ SkAutoLockPixels srcb_lock(src_b);
+ SkAutoLockPixels blended_lock(blended);
+
+ for (int y = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ int i = y * src_w + x;
+ EXPECT_EQ(static_cast<unsigned int>((255 + ((255 - i) % 255)) / 2),
+ SkColorGetA(*blended.getAddr32(x, y)));
+ EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2),
+ SkColorGetR(*blended.getAddr32(x, y)));
+ EXPECT_EQ((static_cast<unsigned int>((i * 2) % 255 + (i * 4) % 255) / 2),
+ SkColorGetG(*blended.getAddr32(x, y)));
+ EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2),
+ SkColorGetB(*blended.getAddr32(x, y)));
+ }
+ }
+}
+
+// Test our masking functions.
+TEST(SkBitmapOperationsTest, CreateMaskedBitmap) {
+ int src_w = 16, src_h = 16;
+
+ SkBitmap src;
+ FillDataToBitmap(src_w, src_h, &src);
+
+ // Generate alpha mask
+ SkBitmap alpha;
+ alpha.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ alpha.allocPixels();
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ *alpha.getAddr32(x, y) = SkColorSetARGB((i + 128) % 255,
+ (i + 128) % 255,
+ (i + 64) % 255,
+ (i + 0) % 255);
+ i++;
+ }
+ }
+
+ SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap(src, alpha);
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels alpha_lock(alpha);
+ SkAutoLockPixels masked_lock(masked);
+ for (int y = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ // Test that the alpha is equal.
+ SkColor src_pixel = SkUnPreMultiply::PMColorToColor(*src.getAddr32(x, y));
+ SkColor alpha_pixel =
+ SkUnPreMultiply::PMColorToColor(*alpha.getAddr32(x, y));
+ SkColor masked_pixel = *masked.getAddr32(x, y);
+
+ int alpha_value = SkAlphaMul(SkColorGetA(src_pixel),
+ SkColorGetA(alpha_pixel));
+ SkColor expected_pixel = SkColorSetARGB(
+ alpha_value,
+ SkAlphaMul(SkColorGetR(src_pixel), alpha_value),
+ SkAlphaMul(SkColorGetG(src_pixel), alpha_value),
+ SkAlphaMul(SkColorGetB(src_pixel), alpha_value));
+
+ EXPECT_TRUE(ColorsClose(expected_pixel, masked_pixel));
+ }
+ }
+}
+
+// Make sure that when shifting a bitmap without any shift parameters,
+// the end result is close enough to the original (rounding errors
+// notwithstanding).
+TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapToSame) {
+ int src_w = 4, src_h = 4;
+ SkBitmap src;
+ src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src.allocPixels();
+
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ *src.getAddr32(x, y) = SkColorSetARGB(i + 128 % 255,
+ i + 128 % 255, i + 64 % 255, i + 0 % 255);
+ i++;
+ }
+ }
+
+ color_utils::HSL hsl = { -1, -1, -1 };
+
+ SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl);
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels shifted_lock(shifted);
+
+ for (int y = 0; y < src_w; y++) {
+ for (int x = 0; x < src_h; x++) {
+ SkColor src_pixel = *src.getAddr32(x, y);
+ SkColor shifted_pixel = *shifted.getAddr32(x, y);
+ EXPECT_TRUE(ColorsClose(src_pixel, shifted_pixel));
+ }
+ }
+}
+
+// Shift a blue bitmap to red.
+TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapHueOnly) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src;
+ src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src.allocPixels();
+
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ *src.getAddr32(x, y) = SkColorSetARGB(255, 0, 0, i % 255);
+ i++;
+ }
+ }
+
+ // Shift to red.
+ color_utils::HSL hsl = { 0, -1, -1 };
+
+ SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl);
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels shifted_lock(shifted);
+
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ EXPECT_TRUE(ColorsClose(*shifted.getAddr32(x, y),
+ SkColorSetARGB(255, i % 255, 0, 0)));
+ i++;
+ }
+ }
+}
+
+// Test our cropping.
+TEST(SkBitmapOperationsTest, CreateCroppedBitmap) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src;
+ FillDataToBitmap(src_w, src_h, &src);
+
+ SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(src, 4, 4,
+ 8, 8);
+ ASSERT_EQ(8, cropped.width());
+ ASSERT_EQ(8, cropped.height());
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels cropped_lock(cropped);
+ for (int y = 4; y < 12; y++) {
+ for (int x = 4; x < 12; x++) {
+ EXPECT_EQ(*src.getAddr32(x, y),
+ *cropped.getAddr32(x - 4, y - 4));
+ }
+ }
+}
+
+// Test whether our cropping correctly wraps across image boundaries.
+TEST(SkBitmapOperationsTest, CreateCroppedBitmapWrapping) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src;
+ FillDataToBitmap(src_w, src_h, &src);
+
+ SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(
+ src, src_w / 2, src_h / 2, src_w, src_h);
+ ASSERT_EQ(src_w, cropped.width());
+ ASSERT_EQ(src_h, cropped.height());
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels cropped_lock(cropped);
+ for (int y = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ EXPECT_EQ(*src.getAddr32(x, y),
+ *cropped.getAddr32((x + src_w / 2) % src_w,
+ (y + src_h / 2) % src_h));
+ }
+ }
+}
+
+TEST(SkBitmapOperationsTest, DownsampleByTwo) {
+ // Use an odd-sized bitmap to make sure the edge cases where there isn't a
+ // 2x2 block of pixels is handled correctly.
+ // Here's the ARGB example
+ //
+ // 50% transparent green opaque 50% blue white
+ // 80008000 FF000080 FFFFFFFF
+ //
+ // 50% transparent red opaque 50% gray black
+ // 80800000 80808080 FF000000
+ //
+ // black white 50% gray
+ // FF000000 FFFFFFFF FF808080
+ //
+ // The result of this computation should be:
+ // A0404040 FF808080
+ // FF808080 FF808080
+ SkBitmap input;
+ input.setConfig(SkBitmap::kARGB_8888_Config, 3, 3);
+ input.allocPixels();
+
+ // The color order may be different, but we don't care (the channels are
+ // trated the same).
+ *input.getAddr32(0, 0) = 0x80008000;
+ *input.getAddr32(1, 0) = 0xFF000080;
+ *input.getAddr32(2, 0) = 0xFFFFFFFF;
+ *input.getAddr32(0, 1) = 0x80800000;
+ *input.getAddr32(1, 1) = 0x80808080;
+ *input.getAddr32(2, 1) = 0xFF000000;
+ *input.getAddr32(0, 2) = 0xFF000000;
+ *input.getAddr32(1, 2) = 0xFFFFFFFF;
+ *input.getAddr32(2, 2) = 0xFF808080;
+
+ SkBitmap result = SkBitmapOperations::DownsampleByTwo(input);
+ EXPECT_EQ(2, result.width());
+ EXPECT_EQ(2, result.height());
+
+ // Some of the values are off-by-one due to rounding.
+ SkAutoLockPixels lock(result);
+ EXPECT_EQ(0x9f404040, *result.getAddr32(0, 0));
+ EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(1, 0));
+ EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1));
+ EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1));
+}
+
+// Test edge cases for DownsampleByTwo.
+TEST(SkBitmapOperationsTest, DownsampleByTwoSmall) {
+ SkPMColor reference = 0xFF4080FF;
+
+ // Test a 1x1 bitmap.
+ SkBitmap one_by_one;
+ one_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
+ one_by_one.allocPixels();
+ *one_by_one.getAddr32(0, 0) = reference;
+ SkBitmap result = SkBitmapOperations::DownsampleByTwo(one_by_one);
+ SkAutoLockPixels lock1(result);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(1, result.height());
+ EXPECT_EQ(reference, *result.getAddr32(0, 0));
+
+ // Test an n by 1 bitmap.
+ SkBitmap one_by_n;
+ one_by_n.setConfig(SkBitmap::kARGB_8888_Config, 300, 1);
+ one_by_n.allocPixels();
+ result = SkBitmapOperations::DownsampleByTwo(one_by_n);
+ SkAutoLockPixels lock2(result);
+ EXPECT_EQ(300, result.width());
+ EXPECT_EQ(1, result.height());
+
+ // Test a 1 by n bitmap.
+ SkBitmap n_by_one;
+ n_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 300);
+ n_by_one.allocPixels();
+ result = SkBitmapOperations::DownsampleByTwo(n_by_one);
+ SkAutoLockPixels lock3(result);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(300, result.height());
+
+ // Test an empty bitmap
+ SkBitmap empty;
+ result = SkBitmapOperations::DownsampleByTwo(empty);
+ EXPECT_TRUE(result.isNull());
+ EXPECT_EQ(0, result.width());
+ EXPECT_EQ(0, result.height());
+}
+
+// Here we assume DownsampleByTwo works correctly (it's tested above) and
+// just make sure that the wrapper function does the right thing.
+TEST(SkBitmapOperationsTest, DownsampleByTwoUntilSize) {
+ // First make sure a "too small" bitmap doesn't get modified at all.
+ SkBitmap too_small;
+ too_small.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
+ too_small.allocPixels();
+ SkBitmap result = SkBitmapOperations::DownsampleByTwoUntilSize(
+ too_small, 16, 16);
+ EXPECT_EQ(10, result.width());
+ EXPECT_EQ(10, result.height());
+
+ // Now make sure giving it a 0x0 target returns something reasonable.
+ result = SkBitmapOperations::DownsampleByTwoUntilSize(too_small, 0, 0);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(1, result.height());
+
+ // Test multiple steps of downsampling.
+ SkBitmap large;
+ large.setConfig(SkBitmap::kARGB_8888_Config, 100, 43);
+ large.allocPixels();
+ result = SkBitmapOperations::DownsampleByTwoUntilSize(large, 6, 6);
+
+ // The result should be divided in half 100x43 -> 50x22 -> 25x11
+ EXPECT_EQ(25, result.width());
+ EXPECT_EQ(11, result.height());
+}
diff --git a/gfx/skia_util.cc b/gfx/skia_util.cc
new file mode 100644
index 0000000..13ef4d9
--- /dev/null
+++ b/gfx/skia_util.cc
@@ -0,0 +1,42 @@
+// Copyright (c) 2010 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.
+
+#include "gfx/skia_util.h"
+
+#include "gfx/rect.h"
+#include "third_party/skia/include/core/SkColorPriv.h"
+#include "third_party/skia/include/core/SkShader.h"
+#include "third_party/skia/include/effects/SkGradientShader.h"
+
+namespace gfx {
+
+SkRect RectToSkRect(const gfx::Rect& rect) {
+ SkRect r;
+ r.set(SkIntToScalar(rect.x()), SkIntToScalar(rect.y()),
+ SkIntToScalar(rect.right()), SkIntToScalar(rect.bottom()));
+ return r;
+}
+
+gfx::Rect SkRectToRect(const SkRect& rect) {
+ return gfx::Rect(SkScalarToFixed(rect.fLeft),
+ SkScalarToFixed(rect.fTop),
+ SkScalarToFixed(rect.width()),
+ SkScalarToFixed(rect.height()));
+}
+
+SkShader* CreateGradientShader(int start_point,
+ int end_point,
+ SkColor start_color,
+ SkColor end_color) {
+ SkColor grad_colors[2] = { start_color, end_color};
+ SkPoint grad_points[2];
+ grad_points[0].set(SkIntToScalar(0), SkIntToScalar(start_point));
+ grad_points[1].set(SkIntToScalar(0), SkIntToScalar(end_point));
+
+ return SkGradientShader::CreateLinear(
+ grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode);
+}
+
+} // namespace gfx
+
diff --git a/gfx/skia_util.h b/gfx/skia_util.h
new file mode 100644
index 0000000..66a8aeb
--- /dev/null
+++ b/gfx/skia_util.h
@@ -0,0 +1,34 @@
+// Copyright (c) 2010 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.
+
+#ifndef APP_GFX_SKIA_UTIL_H_
+#define APP_GFX_SKIA_UTIL_H_
+
+#include "third_party/skia/include/core/SkColor.h"
+#include "third_party/skia/include/core/SkRect.h"
+
+class SkShader;
+
+namespace gfx {
+
+class Rect;
+
+// Convert between Skia and gfx rect types.
+SkRect RectToSkRect(const gfx::Rect& rect);
+gfx::Rect SkRectToRect(const SkRect& rect);
+
+// Creates a vertical gradient shader. The caller owns the shader.
+// Example usage to avoid leaks:
+// paint.setShader(gfx::CreateGradientShader(0, 10, red, blue))->safeUnref();
+//
+// (The old shader in the paint, if any, needs to be freed, and safeUnref will
+// handle the NULL case.)
+SkShader* CreateGradientShader(int start_point,
+ int end_point,
+ SkColor start_color,
+ SkColor end_color);
+
+} // namespace gfx;
+
+#endif // APP_GFX_SKIA_UTIL_H_
diff --git a/gfx/skia_utils_gtk.cc b/gfx/skia_utils_gtk.cc
new file mode 100644
index 0000000..8ed4bec
--- /dev/null
+++ b/gfx/skia_utils_gtk.cc
@@ -0,0 +1,32 @@
+// Copyright (c) 2010 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.
+
+#include "gfx/skia_utils_gtk.h"
+
+#include <gdk/gdkcolor.h>
+
+namespace gfx {
+
+const int kSkiaToGDKMultiplier = 257;
+
+// GDK_COLOR_RGB multiplies by 257 (= 0x10001) to distribute the bits evenly
+// See: http://www.mindcontrol.org/~hplus/graphics/expand-bits.html
+// To get back, we can just right shift by eight
+// (or, formulated differently, i == (i*257)/256 for all i < 256).
+
+SkColor GdkColorToSkColor(GdkColor color) {
+ return SkColorSetRGB(color.red >> 8, color.green >> 8, color.blue >> 8);
+}
+
+GdkColor SkColorToGdkColor(SkColor color) {
+ GdkColor gdk_color = {
+ 0,
+ SkColorGetR(color) * kSkiaToGDKMultiplier,
+ SkColorGetG(color) * kSkiaToGDKMultiplier,
+ SkColorGetB(color) * kSkiaToGDKMultiplier
+ };
+ return gdk_color;
+}
+
+} // namespace gfx
diff --git a/gfx/skia_utils_gtk.h b/gfx/skia_utils_gtk.h
new file mode 100644
index 0000000..df68581
--- /dev/null
+++ b/gfx/skia_utils_gtk.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2010 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.
+
+#ifndef APP_GFX_SKIA_UTILS_GTK_H_
+#define APP_GFX_SKIA_UTILS_GTK_H_
+
+#include "third_party/skia/include/core/SkColor.h"
+
+typedef struct _GdkColor GdkColor;
+
+namespace gfx {
+
+// Converts GdkColors to the ARGB layout Skia expects.
+SkColor GdkColorToSkColor(GdkColor color);
+
+// Converts ARGB to GdkColor.
+GdkColor SkColorToGdkColor(SkColor color);
+
+} // namespace gfx
+
+#endif // APP_GFX_SKIA_UTILS_GTK_H_