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

Side by Side Diff: content/browser/devtools/renderer_overrides_handler.cc

Issue 474243003: DevTools: make eye dropper live (refresh on frame update). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « content/browser/devtools/renderer_overrides_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/browser/devtools/renderer_overrides_handler.h" 5 #include "content/browser/devtools/renderer_overrides_handler.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/barrier_closure.h" 10 #include "base/barrier_closure.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 static int kDefaultScreenshotQuality = 80; 68 static int kDefaultScreenshotQuality = 80;
69 static int kFrameRateThresholdMs = 100; 69 static int kFrameRateThresholdMs = 100;
70 static int kCaptureRetryLimit = 2; 70 static int kCaptureRetryLimit = 2;
71 71
72 } // namespace 72 } // namespace
73 73
74 RendererOverridesHandler::RendererOverridesHandler() 74 RendererOverridesHandler::RendererOverridesHandler()
75 : has_last_compositor_frame_metadata_(false), 75 : has_last_compositor_frame_metadata_(false),
76 capture_retry_count_(0), 76 capture_retry_count_(0),
77 color_picker_enabled_(false), 77 color_picker_enabled_(false),
78 last_cursor_x_(-1),
79 last_cursor_y_(-1),
78 weak_factory_(this) { 80 weak_factory_(this) {
79 RegisterCommandHandler( 81 RegisterCommandHandler(
80 devtools::DOM::setFileInputFiles::kName, 82 devtools::DOM::setFileInputFiles::kName,
81 base::Bind( 83 base::Bind(
82 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, 84 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles,
83 base::Unretained(this))); 85 base::Unretained(this)));
84 RegisterCommandHandler( 86 RegisterCommandHandler(
85 devtools::Network::clearBrowserCache::kName, 87 devtools::Network::clearBrowserCache::kName,
86 base::Bind( 88 base::Bind(
87 &RendererOverridesHandler::ClearBrowserCache, 89 &RendererOverridesHandler::ClearBrowserCache,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 184
183 void RendererOverridesHandler::OnVisibilityChanged(bool visible) { 185 void RendererOverridesHandler::OnVisibilityChanged(bool visible) {
184 if (!screencast_command_) 186 if (!screencast_command_)
185 return; 187 return;
186 NotifyScreencastVisibility(visible); 188 NotifyScreencastVisibility(visible);
187 } 189 }
188 190
189 void RendererOverridesHandler::SetRenderViewHost( 191 void RendererOverridesHandler::SetRenderViewHost(
190 RenderViewHostImpl* host) { 192 RenderViewHostImpl* host) {
191 host_ = host; 193 host_ = host;
192 if (screencast_command_ && host) 194 if (!host)
195 return;
196 if (screencast_command_)
193 host->SetTouchEventEmulationEnabled(true, true); 197 host->SetTouchEventEmulationEnabled(true, true);
194 if (color_picker_enabled_) 198 if (color_picker_enabled_)
195 host->AddMouseEventCallback(mouse_event_callback_); 199 host->AddMouseEventCallback(mouse_event_callback_);
196 } 200 }
197 201
198 void RendererOverridesHandler::ClearRenderViewHost() { 202 void RendererOverridesHandler::ClearRenderViewHost() {
199 if (host_) 203 if (host_)
200 host_->RemoveMouseEventCallback(mouse_event_callback_); 204 host_->RemoveMouseEventCallback(mouse_event_callback_);
201 host_ = NULL; 205 host_ = NULL;
dgozman 2014/08/18 13:50:52 Let's clear last_cursor_x{y} and color_picker_fram
pfeldman 2014/08/18 14:20:38 Done.
202 } 206 }
203 207
204 bool RendererOverridesHandler::OnSetTouchEventEmulationEnabled() { 208 bool RendererOverridesHandler::OnSetTouchEventEmulationEnabled() {
205 return screencast_command_.get() != NULL; 209 return screencast_command_.get() != NULL;
206 } 210 }
207 211
208 void RendererOverridesHandler::InnerSwapCompositorFrame() { 212 void RendererOverridesHandler::InnerSwapCompositorFrame() {
209 if ((base::TimeTicks::Now() - last_frame_time_).InMilliseconds() < 213 if ((base::TimeTicks::Now() - last_frame_time_).InMilliseconds() <
210 kFrameRateThresholdMs) { 214 kFrameRateThresholdMs) {
211 return; 215 return;
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 UpdateColorPickerFrame(); 901 UpdateColorPickerFrame();
898 } else { 902 } else {
899 host_->RemoveMouseEventCallback(mouse_event_callback_); 903 host_->RemoveMouseEventCallback(mouse_event_callback_);
900 color_picker_frame_.reset(); 904 color_picker_frame_.reset();
901 905
902 WebCursor pointer_cursor; 906 WebCursor pointer_cursor;
903 WebCursor::CursorInfo cursor_info; 907 WebCursor::CursorInfo cursor_info;
904 cursor_info.type = blink::WebCursorInfo::TypePointer; 908 cursor_info.type = blink::WebCursorInfo::TypePointer;
905 pointer_cursor.InitFromCursorInfo(cursor_info); 909 pointer_cursor.InitFromCursorInfo(cursor_info);
906 host_->SetCursor(pointer_cursor); 910 host_->SetCursor(pointer_cursor);
911 last_cursor_x_ = -1;
912 last_cursor_y_ = -1;
907 } 913 }
908 } 914 }
909 915
910 void RendererOverridesHandler::UpdateColorPickerFrame() { 916 void RendererOverridesHandler::UpdateColorPickerFrame() {
911 if (!host_) 917 if (!host_)
912 return; 918 return;
913 RenderWidgetHostViewBase* view = 919 RenderWidgetHostViewBase* view =
914 static_cast<RenderWidgetHostViewBase*>(host_->GetView()); 920 static_cast<RenderWidgetHostViewBase*>(host_->GetView());
915 if (!view) 921 if (!view)
916 return; 922 return;
917 923
918 gfx::Size size = view->GetViewBounds().size(); 924 gfx::Size size = view->GetViewBounds().size();
919 view->CopyFromCompositingSurface( 925 view->CopyFromCompositingSurface(
920 gfx::Rect(size), size, 926 gfx::Rect(size), size,
921 base::Bind(&RendererOverridesHandler::ColorPickerFrameUpdated, 927 base::Bind(&RendererOverridesHandler::ColorPickerFrameUpdated,
922 weak_factory_.GetWeakPtr()), 928 weak_factory_.GetWeakPtr()),
923 kN32_SkColorType); 929 kN32_SkColorType);
924 } 930 }
925 931
926 void RendererOverridesHandler::ColorPickerFrameUpdated( 932 void RendererOverridesHandler::ColorPickerFrameUpdated(
927 bool succeeded, 933 bool succeeded,
928 const SkBitmap& bitmap) { 934 const SkBitmap& bitmap) {
929 if (succeeded) 935 if (!color_picker_enabled_)
936 return;
937
938 if (succeeded) {
930 color_picker_frame_ = bitmap; 939 color_picker_frame_ = bitmap;
940 UpdateColorPickerCursor();
941 }
931 } 942 }
932 943
933 bool RendererOverridesHandler::HandleMouseEvent( 944 bool RendererOverridesHandler::HandleMouseEvent(
934 const blink::WebMouseEvent& event) { 945 const blink::WebMouseEvent& event) {
946 last_cursor_x_ = event.x;
947 last_cursor_y_ = event.y;
935 if (color_picker_frame_.drawsNothing()) 948 if (color_picker_frame_.drawsNothing())
936 return true; 949 return true;
937 950
938 if (event.button == blink::WebMouseEvent::ButtonLeft) { 951 if (event.button == blink::WebMouseEvent::ButtonLeft) {
939 color_picker_frame_.lockPixels(); 952 if (last_cursor_x_ < 0 || last_cursor_x_ >= color_picker_frame_.width() ||
953 last_cursor_y_ < 0 || last_cursor_y_ >= color_picker_frame_.height()) {
954 return true;
955 }
956
957 SkAutoLockPixels lock_image(color_picker_frame_);
940 SkColor color = color_picker_frame_.getColor(event.x, event.y); 958 SkColor color = color_picker_frame_.getColor(event.x, event.y);
dgozman 2014/08/18 13:50:52 last_cursor_x_, last_cursor_y_
pfeldman 2014/08/18 14:20:38 Done.
941 color_picker_frame_.unlockPixels();
942 base::DictionaryValue* color_dict = new base::DictionaryValue(); 959 base::DictionaryValue* color_dict = new base::DictionaryValue();
943 color_dict->SetInteger("r", SkColorGetR(color)); 960 color_dict->SetInteger("r", SkColorGetR(color));
944 color_dict->SetInteger("g", SkColorGetG(color)); 961 color_dict->SetInteger("g", SkColorGetG(color));
945 color_dict->SetInteger("b", SkColorGetB(color)); 962 color_dict->SetInteger("b", SkColorGetB(color));
946 color_dict->SetInteger("a", SkColorGetA(color)); 963 color_dict->SetInteger("a", SkColorGetA(color));
947 base::DictionaryValue* response = new base::DictionaryValue(); 964 base::DictionaryValue* response = new base::DictionaryValue();
948 response->Set(devtools::Page::colorPicked::kParamColor, color_dict); 965 response->Set(devtools::Page::colorPicked::kParamColor, color_dict);
949 SendNotification(devtools::Page::colorPicked::kName, response); 966 SendNotification(devtools::Page::colorPicked::kName, response);
950 } 967 }
968 UpdateColorPickerCursor();
969 return true;
970 }
951 971
952 if (!host_) 972 void RendererOverridesHandler::UpdateColorPickerCursor() {
953 return true; 973 if (!host_ || color_picker_frame_.drawsNothing())
974 return;
975
976 if (last_cursor_x_ < 0 || last_cursor_x_ >= color_picker_frame_.width() ||
977 last_cursor_y_ < 0 || last_cursor_y_ >= color_picker_frame_.height()) {
978 return;
979 }
980
954 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>( 981 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>(
955 host_->GetView()); 982 host_->GetView());
956 if (!view) 983 if (!view)
957 return true; 984 return;
958 985
959 // Due to platform limitations, we are using two different cursors 986 // Due to platform limitations, we are using two different cursors
960 // depending on the platform. Mac and Win have large cursors with two circles 987 // depending on the platform. Mac and Win have large cursors with two circles
961 // for original spot and its magnified projection; Linux gets smaller (64 px) 988 // for original spot and its magnified projection; Linux gets smaller (64 px)
962 // magnified projection only with centered hotspot. 989 // magnified projection only with centered hotspot.
963 // Mac Retina requires cursor to be > 120px in order to render smoothly. 990 // Mac Retina requires cursor to be > 120px in order to render smoothly.
964 991
965 #if defined(OS_LINUX) 992 #if defined(OS_LINUX)
966 const float kCursorSize = 63; 993 const float kCursorSize = 63;
967 const float kDiameter = 63; 994 const float kDiameter = 63;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 1031
1005 // Clip circle for magnified projection. 1032 // Clip circle for magnified projection.
1006 float padding = (kCursorSize - kDiameter) / 2; 1033 float padding = (kCursorSize - kDiameter) / 2;
1007 SkPath clip_path; 1034 SkPath clip_path;
1008 clip_path.addOval(SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter)); 1035 clip_path.addOval(SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter));
1009 clip_path.close(); 1036 clip_path.close();
1010 canvas->clipPath(clip_path, SkRegion::kIntersect_Op, true); 1037 canvas->clipPath(clip_path, SkRegion::kIntersect_Op, true);
1011 1038
1012 // Project pixels. 1039 // Project pixels.
1013 int pixel_count = kDiameter / kPixelSize; 1040 int pixel_count = kDiameter / kPixelSize;
1014 SkRect src_rect = SkRect::MakeXYWH(event.x - pixel_count / 2, 1041 SkRect src_rect = SkRect::MakeXYWH(last_cursor_x_ - pixel_count / 2,
1015 event.y - pixel_count / 2, 1042 last_cursor_y_ - pixel_count / 2,
1016 pixel_count, pixel_count); 1043 pixel_count, pixel_count);
1017 SkRect dst_rect = SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter); 1044 SkRect dst_rect = SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter);
1018 canvas->drawBitmapRectToRect(color_picker_frame_, &src_rect, dst_rect); 1045 canvas->drawBitmapRectToRect(color_picker_frame_, &src_rect, dst_rect);
1019 1046
1020 // Paint grid. 1047 // Paint grid.
1021 paint.setStrokeWidth(1); 1048 paint.setStrokeWidth(1);
1022 paint.setAntiAlias(false); 1049 paint.setAntiAlias(false);
1023 paint.setColor(SK_ColorGRAY); 1050 paint.setColor(SK_ColorGRAY);
1024 for (int i = 0; i < pixel_count; ++i) { 1051 for (int i = 0; i < pixel_count; ++i) {
1025 canvas->drawLine(padding + i * kPixelSize, padding, 1052 canvas->drawLine(padding + i * kPixelSize, padding,
1026 padding + i * kPixelSize, kCursorSize - padding, paint); 1053 padding + i * kPixelSize, kCursorSize - padding, paint);
1027 canvas->drawLine(padding, padding + i * kPixelSize, 1054 canvas->drawLine(padding, padding + i * kPixelSize,
1028 kCursorSize - padding, padding + i * kPixelSize, paint); 1055 kCursorSize - padding, padding + i * kPixelSize, paint);
1029 } 1056 }
1030 1057
1031 // Paint central pixel in red. 1058 // Paint central pixel in red.
1032 SkRect pixel = SkRect::MakeXYWH((kCursorSize - kPixelSize) / 2, 1059 SkRect pixel = SkRect::MakeXYWH((kCursorSize - kPixelSize) / 2,
1033 (kCursorSize - kPixelSize) / 2, 1060 (kCursorSize - kPixelSize) / 2,
1034 kPixelSize, kPixelSize); 1061 kPixelSize, kPixelSize);
1035 paint.setColor(SK_ColorRED); 1062 paint.setColor(SK_ColorRED);
1063 paint.setStyle(SkPaint::kStroke_Style);
1036 canvas->drawRect(pixel, paint); 1064 canvas->drawRect(pixel, paint);
1037 1065
1038 // Paint outline. 1066 // Paint outline.
1039 paint.setStrokeWidth(2); 1067 paint.setStrokeWidth(2);
1040 paint.setColor(SK_ColorDKGRAY); 1068 paint.setColor(SK_ColorDKGRAY);
1041 paint.setAntiAlias(true); 1069 paint.setAntiAlias(true);
1042 paint.setStyle(SkPaint::kStroke_Style);
1043 canvas->drawCircle(kCursorSize / 2, kCursorSize / 2, kDiameter / 2, paint); 1070 canvas->drawCircle(kCursorSize / 2, kCursorSize / 2, kDiameter / 2, paint);
1044 1071
1045 SkBitmap result; 1072 SkBitmap result;
1046 result.allocN32Pixels(kCursorSize * device_scale_factor, 1073 result.allocN32Pixels(kCursorSize * device_scale_factor,
1047 kCursorSize * device_scale_factor); 1074 kCursorSize * device_scale_factor);
1048 canvas->readPixels(&result, 0, 0); 1075 canvas->readPixels(&result, 0, 0);
1049 1076
1050 WebCursor cursor; 1077 WebCursor cursor;
1051 WebCursor::CursorInfo cursor_info; 1078 WebCursor::CursorInfo cursor_info;
1052 cursor_info.type = blink::WebCursorInfo::TypeCustom; 1079 cursor_info.type = blink::WebCursorInfo::TypeCustom;
1053 cursor_info.image_scale_factor = device_scale_factor; 1080 cursor_info.image_scale_factor = device_scale_factor;
1054 cursor_info.custom_image = result; 1081 cursor_info.custom_image = result;
1055 cursor_info.hotspot = 1082 cursor_info.hotspot =
1056 gfx::Point(kHotspotOffset * device_scale_factor, 1083 gfx::Point(kHotspotOffset * device_scale_factor,
1057 kHotspotOffset * device_scale_factor); 1084 kHotspotOffset * device_scale_factor);
1058 #if defined(OS_WIN) 1085 #if defined(OS_WIN)
1059 cursor_info.external_handle = 0; 1086 cursor_info.external_handle = 0;
1060 #endif 1087 #endif
1061 1088
1062 cursor.InitFromCursorInfo(cursor_info); 1089 cursor.InitFromCursorInfo(cursor_info);
1063 DCHECK(host_); 1090 DCHECK(host_);
1064 host_->SetCursor(cursor); 1091 host_->SetCursor(cursor);
1065 return true;
1066 } 1092 }
1067 1093
1068 // Input agent handlers ------------------------------------------------------ 1094 // Input agent handlers ------------------------------------------------------
1069 1095
1070 scoped_refptr<DevToolsProtocol::Response> 1096 scoped_refptr<DevToolsProtocol::Response>
1071 RendererOverridesHandler::InputEmulateTouchFromMouseEvent( 1097 RendererOverridesHandler::InputEmulateTouchFromMouseEvent(
1072 scoped_refptr<DevToolsProtocol::Command> command) { 1098 scoped_refptr<DevToolsProtocol::Command> command) {
1073 if (!screencast_command_) 1099 if (!screencast_command_)
1074 return command->InternalErrorResponse("Screencast should be turned on"); 1100 return command->InternalErrorResponse("Screencast should be turned on");
1075 1101
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 return command->InternalErrorResponse("Could not connect to view"); 1216 return command->InternalErrorResponse("Could not connect to view");
1191 1217
1192 if (event->type == WebInputEvent::MouseWheel) 1218 if (event->type == WebInputEvent::MouseWheel)
1193 host_->ForwardWheelEvent(wheel_event); 1219 host_->ForwardWheelEvent(wheel_event);
1194 else 1220 else
1195 host_->ForwardMouseEvent(mouse_event); 1221 host_->ForwardMouseEvent(mouse_event);
1196 return command->SuccessResponse(NULL); 1222 return command->SuccessResponse(NULL);
1197 } 1223 }
1198 1224
1199 } // namespace content 1225 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/renderer_overrides_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698