| 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_value, |
| 40 sizeof(enable_value)); |
| 41 ::DwmSetWindowAttribute(hwnd, |
| 42 DWMWA_HAS_ICONIC_BITMAP, |
| 43 &enable_value, |
| 44 sizeof(enable_value)); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 |
| 50 TaskbarWindowThumbnailerWin::TaskbarWindowThumbnailerWin(HWND hwnd) |
| 51 : hwnd_(hwnd) { |
| 52 } |
| 53 |
| 54 TaskbarWindowThumbnailerWin::~TaskbarWindowThumbnailerWin() { |
| 55 } |
| 56 |
| 57 void TaskbarWindowThumbnailerWin::Start() { |
| 58 capture_bitmap_.reset(CaptureWindowImage()); |
| 59 EnableCustomThumbnail(hwnd_, true); |
| 60 } |
| 61 |
| 62 void TaskbarWindowThumbnailerWin::Stop() { |
| 63 capture_bitmap_.reset(); |
| 64 EnableCustomThumbnail(hwnd_, false); |
| 65 } |
| 66 |
| 67 bool TaskbarWindowThumbnailerWin::FilterMessage(HWND hwnd, |
| 68 UINT message, |
| 69 WPARAM w_param, |
| 70 LPARAM l_param, |
| 71 LRESULT* l_result) { |
| 72 DCHECK_EQ(hwnd_, hwnd); |
| 73 switch (message) { |
| 74 case WM_DWMSENDICONICTHUMBNAIL: |
| 75 return OnDwmSendIconicThumbnail(HIWORD(l_param), |
| 76 LOWORD(l_param), |
| 77 l_result); |
| 78 case WM_DWMSENDICONICLIVEPREVIEWBITMAP: |
| 79 return OnDwmSendIconicLivePreviewBitmap(l_result); |
| 80 } |
| 81 return false; |
| 82 } |
| 83 |
| 84 bool TaskbarWindowThumbnailerWin::OnDwmSendIconicThumbnail( |
| 85 int width, int height, LRESULT* l_result) { |
| 86 DCHECK(capture_bitmap_.get()); |
| 87 |
| 88 SkBitmap* thumbnail_bitmap = capture_bitmap_.get(); |
| 89 |
| 90 // Scale the image if needed. |
| 91 SkBitmap scaled_bitmap; |
| 92 if (capture_bitmap_->width() != width || |
| 93 capture_bitmap_->height() != height) { |
| 94 double x_scale = static_cast<double>(width) / capture_bitmap_->width(); |
| 95 double y_scale = static_cast<double>(height) / capture_bitmap_->height(); |
| 96 double scale = std::min(x_scale, y_scale); |
| 97 width = capture_bitmap_->width() * scale; |
| 98 height = capture_bitmap_->height() * scale; |
| 99 scaled_bitmap = skia::ImageOperations::Resize( |
| 100 *capture_bitmap_, skia::ImageOperations::RESIZE_GOOD, width, height); |
| 101 thumbnail_bitmap = &scaled_bitmap; |
| 102 } |
| 103 |
| 104 HBITMAP native_bitmap = GetNativeBitmapFromSkBitmap(*thumbnail_bitmap); |
| 105 ::DwmSetIconicThumbnail(hwnd_, native_bitmap, 0); |
| 106 ::DeleteObject(native_bitmap); |
| 107 |
| 108 *l_result = 0; |
| 109 return true; |
| 110 } |
| 111 |
| 112 bool TaskbarWindowThumbnailerWin::OnDwmSendIconicLivePreviewBitmap( |
| 113 LRESULT* l_result) { |
| 114 scoped_ptr<SkBitmap> live_bitmap(CaptureWindowImage()); |
| 115 HBITMAP native_bitmap = GetNativeBitmapFromSkBitmap(*live_bitmap); |
| 116 ::DwmSetIconicLivePreviewBitmap(hwnd_, native_bitmap, NULL, 0); |
| 117 ::DeleteObject(native_bitmap); |
| 118 |
| 119 *l_result = 0; |
| 120 return true; |
| 121 } |
| 122 |
| 123 SkBitmap* TaskbarWindowThumbnailerWin::CaptureWindowImage() const { |
| 124 RECT bounds; |
| 125 ::GetWindowRect(hwnd_, &bounds); |
| 126 int width = bounds.right - bounds.left; |
| 127 int height = bounds.bottom - bounds.top; |
| 128 |
| 129 gfx::Canvas canvas(gfx::Size(width, height), false); |
| 130 HDC target_dc = canvas.BeginPlatformPaint(); |
| 131 HDC source_dc = ::GetDC(hwnd_); |
| 132 ::BitBlt(target_dc, 0, 0, width, height, source_dc, 0, 0, SRCCOPY); |
| 133 ::ReleaseDC(hwnd_, source_dc); |
| 134 canvas.EndPlatformPaint(); |
| 135 return new SkBitmap(canvas.ExtractBitmap()); |
| 136 } |
| OLD | NEW |