Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / compositor / software_output_device_win.cc
blob3c938bf325ae2eac4bdfd032bfdd27b5b625b50e
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.
5 #include "content/browser/compositor/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"
14 #include "ui/gfx/skia_util.h"
16 namespace content {
18 SoftwareOutputDeviceWin::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));
24 LONG style = GetWindowLong(hwnd_, GWL_EXSTYLE);
25 is_hwnd_composited_ = !!(style & WS_EX_COMPOSITED);
28 SoftwareOutputDeviceWin::~SoftwareOutputDeviceWin() {
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32 void SoftwareOutputDeviceWin::Resize(const gfx::Size& viewport_size) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35 if (viewport_size_ == viewport_size)
36 return;
38 viewport_size_ = viewport_size;
39 contents_.reset(new gfx::Canvas(viewport_size, 1.0f, true));
40 memset(&bitmap_info_, 0, sizeof(bitmap_info_));
41 gfx::CreateBitmapHeader(viewport_size_.width(), viewport_size_.height(),
42 &bitmap_info_.bmiHeader);
45 SkCanvas* SoftwareOutputDeviceWin::BeginPaint(const gfx::Rect& damage_rect) {
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
47 DCHECK(contents_);
49 damage_rect_ = damage_rect;
50 return contents_ ? contents_->sk_canvas() : NULL;
53 void SoftwareOutputDeviceWin::EndPaint(cc::SoftwareFrameData* frame_data) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 DCHECK(contents_);
56 DCHECK(frame_data);
58 if (!contents_)
59 return;
61 SoftwareOutputDevice::EndPaint(frame_data);
63 gfx::Rect rect = damage_rect_;
64 rect.Intersect(gfx::Rect(viewport_size_));
65 if (rect.IsEmpty())
66 return;
68 SkCanvas* canvas = contents_->sk_canvas();
69 DCHECK(canvas);
70 if (is_hwnd_composited_) {
71 RECT wr;
72 GetWindowRect(hwnd_, &wr);
73 SIZE size = {wr.right - wr.left, wr.bottom - wr.top};
74 POINT position = {wr.left, wr.top};
75 POINT zero = {0, 0};
76 BLENDFUNCTION blend = {AC_SRC_OVER, 0x00, 0xFF, AC_SRC_ALPHA};
78 DWORD style = GetWindowLong(hwnd_, GWL_EXSTYLE);
79 style &= ~WS_EX_COMPOSITED;
80 style |= WS_EX_LAYERED;
81 SetWindowLong(hwnd_, GWL_EXSTYLE, style);
83 HDC dib_dc = skia::BeginPlatformPaint(canvas);
84 ::UpdateLayeredWindow(hwnd_, NULL, &position, &size, dib_dc, &zero,
85 RGB(0xFF, 0xFF, 0xFF), &blend, ULW_ALPHA);
86 skia::EndPlatformPaint(canvas);
87 } else {
88 HDC hdc = ::GetDC(hwnd_);
89 RECT src_rect = rect.ToRECT();
90 skia::DrawToNativeContext(canvas, hdc, rect.x(), rect.y(), &src_rect);
91 ::ReleaseDC(hwnd_, hdc);
95 void SoftwareOutputDeviceWin::CopyToPixels(const gfx::Rect& rect,
96 void* pixels) {
97 DCHECK(contents_);
98 SkImageInfo info = SkImageInfo::MakeN32Premul(rect.width(), rect.height());
99 contents_->sk_canvas()->readPixels(
100 info, pixels, info.minRowBytes(), rect.x(), rect.y());
103 } // namespace content