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