OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 5 |
6 // Namespace object for the utilities. | 6 // Namespace object for the utilities. |
7 function ImageUtil() {} | 7 function ImageUtil() {} |
8 | 8 |
9 // Performance trace. | 9 // Performance trace. |
10 ImageUtil.trace = (function() { | 10 ImageUtil.trace = (function() { |
11 function PerformanceTrace() { | 11 function PerformanceTrace() { |
12 this.lines_ = {}; | 12 this.lines_ = {}; |
13 this.timers_ = {}; | 13 this.timers_ = {}; |
14 this.container_ = null; | 14 this.container_ = null; |
15 } | 15 } |
16 | 16 |
17 PerformanceTrace.prototype.bindToDOM = function(container) { | 17 PerformanceTrace.prototype.bindToDOM = function(container) { |
18 this.container_ = container; | 18 this.container_ = container; |
19 }; | 19 }; |
20 | 20 |
21 PerformanceTrace.prototype.report = function(key, value) { | 21 PerformanceTrace.prototype.report = function(key, value) { |
22 if (!this.container_) return; | |
23 if (!(key in this.lines_)) { | 22 if (!(key in this.lines_)) { |
24 var div = this.lines_[key] = document.createElement('div'); | 23 if (this.container_) { |
25 this.container_.appendChild(div); | 24 var div = this.lines_[key] = document.createElement('div'); |
| 25 this.container_.appendChild(div); |
| 26 } else { |
| 27 this.lines_[key] = {}; |
| 28 } |
26 } | 29 } |
27 this.lines_[key].textContent = key + ': ' + value; | 30 this.lines_[key].textContent = key + ': ' + value; |
| 31 if (localStorage.logTrace) this.dumpLine(key); |
28 }; | 32 }; |
29 | 33 |
30 PerformanceTrace.prototype.resetTimer = function(key) { | 34 PerformanceTrace.prototype.resetTimer = function(key) { |
31 this.timers_[key] = Date.now(); | 35 this.timers_[key] = Date.now(); |
32 }; | 36 }; |
33 | 37 |
34 PerformanceTrace.prototype.reportTimer = function(key) { | 38 PerformanceTrace.prototype.reportTimer = function(key) { |
35 this.report(key, (Date.now() - this.timers_[key]) + 'ms'); | 39 this.report(key, (Date.now() - this.timers_[key]) + 'ms'); |
36 }; | 40 }; |
37 | 41 |
| 42 PerformanceTrace.prototype.dump = function() { |
| 43 for (var key in this.lines_) |
| 44 this.dumpLine(key); |
| 45 }; |
| 46 |
| 47 PerformanceTrace.prototype.dumpLine = function(key) { |
| 48 console.log('trace.' + this.lines_[key].textContent); |
| 49 }; |
| 50 |
38 return new PerformanceTrace(); | 51 return new PerformanceTrace(); |
39 })(); | 52 })(); |
40 | 53 |
41 | 54 |
42 ImageUtil.clamp = function(min, value, max) { | 55 ImageUtil.clamp = function(min, value, max) { |
43 return Math.max(min, Math.min(max, value)); | 56 return Math.max(min, Math.min(max, value)); |
44 }; | 57 }; |
45 | 58 |
46 ImageUtil.between = function(min, value, max) { | 59 ImageUtil.between = function(min, value, max) { |
47 return (value - min) * (value - max) <= 0; | 60 return (value - min) * (value - max) <= 0; |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 ImageUtil.ImageLoader.prototype.load = function( | 356 ImageUtil.ImageLoader.prototype.load = function( |
344 url, transform, callback, opt_delay) { | 357 url, transform, callback, opt_delay) { |
345 this.cancel(); | 358 this.cancel(); |
346 | 359 |
347 this.url_ = url; | 360 this.url_ = url; |
348 this.transform_ = transform || { scaleX: 1, scaleY: 1, rotate90: 0}; | 361 this.transform_ = transform || { scaleX: 1, scaleY: 1, rotate90: 0}; |
349 this.callback_ = callback; | 362 this.callback_ = callback; |
350 | 363 |
351 var self = this; | 364 var self = this; |
352 function startLoad() { | 365 function startLoad() { |
| 366 ImageUtil.metrics.startInterval(ImageUtil.getMetricName('ImageLoad')); |
353 self.timeout_ = null; | 367 self.timeout_ = null; |
354 // The clients of this class sometimes request the same url repeatedly. | 368 // The clients of this class sometimes request the same url repeatedly. |
355 // The onload fires only if the src is different from the previous value. | 369 // The onload fires only if the src is different from the previous value. |
356 // To work around that we create a new Image every time. | 370 // To work around that we create a new Image every time. |
357 self.image_ = new Image(); | 371 self.image_ = new Image(); |
358 self.image_.onload = function(e) { | 372 self.image_.onload = function(e) { |
359 self.image_ = null; | 373 self.image_ = null; |
360 self.convertImage_(e.target); | 374 self.convertImage_(e.target); |
361 }; | 375 }; |
362 self.image_.src = url; | 376 self.image_.src = url; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 var canvas = this.document_.createElement('canvas'); | 411 var canvas = this.document_.createElement('canvas'); |
398 | 412 |
399 if (this.transform_.rotate90 & 1) { // Rotated +/-90deg, swap the dimensions. | 413 if (this.transform_.rotate90 & 1) { // Rotated +/-90deg, swap the dimensions. |
400 canvas.width = image.height; | 414 canvas.width = image.height; |
401 canvas.height = image.width; | 415 canvas.height = image.width; |
402 } else { | 416 } else { |
403 canvas.width = image.width; | 417 canvas.width = image.width; |
404 canvas.height = image.height; | 418 canvas.height = image.height; |
405 } | 419 } |
406 | 420 |
407 ImageUtil.trace.resetTimer('load-convert'); | |
408 | |
409 var context = canvas.getContext('2d'); | 421 var context = canvas.getContext('2d'); |
410 context.save(); | 422 context.save(); |
411 context.translate(canvas.width / 2, canvas.height / 2); | 423 context.translate(canvas.width / 2, canvas.height / 2); |
412 context.rotate(this.transform_.rotate90 * Math.PI/2); | 424 context.rotate(this.transform_.rotate90 * Math.PI/2); |
413 context.scale(this.transform_.scaleX, this.transform_.scaleY); | 425 context.scale(this.transform_.scaleX, this.transform_.scaleY); |
414 | 426 |
415 var stripCount = Math.ceil(image.width * image.height / ( 1 << 21)); | 427 var stripCount = Math.ceil(image.width * image.height / ( 1 << 21)); |
416 var step = Math.max(16, Math.ceil(image.height / stripCount)) & 0xFFFFF0; | 428 var step = Math.max(16, Math.ceil(image.height / stripCount)) & 0xFFFFF0; |
417 | 429 |
418 this.copyStrip_(context, image, 0, step); | 430 this.copyStrip_(context, image, 0, step); |
419 }; | 431 }; |
420 | 432 |
421 ImageUtil.ImageLoader.prototype.copyStrip_ = function( | 433 ImageUtil.ImageLoader.prototype.copyStrip_ = function( |
422 context, image, firstRow, rowCount) { | 434 context, image, firstRow, rowCount) { |
423 var lastRow = Math.min (firstRow + rowCount, image.height); | 435 var lastRow = Math.min (firstRow + rowCount, image.height); |
424 | 436 |
425 context.drawImage( | 437 context.drawImage( |
426 image, 0, firstRow, image.width, lastRow - firstRow, | 438 image, 0, firstRow, image.width, lastRow - firstRow, |
427 -image.width / 2, firstRow - image.height / 2, | 439 -image.width / 2, firstRow - image.height / 2, |
428 image.width, lastRow - firstRow); | 440 image.width, lastRow - firstRow); |
429 | 441 |
430 if (lastRow == image.height) { | 442 if (lastRow == image.height) { |
431 context.restore(); | 443 context.restore(); |
432 ImageUtil.trace.reportTimer('load-convert'); | 444 if (image.src.substr(0, 5) != 'data:') { // Ignore data urls. |
| 445 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('ImageLoad')); |
| 446 } |
433 var callback = this.callback_; | 447 var callback = this.callback_; |
434 this.callback_ = null; | 448 this.callback_ = null; |
435 callback(context.canvas); | 449 callback(context.canvas); |
436 } else { | 450 } else { |
437 var self = this; | 451 var self = this; |
438 this.timeout_ = setTimeout( | 452 this.timeout_ = setTimeout( |
439 function() { | 453 function() { |
440 self.timeout_ = null; | 454 self.timeout_ = null; |
441 self.copyStrip_(context, image, lastRow, rowCount); | 455 self.copyStrip_(context, image, lastRow, rowCount); |
442 }, 0); | 456 }, 0); |
(...skipping 23 matching lines...) Expand all Loading... |
466 return ImageUtil.getFileNameFromFullName(ImageUtil.getFullNameFromUrl(url)); | 480 return ImageUtil.getFileNameFromFullName(ImageUtil.getFullNameFromUrl(url)); |
467 }; | 481 }; |
468 | 482 |
469 ImageUtil.replaceFileNameInFullName = function(fullName, name) { | 483 ImageUtil.replaceFileNameInFullName = function(fullName, name) { |
470 var index = fullName.lastIndexOf('.'); | 484 var index = fullName.lastIndexOf('.'); |
471 if (index != -1) | 485 if (index != -1) |
472 return name + fullName.substr(index); | 486 return name + fullName.substr(index); |
473 else | 487 else |
474 return name; | 488 return name; |
475 }; | 489 }; |
| 490 |
| 491 ImageUtil.metrics = null; |
| 492 |
| 493 ImageUtil.getMetricName = function(name) { return 'PhotoEditor.' + name } |
OLD | NEW |