| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/panels/taskbar_window_thumbnailer_win.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "skia/ext/image_operations.h" |
| 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/gdi_util.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 HBITMAP GetNativeBitmapFromSkBitmap(const SkBitmap& bitmap) { |
| 15 int width = bitmap.width(); |
| 16 int height = bitmap.height(); |
| 17 |
| 18 BITMAPV4HEADER native_bitmap_header; |
| 19 gfx::CreateBitmapV4Header(width, height, &native_bitmap_header); |
| 20 |
| 21 HDC dc = ::GetDC(NULL); |
| 22 void* bits; |
| 23 HBITMAP native_bitmap = ::CreateDIBSection(dc, |
| 24 reinterpret_cast<BITMAPINFO*>(&native_bitmap_header), |
| 25 DIB_RGB_COLORS, |
| 26 &bits, |
| 27 NULL, |
| 28 0); |
| 29 DCHECK(native_bitmap); |
| 30 ::ReleaseDC(NULL, dc); |
| 31 bitmap.copyPixelsTo(bits, width * height * 4, width * 4); |
| 32 return native_bitmap; |
| 33 } |
| 34 |
| 35 void EnableCustomThumbnail(HWND hwnd, bool enable) { |
| 36 BOOL enable_value = enable; |
| 37 ::DwmSetWindowAttribute(hwnd, |
| 38 DWMWA_FORCE_ICONIC_REPRESENTATION, |
| 39 &enable, |
| 40 sizeof(enable_value)); |
| 41 ::DwmSetWindowAttribute(hwnd, |
| 42 DWMWA_HAS_ICONIC_BITMAP, |
| 43 &enable, |
| 44 sizeof(enable_value)); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 |
| 50 TaskbarWindowThumbnailerWin::TaskbarWindowThumbnailerWin(HWND hwnd) |
| 51 : hwnd_(hwnd) { |
| 52 capture_bitmap_.reset(CaptureWindowImage()); |
| 53 EnableCustomThumbnail(hwnd_, true); |
| 54 } |
| 55 |
| 56 TaskbarWindowThumbnailerWin::~TaskbarWindowThumbnailerWin() { |
| 57 EnableCustomThumbnail(hwnd_, false); |
| 58 } |
| 59 |
| 60 bool TaskbarWindowThumbnailerWin::FilterMessage(HWND hwnd, |
| 61 UINT message, |
| 62 WPARAM w_param, |
| 63 LPARAM l_param, |
| 64 LRESULT* l_result) { |
| 65 DCHECK_EQ(hwnd_, hwnd); |
| 66 switch (message) { |
| 67 case WM_DWMSENDICONICTHUMBNAIL: |
| 68 return OnDwmSendIconicThumbnail(HIWORD(l_param), |
| 69 LOWORD(l_param), |
| 70 l_result); |
| 71 case WM_DWMSENDICONICLIVEPREVIEWBITMAP: |
| 72 return OnDwmSendIconicLivePreviewBitmap(l_result); |
| 73 } |
| 74 return false; |
| 75 } |
| 76 |
| 77 bool TaskbarWindowThumbnailerWin::OnDwmSendIconicThumbnail( |
| 78 int width, int height, LRESULT* l_result) { |
| 79 DCHECK(capture_bitmap_.get()); |
| 80 |
| 81 SkBitmap* thumbnail_bitmap = capture_bitmap_.get(); |
| 82 |
| 83 // Scale the image if needed. |
| 84 SkBitmap scaled_bitmap; |
| 85 if (capture_bitmap_->width() != width || |
| 86 capture_bitmap_->height() != height) { |
| 87 double x_scale = static_cast<double>(width) / capture_bitmap_->width(); |
| 88 double y_scale = static_cast<double>(height) / capture_bitmap_->height(); |
| 89 double scale = std::min(x_scale, y_scale); |
| 90 width = capture_bitmap_->width() * scale; |
| 91 height = capture_bitmap_->height() * scale; |
| 92 scaled_bitmap = skia::ImageOperations::Resize( |
| 93 *capture_bitmap_, skia::ImageOperations::RESIZE_GOOD, width, height); |
| 94 thumbnail_bitmap = &scaled_bitmap; |
| 95 } |
| 96 |
| 97 HBITMAP native_bitmap = GetNativeBitmapFromSkBitmap(*thumbnail_bitmap); |
| 98 ::DwmSetIconicThumbnail(hwnd_, native_bitmap, 0); |
| 99 ::DeleteObject(native_bitmap); |
| 100 |
| 101 *l_result = 0; |
| 102 return true; |
| 103 } |
| 104 |
| 105 bool TaskbarWindowThumbnailerWin::OnDwmSendIconicLivePreviewBitmap( |
| 106 LRESULT* l_result) { |
| 107 scoped_ptr<SkBitmap> live_bitmap(CaptureWindowImage()); |
| 108 HBITMAP native_bitmap = GetNativeBitmapFromSkBitmap(*live_bitmap); |
| 109 ::DwmSetIconicLivePreviewBitmap(hwnd_, native_bitmap, NULL, 0); |
| 110 ::DeleteObject(native_bitmap); |
| 111 |
| 112 *l_result = 0; |
| 113 return true; |
| 114 } |
| 115 |
| 116 SkBitmap* TaskbarWindowThumbnailerWin::CaptureWindowImage() const { |
| 117 RECT bounds; |
| 118 ::GetWindowRect(hwnd_, &bounds); |
| 119 int width = bounds.right - bounds.left; |
| 120 int height = bounds.bottom - bounds.top; |
| 121 |
| 122 gfx::Canvas canvas(gfx::Size(width, height), false); |
| 123 HDC target_dc = canvas.BeginPlatformPaint(); |
| 124 HDC source_dc = ::GetDC(hwnd_); |
| 125 ::BitBlt(target_dc, 0, 0, width, height, source_dc, 0, 0, SRCCOPY); |
| 126 ::ReleaseDC(hwnd_, source_dc); |
| 127 canvas.EndPlatformPaint(); |
| 128 return new SkBitmap(canvas.ExtractBitmap()); |
| 129 } |
| OLD | NEW |