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 // Each column represents a successive iteration of decoding the whole animated | |
16 // image. | |
17 // This means non-animated images will show up as one column. | |
18 // | |
13 // TODO(noel): Consider adding md5 checksum support to WTF. Use it to compute | 19 // TODO(noel): Consider adding md5 checksum support to WTF. Use it to compute |
14 // the decoded image frame md5 and output that value. | 20 // the decoded image frame md5 and output that value. |
15 // | 21 // |
16 // TODO(noel): Consider integrating this tool in Chrome telemetry for realz, | 22 // TODO(noel): Consider integrating this tool in Chrome telemetry for realz, |
17 // using the image corpii used to assess Blink image decode performance. Refer | 23 // 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 | 24 // to http://crbug.com/398235#c103 and http://crbug.com/258324#c5 |
19 | 25 |
20 #include <memory> | 26 #include <memory> |
27 #include <vector> | |
21 #include "base/command_line.h" | 28 #include "base/command_line.h" |
22 #include "platform/SharedBuffer.h" | 29 #include "platform/SharedBuffer.h" |
23 #include "platform/image-decoders/ImageDecoder.h" | 30 #include "platform/image-decoders/ImageDecoder.h" |
24 #include "platform/wtf/PassRefPtr.h" | 31 #include "platform/wtf/PassRefPtr.h" |
25 #include "platform/wtf/PtrUtil.h" | 32 #include "platform/wtf/PtrUtil.h" |
26 #include "public/platform/Platform.h" | 33 #include "public/platform/Platform.h" |
27 #include "ui/gfx/test/icc_profiles.h" | 34 #include "ui/gfx/test/icc_profiles.h" |
28 | 35 |
29 #if defined(_WIN32) | 36 #if defined(_WIN32) |
30 #include <mmsystem.h> | 37 #include <mmsystem.h> |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 WrapArrayUnique(new unsigned char[file_size]); | 204 WrapArrayUnique(new unsigned char[file_size]); |
198 if (file_size != fread(buffer.get(), 1, file_size, fp)) { | 205 if (file_size != fread(buffer.get(), 1, file_size, fp)) { |
199 fprintf(stderr, "Error reading file %s\n", file_name); | 206 fprintf(stderr, "Error reading file %s\n", file_name); |
200 exit(2); | 207 exit(2); |
201 } | 208 } |
202 | 209 |
203 fclose(fp); | 210 fclose(fp); |
204 return SharedBuffer::Create(buffer.get(), file_size); | 211 return SharedBuffer::Create(buffer.get(), file_size); |
205 } | 212 } |
206 | 213 |
207 bool DecodeImageData(SharedBuffer* data, | 214 // This vector represents a single iteration of one (possibly animated) image. |
208 bool color_correction, | 215 // Each entry is a single timing of a single frame. |
209 size_t packet_size) { | 216 using FrameTimings = std::vector<double>; |
210 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create( | 217 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 | 218 |
218 int frame_count = decoder->FrameCount(); | 219 void Print2DResults(const IterationsOfFrameTimings& timings) { |
219 for (int i = 0; i < frame_count; ++i) { | 220 for (const FrameTimings& iteration : timings) { |
220 if (!decoder->FrameBufferAtIndex(i)) | 221 for (double frame_time : iteration) { |
221 return false; | 222 printf("%f,", frame_time); |
222 } | 223 } |
224 printf("\n"); | |
225 } | |
226 printf("\n"); | |
227 } | |
223 | 228 |
224 return !decoder->Failed(); | 229 void TimeDecode(ImageDecoder* decoder, |
230 PassRefPtr<SharedBuffer> data, | |
231 size_t iterations) { | |
232 bool all_data_received = true; | |
233 decoder->SetData(data.Get(), all_data_received); | |
234 | |
235 size_t frame_count = decoder->FrameCount(); | |
236 | |
237 IterationsOfFrameTimings timings(iterations, FrameTimings(frame_count, 0.0)); | |
238 | |
239 for (size_t i = 0; i < iterations; ++i) { | |
240 for (size_t frame_index = 0; frame_index < frame_count; ++frame_index) { | |
241 double start_time = GetCurrentTime(); | |
242 ImageFrame* frame = decoder->FrameBufferAtIndex(frame_index); | |
243 double elapsed_time = GetCurrentTime() - start_time; | |
244 if (frame->GetStatus() != ImageFrame::kFrameComplete) { | |
245 fprintf(stderr, "Image decode failed\n"); | |
246 exit(3); | |
247 } | |
248 timings[i][frame_index] = elapsed_time; | |
249 } | |
225 } | 250 } |
226 | 251 |
252 Print2DResults(timings); | |
253 } | |
254 | |
255 // This function mimics deferred decoding in Chromium when not all data has been | |
256 // received yet. | |
257 void TimePacketedDecode(ImageDecoder* decoder, | |
258 PassRefPtr<SharedBuffer> data, | |
259 size_t packet_size, | |
260 size_t iterations) { | |
261 // Find total frame count. | |
262 // Doing this requires a decoder with full data (no packet size). | |
263 std::unique_ptr<ImageDecoder> frame_count_decoder = | |
264 ImageDecoder::Create(data.Get(), true, ImageDecoder::kAlphaPremultiplied, | |
265 ColorBehavior::Ignore()); | |
266 | |
267 constexpr bool total_all_data_received = true; | |
268 frame_count_decoder->SetData(data.Get(), total_all_data_received); | |
269 size_t total_frame_count = frame_count_decoder->FrameCount(); | |
270 | |
271 IterationsOfFrameTimings timings(iterations, | |
272 FrameTimings(total_frame_count, 0.0)); | |
273 | |
227 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create(); | 274 RefPtr<SharedBuffer> packet_data = SharedBuffer::Create(); |
228 size_t position = 0; | 275 size_t position = 0; |
229 size_t next_frame_to_decode = 0; | 276 size_t next_frame_to_decode = 0; |
230 while (true) { | 277 while (true) { |
231 const char* packet; | 278 const char* packet; |
232 size_t length = data->GetSomeData(packet, position); | 279 size_t length = data->GetSomeData(packet, position); |
233 | 280 |
234 length = std::min(length, packet_size); | 281 length = std::min(length, packet_size); |
235 packet_data->Append(packet, length); | 282 packet_data->Append(packet, length); |
236 position += length; | 283 position += length; |
237 | 284 |
238 bool all_data_received = position == data->size(); | 285 bool all_data_received = position == data->size(); |
239 decoder->SetData(packet_data.Get(), all_data_received); | |
240 | 286 |
241 size_t frame_count = decoder->FrameCount(); | 287 size_t frame_count = decoder->FrameCount(); |
242 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) { | 288 for (size_t i = 0; i < iterations; ++i) { |
243 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode); | 289 for (; next_frame_to_decode < frame_count; ++next_frame_to_decode) { |
244 if (frame->GetStatus() != ImageFrame::kFrameComplete) | 290 decoder->SetData(packet_data.Get(), all_data_received); |
245 break; | 291 double start_time = GetCurrentTime(); |
292 ImageFrame* frame = decoder->FrameBufferAtIndex(next_frame_to_decode); | |
293 double elapsed_time = GetCurrentTime() - start_time; | |
294 if (frame->GetStatus() != ImageFrame::kFrameComplete) { | |
295 fprintf(stderr, "Image decode failed\n"); | |
296 exit(3); | |
scroggo_chromium
2017/05/23 15:42:23
Please pass different values to "exit" so it can e
scroggo_chromium
2017/05/23 15:46:02
My mistake, it looks like exit is already not usin
| |
297 } | |
298 timings[i][next_frame_to_decode] = elapsed_time; | |
299 decoder->SetData(PassRefPtr<SegmentReader>(nullptr), false); | |
300 decoder->ClearCacheExceptFrame(next_frame_to_decode); | |
301 } | |
246 } | 302 } |
247 | 303 |
248 if (all_data_received || decoder->Failed()) | 304 if (all_data_received || decoder->Failed()) { |
249 break; | 305 fprintf(stderr, "Image decode failed\n"); |
306 exit(3); | |
307 } | |
250 } | 308 } |
251 | 309 |
252 return !decoder->Failed(); | 310 Print2DResults(timings); |
253 } | 311 } |
254 | 312 |
255 } // namespace | 313 } // namespace |
256 | 314 |
257 int Main(int argc, char* argv[]) { | 315 int Main(int argc, char* argv[]) { |
258 base::CommandLine::Init(argc, argv); | 316 base::CommandLine::Init(argc, argv); |
259 | 317 |
260 // If the platform supports color correction, allow it to be controlled. | 318 // If the platform supports color correction, allow it to be controlled. |
261 | 319 |
262 bool apply_color_correction = false; | 320 bool apply_color_correction = false; |
263 | |
264 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) { | 321 if (argc >= 2 && strcmp(argv[1], "--color-correct") == 0) { |
265 apply_color_correction = (--argc, ++argv, true); | 322 --argc; |
323 ++argv; | |
324 apply_color_correction = true; | |
266 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin(); | 325 gfx::ICCProfile profile = gfx::ICCProfileForTestingColorSpin(); |
267 ColorBehavior::SetGlobalTargetColorProfile(profile); | 326 ColorBehavior::SetGlobalTargetColorProfile(profile); |
268 } | 327 } |
269 | 328 |
270 if (argc < 2) { | 329 if (argc < 2) { |
271 fprintf(stderr, | 330 fprintf(stderr, |
272 "Usage: %s [--color-correct] file [iterations] [packetSize]\n", | 331 "Usage: %s [--color-correct] file [iterations] [packetSize]\n", |
273 argv[0]); | 332 argv[0]); |
274 exit(1); | 333 exit(1); |
275 } | 334 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 // segments into one, contiguous block of memory. | 373 // segments into one, contiguous block of memory. |
315 | 374 |
316 RefPtr<SharedBuffer> data = ReadFile(argv[1]); | 375 RefPtr<SharedBuffer> data = ReadFile(argv[1]); |
317 if (!data.Get() || !data->size()) { | 376 if (!data.Get() || !data->size()) { |
318 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]); | 377 fprintf(stderr, "Error reading image data from [%s]\n", argv[1]); |
319 exit(2); | 378 exit(2); |
320 } | 379 } |
321 | 380 |
322 data->Data(); | 381 data->Data(); |
323 | 382 |
324 // Warm-up: throw out the first iteration for more consistent results. | 383 // Image decode bench for iterations. |
325 | 384 |
326 if (!DecodeImageData(data.Get(), apply_color_correction, packet_size)) { | 385 std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create( |
327 fprintf(stderr, "Image decode failed [%s]\n", argv[1]); | 386 data, true, ImageDecoder::kAlphaPremultiplied, |
328 exit(3); | 387 apply_color_correction ? ColorBehavior::TransformToTargetForTesting() |
388 : ColorBehavior::Ignore()); | |
389 if (packet_size) { | |
390 TimePacketedDecode(decoder.get(), data.Get(), packet_size, iterations); | |
391 } else { | |
392 TimeDecode(decoder.get(), data.Get(), iterations); | |
329 } | 393 } |
330 | 394 |
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; | 395 return 0; |
352 } | 396 } |
353 | 397 |
354 } // namespace blink | 398 } // namespace blink |
355 | 399 |
356 int main(int argc, char* argv[]) { | 400 int main(int argc, char* argv[]) { |
357 return blink::Main(argc, argv); | 401 return blink::Main(argc, argv); |
358 } | 402 } |
OLD | NEW |