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 "content/shell/test_runner/pixel_dump.h" | 5 #include "content/shell/test_runner/pixel_dump.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 12 #include "base/callback.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
14 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
15 #include "cc/paint/paint_flags.h" | 16 #include "cc/paint/paint_flags.h" |
16 #include "cc/paint/skia_paint_canvas.h" | 17 #include "cc/paint/skia_paint_canvas.h" |
17 #include "content/shell/test_runner/layout_test_runtime_flags.h" | 18 #include "content/shell/test_runner/layout_test_runtime_flags.h" |
(...skipping 23 matching lines...) Expand all Loading... |
41 layout_test_runtime_flags(layout_test_runtime_flags), | 42 layout_test_runtime_flags(layout_test_runtime_flags), |
42 callback(callback) {} | 43 callback(callback) {} |
43 | 44 |
44 blink::WebView* web_view; | 45 blink::WebView* web_view; |
45 const LayoutTestRuntimeFlags& layout_test_runtime_flags; | 46 const LayoutTestRuntimeFlags& layout_test_runtime_flags; |
46 base::Callback<void(const SkBitmap&)> callback; | 47 base::Callback<void(const SkBitmap&)> callback; |
47 }; | 48 }; |
48 | 49 |
49 class CaptureCallback : public blink::WebCompositeAndReadbackAsyncCallback { | 50 class CaptureCallback : public blink::WebCompositeAndReadbackAsyncCallback { |
50 public: | 51 public: |
51 CaptureCallback(const base::Callback<void(const SkBitmap&)>& callback); | 52 explicit CaptureCallback( |
| 53 const base::Callback<void(const SkBitmap&)>& callback); |
52 virtual ~CaptureCallback(); | 54 virtual ~CaptureCallback(); |
53 | 55 |
54 void set_wait_for_popup(bool wait) { wait_for_popup_ = wait; } | 56 void set_wait_for_popup(bool wait) { wait_for_popup_ = wait; } |
55 void set_popup_position(const gfx::Point& position) { | 57 void set_popup_position(const gfx::Point& position) { |
56 popup_position_ = position; | 58 popup_position_ = position; |
57 } | 59 } |
58 | 60 |
59 // WebCompositeAndReadbackAsyncCallback implementation. | 61 // WebCompositeAndReadbackAsyncCallback implementation. |
60 void DidCompositeAndReadback(const SkBitmap& bitmap) override; | 62 void DidCompositeAndReadback(const SkBitmap& bitmap) override; |
61 | 63 |
62 private: | 64 private: |
63 base::Callback<void(const SkBitmap&)> callback_; | 65 base::Callback<void(const SkBitmap&)> callback_; |
64 SkBitmap main_bitmap_; | 66 SkBitmap main_bitmap_; |
65 bool wait_for_popup_; | 67 bool wait_for_popup_; |
66 gfx::Point popup_position_; | 68 gfx::Point popup_position_; |
67 }; | 69 }; |
68 | 70 |
69 void DrawSelectionRect(const PixelsDumpRequest& dump_request, | 71 void DrawSelectionRect(const PixelsDumpRequest& dump_request, |
70 cc::PaintCanvas* canvas) { | 72 cc::PaintCanvas* canvas) { |
71 // See if we need to draw the selection bounds rect. Selection bounds | 73 // See if we need to draw the selection bounds rect. Selection bounds |
72 // rect is the rect enclosing the (possibly transformed) selection. | 74 // rect is the rect enclosing the (possibly transformed) selection. |
73 // The rect should be drawn after everything is laid out and painted. | 75 // The rect should be drawn after everything is laid out and painted. |
74 if (!dump_request.layout_test_runtime_flags.dump_selection_rect()) | 76 if (!dump_request.layout_test_runtime_flags.dump_selection_rect()) |
75 return; | 77 return; |
76 // If there is a selection rect - draw a red 1px border enclosing rect | 78 // If there is a selection rect - draw a red 1px border enclosing rect |
77 blink::WebRect wr = dump_request.web_view->MainFrame()->SelectionBoundsRect(); | 79 CHECK(dump_request.web_view->MainFrame()->IsWebLocalFrame()) |
| 80 << "This function cannot be called if the main frame is not a " |
| 81 "local frame."; |
| 82 blink::WebRect wr = dump_request.web_view->MainFrame() |
| 83 ->ToWebLocalFrame() |
| 84 ->GetSelectionBoundsRectForTesting(); |
78 if (wr.IsEmpty()) | 85 if (wr.IsEmpty()) |
79 return; | 86 return; |
80 // Render a red rectangle bounding selection rect | 87 // Render a red rectangle bounding selection rect |
81 cc::PaintFlags flags; | 88 cc::PaintFlags flags; |
82 flags.setColor(0xFFFF0000); // Fully opaque red | 89 flags.setColor(0xFFFF0000); // Fully opaque red |
83 flags.setStyle(cc::PaintFlags::kStroke_Style); | 90 flags.setStyle(cc::PaintFlags::kStroke_Style); |
84 flags.setAntiAlias(true); | 91 flags.setAntiAlias(true); |
85 flags.setStrokeWidth(1.0f); | 92 flags.setStrokeWidth(1.0f); |
86 SkIRect rect; // Bounding rect | 93 SkIRect rect; // Bounding rect |
87 rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height); | 94 rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height); |
88 canvas->drawIRect(rect, flags); | 95 canvas->drawIRect(rect, flags); |
89 } | 96 } |
90 | 97 |
91 void CapturePixelsForPrinting(std::unique_ptr<PixelsDumpRequest> dump_request) { | 98 void CapturePixelsForPrinting(std::unique_ptr<PixelsDumpRequest> dump_request) { |
92 dump_request->web_view->UpdateAllLifecyclePhases(); | 99 dump_request->web_view->UpdateAllLifecyclePhases(); |
93 | 100 |
94 blink::WebSize page_size_in_pixels = dump_request->web_view->Size(); | 101 blink::WebSize page_size_in_pixels = dump_request->web_view->Size(); |
95 blink::WebFrame* web_frame = dump_request->web_view->MainFrame(); | 102 |
| 103 CHECK(dump_request->web_view->MainFrame()->IsWebLocalFrame()) |
| 104 << "This function cannot be called if the main frame is not a " |
| 105 "local frame."; |
| 106 blink::WebLocalFrame* web_frame = |
| 107 dump_request->web_view->MainFrame()->ToWebLocalFrame(); |
96 | 108 |
97 int page_count = web_frame->PrintBegin(page_size_in_pixels); | 109 int page_count = web_frame->PrintBegin(page_size_in_pixels); |
98 int totalHeight = page_count * (page_size_in_pixels.height + 1) - 1; | 110 int totalHeight = page_count * (page_size_in_pixels.height + 1) - 1; |
99 | 111 |
100 bool is_opaque = false; | 112 bool is_opaque = false; |
101 | 113 |
102 SkBitmap bitmap; | 114 SkBitmap bitmap; |
103 if (!bitmap.tryAllocN32Pixels(page_size_in_pixels.width, totalHeight, | 115 if (!bitmap.tryAllocN32Pixels(page_size_in_pixels.width, totalHeight, |
104 is_opaque)) { | 116 is_opaque)) { |
105 LOG(ERROR) << "Failed to create bitmap width=" << page_size_in_pixels.width | 117 LOG(ERROR) << "Failed to create bitmap width=" << page_size_in_pixels.width |
106 << " height=" << totalHeight; | 118 << " height=" << totalHeight; |
107 dump_request->callback.Run(SkBitmap()); | 119 dump_request->callback.Run(SkBitmap()); |
108 return; | 120 return; |
109 } | 121 } |
110 | 122 |
111 cc::SkiaPaintCanvas canvas(bitmap); | 123 cc::SkiaPaintCanvas canvas(bitmap); |
112 web_frame->PrintPagesWithBoundaries(&canvas, page_size_in_pixels); | 124 web_frame->PrintPagesForTesting(&canvas, page_size_in_pixels); |
113 web_frame->PrintEnd(); | 125 web_frame->PrintEnd(); |
114 | 126 |
115 DrawSelectionRect(*dump_request, &canvas); | 127 DrawSelectionRect(*dump_request, &canvas); |
116 dump_request->callback.Run(bitmap); | 128 dump_request->callback.Run(bitmap); |
117 } | 129 } |
118 | 130 |
119 CaptureCallback::CaptureCallback( | 131 CaptureCallback::CaptureCallback( |
120 const base::Callback<void(const SkBitmap&)>& callback) | 132 const base::Callback<void(const SkBitmap&)>& callback) |
121 : callback_(callback), wait_for_popup_(false) {} | 133 : callback_(callback), wait_for_popup_(false) {} |
122 | 134 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 return; | 216 return; |
205 } | 217 } |
206 | 218 |
207 blink::WebImage image = static_cast<blink::WebMockClipboard*>( | 219 blink::WebImage image = static_cast<blink::WebMockClipboard*>( |
208 blink::Platform::Current()->Clipboard()) | 220 blink::Platform::Current()->Clipboard()) |
209 ->ReadRawImage(blink::WebClipboard::Buffer()); | 221 ->ReadRawImage(blink::WebClipboard::Buffer()); |
210 callback.Run(image.GetSkBitmap()); | 222 callback.Run(image.GetSkBitmap()); |
211 } | 223 } |
212 | 224 |
213 } // namespace test_runner | 225 } // namespace test_runner |
OLD | NEW |