| OLD | NEW |
| (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 <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | |
| 14 #include "base/trace_event/trace_event.h" | |
| 15 #include "cc/paint/paint_canvas.h" | |
| 16 #include "cc/paint/paint_flags.h" | |
| 17 #include "components/test_runner/layout_test_runtime_flags.h" | |
| 18 // FIXME: Including platform_canvas.h here is a layering violation. | |
| 19 #include "skia/ext/platform_canvas.h" | |
| 20 #include "third_party/WebKit/public/platform/Platform.h" | |
| 21 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallbac
k.h" | |
| 22 #include "third_party/WebKit/public/platform/WebImage.h" | |
| 23 #include "third_party/WebKit/public/platform/WebMockClipboard.h" | |
| 24 #include "third_party/WebKit/public/platform/WebPoint.h" | |
| 25 #include "third_party/WebKit/public/web/WebFrame.h" | |
| 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 27 #include "third_party/WebKit/public/web/WebPagePopup.h" | |
| 28 #include "third_party/WebKit/public/web/WebPrintParams.h" | |
| 29 #include "third_party/WebKit/public/web/WebView.h" | |
| 30 #include "ui/gfx/geometry/point.h" | |
| 31 | |
| 32 namespace test_runner { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 struct PixelsDumpRequest { | |
| 37 PixelsDumpRequest(blink::WebView* web_view, | |
| 38 const LayoutTestRuntimeFlags& layout_test_runtime_flags, | |
| 39 const base::Callback<void(const SkBitmap&)>& callback) | |
| 40 : web_view(web_view), | |
| 41 layout_test_runtime_flags(layout_test_runtime_flags), | |
| 42 callback(callback) {} | |
| 43 | |
| 44 blink::WebView* web_view; | |
| 45 const LayoutTestRuntimeFlags& layout_test_runtime_flags; | |
| 46 base::Callback<void(const SkBitmap&)> callback; | |
| 47 }; | |
| 48 | |
| 49 class CaptureCallback : public blink::WebCompositeAndReadbackAsyncCallback { | |
| 50 public: | |
| 51 CaptureCallback(const base::Callback<void(const SkBitmap&)>& callback); | |
| 52 virtual ~CaptureCallback(); | |
| 53 | |
| 54 void set_wait_for_popup(bool wait) { wait_for_popup_ = wait; } | |
| 55 void set_popup_position(const gfx::Point& position) { | |
| 56 popup_position_ = position; | |
| 57 } | |
| 58 | |
| 59 // WebCompositeAndReadbackAsyncCallback implementation. | |
| 60 void didCompositeAndReadback(const SkBitmap& bitmap) override; | |
| 61 | |
| 62 private: | |
| 63 base::Callback<void(const SkBitmap&)> callback_; | |
| 64 SkBitmap main_bitmap_; | |
| 65 bool wait_for_popup_; | |
| 66 gfx::Point popup_position_; | |
| 67 }; | |
| 68 | |
| 69 void DrawSelectionRect(const PixelsDumpRequest& dump_request, | |
| 70 cc::PaintCanvas* canvas) { | |
| 71 // See if we need to draw the selection bounds rect. Selection bounds | |
| 72 // rect is the rect enclosing the (possibly transformed) selection. | |
| 73 // The rect should be drawn after everything is laid out and painted. | |
| 74 if (!dump_request.layout_test_runtime_flags.dump_selection_rect()) | |
| 75 return; | |
| 76 // If there is a selection rect - draw a red 1px border enclosing rect | |
| 77 blink::WebRect wr = dump_request.web_view->mainFrame()->selectionBoundsRect(); | |
| 78 if (wr.isEmpty()) | |
| 79 return; | |
| 80 // Render a red rectangle bounding selection rect | |
| 81 cc::PaintFlags flags; | |
| 82 flags.setColor(0xFFFF0000); // Fully opaque red | |
| 83 flags.setStyle(cc::PaintFlags::kStroke_Style); | |
| 84 flags.setAntiAlias(true); | |
| 85 flags.setStrokeWidth(1.0f); | |
| 86 SkIRect rect; // Bounding rect | |
| 87 rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height); | |
| 88 canvas->drawIRect(rect, flags); | |
| 89 } | |
| 90 | |
| 91 void CapturePixelsForPrinting(std::unique_ptr<PixelsDumpRequest> dump_request) { | |
| 92 dump_request->web_view->updateAllLifecyclePhases(); | |
| 93 | |
| 94 blink::WebSize page_size_in_pixels = dump_request->web_view->size(); | |
| 95 blink::WebFrame* web_frame = dump_request->web_view->mainFrame(); | |
| 96 | |
| 97 int page_count = web_frame->printBegin(page_size_in_pixels); | |
| 98 int totalHeight = page_count * (page_size_in_pixels.height + 1) - 1; | |
| 99 | |
| 100 bool is_opaque = false; | |
| 101 | |
| 102 SkBitmap bitmap; | |
| 103 if (!bitmap.tryAllocN32Pixels(page_size_in_pixels.width, totalHeight, | |
| 104 is_opaque)) { | |
| 105 LOG(ERROR) << "Failed to create bitmap width=" << page_size_in_pixels.width | |
| 106 << " height=" << totalHeight; | |
| 107 dump_request->callback.Run(SkBitmap()); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 cc::PaintCanvas canvas(bitmap); | |
| 112 web_frame->printPagesWithBoundaries(&canvas, page_size_in_pixels); | |
| 113 web_frame->printEnd(); | |
| 114 | |
| 115 DrawSelectionRect(*dump_request, &canvas); | |
| 116 dump_request->callback.Run(bitmap); | |
| 117 } | |
| 118 | |
| 119 CaptureCallback::CaptureCallback( | |
| 120 const base::Callback<void(const SkBitmap&)>& callback) | |
| 121 : callback_(callback), wait_for_popup_(false) {} | |
| 122 | |
| 123 CaptureCallback::~CaptureCallback() {} | |
| 124 | |
| 125 void CaptureCallback::didCompositeAndReadback(const SkBitmap& bitmap) { | |
| 126 TRACE_EVENT2("shell", "CaptureCallback::didCompositeAndReadback", "x", | |
| 127 bitmap.info().width(), "y", bitmap.info().height()); | |
| 128 if (!wait_for_popup_) { | |
| 129 callback_.Run(bitmap); | |
| 130 delete this; | |
| 131 return; | |
| 132 } | |
| 133 if (main_bitmap_.isNull()) { | |
| 134 bitmap.deepCopyTo(&main_bitmap_); | |
| 135 return; | |
| 136 } | |
| 137 SkCanvas canvas(main_bitmap_); | |
| 138 canvas.drawBitmap(bitmap, popup_position_.x(), popup_position_.y()); | |
| 139 callback_.Run(main_bitmap_); | |
| 140 delete this; | |
| 141 } | |
| 142 | |
| 143 void DidCapturePixelsAsync(std::unique_ptr<PixelsDumpRequest> dump_request, | |
| 144 const SkBitmap& bitmap) { | |
| 145 cc::PaintCanvas canvas(bitmap); | |
| 146 DrawSelectionRect(*dump_request, &canvas); | |
| 147 if (!dump_request->callback.is_null()) | |
| 148 dump_request->callback.Run(bitmap); | |
| 149 } | |
| 150 | |
| 151 } // namespace | |
| 152 | |
| 153 void DumpPixelsAsync(blink::WebView* web_view, | |
| 154 const LayoutTestRuntimeFlags& layout_test_runtime_flags, | |
| 155 float device_scale_factor_for_test, | |
| 156 const base::Callback<void(const SkBitmap&)>& callback) { | |
| 157 TRACE_EVENT0("shell", "WebViewTestProxyBase::CapturePixelsAsync"); | |
| 158 DCHECK(!callback.is_null()); | |
| 159 DCHECK(!layout_test_runtime_flags.dump_drag_image()); | |
| 160 | |
| 161 std::unique_ptr<PixelsDumpRequest> pixels_request( | |
| 162 new PixelsDumpRequest(web_view, layout_test_runtime_flags, callback)); | |
| 163 | |
| 164 if (layout_test_runtime_flags.is_printing()) { | |
| 165 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 166 FROM_HERE, base::Bind(&CapturePixelsForPrinting, | |
| 167 base::Passed(std::move(pixels_request)))); | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 CaptureCallback* capture_callback = new CaptureCallback(base::Bind( | |
| 172 &DidCapturePixelsAsync, base::Passed(std::move(pixels_request)))); | |
| 173 web_view->compositeAndReadbackAsync(capture_callback); | |
| 174 if (blink::WebPagePopup* popup = web_view->pagePopup()) { | |
| 175 capture_callback->set_wait_for_popup(true); | |
| 176 blink::WebPoint position = popup->positionRelativeToOwner(); | |
| 177 position.x *= device_scale_factor_for_test; | |
| 178 position.y *= device_scale_factor_for_test; | |
| 179 capture_callback->set_popup_position(position); | |
| 180 popup->compositeAndReadbackAsync(capture_callback); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 void CopyImageAtAndCapturePixels( | |
| 185 blink::WebView* web_view, | |
| 186 int x, | |
| 187 int y, | |
| 188 const base::Callback<void(const SkBitmap&)>& callback) { | |
| 189 DCHECK(!callback.is_null()); | |
| 190 uint64_t sequence_number = | |
| 191 blink::Platform::current()->clipboard()->sequenceNumber( | |
| 192 blink::WebClipboard::Buffer()); | |
| 193 // TODO(lukasza): Support image capture in OOPIFs for | |
| 194 // https://crbug.com/477150. | |
| 195 web_view->mainFrame()->toWebLocalFrame()->copyImageAt(blink::WebPoint(x, y)); | |
| 196 if (sequence_number == | |
| 197 blink::Platform::current()->clipboard()->sequenceNumber( | |
| 198 blink::WebClipboard::Buffer())) { | |
| 199 SkBitmap emptyBitmap; | |
| 200 callback.Run(emptyBitmap); | |
| 201 return; | |
| 202 } | |
| 203 | |
| 204 blink::WebImage image = static_cast<blink::WebMockClipboard*>( | |
| 205 blink::Platform::current()->clipboard()) | |
| 206 ->readRawImage(blink::WebClipboard::Buffer()); | |
| 207 const SkBitmap& bitmap = image.getSkBitmap(); | |
| 208 SkAutoLockPixels autoLock(bitmap); | |
| 209 callback.Run(bitmap); | |
| 210 } | |
| 211 | |
| 212 } // namespace test_runner | |
| OLD | NEW |