OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // Provides a minimal wrapping of the Blink image decoders. Used to perform | 5 // Provides a minimal wrapping of the Blink image decoders. Used to perform |
6 // a non-threaded, memory-to-memory image decode using micro second accuracy | 6 // a non-threaded, memory-to-memory image decode using micro second accuracy |
7 // clocks to measure image decode time. Optionally applies color correction | 7 // clocks to measure image decode time. Optionally applies color correction |
8 // during image decoding on supported platforms (default off). Usage: | 8 // during image decoding on supported platforms (default off). Usage: |
9 // | 9 // |
10 // % ninja -C out/Release image_decode_bench && | 10 // % ninja -C out/Release image_decode_bench && |
11 // ./out/Release/image_decode_bench file [iterations] | 11 // ./out/Release/image_decode_bench file [iterations] |
12 // | 12 // |
13 // The output is formatted for use in a .csv file (comma-separated variable). | |
14 // Each row represents successive frames in an animated image. | |
15 // (frame 1, frame 2, frame 3...) | |
16 // Each column represents a successive iteration of decoding the whole animated | |
scroggo_chromium
2017/05/23 21:04:23
You said you would change this to "Each column rep
cblume
2017/05/23 21:42:06
Oops. Done.
| |
17 // image. | |
18 // Iteration 1: (frame 1, frame 2, frame 3...) | |
scroggo_chromium
2017/05/23 21:04:23
nit: I think in zero-based indices.
cblume
2017/05/23 21:42:07
Done.
| |
19 // Iteration 2: (frame 1, frame 2, frame 3...) | |
20 // | |
21 // This means non-animated images will show up as one column. | |
22 // | |
23 // The output of this program can be saved to a .csv file, which can then be | |
24 // imported into Google Sheets. From Sheets, the user can create graphs to | |
scroggo_chromium
2017/05/23 21:04:23
Sorry, I should've been more clear here. We probab
cblume
2017/05/23 21:42:07
Oh, you're right. I definitely don't want Chromium
| |
25 // visualize the decode times of a given frame and trim noise as appropriate. | |
26 // | |
27 // Each frame is different and so comparing separate frames may not be fruitful. | |
28 // For example, frame 1 may cover a large area and require more time to decode. | |
29 // Frame 2 may have a small rect and be faster to decode. | |
30 // | |
13 // TODO(noel): Consider adding md5 checksum support to WTF. Use it to compute | 31 // TODO(noel): Consider adding md5 checksum support to WTF. Use it to compute |
14 // the decoded image frame md5 and output that value. | 32 // the decoded image frame md5 and output that value. |
15 // | 33 // |
16 // TODO(noel): Consider integrating this tool in Chrome telemetry for realz, | 34 // TODO(noel): Consider integrating this tool in Chrome telemetry for realz, |
17 // using the image corpii used to assess Blink image decode performance. Refer | 35 // using the image corpii used to assess Blink image decode performance. Refer |
18 // to http://crbug.com/398235#c103 and http://crbug.com/258324#c5 | 36 // to http://crbug.com/398235#c103 and http://crbug.com/258324#c5 |
19 | 37 |
20 #include <memory> | 38 #include <memory> |
39 #include <vector> | |
21 #include "base/command_line.h" | 40 #include "base/command_line.h" |
22 #include "platform/SharedBuffer.h" | 41 #include "platform/SharedBuffer.h" |
23 #include "platform/image-decoders/ImageDecoder.h" | 42 #include "platform/image-decoders/ImageDecoder.h" |
24 #include "platform/wtf/PassRefPtr.h" | 43 #include "platform/wtf/PassRefPtr.h" |
25 #include "platform/wtf/PtrUtil.h" | 44 #include "platform/wtf/PtrUtil.h" |
26 #include "public/platform/Platform.h" | 45 #include "public/platform/Platform.h" |
27 #include "ui/gfx/test/icc_profiles.h" | 46 #include "ui/gfx/test/icc_profiles.h" |
28 | 47 |
29 #if defined(_WIN32) | 48 #if defined(_WIN32) |
30 #include <mmsystem.h> | 49 #include <mmsystem.h> |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 WrapArrayUnique(new unsigned char[file_size]); | 216 WrapArrayUnique(new unsigned char[file_size]); |
198 if (file_size != fread(buffer.get(), 1, file_size, fp)) { | 217 if (file_size != fread(buffer.get(), 1, file_size, fp)) { |
199 fprintf(stderr, "Error reading file %s\n", file_name); | 218 fprintf(stderr, "Error reading file %s\n", file_name); |
200 exit(2); | 219 exit(2); |
201 } | 220 } |
202 | 221 |
203 fclose(fp); | 222 fclose(fp); |
204 return SharedBuffer::Create(buffer.get(), file_size); | 223 return SharedBuffer::Create(buffer.get(), file_size); |
205 } | 224 } |
206 | 225 |
207 bool DecodeImageData(SharedBuffer* data, | 226 // This vector represents a single iteration of one (possibly animated) image. |
208 bool color_correction, | 227 // Each entry is a single timing of a single frame. |
209 size_t packet_size) { | 228 using FrameTimings = std::vector<double>; |
210 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create( | 229 using IterationsOfFrameTimings = std::vector<FrameTimings>; |
211 data, true, ImageDecoder::kAlphaPremultiplied, | |
212 color_correction ? ColorBehavior::TransformToTargetForTesting() | |
213 : ColorBehavior::Ignore()); | |
214 if (!packet_size) { | |
215 bool all_data_received = true; | |
216 decoder->SetData(data, all_data_received); | |
217 | 230 |
218 int frame_count = decoder->FrameCount(); | 231 void Print2DResults(const IterationsOfFrameTimings& timings) { |
219 for (int i = 0; i < frame_count; ++i) { | 232 for (const FrameTimings& iteration : timings) { |
220 if (!decoder->FrameBufferAtIndex(i)) | 233 for (double frame_time : iteration) { |
221 return false; | 234 printf("%f,", frame_time); |
222 } | 235 } |
236 printf("\n"); | |
237 } | |
238 printf("\n"); | |
239 } | |
223 | 240 |
224 return !decoder->Failed(); | 241 void TimeDecode(ImageDecoder* decoder, |
242 PassRefPtr<SharedBuffer> data, | |
243 size_t iterations) { | |
244 bool all_data_received = true; | |
245 decoder->SetData(data.Get(), all_data_received); | |
246 | |
247 size_t frame_count = decoder->FrameCount(); | |
248 | |
249 IterationsOfFrameTimings timings(iterations, FrameTimings(frame_count, 0.0)); | |
250 | |
251 for (size_t i = 0; i < iterations; ++i) { | |
252 for (size_t frame_index = 0; frame_index < frame_count; ++frame_index) { | |
253 double start_time = GetCurrentTime(); | |
254 ImageFrame* frame = decoder->FrameBufferAtIndex(frame_index); | |
255 double elapsed_time = GetCurrentTime() - start_time; | |
256 if (frame->GetStatus() != ImageFrame::kFrameComplete) { | |
257 fprintf(stderr, "Image decode failed\n"); | |
258 exit(3); | |
259 } | |
260 timings[i][frame_index] = elapsed_time; | |
261 } | |
225 } | 262 } |
226 | 263 |
264 Print2DResults(timings); | |
265 } | |
266 | |
267 // This function mimics deferred decoding in Chromium when not all data has been | |
268 // received yet. | |
269 void TimePacketedDecode(ImageDecoder* decoder, | |
270 PassRefPtr<SharedBuffer> data, | |
271 size_t packet_size, | |
272 size_t iterations) { | |
273 // Find total frame count. | |
274 // Doing this requires a decoder with full data (no packet size). | |
275 std::unique_ptr<ImageDecoder> frame_count_decoder = | |
276 ImageDecoder::Create(data.Get(), true, ImageDecoder::kAlphaPremultiplied, | |
277 ColorBehavior::Ignore()); | |
278 | |
279 constexpr bool total_all_data_received = true; | |
280 frame_count_decoder->SetData(data.Get(), total_all_data_received); | |
281 size_t total_frame_count = frame_count_decoder->FrameCount(); | |
282 | |
283 IterationsOfFrameTimings timings(iterations, | |
284 FrameTimings(total_frame_count, 0.0)); | |
285 | |
227 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create(); | 286 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create(); |
228 size_t position = 0; | 287 size_t position = 0; |
229 size_t next_frame_to_decode = 0; | 288 size_t next_frame_to_decode = 0; |
230 while (true) { | 289 while (true) { |
231 const char* packet; | 290 const char* packet; |
232 size_t length = data->GetSomeData(packet, position); | 291 size_t length = data->GetSomeData(packet, position); |
233 | 292 |
234 length = std::min(length, packet_size); | 293 length = std::min(length, packet_size); |
235 packet_data->Append(packet, length); | 294 packet_data->Append(packet, length); |
236 position += length; | 295 position += length; |
237 | 296 |
238 bool all_data_received = position == data->size(); | 297 bool all_data_received = position == data->size(); |
239 decoder->SetData(packet_data.Get(), all_data_received); | 298 decoder->SetData(packet_data.Get(), all_data_received); |
240 | 299 |
241 size_t frame_count = decoder->FrameCount(); | 300 size_t frame_count = decoder->FrameCount(); |
242 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) { | 301 for (size_t i = 0; i < iterations; ++i) { |
243 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode); | 302 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) { |
244 if (frame->GetStatus() != ImageFrame::kFrameComplete) | 303 double start_time = GetCurrentTime(); |
245 break; | 304 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode); |
305 double elapsed_time = GetCurrentTime() - start_time; | |
306 if (frame->GetStatus() != ImageFrame::kFrameComplete) { | |
307 fprintf(stderr, "Image decode failed\n"); | |
308 exit(3); | |
309 } | |
310 timings[i][next_frame_to_decode] = elapsed_time; | |
311 } | |
246 } | 312 } |
247 | 313 |
248 if (all_data_received || decoder->Failed()) | 314 if (all_data_received || decoder->Failed()) { |
249 break; | 315 fprintf(stderr, "Image decode failed\n"); |
316 exit(3); | |
317 } | |
250 } | 318 } |
251 | 319 |
252 return !decoder->Failed(); | 320 Print2DResults(timings); |
253 } | 321 } |
254 | 322 |
255 } // namespace | 323 } // namespace |
256 | 324 |
257 int Main(int argc, char* argv[]) { | 325 int Main(int argc, char* argv[]) { |
258 base::CommandLine::Init(argc, argv); | 326 base::CommandLine::Init(argc, argv); |
259 | 327 |
260 // If the platform supports color correction, allow it to be controlled. | 328 // If the platform supports color correction, allow it to be controlled. |
261 | 329 |
262 bool apply_color_correction = false; | 330 bool apply_color_correction = false; |
263 | |
264 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) { | 331 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) { |
265 apply_color_correction = (--argc, ++argv, true); | 332 --argc; |
333 ++argv; | |
334 apply_color_correction = true; | |
266 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin(); | 335 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin(); |
267 ColorBehavior::SetGlobalTargetColorProfile(profile); | 336 ColorBehavior::SetGlobalTargetColorProfile(profile); |
268 } | 337 } |
269 | 338 |
270 if (argc < 2) { | 339 if (argc < 2) { |
271 fprintf(stderr, | 340 fprintf(stderr, |
272 "Usage: %s [--color-correct] file [iterations] [packetSize]\n", | 341 "Usage: %s [--color-correct] file [iterations] [packetSize]\n", |
273 argv[0]); | 342 argv[0]); |
274 exit(1); | 343 exit(1); |
275 } | 344 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 // segments into one, contiguous block of memory. | 383 // segments into one, contiguous block of memory. |
315 | 384 |
316 RefPtr<SharedBuffer> data = ReadFile(argv[1]); | 385 RefPtr<SharedBuffer> data = ReadFile(argv[1]); |
317 if (!data.Get() || !data->size()) { | 386 if (!data.Get() || !data->size()) { |
318 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]); | 387 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]); |
319 exit(2); | 388 exit(2); |
320 } | 389 } |
321 | 390 |
322 data->Data(); | 391 data->Data(); |
323 | 392 |
324 // Warm-up: throw out the first iteration for more consistent results. | 393 // Image decode bench for iterations. |
325 | 394 |
326 if (!DecodeImageData(data.Get(), apply_color_correction, packet_size)) { | 395 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create( |
327 fprintf(stderr, "Image decode failed [%s]\n", argv[1]); | 396 data, true, ImageDecoder::kAlphaPremultiplied, |
328 exit(3); | 397 apply_color_correction ? ColorBehavior::TransformToTargetForTesting() |
398 : ColorBehavior::Ignore()); | |
399 if (packet_size) { | |
400 TimePacketedDecode(decoder.get(), data.Get(), packet_size, iterations); | |
401 } else { | |
402 TimeDecode(decoder.get(), data.Get(), iterations); | |
329 } | 403 } |
330 | 404 |
331 // Image decode bench for iterations. | |
332 | |
333 double total_time = 0.0; | |
334 | |
335 for (size_t i = 0; i < iterations; ++i) { | |
336 double start_time = GetCurrentTime(); | |
337 bool decoded = | |
338 DecodeImageData(data.Get(), apply_color_correction, packet_size); | |
339 double elapsed_time = GetCurrentTime() - start_time; | |
340 total_time += elapsed_time; | |
341 if (!decoded) { | |
342 fprintf(stderr, "Image decode failed [%s]\n", argv[1]); | |
343 exit(3); | |
344 } | |
345 } | |
346 | |
347 // Results to stdout. | |
348 | |
349 double average_time = total_time / static_cast<double>(iterations); | |
350 printf("%f %f\n", total_time, average_time); | |
351 return 0; | 405 return 0; |
352 } | 406 } |
353 | 407 |
354 } // namespace blink | 408 } // namespace blink |
355 | 409 |
356 int main(int argc, char* argv[]) { | 410 int main(int argc, char* argv[]) { |
357 return blink::Main(argc, argv); | 411 return blink::Main(argc, argv); |
358 } | 412 } |
OLD | NEW |