976e36599d7fd2b866aea318bc343788bd0c425b
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / cc / picture_as_image_data.js
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
3 // found in the LICENSE file.
4
5 'use strict';
6
7 base.exportTo('cc', function() {
8
9   /**
10    * @constructor
11    */
12   function PictureAsImageData(picture, errorOrImageData) {
13     this.picture_ = picture;
14     if (errorOrImageData instanceof ImageData) {
15       this.error_ = undefined;
16       this.imageData_ = errorOrImageData;
17     } else {
18       this.error_ = errorOrImageData;
19       this.imageData_ = undefined;
20     }
21   };
22
23   /**
24    * Creates a new pending PictureAsImageData (no image data and no error).
25    *
26    * @return {PictureAsImageData} a new pending PictureAsImageData.
27    */
28   PictureAsImageData.Pending = function(picture) {
29     return new PictureAsImageData(picture, undefined);
30   };
31
32   PictureAsImageData.prototype = {
33     get picture() {
34       return this.picture_;
35     },
36
37     get error() {
38       return this.error_;
39     },
40
41     get imageData() {
42       return this.imageData_;
43     },
44
45     isPending: function() {
46       return this.error_ === undefined && this.imageData_ === undefined;
47     },
48
49     asCanvas: function() {
50       if (!this.imageData_)
51         return;
52
53       var canvas = document.createElement('canvas');
54       var ctx = canvas.getContext('2d');
55
56       canvas.width = this.imageData_.width;
57       canvas.height = this.imageData_.height;
58       ctx.putImageData(this.imageData_, 0, 0);
59       return canvas;
60     }
61   };
62
63   return {
64     PictureAsImageData: PictureAsImageData
65   };
66 });