Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/base/dragdrop/drag_utils.h" | 5 #include "ui/base/dragdrop/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 #include <stddef.h> | 10 #include <stddef.h> |
| 11 | 11 |
| 12 #include "base/win/scoped_comptr.h" | 12 #include "base/win/scoped_comptr.h" |
| 13 #include "base/win/scoped_hdc.h" | 13 #include "base/win/scoped_hdc.h" |
| 14 #include "skia/ext/skia_utils_win.h" | 14 #include "skia/ext/skia_utils_win.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/base/dragdrop/os_exchange_data.h" | 16 #include "ui/base/dragdrop/os_exchange_data.h" |
| 17 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" | 17 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" |
| 18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/geometry/point.h" | 19 #include "ui/gfx/geometry/point.h" |
| 20 #include "ui/gfx/geometry/size.h" | 20 #include "ui/gfx/geometry/size.h" |
| 21 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
| 22 #include "ui/gfx/skbitmap_operations.h" | 22 #include "ui/gfx/skbitmap_operations.h" |
| 23 | 23 |
| 24 namespace drag_utils { | 24 namespace drag_utils { |
| 25 | 25 |
| 26 static void SetDragImageOnDataObject(HBITMAP hbitmap, | |
|
danakj
2017/04/03 22:34:01
Since these have one caller, I collapsed them all
| |
| 27 const gfx::Size& size_in_pixels, | |
| 28 const gfx::Vector2d& cursor_offset, | |
| 29 IDataObject* data_object) { | |
| 30 base::win::ScopedComPtr<IDragSourceHelper> helper; | |
| 31 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, | |
| 32 IID_IDragSourceHelper, helper.ReceiveVoid()); | |
| 33 if (SUCCEEDED(rv)) { | |
| 34 SHDRAGIMAGE sdi; | |
| 35 sdi.sizeDragImage = size_in_pixels.ToSIZE(); | |
| 36 sdi.crColorKey = 0xFFFFFFFF; | |
| 37 sdi.hbmpDragImage = hbitmap; | |
| 38 sdi.ptOffset = gfx::PointAtOffsetFromOrigin(cursor_offset).ToPOINT(); | |
| 39 helper->InitializeFromBitmap(&sdi, data_object); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 // Blit the contents of the canvas to a new HBITMAP. It is the caller's | |
|
danakj
2017/04/03 22:34:01
I read MSDN for a while and I think this comment i
| |
| 44 // responsibility to release the |bits| buffer. | |
| 45 static HBITMAP CreateHBITMAPFromSkBitmap(const SkBitmap& sk_bitmap) { | |
| 46 base::win::ScopedGetDC screen_dc(NULL); | |
| 47 BITMAPINFOHEADER header; | |
| 48 skia::CreateBitmapHeader(sk_bitmap.width(), sk_bitmap.height(), &header); | |
| 49 void* bits; | |
| 50 HBITMAP bitmap = | |
| 51 CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header), | |
| 52 DIB_RGB_COLORS, &bits, NULL, 0); | |
| 53 if (!bitmap || !bits) | |
| 54 return NULL; | |
| 55 DCHECK_EQ(sk_bitmap.rowBytes(), static_cast<size_t>(sk_bitmap.width() * 4)); | |
| 56 SkAutoLockPixels lock(sk_bitmap); | |
| 57 memcpy( | |
| 58 bits, sk_bitmap.getPixels(), sk_bitmap.height() * sk_bitmap.rowBytes()); | |
| 59 return bitmap; | |
| 60 } | |
| 61 | |
| 62 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, | 26 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, |
| 63 const gfx::Vector2d& cursor_offset, | 27 const gfx::Vector2d& cursor_offset, |
| 64 ui::OSExchangeData* data_object) { | 28 ui::OSExchangeData* data_object) { |
| 65 DCHECK(data_object && !image_skia.size().IsEmpty()); | 29 DCHECK(data_object && !image_skia.size().IsEmpty()); |
| 30 | |
| 66 // InitializeFromBitmap() doesn't expect an alpha channel and is confused | 31 // InitializeFromBitmap() doesn't expect an alpha channel and is confused |
| 67 // by premultiplied colors, so unpremultiply the bitmap. | 32 // by premultiplied colors, so unpremultiply the bitmap. |
| 68 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. | 33 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. |
| 69 HBITMAP bitmap = CreateHBITMAPFromSkBitmap( | 34 SkBitmap unpremul_bitmap = |
| 70 SkBitmapOperations::UnPreMultiply(*image_skia.bitmap())); | 35 SkBitmapOperations::UnPreMultiply(*image_skia.bitmap()); |
| 71 if (bitmap) { | 36 int width = unpremul_bitmap.width(); |
| 72 // Attach 'bitmap' to the data_object. | 37 int height = unpremul_bitmap.height(); |
| 73 SetDragImageOnDataObject( | 38 size_t rowbytes = unpremul_bitmap.rowBytes(); |
| 74 bitmap, | 39 DCHECK_EQ(rowbytes, static_cast<size_t>(width) * 4u); |
| 75 gfx::Size(image_skia.bitmap()->width(), image_skia.bitmap()->height()), | 40 |
| 76 cursor_offset, | 41 void* bits; |
| 77 ui::OSExchangeDataProviderWin::GetIDataObject(*data_object)); | 42 HBITMAP hbitmap; |
| 43 { | |
| 44 BITMAPINFOHEADER header; | |
| 45 skia::CreateBitmapHeader(width, height, &header); | |
| 46 | |
| 47 base::win::ScopedGetDC screen_dc(NULL); | |
|
danakj
2017/04/03 22:34:01
I scoped this DC to just be around CreateDIBSectio
Peter Kasting
2017/04/04 00:31:14
It makes me a little leery. I suggest just not pu
danakj
2017/04/04 15:25:33
I like to scope things as tightly as possible, if
| |
| 48 // By giving a null hSection, the |bits| will be destroyed when the | |
| 49 // |hbitmap| is destroyed. | |
| 50 hbitmap = | |
| 51 CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header), | |
| 52 DIB_RGB_COLORS, &bits, NULL, 0); | |
| 53 } | |
| 54 if (!hbitmap) | |
| 55 return; | |
| 56 | |
| 57 { | |
| 58 SkAutoLockPixels lock(unpremul_bitmap); | |
| 59 memcpy(bits, unpremul_bitmap.getPixels(), height * rowbytes); | |
| 78 } | 60 } |
| 79 | 61 |
| 80 // TODO: the above code is used in non-Ash, while below is used in Ash. If we | 62 base::win::ScopedComPtr<IDragSourceHelper> helper; |
| 81 // could figure this context out then we wouldn't do unnecessary work. However | 63 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, |
| 82 // as it stands getting this information in ui/base would be a layering | 64 IID_IDragSourceHelper, helper.ReceiveVoid()); |
| 83 // violation. | 65 if (!SUCCEEDED(rv)) |
| 84 data_object->provider().SetDragImage(image_skia, cursor_offset); | 66 return; |
| 67 | |
| 68 SHDRAGIMAGE sdi; | |
| 69 sdi.sizeDragImage.cx = width; | |
| 70 sdi.sizeDragImage.cy = height; | |
| 71 sdi.crColorKey = 0xFFFFFFFF; | |
| 72 sdi.hbmpDragImage = hbitmap; | |
| 73 sdi.ptOffset = gfx::PointAtOffsetFromOrigin(cursor_offset).ToPOINT(); | |
| 74 helper->InitializeFromBitmap(&sdi, data_object); | |
| 85 } | 75 } |
| 86 | 76 |
| 87 } // namespace drag_utils | 77 } // namespace drag_utils |
| OLD | NEW |