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"
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_CURRENTLY_ON(BrowserThread::UI
);
24 LONG style
= GetWindowLong(hwnd_
, GWL_EXSTYLE
);
25 is_hwnd_composited_
= !!(style
& WS_EX_COMPOSITED
);
28 SoftwareOutputDeviceWin::~SoftwareOutputDeviceWin() {
29 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
32 void SoftwareOutputDeviceWin::Resize(const gfx::Size
& viewport_pixel_size
,
34 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
36 scale_factor_
= scale_factor
;
38 if (viewport_pixel_size_
== viewport_pixel_size
)
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
);
49 SkCanvas
* SoftwareOutputDeviceWin::BeginPaint(const gfx::Rect
& damage_rect
) {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
53 damage_rect_
= damage_rect
;
54 return contents_
? contents_
->sk_canvas() : NULL
;
57 void SoftwareOutputDeviceWin::EndPaint(cc::SoftwareFrameData
* frame_data
) {
58 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
65 SoftwareOutputDevice::EndPaint(frame_data
);
67 gfx::Rect rect
= damage_rect_
;
68 rect
.Intersect(gfx::Rect(viewport_pixel_size_
));
72 SkCanvas
* canvas
= contents_
->sk_canvas();
74 if (is_hwnd_composited_
) {
76 GetWindowRect(hwnd_
, &wr
);
77 SIZE size
= {wr
.right
- wr
.left
, wr
.bottom
- wr
.top
};
78 POINT position
= {wr
.left
, wr
.top
};
80 BLENDFUNCTION blend
= {AC_SRC_OVER
, 0x00, 0xFF, AC_SRC_ALPHA
};
82 DWORD style
= GetWindowLong(hwnd_
, GWL_EXSTYLE
);
83 style
&= ~WS_EX_COMPOSITED
;
84 style
|= WS_EX_LAYERED
;
85 SetWindowLong(hwnd_
, GWL_EXSTYLE
, style
);
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
);
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
);
99 void SoftwareOutputDeviceWin::CopyToPixels(const gfx::Rect
& rect
,
102 SkImageInfo info
= SkImageInfo::MakeN32Premul(rect
.width(), rect
.height());
103 contents_
->sk_canvas()->readPixels(
104 info
, pixels
, info
.minRowBytes(), rect
.x(), rect
.y());
107 } // namespace content