Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: components/test_runner/pixel_dump.cc

Issue 1842353002: Revert of Moving pixel-capturing code from web_test_proxy_base.* into pixel_dump.* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@replicating-accept-languages
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/test_runner/pixel_dump.h ('k') | components/test_runner/test_runner.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 "components/test_runner/pixel_dump.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/trace_event/trace_event.h"
14 #include "components/test_runner/layout_test_runtime_flags.h"
15 // FIXME: Including platform_canvas.h here is a layering violation.
16 #include "skia/ext/platform_canvas.h"
17 #include "third_party/WebKit/public/platform/Platform.h"
18 #include "third_party/WebKit/public/platform/WebClipboard.h"
19 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallbac k.h"
20 #include "third_party/WebKit/public/platform/WebData.h"
21 #include "third_party/WebKit/public/platform/WebImage.h"
22 #include "third_party/WebKit/public/platform/WebPoint.h"
23 #include "third_party/WebKit/public/web/WebFrame.h"
24 #include "third_party/WebKit/public/web/WebPagePopup.h"
25 #include "third_party/WebKit/public/web/WebPrintParams.h"
26 #include "third_party/WebKit/public/web/WebView.h"
27 #include "ui/gfx/geometry/point.h"
28
29 namespace test_runner {
30
31 namespace {
32
33 struct PixelsDumpRequest {
34 PixelsDumpRequest(blink::WebView* web_view,
35 const LayoutTestRuntimeFlags& layout_test_runtime_flags,
36 const base::Callback<void(const SkBitmap&)>& callback)
37 : web_view(web_view),
38 layout_test_runtime_flags(layout_test_runtime_flags),
39 callback(callback) {}
40
41 blink::WebView* web_view;
42 const LayoutTestRuntimeFlags& layout_test_runtime_flags;
43 base::Callback<void(const SkBitmap&)> callback;
44 };
45
46 class CaptureCallback : public blink::WebCompositeAndReadbackAsyncCallback {
47 public:
48 CaptureCallback(const base::Callback<void(const SkBitmap&)>& callback);
49 virtual ~CaptureCallback();
50
51 void set_wait_for_popup(bool wait) { wait_for_popup_ = wait; }
52 void set_popup_position(const gfx::Point& position) {
53 popup_position_ = position;
54 }
55
56 // WebCompositeAndReadbackAsyncCallback implementation.
57 void didCompositeAndReadback(const SkBitmap& bitmap) override;
58
59 private:
60 base::Callback<void(const SkBitmap&)> callback_;
61 SkBitmap main_bitmap_;
62 bool wait_for_popup_;
63 gfx::Point popup_position_;
64 };
65
66 void DrawSelectionRect(const PixelsDumpRequest& dump_request,
67 SkCanvas* canvas) {
68 // See if we need to draw the selection bounds rect. Selection bounds
69 // rect is the rect enclosing the (possibly transformed) selection.
70 // The rect should be drawn after everything is laid out and painted.
71 if (!dump_request.layout_test_runtime_flags.dump_selection_rect())
72 return;
73 // If there is a selection rect - draw a red 1px border enclosing rect
74 blink::WebRect wr = dump_request.web_view->mainFrame()->selectionBoundsRect();
75 if (wr.isEmpty())
76 return;
77 // Render a red rectangle bounding selection rect
78 SkPaint paint;
79 paint.setColor(0xFFFF0000); // Fully opaque red
80 paint.setStyle(SkPaint::kStroke_Style);
81 paint.setFlags(SkPaint::kAntiAlias_Flag);
82 paint.setStrokeWidth(1.0f);
83 SkIRect rect; // Bounding rect
84 rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height);
85 canvas->drawIRect(rect, paint);
86 }
87
88 void CapturePixelsForPrinting(scoped_ptr<PixelsDumpRequest> dump_request) {
89 dump_request->web_view->updateAllLifecyclePhases();
90
91 blink::WebSize page_size_in_pixels = dump_request->web_view->size();
92 blink::WebFrame* web_frame = dump_request->web_view->mainFrame();
93
94 int page_count = web_frame->printBegin(page_size_in_pixels);
95 int totalHeight = page_count * (page_size_in_pixels.height + 1) - 1;
96
97 bool is_opaque = false;
98 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::TryCreateBitmapCanvas(
99 page_size_in_pixels.width, totalHeight, is_opaque)));
100 if (!canvas) {
101 dump_request->callback.Run(SkBitmap());
102 return;
103 }
104 web_frame->printPagesWithBoundaries(canvas.get(), page_size_in_pixels);
105 web_frame->printEnd();
106
107 DrawSelectionRect(*dump_request, canvas.get());
108 const SkBitmap bitmap = skia::ReadPixels(canvas.get());
109 dump_request->callback.Run(bitmap);
110 }
111
112 CaptureCallback::CaptureCallback(
113 const base::Callback<void(const SkBitmap&)>& callback)
114 : callback_(callback), wait_for_popup_(false) {}
115
116 CaptureCallback::~CaptureCallback() {}
117
118 void CaptureCallback::didCompositeAndReadback(const SkBitmap& bitmap) {
119 TRACE_EVENT2("shell", "CaptureCallback::didCompositeAndReadback", "x",
120 bitmap.info().width(), "y", bitmap.info().height());
121 if (!wait_for_popup_) {
122 callback_.Run(bitmap);
123 delete this;
124 return;
125 }
126 if (main_bitmap_.isNull()) {
127 bitmap.deepCopyTo(&main_bitmap_);
128 return;
129 }
130 SkCanvas canvas(main_bitmap_);
131 canvas.drawBitmap(bitmap, popup_position_.x(), popup_position_.y());
132 callback_.Run(main_bitmap_);
133 delete this;
134 }
135
136 void DidCapturePixelsAsync(scoped_ptr<PixelsDumpRequest> dump_request,
137 const SkBitmap& bitmap) {
138 SkCanvas canvas(bitmap);
139 DrawSelectionRect(*dump_request, &canvas);
140 if (!dump_request->callback.is_null())
141 dump_request->callback.Run(bitmap);
142 }
143
144 } // namespace
145
146 void DumpPixelsAsync(blink::WebView* web_view,
147 const LayoutTestRuntimeFlags& layout_test_runtime_flags,
148 float device_scale_factor_for_test,
149 const base::Callback<void(const SkBitmap&)>& callback) {
150 TRACE_EVENT0("shell", "WebTestProxyBase::CapturePixelsAsync");
151 DCHECK(!callback.is_null());
152 DCHECK(!layout_test_runtime_flags.dump_drag_image());
153
154 scoped_ptr<PixelsDumpRequest> pixels_request(
155 new PixelsDumpRequest(web_view, layout_test_runtime_flags, callback));
156
157 if (layout_test_runtime_flags.is_printing()) {
158 base::ThreadTaskRunnerHandle::Get()->PostTask(
159 FROM_HERE, base::Bind(&CapturePixelsForPrinting,
160 base::Passed(std::move(pixels_request))));
161 return;
162 }
163
164 CaptureCallback* capture_callback = new CaptureCallback(base::Bind(
165 &DidCapturePixelsAsync, base::Passed(std::move(pixels_request))));
166 web_view->compositeAndReadbackAsync(capture_callback);
167 if (blink::WebPagePopup* popup = web_view->pagePopup()) {
168 capture_callback->set_wait_for_popup(true);
169 blink::WebPoint position = popup->positionRelativeToOwner();
170 position.x *= device_scale_factor_for_test;
171 position.y *= device_scale_factor_for_test;
172 capture_callback->set_popup_position(position);
173 popup->compositeAndReadbackAsync(capture_callback);
174 }
175 }
176
177 void CopyImageAtAndCapturePixels(
178 blink::WebView* web_view,
179 int x,
180 int y,
181 const base::Callback<void(const SkBitmap&)>& callback) {
182 DCHECK(!callback.is_null());
183 uint64_t sequence_number =
184 blink::Platform::current()->clipboard()->sequenceNumber(
185 blink::WebClipboard::Buffer());
186 web_view->copyImageAt(blink::WebPoint(x, y));
187 if (sequence_number ==
188 blink::Platform::current()->clipboard()->sequenceNumber(
189 blink::WebClipboard::Buffer())) {
190 SkBitmap emptyBitmap;
191 callback.Run(emptyBitmap);
192 return;
193 }
194
195 blink::WebData data = blink::Platform::current()->clipboard()->readImage(
196 blink::WebClipboard::Buffer());
197 blink::WebImage image = blink::WebImage::fromData(data, blink::WebSize());
198 const SkBitmap& bitmap = image.getSkBitmap();
199 SkAutoLockPixels autoLock(bitmap);
200 callback.Run(bitmap);
201 }
202
203 } // namespace test_runner
OLDNEW
« no previous file with comments | « components/test_runner/pixel_dump.h ('k') | components/test_runner/test_runner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698