| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/test_runner/pixel_dump.h" | 5 #include "components/test_runner/pixel_dump.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
| 15 #include "cc/paint/paint_canvas.h" |
| 16 #include "cc/paint/paint_flags.h" |
| 15 #include "components/test_runner/layout_test_runtime_flags.h" | 17 #include "components/test_runner/layout_test_runtime_flags.h" |
| 16 // FIXME: Including platform_canvas.h here is a layering violation. | 18 // FIXME: Including platform_canvas.h here is a layering violation. |
| 17 #include "skia/ext/platform_canvas.h" | 19 #include "skia/ext/platform_canvas.h" |
| 18 #include "third_party/WebKit/public/platform/Platform.h" | 20 #include "third_party/WebKit/public/platform/Platform.h" |
| 19 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallbac
k.h" | 21 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallbac
k.h" |
| 20 #include "third_party/WebKit/public/platform/WebImage.h" | 22 #include "third_party/WebKit/public/platform/WebImage.h" |
| 21 #include "third_party/WebKit/public/platform/WebMockClipboard.h" | 23 #include "third_party/WebKit/public/platform/WebMockClipboard.h" |
| 22 #include "third_party/WebKit/public/platform/WebPoint.h" | 24 #include "third_party/WebKit/public/platform/WebPoint.h" |
| 23 #include "third_party/WebKit/public/web/WebFrame.h" | 25 #include "third_party/WebKit/public/web/WebFrame.h" |
| 24 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 void didCompositeAndReadback(const SkBitmap& bitmap) override; | 60 void didCompositeAndReadback(const SkBitmap& bitmap) override; |
| 59 | 61 |
| 60 private: | 62 private: |
| 61 base::Callback<void(const SkBitmap&)> callback_; | 63 base::Callback<void(const SkBitmap&)> callback_; |
| 62 SkBitmap main_bitmap_; | 64 SkBitmap main_bitmap_; |
| 63 bool wait_for_popup_; | 65 bool wait_for_popup_; |
| 64 gfx::Point popup_position_; | 66 gfx::Point popup_position_; |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 void DrawSelectionRect(const PixelsDumpRequest& dump_request, | 69 void DrawSelectionRect(const PixelsDumpRequest& dump_request, |
| 68 SkCanvas* canvas) { | 70 cc::PaintCanvas* canvas) { |
| 69 // See if we need to draw the selection bounds rect. Selection bounds | 71 // See if we need to draw the selection bounds rect. Selection bounds |
| 70 // rect is the rect enclosing the (possibly transformed) selection. | 72 // rect is the rect enclosing the (possibly transformed) selection. |
| 71 // The rect should be drawn after everything is laid out and painted. | 73 // The rect should be drawn after everything is laid out and painted. |
| 72 if (!dump_request.layout_test_runtime_flags.dump_selection_rect()) | 74 if (!dump_request.layout_test_runtime_flags.dump_selection_rect()) |
| 73 return; | 75 return; |
| 74 // If there is a selection rect - draw a red 1px border enclosing rect | 76 // If there is a selection rect - draw a red 1px border enclosing rect |
| 75 blink::WebRect wr = dump_request.web_view->mainFrame()->selectionBoundsRect(); | 77 blink::WebRect wr = dump_request.web_view->mainFrame()->selectionBoundsRect(); |
| 76 if (wr.isEmpty()) | 78 if (wr.isEmpty()) |
| 77 return; | 79 return; |
| 78 // Render a red rectangle bounding selection rect | 80 // Render a red rectangle bounding selection rect |
| 79 SkPaint paint; | 81 cc::PaintFlags paint; |
| 80 paint.setColor(0xFFFF0000); // Fully opaque red | 82 paint.setColor(0xFFFF0000); // Fully opaque red |
| 81 paint.setStyle(SkPaint::kStroke_Style); | 83 paint.setStyle(cc::PaintFlags::kStroke_Style); |
| 82 paint.setFlags(SkPaint::kAntiAlias_Flag); | 84 paint.setAntiAlias(true); |
| 83 paint.setStrokeWidth(1.0f); | 85 paint.setStrokeWidth(1.0f); |
| 84 SkIRect rect; // Bounding rect | 86 SkIRect rect; // Bounding rect |
| 85 rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height); | 87 rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height); |
| 86 canvas->drawIRect(rect, paint); | 88 canvas->drawIRect(rect, paint); |
| 87 } | 89 } |
| 88 | 90 |
| 89 void CapturePixelsForPrinting(std::unique_ptr<PixelsDumpRequest> dump_request) { | 91 void CapturePixelsForPrinting(std::unique_ptr<PixelsDumpRequest> dump_request) { |
| 90 dump_request->web_view->updateAllLifecyclePhases(); | 92 dump_request->web_view->updateAllLifecyclePhases(); |
| 91 | 93 |
| 92 blink::WebSize page_size_in_pixels = dump_request->web_view->size(); | 94 blink::WebSize page_size_in_pixels = dump_request->web_view->size(); |
| 93 blink::WebFrame* web_frame = dump_request->web_view->mainFrame(); | 95 blink::WebFrame* web_frame = dump_request->web_view->mainFrame(); |
| 94 | 96 |
| 95 int page_count = web_frame->printBegin(page_size_in_pixels); | 97 int page_count = web_frame->printBegin(page_size_in_pixels); |
| 96 int totalHeight = page_count * (page_size_in_pixels.height + 1) - 1; | 98 int totalHeight = page_count * (page_size_in_pixels.height + 1) - 1; |
| 97 | 99 |
| 98 bool is_opaque = false; | 100 bool is_opaque = false; |
| 99 std::unique_ptr<SkCanvas> canvas = skia::TryCreateBitmapCanvas( | 101 |
| 100 page_size_in_pixels.width, totalHeight, is_opaque); | 102 SkBitmap bitmap; |
| 101 if (!canvas) { | 103 if (!bitmap.tryAllocN32Pixels(page_size_in_pixels.width, totalHeight, |
| 102 LOG(ERROR) << "Failed to create canvas width=" | 104 is_opaque)) { |
| 103 << page_size_in_pixels.width << " height=" << totalHeight; | 105 LOG(ERROR) << "Failed to create bitmap width=" << page_size_in_pixels.width |
| 106 << " height=" << totalHeight; |
| 104 dump_request->callback.Run(SkBitmap()); | 107 dump_request->callback.Run(SkBitmap()); |
| 105 return; | 108 return; |
| 106 } | 109 } |
| 107 web_frame->printPagesWithBoundaries(canvas.get(), page_size_in_pixels); | 110 |
| 111 cc::PaintCanvas canvas(bitmap); |
| 112 web_frame->printPagesWithBoundaries(&canvas, page_size_in_pixels); |
| 108 web_frame->printEnd(); | 113 web_frame->printEnd(); |
| 109 | 114 |
| 110 DrawSelectionRect(*dump_request, canvas.get()); | 115 DrawSelectionRect(*dump_request, &canvas); |
| 111 const SkBitmap bitmap = skia::ReadPixels(canvas.get()); | |
| 112 dump_request->callback.Run(bitmap); | 116 dump_request->callback.Run(bitmap); |
| 113 } | 117 } |
| 114 | 118 |
| 115 CaptureCallback::CaptureCallback( | 119 CaptureCallback::CaptureCallback( |
| 116 const base::Callback<void(const SkBitmap&)>& callback) | 120 const base::Callback<void(const SkBitmap&)>& callback) |
| 117 : callback_(callback), wait_for_popup_(false) {} | 121 : callback_(callback), wait_for_popup_(false) {} |
| 118 | 122 |
| 119 CaptureCallback::~CaptureCallback() {} | 123 CaptureCallback::~CaptureCallback() {} |
| 120 | 124 |
| 121 void CaptureCallback::didCompositeAndReadback(const SkBitmap& bitmap) { | 125 void CaptureCallback::didCompositeAndReadback(const SkBitmap& bitmap) { |
| 122 TRACE_EVENT2("shell", "CaptureCallback::didCompositeAndReadback", "x", | 126 TRACE_EVENT2("shell", "CaptureCallback::didCompositeAndReadback", "x", |
| 123 bitmap.info().width(), "y", bitmap.info().height()); | 127 bitmap.info().width(), "y", bitmap.info().height()); |
| 124 if (!wait_for_popup_) { | 128 if (!wait_for_popup_) { |
| 125 callback_.Run(bitmap); | 129 callback_.Run(bitmap); |
| 126 delete this; | 130 delete this; |
| 127 return; | 131 return; |
| 128 } | 132 } |
| 129 if (main_bitmap_.isNull()) { | 133 if (main_bitmap_.isNull()) { |
| 130 bitmap.deepCopyTo(&main_bitmap_); | 134 bitmap.deepCopyTo(&main_bitmap_); |
| 131 return; | 135 return; |
| 132 } | 136 } |
| 133 SkCanvas canvas(main_bitmap_); | 137 SkCanvas canvas(main_bitmap_); |
| 134 canvas.drawBitmap(bitmap, popup_position_.x(), popup_position_.y()); | 138 canvas.drawBitmap(bitmap, popup_position_.x(), popup_position_.y()); |
| 135 callback_.Run(main_bitmap_); | 139 callback_.Run(main_bitmap_); |
| 136 delete this; | 140 delete this; |
| 137 } | 141 } |
| 138 | 142 |
| 139 void DidCapturePixelsAsync(std::unique_ptr<PixelsDumpRequest> dump_request, | 143 void DidCapturePixelsAsync(std::unique_ptr<PixelsDumpRequest> dump_request, |
| 140 const SkBitmap& bitmap) { | 144 const SkBitmap& bitmap) { |
| 141 SkCanvas canvas(bitmap); | 145 cc::PaintCanvas canvas(bitmap); |
| 142 DrawSelectionRect(*dump_request, &canvas); | 146 DrawSelectionRect(*dump_request, &canvas); |
| 143 if (!dump_request->callback.is_null()) | 147 if (!dump_request->callback.is_null()) |
| 144 dump_request->callback.Run(bitmap); | 148 dump_request->callback.Run(bitmap); |
| 145 } | 149 } |
| 146 | 150 |
| 147 } // namespace | 151 } // namespace |
| 148 | 152 |
| 149 void DumpPixelsAsync(blink::WebView* web_view, | 153 void DumpPixelsAsync(blink::WebView* web_view, |
| 150 const LayoutTestRuntimeFlags& layout_test_runtime_flags, | 154 const LayoutTestRuntimeFlags& layout_test_runtime_flags, |
| 151 float device_scale_factor_for_test, | 155 float device_scale_factor_for_test, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 | 203 |
| 200 blink::WebImage image = static_cast<blink::WebMockClipboard*>( | 204 blink::WebImage image = static_cast<blink::WebMockClipboard*>( |
| 201 blink::Platform::current()->clipboard()) | 205 blink::Platform::current()->clipboard()) |
| 202 ->readRawImage(blink::WebClipboard::Buffer()); | 206 ->readRawImage(blink::WebClipboard::Buffer()); |
| 203 const SkBitmap& bitmap = image.getSkBitmap(); | 207 const SkBitmap& bitmap = image.getSkBitmap(); |
| 204 SkAutoLockPixels autoLock(bitmap); | 208 SkAutoLockPixels autoLock(bitmap); |
| 205 callback.Run(bitmap); | 209 callback.Run(bitmap); |
| 206 } | 210 } |
| 207 | 211 |
| 208 } // namespace test_runner | 212 } // namespace test_runner |
| OLD | NEW |