diff options
author | noelallen@chromium.org <noelallen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-29 16:11:14 +0000 |
---|---|---|
committer | noelallen@chromium.org <noelallen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-29 16:11:14 +0000 |
commit | 804e103508e5f0c47ead50119ec9ee84e2e2959a (patch) | |
tree | 85d5112abb9292402d9690dc6efd9893a3636348 /native_client_sdk | |
parent | 93bb5b77e9a96defb3e25ccb64fe6700ca9c4d37 (diff) | |
download | chromium_src-804e103508e5f0c47ead50119ec9ee84e2e2959a.zip chromium_src-804e103508e5f0c47ead50119ec9ee84e2e2959a.tar.gz chromium_src-804e103508e5f0c47ead50119ec9ee84e2e2959a.tar.bz2 |
Add Core example
As part of the example reorg, we are adding examples for all APIs.
This CL shows how to use Core to get the time and call on main.
BUG=234855
R=binji@chromium.org
Review URL: https://codereview.chromium.org/14049038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@197054 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk')
-rw-r--r-- | native_client_sdk/src/examples/api/core/core.cc | 74 | ||||
-rw-r--r-- | native_client_sdk/src/examples/api/core/example.dsc | 19 | ||||
-rw-r--r-- | native_client_sdk/src/examples/api/core/example.js | 68 | ||||
-rw-r--r-- | native_client_sdk/src/examples/api/core/index.html | 47 |
4 files changed, 208 insertions, 0 deletions
diff --git a/native_client_sdk/src/examples/api/core/core.cc b/native_client_sdk/src/examples/api/core/core.cc new file mode 100644 index 0000000..faf176b --- /dev/null +++ b/native_client_sdk/src/examples/api/core/core.cc @@ -0,0 +1,74 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <string> + +#include "ppapi/cpp/instance.h" +#include "ppapi/cpp/module.h" +#include "ppapi/cpp/var.h" +#include "ppapi/utility/completion_callback_factory.h" + + +/// The Instance class. One of these exists for each instance of your NaCl +/// module on the web page. The browser will ask the Module object to create +/// a new Instance for each occurrence of the <embed> tag that has these +/// attributes: +/// type="application/x-nacl" +/// src="file_histogram.nmf" +class CoreInstance : public pp::Instance { + public: + /// The constructor creates the plugin-side instance. + /// @param[in] instance the handle to the browser-side plugin instance. + explicit CoreInstance(PP_Instance instance) + : pp::Instance(instance), + callback_factory_(this) { + } + + private: + /// Handler for messages coming in from the browser via postMessage(). The + /// @a var_message will contain the requested delay time. + /// + /// @param[in] var_message The message posted by the browser. + virtual void HandleMessage(const pp::Var& var_message) { + int32_t delay = var_message.AsInt(); + if (delay) { + // If a delay is requested, issue a callback after delay ms. + last_receive_time_ = pp::Module::Get()->core()->GetTimeTicks(); + pp::Module::Get()->core()->CallOnMainThread( + delay, callback_factory_.NewCallback(&CoreInstance::DelayedPost), + 0); + } else { + // If no delay is requested, reply immediately with zero time elapsed. + pp::Var msg(0); + PostMessage(msg); + } + } + + void DelayedPost(int32_t) { + // Send the time elapsed until the callbacked fired. + pp::Var msg( + pp::Module::Get()->core()->GetTimeTicks() - last_receive_time_); + PostMessage(msg); + } + + private: + pp::CompletionCallbackFactory<CoreInstance> callback_factory_; + PP_TimeTicks last_receive_time_; +}; + +class CoreModule : public pp::Module { + public: + CoreModule() : pp::Module() {} + virtual ~CoreModule() {} + + virtual pp::Instance* CreateInstance(PP_Instance instance) { + return new CoreInstance(instance); + } +}; + +namespace pp { +Module* CreateModule() { + return new CoreModule(); +} +} // namespace pp diff --git a/native_client_sdk/src/examples/api/core/example.dsc b/native_client_sdk/src/examples/api/core/example.dsc new file mode 100644 index 0000000..885ad10 --- /dev/null +++ b/native_client_sdk/src/examples/api/core/example.dsc @@ -0,0 +1,19 @@ +{ + 'TOOLS': ['newlib', 'glibc', 'pnacl', 'win', 'linux'], + 'TARGETS': [ + { + 'NAME' : 'core', + 'TYPE' : 'main', + 'SOURCES' : ['core.cc'], + 'LIBS' : ['ppapi_cpp', 'ppapi', 'pthread'] + } + ], + 'DATA': [ + 'example.js', + ], + 'DEST': 'examples/api', + 'NAME': 'core', + 'TITLE': 'Core', + 'GROUP': 'API' +} + diff --git a/native_client_sdk/src/examples/api/core/example.js b/native_client_sdk/src/examples/api/core/example.js new file mode 100644 index 0000000..03e5a0b --- /dev/null +++ b/native_client_sdk/src/examples/api/core/example.js @@ -0,0 +1,68 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +var itrMax = 1000; +var itrCount = 0; +var itrSend = new Float64Array(itr_max); +var itrNaCl = new Float64Array(itr_max); +var itrRecv = new Float64Array(itr_max); +var delay = 0; + +function attachListeners() { + document.getElementById('start').addEventListener('click', + startTest); + count_pp = document.getElementById('count') + count_pp.textContent = itrMax; +} + +function startTest() { + if (common.naclModule) { + var startButton = document.getElementById('start'); + startButton.disabled = true; + + var delayControl = document.getElementById('delay'); + delay = parseInt(delayControl.value, 10); + + common.updateStatus('Running Test'); + itrCount = 0; + itrSend[0] = (new Date()).getTime(); + common.naclModule.postMessage(delay); + } +} + +function setStats(nacl, compute, total) { + var statNaCl = document.getElementById('NaCl'); + var statRound = document.getElementById('Round'); + var statAll = document.getElementById('Total'); + + statNaCl.textContent = (nacl / itrMax) + ' ms'; + statRound.textContent = (compute / itrMax) + ' ms'; + statAll.textContent = (total / itrMax) + ' ms'; +} + +// Called by the common.js module. +function handleMessage(message_event) { + // Convert NaCl Seconds elapsed to MS + itrNaCl[itrCount] = message_event.data * 1000.0; + itrRecv[itrCount] = (new Date()).getTime(); + itrCount++; + + if (itrCount == itrMax) { + common.updateStatus('Test Finished'); + var startButton = document.getElementById('start'); + startButton.disabled = false; + + var naclMS = 0.0; + var computeMS = 0.0; + for (var i=0; i < itrMax; i++) { + naclMS += itrNaCl[i]; + computeMS += itrRecv[i] - itrSend[i]; + } + + SetStats(naclMS, computeMS, itrRecv[itrMax - 1] - itrSend[0]); + } else { + itrSend[itrCount] = (new Date()).getTime(); + common.naclModule.postMessage(delay); + } +} diff --git a/native_client_sdk/src/examples/api/core/index.html b/native_client_sdk/src/examples/api/core/index.html new file mode 100644 index 0000000..a3fa865 --- /dev/null +++ b/native_client_sdk/src/examples/api/core/index.html @@ -0,0 +1,47 @@ +<!DOCTYPE html> +<html> + <!-- + Copyright (c) 2013 The Chromium Authors. All rights reserved. + Use of this source code is governed by a BSD-style license that can be + found in the LICENSE file. + --> +<head> + <meta http-equiv="Pragma" content="no-cache"> + <meta http-equiv="Expires" content="-1"> + <title>{{title}}</title> + <script type="text/javascript" src="common.js"></script> + <script type ="text/javascript" src="example.js"></script> +</head> +<body {{attrs}}> + <h1>{{title}}</h1> + <h2>Status: <code id="statusField">NO-STATUS</code></h2> + <p>The Core API example demonstrates the basic use of the Time and Callback + functions.</p> + <p> Push 'Start Test' to run <span id="count">0</span> iterations. Each + iteration will send a message to the NaCl module containing the number of + milliseconds to delay. The modules will immediately post a message back + if the delay is zero, otherwise, it will issue a CallOnMainThread with + the delay specified.</p> + <p> Once the test completes, it will calculate the average time spent + between the message reception and the callback triggering (NaCl Delay), + the average time for a single round trip (Round Trip), and the + average iteration including other JS overhead (Total). </p> + <div> + NaCl Delay + <input type="number" id="delay" value="0" min="0" max="20" + style="text-align:right"> ms + <input type="button" id="start" value="Start Test"> + <table style="border:1px solid black"> + <tr> + <th>NaCl Delay</th><th>Round Trip</th><th>Total</th> + </tr> + <tr> + <td id="NaCl">Unknown</td> + <td id="Round">Unknown</td> + <td id="Total">Unknown</td> + </tr> + </table> + </div> + <div id="listener"></div> +</body> +</html> |