Mercurial > hg > orthanc-stone
comparison Framework/Viewport/WebAssemblyCairoViewport.cpp @ 1232:a28861abf888 broker
viewports for WebAssembly
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Dec 2019 17:46:33 +0100 |
parents | |
children | 0ca50d275b9a |
comparison
equal
deleted
inserted
replaced
1229:b9f2a111c5b9 | 1232:a28861abf888 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "WebAssemblyCairoViewport.h" | |
23 | |
24 #include "../Scene2D/CairoCompositor.h" | |
25 | |
26 #include <Core/Images/Image.h> | |
27 | |
28 namespace OrthancStone | |
29 { | |
30 void WebAssemblyCairoViewport::GetCanvasSize(unsigned int& width, | |
31 unsigned int& height) | |
32 { | |
33 double w, h; | |
34 emscripten_get_element_css_size(GetFullCanvasId().c_str(), &w, &h); | |
35 | |
36 /** | |
37 * Emscripten has the function emscripten_get_element_css_size() | |
38 * to query the width and height of a named HTML element. I'm | |
39 * calling this first to get the initial size of the canvas DOM | |
40 * element, and then call emscripten_set_canvas_size() to | |
41 * initialize the framebuffer size of the canvas to the same | |
42 * size as its DOM element. | |
43 * https://floooh.github.io/2017/02/22/emsc-html.html | |
44 **/ | |
45 if (w > 0 && | |
46 h > 0) | |
47 { | |
48 width = static_cast<unsigned int>(boost::math::iround(w)); | |
49 height = static_cast<unsigned int>(boost::math::iround(h)); | |
50 } | |
51 else | |
52 { | |
53 width = 0; | |
54 height = 0; | |
55 } | |
56 } | |
57 | |
58 | |
59 void WebAssemblyCairoViewport::Paint(ICompositor& compositor, | |
60 ViewportController& controller) | |
61 { | |
62 compositor.Refresh(controller.GetScene()); | |
63 | |
64 // Create a temporary memory buffer for the canvas in JavaScript | |
65 Orthanc::ImageAccessor cairo; | |
66 dynamic_cast<CairoCompositor&>(compositor).GetCanvas().GetReadOnlyAccessor(cairo); | |
67 | |
68 const unsigned int width = cairo.GetWidth(); | |
69 const unsigned int height = cairo.GetHeight(); | |
70 | |
71 if (javascript_.get() == NULL || | |
72 javascript_->GetWidth() != width || | |
73 javascript_->GetHeight() != height) | |
74 { | |
75 javascript_.reset(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, width, height, | |
76 true /* force minimal pitch */)); | |
77 } | |
78 | |
79 // Convert from BGRA32 memory layout (only color mode supported | |
80 // by Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 | |
81 // (as expected by HTML5 canvas). This simply amounts to | |
82 // swapping the B and R channels. Alpha channel is also set to | |
83 // full opacity (255). | |
84 uint8_t* q = reinterpret_cast<uint8_t*>(javascript_->GetBuffer()); | |
85 for (unsigned int y = 0; y < height; y++) | |
86 { | |
87 const uint8_t* p = reinterpret_cast<const uint8_t*>(cairo.GetConstRow(y)); | |
88 for (unsigned int x = 0; x < width; x++) | |
89 { | |
90 q[0] = p[2]; // R | |
91 q[1] = p[1]; // G | |
92 q[2] = p[0]; // B | |
93 q[3] = 255; // A | |
94 | |
95 p += 4; | |
96 q += 4; | |
97 } | |
98 } | |
99 | |
100 // Execute JavaScript commands to blit the image buffer onto the | |
101 // 2D drawing context of the HTML5 canvas | |
102 EM_ASM({ | |
103 const data = new Uint8ClampedArray(Module.HEAP8.buffer, $1, 4 * $2 * $3); | |
104 const img = new ImageData(data, $2, $3); | |
105 const ctx = document.getElementById(UTF8ToString($0)).getContext('2d'); | |
106 ctx.putImageData(img, 0, 0); | |
107 }, | |
108 GetShortCanvasId().c_str(), // $0 | |
109 javascript_->GetBuffer(), // $1 | |
110 javascript_->GetWidth(), // $2 | |
111 javascript_->GetHeight()); // $3 | |
112 } | |
113 | |
114 | |
115 void WebAssemblyCairoViewport::UpdateSize(ICompositor& compositor) | |
116 { | |
117 unsigned int width, height; | |
118 GetCanvasSize(width, height); | |
119 emscripten_set_canvas_element_size(GetFullCanvasId().c_str(), width, height); | |
120 | |
121 dynamic_cast<CairoCompositor&>(compositor).UpdateSize(width, height); | |
122 } | |
123 | |
124 | |
125 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvasId) : | |
126 WebAssemblyViewport(canvasId, NULL) | |
127 { | |
128 unsigned int width, height; | |
129 GetCanvasSize(width, height); | |
130 emscripten_set_canvas_element_size(GetFullCanvasId().c_str(), width, height); | |
131 AcquireCompositor(new CairoCompositor(width, height)); | |
132 } | |
133 } |