@windowkit/appkit 0.6.0 → 0.7.0

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
@@ -251,6 +251,62 @@ vibrancy materials need private API to reproduce; expose real `NSMenu` instead.
251
251
  `native.postMouseEvent(win, 'down'|'up'|'move'|'drag', x, y)` synthesizes events through
252
252
  the real pump — used by the demo's self-test (`CAL_CLICKS="x,y;x,y" npm run demo`).
253
253
 
254
+ ## Surfaces: blend modes and blits
255
+
256
+ A surface (`native.createSurface(wPx, hPx, scale)`, or `createSurfaceIOSurface` for the
257
+ zero-copy presentation kind) is a `CGBitmapContext` with a top-left origin and the canvas
258
+ drawing verbs over it — `ctxFillRect`, `ctxDrawSurface`, `ctxDrawGlyphs` and the rest.
259
+ Two of those verbs are what a 2d context needs to composite one surface into another
260
+ without paying for a `CGImage`:
261
+
262
+ - **`native.ctxSetBlendMode(surface, mode)`** — canvas's `globalCompositeOperation`, in
263
+ CoreGraphics' spelling. Every canvas name maps to an exact `CGBlendMode`:
264
+ `source-over` (the default) through `xor` and `lighter`, and the separable and
265
+ non-separable blend modes below them, `multiply` … `luminosity`; `clear`,
266
+ `plus-lighter` and `plus-darker` are CoreGraphics' own and go through too. A name off
267
+ that list leaves the mode in force alone and answers `false` — canvas's rule for an
268
+ unknown value, so a caller can keep its own property in step — and a known one answers
269
+ `true`. The mode is graphics state, so `ctxSave`/`ctxRestore` bracket it.
270
+
271
+ `copy` is the one that matters for compositing: it makes a paint a *replacement*
272
+ rather than a blend, alpha included, which is what an offscreen surface presented into
273
+ a window wants and what `PictOp.Src` means on the X11 side.
274
+
275
+ - **`native.blitSurface(src, sx, sy, w, h, dst, dx, dy, clip?)`** — a row `memcpy` of a
276
+ rect of one surface into another, at surfaces of any two sizes, returning the
277
+ destination rect it actually wrote as `[x, y, w, h]` or `null` when the intersection
278
+ came out empty. `copySurfaceRegion` is the same-size special case a swapchain wants;
279
+ this is the general one, for a caller compositing an offscreen surface into a window at
280
+ a translate — a terminal's grid, an element's retained scene. It is exactly what a
281
+ `copy`-mode `ctxDrawSurface` at 1:1 under a translate-only transform produces, byte for
282
+ byte, without building a `CGImage` of the whole source: 2000x1620 into a window-sized
283
+ surface is 1.2ms that way and 0.42ms this way on an M1 Pro.
284
+
285
+ Coordinates are device pixels, top-left origin — the convention `createSurface`'s CTM
286
+ gives user space, and the one `copySurfaceRegion`'s rects already use. Neither the
287
+ destination's CTM nor its clip is visible to a `memcpy`, so `clip`, when given, is
288
+ `[x, y, w, h]` in the **destination's** pixels and the caller passes the clip it is
289
+ drawing under; a damage region of several rects is several calls. The rect copied is
290
+ the destination rect intersected with that clip and with both surfaces' bounds, the
291
+ source origin moving with it. An IOSurface-backed surface at either end wants the usual
292
+ `surfaceLock`/`surfaceUnlock` bracketing, and two handles onto one bitmap — a shared
293
+ IOSurface looked up at both ends, or a surface onto itself — are refused, because
294
+ overlapping `memcpy` rows have no defined result.
295
+
296
+ ```js
297
+ const grid = native.createSurface(2000, 1620, 2); // the offscreen scene
298
+ const win = native.createSurface(2200, 1800, 2); // what the window presents
299
+
300
+ // the composite, clipped to the paint pass's damage rect
301
+ native.blitSurface(grid, 0, 0, 2000, 1620, win, 40, 30, [100, 100, 1200, 900]);
302
+ // -> [100, 100, 1200, 900] — the clip, which the blit covered whole
303
+
304
+ // the same pixels the slow way, for anything that is not a 1:1 translate
305
+ native.ctxSetBlendMode(win, 'copy');
306
+ native.ctxDrawSurface(win, grid, 0, 0, 2000, 1620, 40, 30, 2000, 1620);
307
+ native.ctxSetBlendMode(win, 'source-over');
308
+ ```
309
+
254
310
  ## App lifecycle: open-URL, open-file, reopen, quit
255
311
 
256
312
  The OS talks to the application as a whole through Apple Events: a URL for a scheme
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
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",
@@ -43,7 +43,7 @@
43
43
  "install": "node scripts/install.js",
44
44
  "build": "node-gyp rebuild",
45
45
  "demo": "node demo/demo.js",
46
- "test": "node test/app-lifecycle.js && node test/animation.js && node test/accessibility-display.js && node test/ignores-mouse-events.js"
46
+ "test": "node test/app-lifecycle.js && node test/animation.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js"
47
47
  },
48
48
  "dependencies": {
49
49
  "node-addon-api": "^8.3.0"
Binary file
package/src/backend.mm CHANGED
@@ -1262,6 +1262,98 @@ static Napi::Value CopySurfaceRegion(const Napi::CallbackInfo& info) {
1262
1262
  return env.Undefined();
1263
1263
  }
1264
1264
 
1265
+ // blitSurface(src, sx, sy, w, h, dst, dx, dy, clip?) -> [x, y, w, h] | null
1266
+ // A row memcpy of a rect of one surface into another, at surfaces of any
1267
+ // two sizes. copySurfaceRegion is the same-size special case a swapchain
1268
+ // wants; this is the general one, for a caller compositing an offscreen
1269
+ // surface into a window at a translate — a terminal's grid, an element's
1270
+ // retained scene — where drawSurface builds a CGImage of the whole source
1271
+ // and blends it. On an M1 Pro, a 2000x1620 grid into a window-sized
1272
+ // surface: 1.2ms that way, 0.42ms this way.
1273
+ //
1274
+ // Straight copy, no blending, alpha included: this is the `copy` op, and a
1275
+ // caller reaches it by setting that blend mode and drawing at 1:1 under a
1276
+ // translate-only transform. Coordinates are device pixels with a top-left
1277
+ // origin — the convention createSurface's CTM gives user space, and the one
1278
+ // copySurfaceRegion's rects already use. Neither the destination's CTM nor
1279
+ // its clip is visible to a memcpy, so `clip`, when given, is [x, y, w, h] in
1280
+ // the DESTINATION's pixels and the caller passes the clip it is drawing
1281
+ // under; a damage region of several rects is several calls. The rect copied
1282
+ // is the destination rect intersected with that clip and with both surfaces'
1283
+ // bounds, the source origin moving with it — returned, or null when the
1284
+ // intersection is empty and nothing moved.
1285
+ //
1286
+ // When either surface is IOSurface-backed the caller owes the usual
1287
+ // surfaceLock bracketing. Two handles onto one bitmap are refused:
1288
+ // overlapping memcpy rows have no defined result, and the check is on the
1289
+ // backing store, not the handle, so the two ends of a shared IOSurface do
1290
+ // not slip through as different handles.
1291
+ static Napi::Value BlitSurface(const Napi::CallbackInfo& info) {
1292
+ Napi::Env env = info.Env();
1293
+ CALSurface* src = SurfaceFrom(info[0]);
1294
+ if (!src) return env.Null();
1295
+ CALSurface* dst = SurfaceFrom(info[5]);
1296
+ if (!dst) return env.Null();
1297
+
1298
+ const uint8_t* sbase = (const uint8_t*)CGBitmapContextGetData(src->ctx);
1299
+ uint8_t* dbase = (uint8_t*)CGBitmapContextGetData(dst->ctx);
1300
+ if (!sbase || !dbase) return env.Null();
1301
+ if (sbase == dbase) {
1302
+ Napi::Error::New(env, "blitSurface: source and destination are one bitmap")
1303
+ .ThrowAsJavaScriptException();
1304
+ return env.Null();
1305
+ }
1306
+
1307
+ long sx = (long)info[1].As<Napi::Number>().Int64Value();
1308
+ long sy = (long)info[2].As<Napi::Number>().Int64Value();
1309
+ long w = (long)info[3].As<Napi::Number>().Int64Value();
1310
+ long h = (long)info[4].As<Napi::Number>().Int64Value();
1311
+ long dx = (long)info[6].As<Napi::Number>().Int64Value();
1312
+ long dy = (long)info[7].As<Napi::Number>().Int64Value();
1313
+
1314
+ // the destination rect trimmed to the clip, the source origin moving by
1315
+ // whatever each edge takes off
1316
+ if (info.Length() > 8 && !info[8].IsNull() && !info[8].IsUndefined()) {
1317
+ if (!info[8].IsArray() || info[8].As<Napi::Array>().Length() < 4) {
1318
+ Napi::TypeError::New(env, "blitSurface: clip must be [x, y, w, h]")
1319
+ .ThrowAsJavaScriptException();
1320
+ return env.Null();
1321
+ }
1322
+ Napi::Array clip = info[8].As<Napi::Array>();
1323
+ long cx = (long)clip.Get((uint32_t)0).As<Napi::Number>().Int64Value();
1324
+ long cy = (long)clip.Get((uint32_t)1).As<Napi::Number>().Int64Value();
1325
+ long cw = (long)clip.Get((uint32_t)2).As<Napi::Number>().Int64Value();
1326
+ long ch = (long)clip.Get((uint32_t)3).As<Napi::Number>().Int64Value();
1327
+ if (dx < cx) { long over = cx - dx; sx += over; dx += over; w -= over; }
1328
+ if (dy < cy) { long over = cy - dy; sy += over; dy += over; h -= over; }
1329
+ if (dx + w > cx + cw) w = cx + cw - dx;
1330
+ if (dy + h > cy + ch) h = cy + ch - dy;
1331
+ }
1332
+ // and to both surfaces
1333
+ if (sx < 0) { dx -= sx; w += sx; sx = 0; }
1334
+ if (sy < 0) { dy -= sy; h += sy; sy = 0; }
1335
+ if (dx < 0) { sx -= dx; w += dx; dx = 0; }
1336
+ if (dy < 0) { sy -= dy; h += dy; dy = 0; }
1337
+ if (sx + w > (long)src->width) w = (long)src->width - sx;
1338
+ if (sy + h > (long)src->height) h = (long)src->height - sy;
1339
+ if (dx + w > (long)dst->width) w = (long)dst->width - dx;
1340
+ if (dy + h > (long)dst->height) h = (long)dst->height - dy;
1341
+ if (w <= 0 || h <= 0) return env.Null();
1342
+
1343
+ size_t srow = CGBitmapContextGetBytesPerRow(src->ctx);
1344
+ size_t drow = CGBitmapContextGetBytesPerRow(dst->ctx);
1345
+ for (long r = 0; r < h; r++) {
1346
+ memcpy(dbase + (size_t)(dy + r) * drow + (size_t)dx * 4,
1347
+ sbase + (size_t)(sy + r) * srow + (size_t)sx * 4, (size_t)w * 4);
1348
+ }
1349
+ Napi::Array out = Napi::Array::New(env, 4);
1350
+ out.Set((uint32_t)0, (double)dx);
1351
+ out.Set((uint32_t)1, (double)dy);
1352
+ out.Set((uint32_t)2, (double)w);
1353
+ out.Set((uint32_t)3, (double)h);
1354
+ return out;
1355
+ }
1356
+
1265
1357
  static Napi::Value SurfaceSize(const Napi::CallbackInfo& info) {
1266
1358
  CALSurface* s = SurfaceFrom(info[0]);
1267
1359
  if (!s) return info.Env().Undefined();
@@ -2724,6 +2816,56 @@ static Napi::Value CtxSetLineCap(const Napi::CallbackInfo& info) {
2724
2816
  : kCGLineCapButt);
2725
2817
  return info.Env().Undefined();
2726
2818
  }
2819
+ // ctxSetBlendMode(surface, mode) -> boolean — canvas's
2820
+ // globalCompositeOperation, in CoreGraphics' spelling. Every one of the
2821
+ // canvas names has an exact CGBlendMode: the Porter-Duff dozen a 2d context
2822
+ // composites with, and the separable and non-separable blend modes below
2823
+ // them. `copy` is the one the compositing paths care about — it is what
2824
+ // makes a paint a replacement rather than a blend, and what lets a
2825
+ // translate-only 1:1 drawSurface take blitSurface's memcpy instead.
2826
+ //
2827
+ // A name the list does not have leaves the context's blend mode alone and
2828
+ // answers false, which is canvas's rule for an unknown value (it is
2829
+ // ignored, not reset) and lets a caller keep its own property in step. The
2830
+ // mode is gstate, so ctxSave/ctxRestore bracket it like any other.
2831
+ static Napi::Value CtxSetBlendMode(const Napi::CallbackInfo& info) {
2832
+ Napi::Env env = info.Env();
2833
+ std::string mode = info[1].As<Napi::String>().Utf8Value();
2834
+ CGBlendMode blend;
2835
+ if (mode == "source-over") blend = kCGBlendModeNormal;
2836
+ else if (mode == "copy") blend = kCGBlendModeCopy;
2837
+ else if (mode == "source-in") blend = kCGBlendModeSourceIn;
2838
+ else if (mode == "source-out") blend = kCGBlendModeSourceOut;
2839
+ else if (mode == "source-atop") blend = kCGBlendModeSourceAtop;
2840
+ else if (mode == "destination-over") blend = kCGBlendModeDestinationOver;
2841
+ else if (mode == "destination-in") blend = kCGBlendModeDestinationIn;
2842
+ else if (mode == "destination-out") blend = kCGBlendModeDestinationOut;
2843
+ else if (mode == "destination-atop") blend = kCGBlendModeDestinationAtop;
2844
+ else if (mode == "xor") blend = kCGBlendModeXOR;
2845
+ else if (mode == "lighter") blend = kCGBlendModePlusLighter;
2846
+ else if (mode == "multiply") blend = kCGBlendModeMultiply;
2847
+ else if (mode == "screen") blend = kCGBlendModeScreen;
2848
+ else if (mode == "overlay") blend = kCGBlendModeOverlay;
2849
+ else if (mode == "darken") blend = kCGBlendModeDarken;
2850
+ else if (mode == "lighten") blend = kCGBlendModeLighten;
2851
+ else if (mode == "color-dodge") blend = kCGBlendModeColorDodge;
2852
+ else if (mode == "color-burn") blend = kCGBlendModeColorBurn;
2853
+ else if (mode == "hard-light") blend = kCGBlendModeHardLight;
2854
+ else if (mode == "soft-light") blend = kCGBlendModeSoftLight;
2855
+ else if (mode == "difference") blend = kCGBlendModeDifference;
2856
+ else if (mode == "exclusion") blend = kCGBlendModeExclusion;
2857
+ else if (mode == "hue") blend = kCGBlendModeHue;
2858
+ else if (mode == "saturation") blend = kCGBlendModeSaturation;
2859
+ else if (mode == "color") blend = kCGBlendModeColor;
2860
+ else if (mode == "luminosity") blend = kCGBlendModeLuminosity;
2861
+ // not canvas's, but CoreGraphics' own and the CSS spelling of two of them
2862
+ else if (mode == "clear") blend = kCGBlendModeClear;
2863
+ else if (mode == "plus-lighter") blend = kCGBlendModePlusLighter;
2864
+ else if (mode == "plus-darker") blend = kCGBlendModePlusDarker;
2865
+ else return Napi::Boolean::New(env, false);
2866
+ CGContextSetBlendMode(CtxOf(info[0]), blend);
2867
+ return Napi::Boolean::New(env, true);
2868
+ }
2727
2869
  static Napi::Value CtxSetLineJoin(const Napi::CallbackInfo& info) {
2728
2870
  std::string join = info[1].As<Napi::String>().Utf8Value();
2729
2871
  CGContextSetLineJoin(CtxOf(info[0]),
@@ -5032,6 +5174,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
5032
5174
  BFN("surfaceLock", SurfaceLock);
5033
5175
  BFN("surfaceUnlock", SurfaceUnlock);
5034
5176
  BFN("copySurfaceRegion", CopySurfaceRegion);
5177
+ BFN("blitSurface", BlitSurface);
5035
5178
  BFN("surfaceSize", SurfaceSize);
5036
5179
  BFN("ctxSave", CtxSave);
5037
5180
  BFN("ctxRestore", CtxRestore);
@@ -5055,6 +5198,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
5055
5198
  BFN("ctxSetGlobalAlpha", CtxSetGlobalAlpha);
5056
5199
  BFN("ctxSetLineCap", CtxSetLineCap);
5057
5200
  BFN("ctxSetLineJoin", CtxSetLineJoin);
5201
+ BFN("ctxSetBlendMode", CtxSetBlendMode);
5058
5202
  BFN("ctxSetLineDash", CtxSetLineDash);
5059
5203
  BFN("ctxFill", CtxFill);
5060
5204
  BFN("ctxStroke", CtxStroke);