OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "views/drag_utils.h" | 5 #include "views/drag_utils.h" |
6 | 6 |
7 #include <objidl.h> | 7 #include <objidl.h> |
8 #include <shlobj.h> | 8 #include <shlobj.h> |
9 #include <shobjidl.h> | 9 #include <shobjidl.h> |
10 | 10 |
11 #include "app/os_exchange_data.h" | 11 #include "app/os_exchange_data.h" |
12 #include "app/os_exchange_data_provider_win.h" | 12 #include "app/os_exchange_data_provider_win.h" |
13 #include "gfx/canvas_skia.h" | 13 #include "gfx/canvas_skia.h" |
14 #include "gfx/gdi_util.h" | 14 #include "gfx/gdi_util.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
16 #include "third_party/skia/include/core/SkUnPreMultiply.h" | |
17 | 16 |
18 namespace drag_utils { | 17 namespace drag_utils { |
19 | 18 |
20 static void SetDragImageOnDataObject(HBITMAP hbitmap, | 19 static void SetDragImageOnDataObject(HBITMAP hbitmap, |
21 const gfx::Size& size, | 20 const gfx::Size& size, |
22 const gfx::Point& cursor_offset, | 21 const gfx::Point& cursor_offset, |
23 IDataObject* data_object) { | 22 IDataObject* data_object) { |
24 IDragSourceHelper* helper = NULL; | 23 IDragSourceHelper* helper = NULL; |
25 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, | 24 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, |
26 IID_IDragSourceHelper, reinterpret_cast<LPVOID*>(&helper)); | 25 IID_IDragSourceHelper, reinterpret_cast<LPVOID*>(&helper)); |
(...skipping 28 matching lines...) Expand all Loading... |
55 ReleaseDC(NULL, compatible_dc); | 54 ReleaseDC(NULL, compatible_dc); |
56 ReleaseDC(NULL, screen_dc); | 55 ReleaseDC(NULL, screen_dc); |
57 return bitmap; | 56 return bitmap; |
58 } | 57 } |
59 | 58 |
60 void SetDragImageOnDataObject(const SkBitmap& sk_bitmap, | 59 void SetDragImageOnDataObject(const SkBitmap& sk_bitmap, |
61 const gfx::Size& size, | 60 const gfx::Size& size, |
62 const gfx::Point& cursor_offset, | 61 const gfx::Point& cursor_offset, |
63 OSExchangeData* data_object) { | 62 OSExchangeData* data_object) { |
64 gfx::CanvasSkia canvas(sk_bitmap.width(), sk_bitmap.height(), | 63 gfx::CanvasSkia canvas(sk_bitmap.width(), sk_bitmap.height(), |
65 /*is_opaque=*/true); | 64 /*is_opaque=*/false); |
66 SkBitmap opaque_bitmap; | 65 canvas.DrawBitmapInt(sk_bitmap, 0, 0); |
67 if (sk_bitmap.isOpaque()) { | |
68 opaque_bitmap = sk_bitmap; | |
69 } else { | |
70 // InitializeFromBitmap() doesn't expect an alpha channel and is confused | |
71 // by premultiplied colors, so unpremultiply the bitmap. | |
72 SkBitmap tmp_bitmap; | |
73 tmp_bitmap.setConfig( | |
74 sk_bitmap.config(), sk_bitmap.width(), sk_bitmap.height()); | |
75 tmp_bitmap.allocPixels(); | |
76 | |
77 SkAutoLockPixels lock(tmp_bitmap); | |
78 const int kBytesPerPixel = 4; | |
79 for (int y = 0; y < tmp_bitmap.height(); y++) { | |
80 for (int x = 0; x < tmp_bitmap.width(); x++) { | |
81 uint32 src_pixel = *sk_bitmap.getAddr32(x, y); | |
82 uint32* dst_pixel = tmp_bitmap.getAddr32(x, y); | |
83 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(src_pixel); | |
84 *dst_pixel = unmultiplied; | |
85 } | |
86 } | |
87 tmp_bitmap.setIsOpaque(true); | |
88 opaque_bitmap = tmp_bitmap; | |
89 } | |
90 canvas.DrawBitmapInt(opaque_bitmap, 0, 0); | |
91 | 66 |
92 DCHECK(data_object && !size.IsEmpty()); | 67 DCHECK(data_object && !size.IsEmpty()); |
93 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. | 68 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. |
94 HBITMAP bitmap = CreateBitmapFromCanvas(canvas, size.width(), size.height()); | 69 HBITMAP bitmap = CreateBitmapFromCanvas(canvas, size.width(), size.height()); |
95 | 70 |
96 // Attach 'bitmap' to the data_object. | 71 // Attach 'bitmap' to the data_object. |
97 SetDragImageOnDataObject(bitmap, size, cursor_offset, | 72 SetDragImageOnDataObject(bitmap, size, cursor_offset, |
98 OSExchangeDataProviderWin::GetIDataObject(*data_object)); | 73 OSExchangeDataProviderWin::GetIDataObject(*data_object)); |
99 } | 74 } |
100 | 75 |
101 } // namespace drag_utils | 76 } // namespace drag_utils |
OLD | NEW |