comparison Platforms/Wasm/wasm-viewport.ts @ 526:548eed46f535 dev

introduced a Logger class that displays timing and source (C++/JS)
author Alain Mazy <alain@mazy.be>
date Thu, 14 Mar 2019 19:04:35 +0100
parents 801d2697a1b1
children 11510f92cc38
comparison
equal deleted inserted replaced
524:9e241cef32a4 526:548eed46f535
1 import wasmApplicationRunner = require('./wasm-application-runner'); 1 import wasmApplicationRunner = require('./wasm-application-runner');
2 //import stoneFrameworkLoader = require('./stone-framework-loader'); 2 //import stoneFrameworkLoader = require('./stone-framework-loader');
3 import * as Logger from './logger'
3 4
4 var isPendingRedraw = false; 5 var isPendingRedraw = false;
5 6
6 function ScheduleWebViewportRedraw(cppViewportHandle: any) : void 7 function ScheduleWebViewportRedraw(cppViewportHandle: any) : void
7 { 8 {
8 if (!isPendingRedraw) { 9 if (!isPendingRedraw) {
9 isPendingRedraw = true; 10 isPendingRedraw = true;
10 console.log('Scheduling a refresh of the viewport, as its content changed'); 11 Logger.defaultLogger.debug('Scheduling a refresh of the viewport, as its content changed');
11 window.requestAnimationFrame(function() { 12 window.requestAnimationFrame(function() {
12 isPendingRedraw = false; 13 isPendingRedraw = false;
13 WasmViewport.GetFromCppViewport(cppViewportHandle).Redraw(); 14 WasmViewport.GetFromCppViewport(cppViewportHandle).Redraw();
14 }); 15 });
15 } 16 }
79 80
80 this.module_ = module; 81 this.module_ = module;
81 this.canvasId_ = canvasId; 82 this.canvasId_ = canvasId;
82 this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement; 83 this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement;
83 if (this.htmlCanvas_ == null) { 84 if (this.htmlCanvas_ == null) {
84 console.log("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'"); 85 Logger.defaultLogger.error("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'");
85 } 86 }
86 this.context_ = this.htmlCanvas_.getContext('2d'); 87 this.context_ = this.htmlCanvas_.getContext('2d');
87 88
88 this.ViewportSetSize = this.module_.cwrap('ViewportSetSize', null, [ 'number', 'number', 'number' ]); 89 this.ViewportSetSize = this.module_.cwrap('ViewportSetSize', null, [ 'number', 'number', 'number' ]);
89 this.ViewportRender = this.module_.cwrap('ViewportRender', null, [ 'number', 'number', 'number', 'number' ]); 90 this.ViewportRender = this.module_.cwrap('ViewportRender', null, [ 'number', 'number', 'number', 'number' ]);
105 106
106 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport { 107 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport {
107 if (WasmViewport.viewportsMapByCppHandle_[cppViewportHandle] !== undefined) { 108 if (WasmViewport.viewportsMapByCppHandle_[cppViewportHandle] !== undefined) {
108 return WasmViewport.viewportsMapByCppHandle_[cppViewportHandle]; 109 return WasmViewport.viewportsMapByCppHandle_[cppViewportHandle];
109 } 110 }
110 console.log("WasmViewport not found !"); 111 Logger.defaultLogger.error("WasmViewport not found !");
111 return undefined; 112 return undefined;
112 } 113 }
113 114
114 public static GetFromCanvasId(canvasId: string) : WasmViewport { 115 public static GetFromCanvasId(canvasId: string) : WasmViewport {
115 if (WasmViewport.viewportsMapByCanvasId_[canvasId] !== undefined) { 116 if (WasmViewport.viewportsMapByCanvasId_[canvasId] !== undefined) {
116 return WasmViewport.viewportsMapByCanvasId_[canvasId]; 117 return WasmViewport.viewportsMapByCanvasId_[canvasId];
117 } 118 }
118 console.log("WasmViewport not found !"); 119 Logger.defaultLogger.error("WasmViewport not found !");
119 return undefined; 120 return undefined;
120 } 121 }
121 122
122 public static ResizeAll() { 123 public static ResizeAll() {
123 for (let canvasId in WasmViewport.viewportsMapByCanvasId_) { 124 for (let canvasId in WasmViewport.viewportsMapByCanvasId_) {
130 this.renderingBuffer_ === null || 131 this.renderingBuffer_ === null ||
131 this.ViewportRender(this.pimpl_, 132 this.ViewportRender(this.pimpl_,
132 this.imageData_.width, 133 this.imageData_.width,
133 this.imageData_.height, 134 this.imageData_.height,
134 this.renderingBuffer_) == 0) { 135 this.renderingBuffer_) == 0) {
135 console.log('The rendering has failed'); 136 Logger.defaultLogger.error('The rendering has failed');
136 } else { 137 } else {
137 // Create an accessor to the rendering buffer (i.e. create a 138 // Create an accessor to the rendering buffer (i.e. create a
138 // "window" above the heap of the WASM module), then copy it to 139 // "window" above the heap of the WASM module), then copy it to
139 // the ImageData object 140 // the ImageData object
140 this.imageData_.data.set(new Uint8ClampedArray( 141 this.imageData_.data.set(new Uint8ClampedArray(
155 156
156 // width/height is defined by the parent width/height 157 // width/height is defined by the parent width/height
157 this.htmlCanvas_.width = this.htmlCanvas_.parentElement.offsetWidth; 158 this.htmlCanvas_.width = this.htmlCanvas_.parentElement.offsetWidth;
158 this.htmlCanvas_.height = this.htmlCanvas_.parentElement.offsetHeight; 159 this.htmlCanvas_.height = this.htmlCanvas_.parentElement.offsetHeight;
159 160
160 console.log("resizing WasmViewport: ", this.htmlCanvas_.width, "x", this.htmlCanvas_.height); 161 Logger.defaultLogger.debug("resizing WasmViewport: ", this.htmlCanvas_.width, "x", this.htmlCanvas_.height);
161 162
162 if (this.imageData_ === null) { 163 if (this.imageData_ === null) {
163 this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height); 164 this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height);
164 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); 165 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height);
165 166