OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "content/browser/devtools/protocol/frame_recorder.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/bind.h" | |
9 #include "base/macros.h" | |
10 #include "base/task_runner_util.h" | |
11 #include "base/threading/worker_pool.h" | |
12 #include "content/browser/renderer_host/render_view_host_impl.h" | |
13 #include "content/browser/renderer_host/render_widget_host_view_base.h" | |
14 #include "third_party/skia/include/core/SkBitmap.h" | |
15 #include "ui/gfx/codec/png_codec.h" | |
16 #include "ui/gfx/geometry/size.h" | |
17 | |
18 namespace content { | |
19 namespace devtools { | |
20 namespace page { | |
21 | |
22 namespace { | |
23 | |
24 static int kMaxRecordFrameCount = 180; | |
25 | |
26 scoped_ptr<EncodedFrame> EncodeFrame( | |
27 const SkBitmap& bitmap, double timestamp) { | |
28 std::vector<unsigned char> data; | |
29 SkAutoLockPixels lock_image(bitmap); | |
30 bool encoded = gfx::PNGCodec::Encode( | |
31 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
32 gfx::PNGCodec::FORMAT_SkBitmap, | |
33 gfx::Size(bitmap.width(), bitmap.height()), | |
34 bitmap.width() * bitmap.bytesPerPixel(), | |
35 false, std::vector<gfx::PNGCodec::Comment>(), &data); | |
36 | |
37 scoped_ptr<EncodedFrame> result(new EncodedFrame(std::string(), timestamp)); | |
38 | |
39 if (!encoded) | |
40 return result.Pass(); | |
41 | |
42 std::string base_64_data; | |
43 base::Base64Encode( | |
44 base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()), | |
45 &result->first); | |
46 | |
47 return result.Pass(); | |
48 } | |
49 } // namespace | |
50 | |
51 typedef DevToolsProtocolClient::Response Response; | |
52 | |
53 FrameRecorder::FrameRecorder() | |
54 : host_(nullptr), | |
55 state_(Ready), | |
56 inflight_requests_count_(0), | |
57 max_frame_count_(0), | |
58 captured_frames_count_(0), | |
59 last_captured_frame_timestamp_(base::Time()), | |
60 weak_factory_(this) { | |
61 } | |
62 | |
63 FrameRecorder::~FrameRecorder() { | |
64 } | |
65 | |
66 void FrameRecorder::SetRenderViewHost(RenderViewHostImpl* host) { | |
67 host_ = host; | |
68 } | |
69 | |
70 Response FrameRecorder::StartRecordingFrames(int max_frame_count) { | |
71 if (max_frame_count <= 0 || max_frame_count > kMaxRecordFrameCount) | |
72 return Response::InvalidParams("maxFrameCount"); | |
73 if (state_ != Ready) | |
74 return Response::InternalError("Already recording"); | |
75 state_ = Recording; | |
76 max_frame_count_ = max_frame_count; | |
77 captured_frames_count_ = 0; | |
78 frame_encoded_callback_.Reset(base::Bind( | |
79 &FrameRecorder::FrameEncoded, weak_factory_.GetWeakPtr())); | |
80 last_captured_frame_timestamp_ = base::Time(); | |
81 std::vector<scoped_refptr<devtools::page::RecordedFrame>> frames; | |
82 frames.reserve(max_frame_count); | |
83 frames_.swap(frames); | |
84 | |
85 return Response::OK(); | |
86 } | |
87 | |
88 Response FrameRecorder::StopRecordingFrames( | |
89 StopRecordingFramesCallback callback) { | |
90 if (state_ != Recording) | |
91 return Response::InternalError("Not recording"); | |
92 state_ = Encoding; | |
93 callback_ = callback; | |
94 MaybeSendResponse(); | |
95 return Response::OK(); | |
96 } | |
97 | |
98 Response FrameRecorder::CancelRecordingFrames() { | |
99 frame_encoded_callback_.Cancel(); | |
100 std::vector<scoped_refptr<devtools::page::RecordedFrame>> no_frames; | |
101 frames_.swap(no_frames); | |
102 if (state_ == Encoding) | |
103 callback_.Run(StopRecordingFramesResponse::Create()->set_frames(frames_)); | |
104 state_ = Ready; | |
105 return Response::OK(); | |
106 } | |
107 | |
108 void FrameRecorder::OnSwapCompositorFrame() { | |
109 if (!host_ || state_ != Recording) | |
110 return; | |
111 if (captured_frames_count_ >= max_frame_count_) | |
112 return; | |
113 RenderWidgetHostViewBase* view = | |
114 static_cast<RenderWidgetHostViewBase*>(host_->GetView()); | |
115 if (!view) | |
116 return; | |
117 | |
118 inflight_requests_count_++; | |
119 view->CopyFromCompositingSurface( | |
120 gfx::Rect(), | |
121 gfx::Size(), | |
122 base::Bind(&FrameRecorder::FrameCaptured, weak_factory_.GetWeakPtr()), | |
123 kN32_SkColorType); | |
124 } | |
125 | |
126 void FrameRecorder::FrameCaptured( | |
127 const SkBitmap& bitmap, ReadbackResponse response) { | |
128 inflight_requests_count_--; | |
129 base::Time timestamp = last_captured_frame_timestamp_; | |
130 last_captured_frame_timestamp_ = base::Time::Now(); | |
131 if (timestamp.is_null() || response != READBACK_SUCCESS) { | |
132 MaybeSendResponse(); | |
133 return; | |
134 } | |
135 | |
136 captured_frames_count_++; | |
137 base::PostTaskAndReplyWithResult( | |
138 base::WorkerPool::GetTaskRunner(true).get(), | |
139 FROM_HERE, | |
140 base::Bind(&EncodeFrame, bitmap, timestamp.ToDoubleT()), | |
141 frame_encoded_callback_.callback()); | |
142 } | |
143 | |
144 void FrameRecorder::FrameEncoded( | |
145 const scoped_ptr<EncodedFrame>& encoded_frame) { | |
146 frames_.push_back(RecordedFrame::Create() | |
147 ->set_data(encoded_frame->first) | |
148 ->set_timestamp(encoded_frame->second)); | |
149 MaybeSendResponse(); | |
150 } | |
151 | |
152 void FrameRecorder::MaybeSendResponse() { | |
153 if (state_ != Encoding) | |
154 return; | |
155 if (inflight_requests_count_ || frames_.size() != captured_frames_count_) | |
156 return; | |
157 callback_.Run(StopRecordingFramesResponse::Create()->set_frames(frames_)); | |
158 std::vector<scoped_refptr<devtools::page::RecordedFrame>> frames; | |
159 frames_.swap(frames); | |
160 state_ = Ready; | |
161 } | |
162 | |
163 } // namespace page | |
164 } // namespace devtools | |
165 } // namespace content | |
OLD | NEW |