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.
5 function moduleDidLoad() {
8 function postThreadFunc(numThreads) {
10 common.naclModule.postMessage({'message' : 'set_threads',
11 'value' : numThreads});
15 // Add event listeners after the NaCl module has loaded. These listeners will
16 // forward messages to the NaCl module via postMessage()
17 function attachListeners() {
18 document.getElementById('benchmark').addEventListener('click',
20 common.naclModule.postMessage({'message' : 'run benchmark'});
21 common.updateStatus('BENCHMARKING... (please wait)');
23 var threads = [0, 1, 2, 4, 6, 8, 12, 16, 24, 32];
24 for (var i = 0; i < threads.length; i++) {
25 document.getElementById('radio'+i).addEventListener('click',
26 postThreadFunc(threads[i]));
28 document.getElementById('zoomRange').addEventListener('input',
30 var value = parseFloat(document.getElementById('zoomRange').value);
31 common.naclModule.postMessage({'message' : 'set_zoom',
34 document.getElementById('lightRange').addEventListener('input',
36 var value = parseFloat(document.getElementById('lightRange').value);
37 common.naclModule.postMessage({'message' : 'set_light',
42 // Load a texture and send pixel data down to NaCl module.
43 function loadTexture(name) {
44 // Load image from jpg, decompress into canvas.
45 var img = new Image();
46 img.onload = function() {
47 var graph = document.createElement('canvas');
48 graph.width = img.width;
49 graph.height = img.height;
50 var context = graph.getContext('2d');
51 context.drawImage(img, 0, 0);
52 var imageData = context.getImageData(0, 0, img.width, img.height);
53 // Send NaCl module the raw image data obtained from canvas.
54 common.naclModule.postMessage({'message' : 'texture',
57 'height' : img.height,
58 'data' : imageData.data.buffer});
63 // Handle a message coming from the NaCl module.
64 function handleMessage(message_event) {
65 if (message_event.data['message'] == 'benchmark_result') {
67 var result = message_event.data['value'];
68 console.log('Benchmark result:' + result);
69 result = (Math.round(result * 1000) / 1000).toFixed(3);
70 document.getElementById('result').textContent =
71 'Result: ' + result + ' seconds';
72 common.updateStatus('SUCCESS');
73 } else if (message_event.data['message'] == 'set_zoom') {
75 var zoom = message_event.data['value'];
76 document.getElementById('zoomRange').value = zoom;
77 } else if (message_event.data['message'] == 'set_light') {
79 var light = message_event.data['value'];
80 document.getElementById('lightRange').value = light;
81 } else if (message_event.data['message'] == 'request_textures') {
82 // NaCl module is requesting a set of textures.
83 var names = message_event.data['names'];
84 for (var i = 0; i < names.length; i++)
85 loadTexture(names[i]);