1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/viewer/ImGuiLayer.h"
9
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPixmap.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSamplingOptions.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSurface.h"
21 #include "include/core/SkSwizzle.h"
22 #include "include/core/SkVertices.h"
23 #include "include/private/base/SkTDArray.h"
24 #include "src/base/SkTime.h"
25 #include "tools/skui/InputState.h"
26 #include "tools/skui/Key.h"
27
28 #include <cstdint>
29
30 using namespace sk_app;
31
build_ImFontAtlas(ImFontAtlas & atlas,SkPaint & fontPaint)32 static void build_ImFontAtlas(ImFontAtlas& atlas, SkPaint& fontPaint) {
33 int w, h;
34 unsigned char* pixels;
35 atlas.GetTexDataAsAlpha8(&pixels, &w, &h);
36 SkImageInfo info = SkImageInfo::MakeA8(w, h);
37 SkPixmap pmap(info, pixels, info.minRowBytes());
38 SkMatrix localMatrix = SkMatrix::Scale(1.0f / w, 1.0f / h);
39 auto fontImage = SkImages::RasterFromPixmap(pmap, nullptr, nullptr);
40 auto fontShader = fontImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear), localMatrix);
41 fontPaint.setShader(fontShader);
42 fontPaint.setColor(SK_ColorWHITE);
43 atlas.TexID = &fontPaint;
44 }
45
ImGuiLayer()46 ImGuiLayer::ImGuiLayer() {
47 // ImGui initialization:
48 ImGui::CreateContext();
49 ImGuiIO& io = ImGui::GetIO();
50
51 // Keymap...
52 io.KeyMap[ImGuiKey_Tab] = (int)skui::Key::kTab;
53 io.KeyMap[ImGuiKey_LeftArrow] = (int)skui::Key::kLeft;
54 io.KeyMap[ImGuiKey_RightArrow] = (int)skui::Key::kRight;
55 io.KeyMap[ImGuiKey_UpArrow] = (int)skui::Key::kUp;
56 io.KeyMap[ImGuiKey_DownArrow] = (int)skui::Key::kDown;
57 io.KeyMap[ImGuiKey_PageUp] = (int)skui::Key::kPageUp;
58 io.KeyMap[ImGuiKey_PageDown] = (int)skui::Key::kPageDown;
59 io.KeyMap[ImGuiKey_Home] = (int)skui::Key::kHome;
60 io.KeyMap[ImGuiKey_End] = (int)skui::Key::kEnd;
61 io.KeyMap[ImGuiKey_Delete] = (int)skui::Key::kDelete;
62 io.KeyMap[ImGuiKey_Backspace] = (int)skui::Key::kBack;
63 io.KeyMap[ImGuiKey_Enter] = (int)skui::Key::kOK;
64 io.KeyMap[ImGuiKey_Escape] = (int)skui::Key::kEscape;
65 io.KeyMap[ImGuiKey_A] = (int)skui::Key::kA;
66 io.KeyMap[ImGuiKey_C] = (int)skui::Key::kC;
67 io.KeyMap[ImGuiKey_V] = (int)skui::Key::kV;
68 io.KeyMap[ImGuiKey_X] = (int)skui::Key::kX;
69 io.KeyMap[ImGuiKey_Y] = (int)skui::Key::kY;
70 io.KeyMap[ImGuiKey_Z] = (int)skui::Key::kZ;
71
72 build_ImFontAtlas(*io.Fonts, fFontPaint);
73 }
74
~ImGuiLayer()75 ImGuiLayer::~ImGuiLayer() {
76 ImGui::DestroyContext();
77 }
78
setScaleFactor(float scaleFactor)79 void ImGuiLayer::setScaleFactor(float scaleFactor) {
80 ImGui::GetStyle().ScaleAllSizes(scaleFactor);
81
82 ImFontAtlas& atlas = *ImGui::GetIO().Fonts;
83 atlas.Clear();
84 ImFontConfig cfg;
85 cfg.SizePixels = 13 * scaleFactor;
86 atlas.AddFontDefault(&cfg);
87 build_ImFontAtlas(atlas, fFontPaint);
88 }
89
90 #if defined(SK_BUILD_FOR_UNIX)
get_clipboard_text(void * user_data)91 static const char* get_clipboard_text(void* user_data) {
92 Window* w = (Window*)user_data;
93 return w->getClipboardText();
94 }
95
set_clipboard_text(void * user_data,const char * text)96 static void set_clipboard_text(void* user_data, const char* text) {
97 Window* w = (Window*)user_data;
98 w->setClipboardText(text);
99 }
100 #endif
101
onAttach(Window * window)102 void ImGuiLayer::onAttach(Window* window) {
103 fWindow = window;
104
105 #if defined(SK_BUILD_FOR_UNIX)
106 ImGuiIO& io = ImGui::GetIO();
107 io.ClipboardUserData = fWindow;
108 io.GetClipboardTextFn = get_clipboard_text;
109 io.SetClipboardTextFn = set_clipboard_text;
110 #endif
111 }
112
onMouse(int x,int y,skui::InputState state,skui::ModifierKey modifiers)113 bool ImGuiLayer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
114 ImGuiIO& io = ImGui::GetIO();
115 io.MousePos.x = static_cast<float>(x);
116 io.MousePos.y = static_cast<float>(y);
117 if (skui::InputState::kDown == state) {
118 io.MouseDown[0] = true;
119 } else if (skui::InputState::kUp == state) {
120 io.MouseDown[0] = false;
121 }
122 return io.WantCaptureMouse;
123 }
124
onMouseWheel(float delta,int,int,skui::ModifierKey modifiers)125 bool ImGuiLayer::onMouseWheel(float delta, int, int, skui::ModifierKey modifiers) {
126 ImGuiIO& io = ImGui::GetIO();
127 io.MouseWheel += delta;
128 return io.WantCaptureMouse;
129 }
130
skiaWidget(const ImVec2 & size,SkiaWidgetFunc func)131 void ImGuiLayer::skiaWidget(const ImVec2& size, SkiaWidgetFunc func) {
132 intptr_t funcIndex = fSkiaWidgetFuncs.size();
133 fSkiaWidgetFuncs.push_back(func);
134 ImGui::Image((ImTextureID)funcIndex, size);
135 }
136
onPrePaint()137 void ImGuiLayer::onPrePaint() {
138 // Update ImGui input
139 ImGuiIO& io = ImGui::GetIO();
140
141 static double previousTime = 0.0;
142 double currentTime = SkTime::GetSecs();
143 io.DeltaTime = static_cast<float>(currentTime - previousTime);
144 previousTime = currentTime;
145
146 io.DisplaySize.x = static_cast<float>(fWindow->width());
147 io.DisplaySize.y = static_cast<float>(fWindow->height());
148
149 io.KeyAlt = io.KeysDown[static_cast<int>(skui::Key::kOption)];
150 io.KeyCtrl = io.KeysDown[static_cast<int>(skui::Key::kCtrl)];
151 io.KeyShift = io.KeysDown[static_cast<int>(skui::Key::kShift)];
152 io.KeySuper = io.KeysDown[static_cast<int>(skui::Key::kSuper)];
153
154 ImGui::NewFrame();
155 }
156
onPaint(SkSurface * surface)157 void ImGuiLayer::onPaint(SkSurface* surface) {
158 // This causes ImGui to rebuild vertex/index data based on all immediate-mode commands
159 // (widgets, etc...) that have been issued
160 ImGui::Render();
161
162 // Then we fetch the most recent data, and convert it so we can render with Skia
163 const ImDrawData* drawData = ImGui::GetDrawData();
164 SkTDArray<SkPoint> pos;
165 SkTDArray<SkPoint> uv;
166 SkTDArray<SkColor> color;
167
168 auto canvas = surface->getCanvas();
169
170 for (int i = 0; i < drawData->CmdListsCount; ++i) {
171 const ImDrawList* drawList = drawData->CmdLists[i];
172
173 // De-interleave all vertex data (sigh), convert to Skia types
174 pos.clear(); uv.clear(); color.clear();
175 for (int j = 0; j < drawList->VtxBuffer.size(); ++j) {
176 const ImDrawVert& vert = drawList->VtxBuffer[j];
177 pos.push_back(SkPoint::Make(vert.pos.x, vert.pos.y));
178 uv.push_back(SkPoint::Make(vert.uv.x, vert.uv.y));
179 color.push_back(vert.col);
180 }
181 // ImGui colors are RGBA
182 SkSwapRB(color.begin(), color.begin(), color.size());
183
184 int indexOffset = 0;
185
186 // Draw everything with canvas.drawVertices...
187 for (int j = 0; j < drawList->CmdBuffer.size(); ++j) {
188 const ImDrawCmd* drawCmd = &drawList->CmdBuffer[j];
189
190 SkAutoCanvasRestore acr(canvas, true);
191
192 // TODO: Find min/max index for each draw, so we know how many vertices (sigh)
193 if (drawCmd->UserCallback) {
194 drawCmd->UserCallback(drawList, drawCmd);
195 } else {
196 intptr_t idIndex = (intptr_t)drawCmd->TextureId;
197 if (idIndex < fSkiaWidgetFuncs.size()) {
198 // Small image IDs are actually indices into a list of callbacks. We directly
199 // examing the vertex data to deduce the image rectangle, then reconfigure the
200 // canvas to be clipped and translated so that the callback code gets to use
201 // Skia to render a widget in the middle of an ImGui panel.
202 ImDrawIdx rectIndex = drawList->IdxBuffer[indexOffset];
203 SkPoint tl = pos[rectIndex], br = pos[rectIndex + 2];
204 canvas->clipRect(SkRect::MakeLTRB(tl.fX, tl.fY, br.fX, br.fY));
205 canvas->translate(tl.fX, tl.fY);
206 fSkiaWidgetFuncs[idIndex](canvas);
207 } else {
208 SkPaint* paint = static_cast<SkPaint*>(drawCmd->TextureId);
209 SkASSERT(paint);
210
211 canvas->clipRect(SkRect::MakeLTRB(drawCmd->ClipRect.x, drawCmd->ClipRect.y,
212 drawCmd->ClipRect.z, drawCmd->ClipRect.w));
213 auto vertices = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
214 drawList->VtxBuffer.size(),
215 pos.begin(), uv.begin(), color.begin(),
216 drawCmd->ElemCount,
217 drawList->IdxBuffer.begin() + indexOffset);
218 canvas->drawVertices(vertices, SkBlendMode::kModulate, *paint);
219 }
220 indexOffset += drawCmd->ElemCount;
221 }
222 }
223 }
224
225 fSkiaWidgetFuncs.clear();
226 }
227
onKey(skui::Key key,skui::InputState state,skui::ModifierKey modifiers)228 bool ImGuiLayer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
229 ImGuiIO& io = ImGui::GetIO();
230 io.KeysDown[static_cast<int>(key)] = (skui::InputState::kDown == state);
231 return io.WantCaptureKeyboard;
232 }
233
onChar(SkUnichar c,skui::ModifierKey modifiers)234 bool ImGuiLayer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
235 ImGuiIO& io = ImGui::GetIO();
236 if (io.WantTextInput) {
237 if (c > 0 && c < 0x10000) {
238 io.AddInputCharacter(c);
239 }
240 return true;
241 }
242 return false;
243 }
244