diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-15 15:30:55 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-15 15:30:55 +0000 |
commit | 3eaf0ecd7a4882482d6234f7a115d1b019c13339 (patch) | |
tree | 327b0b0078ed2bdfa101986fb3ae51edc1c87275 | |
parent | 3d428737173006823333a2635df6c4c15ad739a7 (diff) | |
download | chromium_src-3eaf0ecd7a4882482d6234f7a115d1b019c13339.zip chromium_src-3eaf0ecd7a4882482d6234f7a115d1b019c13339.tar.gz chromium_src-3eaf0ecd7a4882482d6234f7a115d1b019c13339.tar.bz2 |
Windows: Fix opacity of drag images.
BUG=XXX
TEST=Go to google.com, drag logo around. Preview image should now look nice.
Review URL: http://codereview.chromium.org/2963009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52484 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | gfx/skbitmap_operations.cc | 27 | ||||
-rw-r--r-- | gfx/skbitmap_operations.h | 6 | ||||
-rw-r--r-- | gfx/skbitmap_operations_unittest.cc | 21 | ||||
-rw-r--r-- | views/drag_utils_win.cc | 27 |
4 files changed, 65 insertions, 16 deletions
diff --git a/gfx/skbitmap_operations.cc b/gfx/skbitmap_operations.cc index 27508ba..b8cec11 100644 --- a/gfx/skbitmap_operations.cc +++ b/gfx/skbitmap_operations.cc @@ -668,3 +668,30 @@ SkBitmap SkBitmapOperations::DownsampleByTwo(const SkBitmap& bitmap) { return result; } +// static +SkBitmap SkBitmapOperations::UnPreMultiply(const SkBitmap& bitmap) { + if (bitmap.isNull()) + return bitmap; + if (bitmap.isOpaque()) + return bitmap; + + SkBitmap opaque_bitmap; + opaque_bitmap.setConfig(bitmap.config(), bitmap.width(), bitmap.height()); + opaque_bitmap.allocPixels(); + + { + SkAutoLockPixels bitmap_lock(bitmap); + SkAutoLockPixels opaque_bitmap_lock(opaque_bitmap); + for (int y = 0; y < opaque_bitmap.height(); y++) { + for (int x = 0; x < opaque_bitmap.width(); x++) { + uint32 src_pixel = *bitmap.getAddr32(x, y); + uint32* dst_pixel = opaque_bitmap.getAddr32(x, y); + SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(src_pixel); + *dst_pixel = unmultiplied; + } + } + } + + opaque_bitmap.setIsOpaque(true); + return opaque_bitmap; +} diff --git a/gfx/skbitmap_operations.h b/gfx/skbitmap_operations.h index 1308308..6f808e7 100644 --- a/gfx/skbitmap_operations.h +++ b/gfx/skbitmap_operations.h @@ -82,6 +82,12 @@ class SkBitmapOperations { // 4 pixels. This is one step in generating a mipmap. static SkBitmap DownsampleByTwo(const SkBitmap& bitmap); + // Unpremultiplies all pixels in |bitmap|. You almost never want to call + // this, as |SkBitmap|s are always premultiplied by conversion. Call this + // only if you will pass the bitmap's data into a system function that + // doesn't expect premultiplied colors. + static SkBitmap UnPreMultiply(const SkBitmap& bitmap); + private: SkBitmapOperations(); // Class for scoping only. diff --git a/gfx/skbitmap_operations_unittest.cc b/gfx/skbitmap_operations_unittest.cc index e99f594..83b732a0 100644 --- a/gfx/skbitmap_operations_unittest.cc +++ b/gfx/skbitmap_operations_unittest.cc @@ -471,3 +471,24 @@ TEST(SkBitmapOperationsTest, DownsampleByTwoUntilSize) { EXPECT_EQ(25, result.width()); EXPECT_EQ(11, result.height()); } + +TEST(SkBitmapOperationsTest, UnPreMultiply) { + SkBitmap input; + input.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); + input.allocPixels(); + + *input.getAddr32(0, 0) = 0x80000000; + *input.getAddr32(1, 0) = 0x80808080; + *input.getAddr32(0, 1) = 0xFF00CC88; + *input.getAddr32(1, 1) = 0x0000CC88; + + SkBitmap result = SkBitmapOperations::UnPreMultiply(input); + EXPECT_EQ(2, result.width()); + EXPECT_EQ(2, result.height()); + + SkAutoLockPixels lock(result); + EXPECT_EQ(0x80000000, *result.getAddr32(0, 0)); + EXPECT_EQ(0x80FFFFFF, *result.getAddr32(1, 0)); + EXPECT_EQ(0xFF00CC88, *result.getAddr32(0, 1)); + EXPECT_EQ(0x00000000u, *result.getAddr32(1, 1)); // "Division by zero". +} diff --git a/views/drag_utils_win.cc b/views/drag_utils_win.cc index 84b4ba1..d21d37f 100644 --- a/views/drag_utils_win.cc +++ b/views/drag_utils_win.cc @@ -12,6 +12,7 @@ #include "app/os_exchange_data_provider_win.h" #include "gfx/canvas_skia.h" #include "gfx/gdi_util.h" +#include "gfx/skbitmap_operations.h" #include "third_party/skia/include/core/SkBitmap.h" namespace drag_utils { @@ -35,23 +36,18 @@ static void SetDragImageOnDataObject(HBITMAP hbitmap, // Blit the contents of the canvas to a new HBITMAP. It is the caller's // responsibility to release the |bits| buffer. -static HBITMAP CreateBitmapFromCanvas(const gfx::CanvasSkia& canvas, - int width, - int height) { +static HBITMAP CreateHBITMAPFromSkBitmap(const SkBitmap& sk_bitmap) { HDC screen_dc = GetDC(NULL); BITMAPINFOHEADER header; - gfx::CreateBitmapHeader(width, height, &header); + gfx::CreateBitmapHeader(sk_bitmap.width(), sk_bitmap.height(), &header); void* bits; HBITMAP bitmap = CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header), DIB_RGB_COLORS, &bits, NULL, 0); - HDC compatible_dc = CreateCompatibleDC(screen_dc); - HGDIOBJ old_object = SelectObject(compatible_dc, bitmap); - BitBlt(compatible_dc, 0, 0, width, height, - canvas.getTopPlatformDevice().getBitmapDC(), - 0, 0, SRCCOPY); - SelectObject(compatible_dc, old_object); - ReleaseDC(NULL, compatible_dc); + DCHECK(sk_bitmap.rowBytes() == sk_bitmap.width() * 4); + SkAutoLockPixels lock(sk_bitmap); + memcpy( + bits, sk_bitmap.getPixels(), sk_bitmap.height() * sk_bitmap.rowBytes()); ReleaseDC(NULL, screen_dc); return bitmap; } @@ -60,13 +56,12 @@ void SetDragImageOnDataObject(const SkBitmap& sk_bitmap, const gfx::Size& size, const gfx::Point& cursor_offset, OSExchangeData* data_object) { - gfx::CanvasSkia canvas(sk_bitmap.width(), sk_bitmap.height(), - /*is_opaque=*/false); - canvas.DrawBitmapInt(sk_bitmap, 0, 0); - DCHECK(data_object && !size.IsEmpty()); + // InitializeFromBitmap() doesn't expect an alpha channel and is confused + // by premultiplied colors, so unpremultiply the bitmap. // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. - HBITMAP bitmap = CreateBitmapFromCanvas(canvas, size.width(), size.height()); + HBITMAP bitmap = CreateHBITMAPFromSkBitmap( + SkBitmapOperations::UnPreMultiply(sk_bitmap)); // Attach 'bitmap' to the data_object. SetDragImageOnDataObject(bitmap, size, cursor_offset, |