Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: ui/base/clipboard/clipboard_win.cc

Issue 2615683002: don't need gfx::Canvas and its native context to convert HBITMAP (Closed)
Patch Set: skia::InitializeDC Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Many of these functions are based on those found in 5 // Many of these functions are based on those found in
6 // webkit/port/platform/PasteboardWin.cpp 6 // webkit/port/platform/PasteboardWin.cpp
7 7
8 #include "ui/base/clipboard/clipboard_win.h" 8 #include "ui/base/clipboard/clipboard_win.h"
9 9
10 #include <shellapi.h> 10 #include <shellapi.h>
11 #include <shlobj.h> 11 #include <shlobj.h>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
19 #include "base/numerics/safe_conversions.h" 19 #include "base/numerics/safe_conversions.h"
20 #include "base/stl_util.h" 20 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
23 #include "base/strings/utf_offset_string_conversions.h" 23 #include "base/strings/utf_offset_string_conversions.h"
24 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
25 #include "base/win/message_window.h" 25 #include "base/win/message_window.h"
26 #include "base/win/scoped_gdi_object.h" 26 #include "base/win/scoped_gdi_object.h"
27 #include "base/win/scoped_hdc.h" 27 #include "base/win/scoped_hdc.h"
28 #include "skia/ext/skia_utils_win.h"
28 #include "third_party/skia/include/core/SkBitmap.h" 29 #include "third_party/skia/include/core/SkBitmap.h"
29 #include "ui/base/clipboard/clipboard_util_win.h" 30 #include "ui/base/clipboard/clipboard_util_win.h"
30 #include "ui/base/clipboard/custom_data_helper.h" 31 #include "ui/base/clipboard/custom_data_helper.h"
31 #include "ui/gfx/canvas.h" 32 #include "ui/gfx/canvas.h"
32 #include "ui/gfx/geometry/size.h" 33 #include "ui/gfx/geometry/size.h"
33 34
34 namespace ui { 35 namespace ui {
35 36
36 namespace { 37 namespace {
37 38
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 color_table_length = 3; 605 color_table_length = 3;
605 break; 606 break;
606 case 24: 607 case 24:
607 break; 608 break;
608 default: 609 default:
609 NOTREACHED(); 610 NOTREACHED();
610 } 611 }
611 const void* bitmap_bits = reinterpret_cast<const char*>(bitmap) 612 const void* bitmap_bits = reinterpret_cast<const char*>(bitmap)
612 + bitmap->bmiHeader.biSize + color_table_length * sizeof(RGBQUAD); 613 + bitmap->bmiHeader.biSize + color_table_length * sizeof(RGBQUAD);
613 614
614 gfx::Canvas canvas(gfx::Size(bitmap->bmiHeader.biWidth, 615 void* dst_bits;
615 bitmap->bmiHeader.biHeight), 616 // dst_hbitmap is freed by the release_proc in skia_bitmap (below)
616 1.0f, 617 HBITMAP dst_hbitmap =
617 false); 618 skia::CreateHBitmap(bitmap->bmiHeader.biWidth, bitmap->bmiHeader.biHeight,
619 false, 0, &dst_bits);
620
618 { 621 {
619 HDC dc = skia::GetNativeDrawingContext(canvas.sk_canvas()); 622 HDC hdc = CreateCompatibleDC(NULL);
dcheng 2017/01/04 22:25:36 Nit: nullptr, and use ScopedCreateDC from base/win
reed1 2017/01/05 02:24:46 I would use it like this? ScopedCreateDC scoped(::
dcheng 2017/01/05 02:39:50 Yes, it would still need to be selected, but it wo
reed1 2017/01/05 16:49:09 Done.
620 ::SetDIBitsToDevice(dc, 0, 0, bitmap->bmiHeader.biWidth, 623 skia::InitializeDC(hdc);
dcheng 2017/01/04 22:25:36 Just curious: do we need to do this if we're just
reed1 2017/01/05 02:24:46 I need to draw 'bitmap' into my dst_hbitmap. Don't
dcheng 2017/01/05 02:39:50 I think the DC should already be initialized; it l
reed1 2017/01/05 03:16:08 Ah, I misinterpreted your question. Got it about I
621 bitmap->bmiHeader.biHeight, 0, 0, 0, 624 HBITMAP old_hbitmap = static_cast<HBITMAP>(SelectObject(hdc, dst_hbitmap));
622 bitmap->bmiHeader.biHeight, bitmap_bits, bitmap, 625 ::SetDIBitsToDevice(
623 DIB_RGB_COLORS); 626 hdc, 0, 0, bitmap->bmiHeader.biWidth, bitmap->bmiHeader.biHeight, 0, 0,
627 0, bitmap->bmiHeader.biHeight, bitmap_bits, bitmap, DIB_RGB_COLORS);
628 SelectObject(hdc, old_hbitmap);
629 DeleteDC(hdc);
624 } 630 }
625 // Windows doesn't really handle alpha channels well in many situations. When 631 // Windows doesn't really handle alpha channels well in many situations. When
626 // the source image is < 32 bpp, we force the bitmap to be opaque. When the 632 // the source image is < 32 bpp, we force the bitmap to be opaque. When the
627 // source image is 32 bpp, the alpha channel might still contain garbage data. 633 // source image is 32 bpp, the alpha channel might still contain garbage data.
628 // Since Windows uses premultiplied alpha, we scan for instances where 634 // Since Windows uses premultiplied alpha, we scan for instances where
629 // (R, G, B) > A. If there are any invalid premultiplied colors in the image, 635 // (R, G, B) > A. If there are any invalid premultiplied colors in the image,
630 // we assume the alpha channel contains garbage and force the bitmap to be 636 // we assume the alpha channel contains garbage and force the bitmap to be
631 // opaque as well. Note that this heuristic will fail on a transparent bitmap 637 // opaque as well. Note that this heuristic will fail on a transparent bitmap
632 // containing only black pixels... 638 // containing only black pixels...
633 SkPixmap device_pixels; 639 SkPixmap device_pixels(SkImageInfo::MakeN32Premul(bitmap->bmiHeader.biWidth,
634 skia::GetWritablePixels(canvas.sk_canvas(), &device_pixels); 640 bitmap->bmiHeader.biHeight),
641 dst_bits, bitmap->bmiHeader.biWidth * 4);
642
635 { 643 {
636 bool has_invalid_alpha_channel = bitmap->bmiHeader.biBitCount < 32 || 644 bool has_invalid_alpha_channel = bitmap->bmiHeader.biBitCount < 32 ||
637 BitmapHasInvalidPremultipliedColors(device_pixels); 645 BitmapHasInvalidPremultipliedColors(device_pixels);
638 if (has_invalid_alpha_channel) { 646 if (has_invalid_alpha_channel) {
639 MakeBitmapOpaque(&device_pixels); 647 MakeBitmapOpaque(&device_pixels);
640 } 648 }
641 } 649 }
642 650
643 return canvas.ExtractImageRep().sk_bitmap(); 651 SkBitmap skia_bitmap;
652 skia_bitmap.installPixels(device_pixels.info(), device_pixels.writable_addr(),
653 device_pixels.rowBytes(), nullptr,
654 [](void* pixels, void* hbitmap) {
655 DeleteObject(static_cast<HBITMAP>(hbitmap));
656 },
657 dst_hbitmap);
658 return skia_bitmap;
644 } 659 }
645 660
646 void ClipboardWin::ReadCustomData(ClipboardType clipboard_type, 661 void ClipboardWin::ReadCustomData(ClipboardType clipboard_type,
647 const base::string16& type, 662 const base::string16& type,
648 base::string16* result) const { 663 base::string16* result) const {
649 DCHECK_EQ(clipboard_type, CLIPBOARD_TYPE_COPY_PASTE); 664 DCHECK_EQ(clipboard_type, CLIPBOARD_TYPE_COPY_PASTE);
650 665
651 // Acquire the clipboard. 666 // Acquire the clipboard.
652 ScopedClipboard clipboard; 667 ScopedClipboard clipboard;
653 if (!clipboard.Acquire(GetClipboardWindow())) 668 if (!clipboard.Acquire(GetClipboardWindow()))
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 if (!clipboard_owner_) 896 if (!clipboard_owner_)
882 return NULL; 897 return NULL;
883 898
884 if (clipboard_owner_->hwnd() == NULL) 899 if (clipboard_owner_->hwnd() == NULL)
885 clipboard_owner_->Create(base::Bind(&ClipboardOwnerWndProc)); 900 clipboard_owner_->Create(base::Bind(&ClipboardOwnerWndProc));
886 901
887 return clipboard_owner_->hwnd(); 902 return clipboard_owner_->hwnd();
888 } 903 }
889 904
890 } // namespace ui 905 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698