1// Copyright 2014 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 "content/browser/compositor/software_output_device_win.h"
6
7#include "content/public/browser/browser_thread.h"
8#include "third_party/skia/include/core/SkBitmap.h"
9#include "third_party/skia/include/core/SkDevice.h"
10#include "ui/compositor/compositor.h"
11#include "ui/gfx/canvas.h"
12#include "ui/gfx/canvas_skia_paint.h"
13#include "ui/gfx/gdi_util.h"
14#include "ui/gfx/skia_util.h"
15
16namespace content {
17
18SoftwareOutputDeviceWin::SoftwareOutputDeviceWin(ui::Compositor* compositor)
19    : hwnd_(compositor->widget()),
20      is_hwnd_composited_(false) {
21  // TODO(skaslev) Remove this when crbug.com/180702 is fixed.
22  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
23
24  LONG style = GetWindowLong(hwnd_, GWL_EXSTYLE);
25  is_hwnd_composited_ = !!(style & WS_EX_COMPOSITED);
26}
27
28SoftwareOutputDeviceWin::~SoftwareOutputDeviceWin() {
29  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
30}
31
32void SoftwareOutputDeviceWin::Resize(const gfx::Size& viewport_pixel_size,
33                                     float scale_factor) {
34  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35
36  scale_factor_ = scale_factor;
37
38  if (viewport_pixel_size_ == viewport_pixel_size)
39    return;
40
41  viewport_pixel_size_ = viewport_pixel_size;
42  contents_.reset(new gfx::Canvas(viewport_pixel_size, 1.0f, true));
43  memset(&bitmap_info_, 0, sizeof(bitmap_info_));
44  gfx::CreateBitmapHeader(viewport_pixel_size_.width(),
45                          viewport_pixel_size_.height(),
46                          &bitmap_info_.bmiHeader);
47}
48
49SkCanvas* SoftwareOutputDeviceWin::BeginPaint(const gfx::Rect& damage_rect) {
50  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
51  DCHECK(contents_);
52
53  damage_rect_ = damage_rect;
54  return contents_ ? contents_->sk_canvas() : NULL;
55}
56
57void SoftwareOutputDeviceWin::EndPaint(cc::SoftwareFrameData* frame_data) {
58  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
59  DCHECK(contents_);
60  DCHECK(frame_data);
61
62  if (!contents_)
63    return;
64
65  SoftwareOutputDevice::EndPaint(frame_data);
66
67  gfx::Rect rect = damage_rect_;
68  rect.Intersect(gfx::Rect(viewport_pixel_size_));
69  if (rect.IsEmpty())
70    return;
71
72  SkCanvas* canvas = contents_->sk_canvas();
73  DCHECK(canvas);
74  if (is_hwnd_composited_) {
75    RECT wr;
76    GetWindowRect(hwnd_, &wr);
77    SIZE size = {wr.right - wr.left, wr.bottom - wr.top};
78    POINT position = {wr.left, wr.top};
79    POINT zero = {0, 0};
80    BLENDFUNCTION blend = {AC_SRC_OVER, 0x00, 0xFF, AC_SRC_ALPHA};
81
82    DWORD style = GetWindowLong(hwnd_, GWL_EXSTYLE);
83    style &= ~WS_EX_COMPOSITED;
84    style |= WS_EX_LAYERED;
85    SetWindowLong(hwnd_, GWL_EXSTYLE, style);
86
87    HDC dib_dc = skia::BeginPlatformPaint(canvas);
88    ::UpdateLayeredWindow(hwnd_, NULL, &position, &size, dib_dc, &zero,
89                          RGB(0xFF, 0xFF, 0xFF), &blend, ULW_ALPHA);
90    skia::EndPlatformPaint(canvas);
91  } else {
92    HDC hdc = ::GetDC(hwnd_);
93    RECT src_rect = rect.ToRECT();
94    skia::DrawToNativeContext(canvas, hdc, rect.x(), rect.y(), &src_rect);
95    ::ReleaseDC(hwnd_, hdc);
96  }
97}
98
99void SoftwareOutputDeviceWin::CopyToPixels(const gfx::Rect& rect,
100                                           void* pixels) {
101  DCHECK(contents_);
102  SkImageInfo info = SkImageInfo::MakeN32Premul(rect.width(), rect.height());
103  contents_->sk_canvas()->readPixels(
104      info, pixels, info.minRowBytes(), rect.x(), rect.y());
105}
106
107}  // namespace content
108