Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / software_output_device_win.cc
blob6535d4d23e382e3627a88493cfa871a185a9786c
1 // Copyright (c) 2013 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.
5 #include "content/browser/renderer_host/software_output_device_win.h"
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"
15 namespace content {
17 SoftwareOutputDeviceWin::SoftwareOutputDeviceWin(ui::Compositor* compositor)
18 : hwnd_(compositor->widget()),
19 is_hwnd_composited_(false) {
20 // TODO(skaslev) Remove this when crbug.com/180702 is fixed.
21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
23 LONG style = GetWindowLong(hwnd_, GWL_EXSTYLE);
24 is_hwnd_composited_ = !!(style & WS_EX_COMPOSITED);
27 SoftwareOutputDeviceWin::~SoftwareOutputDeviceWin() {
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
31 void SoftwareOutputDeviceWin::Resize(gfx::Size viewport_size) {
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
34 if (viewport_size_ == viewport_size)
35 return;
37 viewport_size_ = viewport_size;
38 contents_.reset(new gfx::Canvas(viewport_size, ui::SCALE_FACTOR_100P, false));
39 memset(&bitmap_info_, 0, sizeof(bitmap_info_));
40 gfx::CreateBitmapHeader(viewport_size_.width(), viewport_size_.height(),
41 &bitmap_info_.bmiHeader);
44 SkCanvas* SoftwareOutputDeviceWin::BeginPaint(gfx::Rect damage_rect) {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
46 DCHECK(contents_);
48 damage_rect_ = damage_rect;
49 return contents_ ? contents_->sk_canvas() : NULL;
52 void SoftwareOutputDeviceWin::EndPaint(cc::SoftwareFrameData* frame_data) {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
54 DCHECK(contents_);
55 DCHECK(frame_data);
57 if (!contents_)
58 return;
60 SoftwareOutputDevice::EndPaint(frame_data);
62 gfx::Rect rect = damage_rect_;
63 rect.Intersect(gfx::Rect(viewport_size_));
64 if (rect.IsEmpty())
65 return;
67 SkCanvas* canvas = contents_->sk_canvas();
68 DCHECK(canvas);
69 if (is_hwnd_composited_) {
70 RECT wr;
71 GetWindowRect(hwnd_, &wr);
72 SIZE size = {wr.right - wr.left, wr.bottom - wr.top};
73 POINT position = {wr.left, wr.top};
74 POINT zero = {0, 0};
75 BLENDFUNCTION blend = {AC_SRC_OVER, 0x00, 0xFF, AC_SRC_ALPHA};
77 DWORD style = GetWindowLong(hwnd_, GWL_EXSTYLE);
78 style &= ~WS_EX_COMPOSITED;
79 style |= WS_EX_LAYERED;
80 SetWindowLong(hwnd_, GWL_EXSTYLE, style);
82 HDC dib_dc = skia::BeginPlatformPaint(canvas);
83 ::UpdateLayeredWindow(hwnd_, NULL, &position, &size, dib_dc, &zero,
84 RGB(0xFF, 0xFF, 0xFF), &blend, ULW_ALPHA);
85 skia::EndPlatformPaint(canvas);
86 } else {
87 SkDevice* device = canvas->getDevice();
88 const SkBitmap& bitmap = device->accessBitmap(false);
89 HDC hdc = ::GetDC(hwnd_);
90 gfx::StretchDIBits(hdc,
91 rect.x(), rect.y(),
92 rect.width(), rect.height(),
93 rect.x(), rect.y(),
94 rect.width(), rect.height(),
95 bitmap.getPixels(),
96 &bitmap_info_);
97 ::ReleaseDC(hwnd_, hdc);
101 } // namespace content