@windowkit/appkit 0.35.0 → 0.35.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -655,16 +655,18 @@ native.ctxDrawSurfaceFiltered(win, card, 0, 0, 556, 300, 100, 100, 556, 300, {
655
655
  fractal, sel, grad}` (the reference code's lattice, 514 selector entries and 4 x 514 x
656
656
  2 gradients, or a `seed` it is made from here) and `feimage {image}`, an index into
657
657
  `images`, surfaces the region's size. Each primitive is one pass (two to six for a
658
- blur) into RGBA16F textures kept between runs, each in its colour space, with a
659
- result's texture free again after its last read; `precision: 'float'` makes them
660
- RGBA32F, which matches react-x11-components' `runFilter` within 2 levels where half
661
- floats lose the colour of a pixel all but transparent that a later primitive makes
662
- opaque. The source is read where it lies, without a copy. All of a graph or none:
658
+ blur) into RGBA16F textures kept between runs, of every size a page runs as far
659
+ as 384MB, each in its colour space, with a result's texture free again after its
660
+ last read; an `feTurbulence` is kept, as far as 192MB, by every number it is made
661
+ from, so a graph run again over a source that changed makes its noise once;
662
+ `precision: 'float'` makes them RGBA32F, which matches react-x11-components'
663
+ `runFilter` within 2 levels where half floats lose the colour of a pixel all but
664
+ transparent that a later primitive makes opaque. The source is read where it lies, without a copy. All of a graph or none:
663
665
  answers false, drawing nothing, for a primitive or a value it does not take, a region
664
666
  past 16384 pixels a side, or no Metal device, so the caller runs the filter itself.
665
667
  A FreeHand-sized group (`feTurbulence` of 4 octaves displacing the source) is 6.6ms
666
- at 2198x1172 and 18ms at 4400x2340 on an M1 Pro, where `runFilter` took 319 and
667
- 1319. The shaders compile at the first call, some 100–200ms.
668
+ at 2198x1172 and 18ms at 4400x2340 on an M1 Pro the first time, where `runFilter`
669
+ took 319 and 1319, and 2.5ms at 2198x1172 each time after, its noise kept. The shaders compile at the first call, some 100–200ms.
668
670
  - **`native.filterGraphPrimitives()`** — the primitives `ctxDrawFilterGraph` runs, by
669
671
  element name in lower case (`'feturbulence'`, `'fedisplacementmap'`, …), what the
670
672
  PRD's `filterGraph.primitives` is: empty where there is no Metal device or its shaders
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.35.0",
3
+ "version": "0.35.1",
4
4
  "description": "Retained-mode AppKit backend for Node.js: Core Animation (CALayer) layer trees, CoreText layout and drawing, IOSurface presentation, NSMenu, and native control bezels — the macOS half of react-x11.",
5
5
  "author": "Andrey Sidorov <andrey.sidorov@gmail.com>",
6
6
  "license": "MIT",
Binary file
@@ -21,7 +21,8 @@
21
21
  // from a pool, each in its primitive's colour space — a
22
22
  // conversion is done where an input is read, never as a pass of its
23
23
  // own — and a result's texture goes back to the run's free list after
24
- // its last read, as runFilter lets a plane go;
24
+ // its last read, as runFilter lets a plane go; an feTurbulence is
25
+ // kept by its numbers, and a run after takes it as it is;
25
26
  // 4. the last result, in sRGB, premultiplied 8-bit into a shared buffer;
26
27
  // the command buffer is waited for, and the buffer drawn as a CGImage
27
28
  // with its corner at (dx, dy) under the destination's state, as
@@ -1219,17 +1220,26 @@ GPU* TheGPU() {
1219
1220
 
1220
1221
  // Free plane textures, kept between runs: a scroll runs the same graph at
1221
1222
  // the same size every frame. A run takes from here and gives back only once
1222
- // its command buffer is done, so two threads never share one in flight;
1223
- // what is kept is the last run's size, as many as it held at most.
1223
+ // its command buffer is done, so two threads never share one in flight.
1224
+ // What is kept is the newest, of any size, as far as kPoolBytes: a page
1225
+ // with filters of several sizes — FreeHand's two groups larger than the
1226
+ // window and its dozens of small ones — made textures of 40MB again at
1227
+ // almost every run while only the last run's size was kept.
1224
1228
  std::mutex gPoolLock;
1225
- std::vector<id<MTLTexture>> gPool;
1229
+ std::vector<id<MTLTexture>> gPool; // oldest first
1226
1230
  // the output buffer, kept likewise while no image is drawing from it
1227
1231
  id<MTLBuffer> gSpareOut;
1232
+ constexpr size_t kPoolBytes = 384u << 20;
1233
+ constexpr size_t kPoolMost = 32;
1234
+
1235
+ size_t PlaneBytes(id<MTLTexture> t) {
1236
+ return (size_t)t.width * t.height * (t.pixelFormat == MTLPixelFormatRGBA32Float ? 16 : 8);
1237
+ }
1228
1238
 
1229
1239
  id<MTLTexture> NewPlane(GPU* g, long w, long h, MTLPixelFormat format) {
1230
1240
  {
1231
1241
  std::lock_guard<std::mutex> lock(gPoolLock);
1232
- for (size_t i = 0; i < gPool.size(); i++) {
1242
+ for (size_t i = gPool.size(); i-- > 0;) {
1233
1243
  if ((long)gPool[i].width == w && (long)gPool[i].height == h &&
1234
1244
  gPool[i].pixelFormat == format) {
1235
1245
  id<MTLTexture> t = gPool[i];
@@ -1248,19 +1258,77 @@ id<MTLTexture> NewPlane(GPU* g, long w, long h, MTLPixelFormat format) {
1248
1258
  return [g->device newTextureWithDescriptor:d];
1249
1259
  }
1250
1260
 
1251
- void GiveBack(std::vector<id<MTLTexture>>& planes, long w, long h, MTLPixelFormat format) {
1261
+ void GiveBack(std::vector<id<MTLTexture>>& planes) {
1252
1262
  std::lock_guard<std::mutex> lock(gPoolLock);
1253
- // only the size just run is worth keeping
1254
- gPool.erase(std::remove_if(gPool.begin(), gPool.end(),
1255
- [&](id<MTLTexture> t) {
1256
- return (long)t.width != w || (long)t.height != h ||
1257
- t.pixelFormat != format;
1258
- }),
1259
- gPool.end());
1260
1263
  for (auto& t : planes) {
1261
- if (t && gPool.size() < 8) gPool.push_back(t);
1264
+ if (t) gPool.push_back(t);
1262
1265
  }
1263
1266
  planes.clear();
1267
+ // the oldest go first, past what is kept
1268
+ size_t total = 0;
1269
+ for (auto& t : gPool) total += PlaneBytes(t);
1270
+ size_t drop = 0;
1271
+ while (drop < gPool.size() && (total > kPoolBytes || gPool.size() - drop > kPoolMost)) {
1272
+ total -= PlaneBytes(gPool[drop]);
1273
+ drop++;
1274
+ }
1275
+ gPool.erase(gPool.begin(), gPool.begin() + drop);
1276
+ }
1277
+
1278
+ // Turbulences made, kept by everything they are made from: an feTurbulence
1279
+ // is the same noise at every run of a graph whose numbers for it did not
1280
+ // change, and FreeHand's runs its filters again at each step of a scroll
1281
+ // that changes the strokes they displace, the noise and all. Made once,
1282
+ // in the run's own precision, and taken as an input after; only a run that
1283
+ // completed adds one. The newest are kept, as far as kNoiseBytes.
1284
+ struct KeptNoise {
1285
+ long W, H;
1286
+ MTLPixelFormat format;
1287
+ TurbulenceArgs args;
1288
+ std::vector<int32_t> sel;
1289
+ std::vector<float> grad;
1290
+ id<MTLTexture> tex;
1291
+ };
1292
+ std::mutex gNoiseLock;
1293
+ std::vector<KeptNoise> gNoise; // oldest first
1294
+ constexpr size_t kNoiseBytes = 192u << 20;
1295
+ constexpr size_t kNoiseMost = 8;
1296
+
1297
+ bool SameNoise(const KeptNoise& k, long W, long H, MTLPixelFormat format,
1298
+ const TurbulenceArgs& a, const std::vector<int32_t>& sel,
1299
+ const std::vector<float>& grad) {
1300
+ return k.W == W && k.H == H && k.format == format &&
1301
+ memcmp(&k.args, &a, sizeof a) == 0 && k.sel == sel && k.grad == grad;
1302
+ }
1303
+
1304
+ id<MTLTexture> KeptNoiseFor(long W, long H, MTLPixelFormat format, const TurbulenceArgs& a,
1305
+ const std::vector<int32_t>& sel, const std::vector<float>& grad) {
1306
+ std::lock_guard<std::mutex> lock(gNoiseLock);
1307
+ for (size_t i = 0; i < gNoise.size(); i++) {
1308
+ if (!SameNoise(gNoise[i], W, H, format, a, sel, grad)) continue;
1309
+ // the newest last
1310
+ KeptNoise k = std::move(gNoise[i]);
1311
+ gNoise.erase(gNoise.begin() + i);
1312
+ gNoise.push_back(std::move(k));
1313
+ return gNoise.back().tex;
1314
+ }
1315
+ return nil;
1316
+ }
1317
+
1318
+ void KeepNoise(KeptNoise&& made) {
1319
+ std::lock_guard<std::mutex> lock(gNoiseLock);
1320
+ for (auto& k : gNoise) {
1321
+ if (SameNoise(k, made.W, made.H, made.format, made.args, made.sel, made.grad)) return;
1322
+ }
1323
+ gNoise.push_back(std::move(made));
1324
+ size_t total = 0;
1325
+ for (auto& k : gNoise) total += PlaneBytes(k.tex);
1326
+ size_t drop = 0;
1327
+ while (drop < gNoise.size() && (total > kNoiseBytes || gNoise.size() - drop > kNoiseMost)) {
1328
+ total -= PlaneBytes(gNoise[drop].tex);
1329
+ drop++;
1330
+ }
1331
+ gNoise.erase(gNoise.begin(), gNoise.begin() + drop);
1264
1332
  }
1265
1333
 
1266
1334
  // A surface as a texture to read: the IOSurface under it, a buffer over its
@@ -1562,6 +1630,8 @@ static Napi::Value CtxDrawFilterGraph(const Napi::CallbackInfo& info) {
1562
1630
  left[count - 1]++;
1563
1631
  std::vector<Plane> results(count);
1564
1632
  Plane source{srcTex, false, false, false};
1633
+ // the turbulences this run makes, kept once it has completed
1634
+ std::vector<KeptNoise> noises;
1565
1635
  auto input = [&](int ref) {
1566
1636
  Plane p = ref >= 0 ? results[ref] : source;
1567
1637
  if (ref == kSourceAlpha) p.alpha = true;
@@ -1798,9 +1868,9 @@ static Napi::Value CtxDrawFilterGraph(const Napi::CallbackInfo& info) {
1798
1868
  a.sub = n.sub;
1799
1869
  const double* m = graph.matrix;
1800
1870
  double det = m[0] * m[3] - m[1] * m[2];
1801
- made.tex = plane();
1802
- if (!ok) break;
1803
1871
  if (!det || n.octaves == 0) {
1872
+ made.tex = plane();
1873
+ if (!ok) break;
1804
1874
  FloodArgs f{Sub{0, 0, 0, 0}, {0, 0, 0, 0}};
1805
1875
  run.Dispatch(P_FLOOD, made.tex, nil, nil, &f, sizeof f);
1806
1876
  break;
@@ -1824,9 +1894,19 @@ static Napi::Value CtxDrawFilterGraph(const Napi::CallbackInfo& info) {
1824
1894
  a.by = (float)n.by;
1825
1895
  a.octaves = n.octaves;
1826
1896
  a.fractal = n.fractal ? 1 : 0;
1897
+ // kept, it is no plane of the run's: never freed into its spares
1898
+ made.pooled = false;
1899
+ made.tex = KeptNoiseFor(W, H, run.format, a, n.sel, n.grad);
1900
+ if (made.tex) break;
1901
+ made.tex = NewPlane(g, W, H, run.format);
1902
+ if (!made.tex) {
1903
+ ok = false;
1904
+ break;
1905
+ }
1827
1906
  run.Dispatch(P_TURBULENCE, made.tex, nil, nil, &a, sizeof a, n.sel.data(),
1828
1907
  n.sel.size() * sizeof(int32_t), n.grad.data(),
1829
1908
  n.grad.size() * sizeof(float));
1909
+ noises.push_back(KeptNoise{W, H, run.format, a, n.sel, n.grad, made.tex});
1830
1910
  break;
1831
1911
  }
1832
1912
  case K_IMAGE: {
@@ -1887,8 +1967,9 @@ static Napi::Value CtxDrawFilterGraph(const Napi::CallbackInfo& info) {
1887
1967
  [cb waitUntilCompleted];
1888
1968
  if (cb.status != MTLCommandBufferStatusCompleted) ok = false;
1889
1969
  }
1890
- GiveBack(run.held, W, H, run.format);
1970
+ GiveBack(run.held);
1891
1971
  if (!ok) return no();
1972
+ for (auto& k : noises) KeepNoise(std::move(k));
1892
1973
 
1893
1974
  // the bytes as an image, drawn as ctxDrawSurface draws a surface; the
1894
1975
  // buffer goes back to be the next run's once the image lets it go