@geastack/apple 0.2.18 → 0.2.20

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.
@@ -4,24 +4,145 @@
4
4
  #include "pixel.h"
5
5
  #include "ui/tree_internal.h"
6
6
 
7
+ #include <dispatch/dispatch.h>
8
+ #import <IOSurface/IOSurfaceRef.h>
9
+ #import <Metal/Metal.h>
10
+ #import <QuartzCore/CAMetalLayer.h>
11
+ #include <TargetConditionals.h>
12
+ #include <unistd.h>
13
+
14
+ #include <algorithm>
15
+ #include <mutex>
16
+ #include <vector>
17
+
7
18
  namespace {
8
19
 
9
- std::uint8_t *g_scratch = nullptr;
10
- std::size_t g_scratchBytes = 0;
20
+ // A frame's pixels are handed to Core Animation inside a CGImage, which may
21
+ // still be reading one after the next frame starts: each frame takes a buffer
22
+ // from this pool and the image's release returns it. A fresh allocation per
23
+ // frame instead page-faults the whole buffer in again every time.
24
+ struct FrameBuffer {
25
+ std::uint8_t *bytes;
26
+ std::size_t size;
27
+ };
28
+ std::mutex gFramePoolLock;
29
+ std::vector<FrameBuffer> gFramePool;
30
+
31
+ void releaseFrame(void *, const void *data, std::size_t size)
32
+ {
33
+ std::lock_guard<std::mutex> lock(gFramePoolLock);
34
+ gFramePool.push_back({static_cast<std::uint8_t *>(const_cast<void *>(data)), size});
35
+ }
11
36
 
12
- std::uint8_t *ensureScratch(std::size_t bytes)
37
+ std::uint8_t *takeFrame(std::size_t size)
13
38
  {
14
- if (g_scratchBytes < bytes) {
15
- void *grown = std::realloc(g_scratch, bytes);
16
- if (!grown) return nullptr;
17
- g_scratch = static_cast<std::uint8_t *>(grown);
18
- g_scratchBytes = bytes;
39
+ {
40
+ std::lock_guard<std::mutex> lock(gFramePoolLock);
41
+ while (!gFramePool.empty()) {
42
+ const FrameBuffer buffer = gFramePool.back();
43
+ gFramePool.pop_back();
44
+ if (buffer.size == size) return buffer.bytes;
45
+ std::free(buffer.bytes); // the canvas was resized
46
+ }
19
47
  }
20
- return g_scratch;
48
+ return static_cast<std::uint8_t *>(std::malloc(size));
49
+ }
50
+
51
+ #if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGBA8888
52
+ // Draws the canvas texture over the whole drawable, opaque.
53
+ NSString *const kPresentShader = @R"(
54
+ #include <metal_stdlib>
55
+ using namespace metal;
56
+ struct Corner { float4 position [[position]]; float2 uv; };
57
+ vertex Corner canvasVertex(uint id [[vertex_id]]) {
58
+ const float2 uv = float2((id << 1) & 2, id & 2);
59
+ Corner corner;
60
+ corner.position = float4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 0.0, 1.0);
61
+ corner.uv = uv;
62
+ return corner;
63
+ }
64
+ fragment half4 canvasFragment(Corner in [[stage_in]], texture2d<half> canvas [[texture(0)]]) {
65
+ constexpr sampler nearest(filter::nearest);
66
+ return half4(canvas.sample(nearest, in.uv).rgb, 1.0h);
67
+ }
68
+ )";
69
+
70
+ struct GpuPresenter {
71
+ id<MTLDevice> device;
72
+ id<MTLCommandQueue> queue;
73
+ id<MTLRenderPipelineState> pipeline;
74
+ };
75
+
76
+ // The shared Metal state, or nullptr where there is no usable GPU.
77
+ GpuPresenter *gpuPresenter()
78
+ {
79
+ static GpuPresenter *presenter = []() -> GpuPresenter * {
80
+ id<MTLDevice> device = MTLCreateSystemDefaultDevice();
81
+ if (!device) return nullptr;
82
+ NSError *error = nil;
83
+ id<MTLLibrary> library = [device newLibraryWithSource:kPresentShader options:nil error:&error];
84
+ if (!library) return nullptr;
85
+ MTLRenderPipelineDescriptor *descriptor = [MTLRenderPipelineDescriptor new];
86
+ descriptor.vertexFunction = [library newFunctionWithName:@"canvasVertex"];
87
+ descriptor.fragmentFunction = [library newFunctionWithName:@"canvasFragment"];
88
+ descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
89
+ id<MTLRenderPipelineState> pipeline = [device newRenderPipelineStateWithDescriptor:descriptor error:&error];
90
+ id<MTLCommandQueue> queue = [device newCommandQueue];
91
+ if (!pipeline || !queue) return nullptr;
92
+ return new GpuPresenter{device, queue, pipeline};
93
+ }();
94
+ return presenter;
95
+ }
96
+
97
+ IOSurfaceRef createSurface(int w, int h)
98
+ {
99
+ NSDictionary *properties = @{
100
+ (id)kIOSurfaceWidth: @(w),
101
+ (id)kIOSurfaceHeight: @(h),
102
+ (id)kIOSurfaceBytesPerElement: @4,
103
+ (id)kIOSurfacePixelFormat: @(static_cast<std::uint32_t>('BGRA')),
104
+ };
105
+ return IOSurfaceCreate((__bridge CFDictionaryRef)properties);
21
106
  }
22
107
 
108
+ // RGBA8888 rows into an opaque BGRA surface, sliced across every core.
109
+ void copyToSurface(const std::uint32_t *src, int w, int h, IOSurfaceRef surface)
110
+ {
111
+ auto *base = static_cast<std::uint8_t *>(IOSurfaceGetBaseAddress(surface));
112
+ const std::size_t stride = IOSurfaceGetBytesPerRow(surface);
113
+ constexpr int kRowsPerSlice = 64;
114
+ const int slices = (h + kRowsPerSlice - 1) / kRowsPerSlice;
115
+ dispatch_apply(static_cast<size_t>(slices), dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^(size_t slice) {
116
+ const int y0 = static_cast<int>(slice) * kRowsPerSlice;
117
+ const int y1 = std::min(h, y0 + kRowsPerSlice);
118
+ for (int y = y0; y < y1; ++y) {
119
+ const std::uint32_t *in = src + static_cast<std::size_t>(y) * w;
120
+ auto *out = reinterpret_cast<std::uint32_t *>(base + static_cast<std::size_t>(y) * stride);
121
+ for (int x = 0; x < w; ++x) {
122
+ const std::uint32_t p = in[x]; // bytes R,G,B,A -> B,G,R,A (little-endian words)
123
+ out[x] = (p & 0x0000FF00u) | ((p & 0xFFu) << 16) | ((p >> 16) & 0xFFu) | 0xFF000000u;
124
+ }
125
+ }
126
+ });
127
+ }
128
+ #endif
129
+
23
130
  } // namespace
24
131
 
132
+ @interface GeaCanvasView ()
133
+ #if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGBA8888
134
+ // Surfaces handed to Core Animation, reused once the render server is done
135
+ // with them. The render server reads an IOSurface in place, in the GPU's own
136
+ // BGRA format; a CGImage is converted and uploaded anew every frame.
137
+ @property(nonatomic, strong) NSMutableArray *surfaces;
138
+ // The GPU path: the canvas buffer wrapped as a texture (no copy) and drawn
139
+ // into a Metal layer's drawable.
140
+ @property(nonatomic, strong) CAMetalLayer *metalLayer;
141
+ @property(nonatomic, strong) id<MTLTexture> canvasTexture;
142
+ @property(nonatomic, assign) const void *texturePixels;
143
+ #endif
144
+ @end
145
+
25
146
  @implementation GeaCanvasView
26
147
 
27
148
  - (instancetype)initWithFrame:(CGRect)frame
@@ -36,9 +157,12 @@ std::uint8_t *ensureScratch(std::size_t bytes)
36
157
  return self;
37
158
  }
38
159
 
39
- - (void)drawRect:(CGRect)dirtyRect
160
+ // Sets the canvas pixels as the layer's contents, so the GPU scales and
161
+ // composites them. Drawing them in drawRect instead pushes every pixel through
162
+ // CoreGraphics on the CPU, which for a 4K canvas takes longer than the app's
163
+ // whole frame.
164
+ - (void)present
40
165
  {
41
- (void)dirtyRect;
42
166
  if (self.nodeId < 0) return;
43
167
  auto &tree = gea::embedded::ui::Tree::instance();
44
168
  if (self.nodeId >= tree.nodeCount()) return;
@@ -49,13 +173,57 @@ std::uint8_t *ensureScratch(std::size_t bytes)
49
173
  const int h = canvas->height();
50
174
  if (!src || w <= 0 || h <= 0) return;
51
175
 
176
+ #if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGBA8888
177
+ if ([self presentOnGpu:src width:w height:h]) return;
178
+ {
179
+ if (!self.surfaces) self.surfaces = [NSMutableArray array];
180
+ IOSurfaceRef surface = nullptr;
181
+ const id shown = self.layer.contents;
182
+ for (id candidate in self.surfaces) {
183
+ IOSurfaceRef s = (__bridge IOSurfaceRef)candidate;
184
+ if (static_cast<int>(IOSurfaceGetWidth(s)) != w || static_cast<int>(IOSurfaceGetHeight(s)) != h) continue;
185
+ if (candidate == shown || IOSurfaceIsInUse(s)) continue;
186
+ surface = s;
187
+ break;
188
+ }
189
+ if (!surface && self.surfaces.count < 4) {
190
+ IOSurfaceRef created = createSurface(w, h);
191
+ if (created) {
192
+ [self.surfaces addObject:(__bridge_transfer id)created];
193
+ surface = created;
194
+ }
195
+ }
196
+ if (surface && IOSurfaceLock(surface, 0, nullptr) == 0) {
197
+ copyToSurface(reinterpret_cast<const std::uint32_t *>(src), w, h, surface);
198
+ IOSurfaceUnlock(surface, 0, nullptr);
199
+ // No implicit animation: a contents change would otherwise cross-fade.
200
+ [CATransaction begin];
201
+ [CATransaction setDisableActions:YES];
202
+ self.layer.contentsGravity = kCAGravityResize;
203
+ self.layer.contents = (__bridge id)surface;
204
+ [CATransaction commit];
205
+ return;
206
+ }
207
+ }
208
+ #endif
52
209
  const std::size_t bytes = static_cast<std::size_t>(w) * h * 4;
53
- std::uint8_t *dst = ensureScratch(bytes);
210
+ auto *dst = takeFrame(bytes);
54
211
  if (!dst) return;
55
212
  #if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGBA8888
56
213
  // native_t is already RGBA8888 (R,G,B,A bytes per pixel) — exactly the
57
- // CGImage's RGBX layout — so present it straight through.
58
- std::memcpy(dst, src, bytes);
214
+ // CGImage's RGBX layout — so present it straight through. One core cannot
215
+ // saturate memory bandwidth: a large canvas copies in slices on every core.
216
+ constexpr std::size_t kSlice = 4u << 20;
217
+ if (bytes >= 2 * kSlice) {
218
+ const std::size_t slices = (bytes + kSlice - 1) / kSlice;
219
+ const auto *from = reinterpret_cast<const std::uint8_t *>(src);
220
+ dispatch_apply(slices, dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^(size_t i) {
221
+ const std::size_t offset = i * kSlice;
222
+ std::memcpy(dst + offset, from + offset, std::min(kSlice, bytes - offset));
223
+ });
224
+ } else {
225
+ std::memcpy(dst, src, bytes);
226
+ }
59
227
  #else
60
228
  for (int i = 0; i < w * h; i++) {
61
229
  int r, g, b;
@@ -68,7 +236,7 @@ std::uint8_t *ensureScratch(std::size_t bytes)
68
236
  #endif
69
237
 
70
238
  CGColorSpaceRef cs = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
71
- CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, dst, bytes, nullptr);
239
+ CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, dst, bytes, releaseFrame);
72
240
  const uint32_t bitmapInfo = static_cast<uint32_t>(kCGImageAlphaNoneSkipLast) |
73
241
  static_cast<uint32_t>(kCGBitmapByteOrder32Big);
74
242
  CGImageRef image = CGImageCreate(static_cast<std::size_t>(w),
@@ -83,12 +251,95 @@ std::uint8_t *ensureScratch(std::size_t bytes)
83
251
  false,
84
252
  kCGRenderingIntentDefault);
85
253
  if (image) {
86
- UIImage *uiImage = [UIImage imageWithCGImage:image scale:1.0 orientation:UIImageOrientationUp];
87
- [uiImage drawInRect:self.bounds];
254
+ // No implicit animation: a contents change would otherwise cross-fade.
255
+ [CATransaction begin];
256
+ [CATransaction setDisableActions:YES];
257
+ self.layer.contentsGravity = kCAGravityResize;
258
+ self.layer.contents = (__bridge id)image;
259
+ [CATransaction commit];
88
260
  CGImageRelease(image);
89
261
  }
90
262
  CGDataProviderRelease(provider);
91
263
  CGColorSpaceRelease(cs);
92
264
  }
93
265
 
266
+ #if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGBA8888
267
+ // The GPU reads the canvas buffer where it lies (the canvas store allocates it
268
+ // in whole pages on Apple hosts) and writes the drawable: the CPU copies
269
+ // nothing. Returns NO where the buffer cannot be wrapped, for the copy paths.
270
+ - (BOOL)presentOnGpu:(const void *)src width:(int)w height:(int)h
271
+ {
272
+ #if TARGET_OS_SIMULATOR
273
+ // The simulator's Metal driver cannot wrap app memory (it traps): copy.
274
+ (void)src;
275
+ (void)w;
276
+ (void)h;
277
+ return NO;
278
+ #else
279
+ GpuPresenter *gpu = gpuPresenter();
280
+ if (!gpu) return NO;
281
+ const std::size_t page = static_cast<std::size_t>(getpagesize());
282
+ const std::size_t rowBytes = static_cast<std::size_t>(w) * 4;
283
+ if (reinterpret_cast<std::uintptr_t>(src) % page != 0) return NO;
284
+ if (rowBytes % [gpu->device minimumLinearTextureAlignmentForPixelFormat:MTLPixelFormatRGBA8Unorm] != 0) return NO;
285
+
286
+ if (src != self.texturePixels || !self.canvasTexture || static_cast<int>(self.canvasTexture.width) != w ||
287
+ static_cast<int>(self.canvasTexture.height) != h) {
288
+ self.canvasTexture = nil;
289
+ self.texturePixels = nullptr;
290
+ const std::size_t bytes = (rowBytes * h + page - 1) & ~(page - 1);
291
+ id<MTLBuffer> buffer = [gpu->device newBufferWithBytesNoCopy:const_cast<void *>(src)
292
+ length:bytes
293
+ options:MTLResourceStorageModeShared
294
+ deallocator:nil];
295
+ if (!buffer) return NO;
296
+ MTLTextureDescriptor *descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
297
+ width:w
298
+ height:h
299
+ mipmapped:NO];
300
+ descriptor.storageMode = MTLStorageModeShared;
301
+ descriptor.usage = MTLTextureUsageShaderRead;
302
+ self.canvasTexture = [buffer newTextureWithDescriptor:descriptor offset:0 bytesPerRow:rowBytes];
303
+ if (!self.canvasTexture) return NO;
304
+ self.texturePixels = src;
305
+ }
306
+
307
+ [CATransaction begin];
308
+ [CATransaction setDisableActions:YES];
309
+ if (!self.metalLayer) {
310
+ CAMetalLayer *layer = [CAMetalLayer layer];
311
+ layer.device = gpu->device;
312
+ layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
313
+ layer.framebufferOnly = YES;
314
+ layer.opaque = YES;
315
+ [self.layer addSublayer:layer];
316
+ self.metalLayer = layer;
317
+ self.layer.contents = nil;
318
+ }
319
+ if (!CGRectEqualToRect(self.metalLayer.frame, self.bounds)) self.metalLayer.frame = self.bounds;
320
+ const CGSize size = CGSizeMake(w, h);
321
+ if (!CGSizeEqualToSize(self.metalLayer.drawableSize, size)) self.metalLayer.drawableSize = size;
322
+ [CATransaction commit];
323
+
324
+ id<CAMetalDrawable> drawable = [self.metalLayer nextDrawable];
325
+ if (!drawable) return YES; // none free this frame: skip it
326
+ MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor];
327
+ pass.colorAttachments[0].texture = drawable.texture;
328
+ pass.colorAttachments[0].loadAction = MTLLoadActionDontCare;
329
+ pass.colorAttachments[0].storeAction = MTLStoreActionStore;
330
+ id<MTLCommandBuffer> commands = [gpu->queue commandBuffer];
331
+ id<MTLRenderCommandEncoder> encoder = [commands renderCommandEncoderWithDescriptor:pass];
332
+ [encoder setRenderPipelineState:gpu->pipeline];
333
+ [encoder setFragmentTexture:self.canvasTexture atIndex:0];
334
+ [encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
335
+ [encoder endEncoding];
336
+ [commands presentDrawable:drawable];
337
+ [commands commit];
338
+ // The app draws into this same buffer next frame: wait out the GPU's read.
339
+ [commands waitUntilCompleted];
340
+ return YES;
341
+ #endif
342
+ }
343
+ #endif
344
+
94
345
  @end
@@ -12,6 +12,10 @@ namespace gea::ios {
12
12
  // resolved back to the CSS family name, then matched against fonts registered
13
13
  // from the app bundle. Unknown ids fall back to the system font.
14
14
  UIFont *fontForId(int fontId, CGFloat sizePt);
15
+ // Whether UIKit resolves `family` to a face of that family (not a fallback).
16
+ bool fontResolvesFamily(NSString *family, CGFloat size);
15
17
  #endif
18
+ // Registers the app bundle's .ttf/.otf files with CoreText (once).
19
+ void ensureFontsRegistered();
16
20
 
17
21
  } // namespace gea::ios
@@ -1,3 +1,5 @@
1
+ #include <TargetConditionals.h>
2
+
1
3
  #import <CoreText/CoreText.h>
2
4
  #import <Foundation/Foundation.h>
3
5
  #import <UIKit/UIKit.h>
@@ -77,9 +79,25 @@ UIFont *fontForFamily(NSString *family, CGFloat size)
77
79
 
78
80
  } // namespace
79
81
 
82
+ void ensureFontsRegistered() { ensureBundledFontsRegistered(); }
83
+
84
+ // Whether UIKit has a face of `family` itself, not the system fallback CoreText
85
+ // substitutes for a family it does not know.
86
+ bool fontResolvesFamily(NSString *family, CGFloat size)
87
+ {
88
+ UIFont *font = fontForFamily(family, size > 0 ? size : 16);
89
+ if (!font) return false;
90
+ return [font.familyName caseInsensitiveCompare:family] == NSOrderedSame ||
91
+ [font.fontName caseInsensitiveCompare:family] == NSOrderedSame;
92
+ }
93
+
80
94
  UIFont *fontForId(int fontId, CGFloat sizePt)
81
95
  {
96
+ #if TARGET_OS_TV
97
+ const CGFloat size = sizePt > 0 ? sizePt : [UIFont preferredFontForTextStyle:UIFontTextStyleBody].pointSize;
98
+ #else
82
99
  const CGFloat size = sizePt > 0 ? sizePt : [UIFont systemFontSize];
100
+ #endif
83
101
  ensureBundledFontsRegistered();
84
102
 
85
103
  const char *familyName = gea::framework::graphics::FontRegistry::familyName(fontId);
@@ -105,6 +123,20 @@ extern "C" bool gea_host_measure_text(const char *text,
105
123
  }
106
124
 
107
125
  @autoreleasepool {
126
+ const char *familyName = gea::framework::graphics::FontRegistry::familyName(fontId);
127
+ NSString *family = familyName && familyName[0] ? [NSString stringWithUTF8String:familyName] : nil;
128
+ #ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
129
+ // A family UIKit cannot resolve (a CSS alias such as "Outfit M", baked
130
+ // into the binary with no font file in the bundle) would be measured in
131
+ // the system fallback face, while the engine draws it with its baked
132
+ // glyphs: the box comes out narrower than the text and clips it. Leave
133
+ // those to the engine, which measures the face it draws.
134
+ gea::ios::ensureFontsRegistered();
135
+ if (family && !gea::ios::fontResolvesFamily(family, fontSize) &&
136
+ gea::framework::graphics::FontRegistry::rasterizedFamily(fontId, fontSize).valid()) {
137
+ return false;
138
+ }
139
+ #endif
108
140
  UIFont *font = gea::ios::fontForId(fontId, fontSize);
109
141
  if (!font) return false;
110
142
 
@@ -0,0 +1,55 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Host extensions: native code a package (or an app) compiles into an iOS or
3
+ // tvOS build to own part of the window -- typically a rendering surface such as
4
+ // a CAMetalLayer that a GL or GPU layer presents into, which the generated
5
+ // program draws through. The UIKit counterpart of gea_win32_host.h.
6
+ //
7
+ // The target calls every registered extension at fixed points of its life
8
+ // cycle; an extension registers itself from a static initializer:
9
+ //
10
+ // static const gea_uikit_host_extension kSurface = {
11
+ // GEA_UIKIT_HOST_EXTENSION_VERSION, "my-surface", attach, layout, begin, end, detach };
12
+ // static const bool kRegistered = (gea_uikit_register_host_extension(&kSurface), true);
13
+ //
14
+ // Every callback may be null. Sizes are in points (layout pixels); `scale` is
15
+ // device pixels per point.
16
+ #pragma once
17
+
18
+ #ifdef __cplusplus
19
+ extern "C" {
20
+ #endif
21
+
22
+ #define GEA_UIKIT_HOST_EXTENSION_VERSION 1u
23
+
24
+ typedef struct gea_uikit_host_extension {
25
+ unsigned version; // GEA_UIKIT_HOST_EXTENSION_VERSION
26
+ const char *name; // for diagnostics
27
+ // Once, after the window's root view exists and before the app's top level
28
+ // runs. `rootView` is the root UIView (a bridged UIView *): an extension adds
29
+ // its own subviews to it. Returning zero logs an error naming the extension.
30
+ int (*attach)(void *rootView, double width, double height, double scale);
31
+ // The root view's size or scale changed.
32
+ void (*layout)(double width, double height, double scale);
33
+ // Around the app's initialization and around every frame. A zero return
34
+ // from begin_frame skips that frame; end_frame runs whenever begin_frame ran.
35
+ int (*begin_frame)(void);
36
+ void (*end_frame)(void);
37
+ // Never called today (an iOS app is not shut down); for symmetry with Windows.
38
+ void (*detach)(void);
39
+ } gea_uikit_host_extension;
40
+
41
+ void gea_uikit_register_host_extension(const gea_uikit_host_extension *extension);
42
+
43
+ // Called by the target (ios_main.mm).
44
+ void gea_uikit_host_attach(void *rootView, double width, double height, double scale);
45
+ void gea_uikit_host_layout(double width, double height, double scale);
46
+ // Returns zero when an extension skipped the frame; gea_uikit_host_end_frame
47
+ // must follow a nonzero return.
48
+ int gea_uikit_host_begin_frame(void);
49
+ void gea_uikit_host_end_frame(void);
50
+ // Whether any extension is registered (the host then leaves the window to it).
51
+ int gea_uikit_host_has_extensions(void);
52
+
53
+ #ifdef __cplusplus
54
+ }
55
+ #endif
@@ -280,6 +280,16 @@ DeviceInfo Camera::deviceAt(int index)
280
280
  return {"back", Facing::Back, 0, 0};
281
281
  }
282
282
 
283
+ void Camera::drawPreview(int x, int y, int width, int height)
284
+ {
285
+ auto &camera = state();
286
+ std::lock_guard<std::mutex> lock(camera.frameMutex);
287
+ if (camera.latestFrame.empty()) return;
288
+ gea::platform::display::Display::blitImageScaled(
289
+ camera.latestFrame.data(), nullptr, camera.latestWidth, camera.latestHeight,
290
+ x, y, width > 0 ? width : camera.latestWidth, height > 0 ? height : camera.latestHeight);
291
+ }
292
+
283
293
  int Camera::previewMode() { return 1; } // NativeOverlay
284
294
 
285
295
  bool Camera::fillPreview(gea::framework::graphics::pixel::native_t *, int, int, int, bool)
@@ -100,10 +100,11 @@ void Display::clear() { canvas()->clear(0); }
100
100
  void Display::clearNoFlush() { canvas()->clear(0); }
101
101
  void Display::print(const char *) {}
102
102
 
103
- // No double-buffered framebuffer on the iOS host shim — the canvas is the
104
- // presented surface — so rebinding is a no-op (the embedded backends use this
105
- // to re-point the canvas at a freshly-allocated framebuffer).
106
- void Display::rebindCanvasToFramebuffer() {}
103
+ void Display::rebindCanvasToFramebuffer()
104
+ {
105
+ if (g_canvas.pixels() != g_framebuffer)
106
+ g_canvas.bindPixels(g_framebuffer, g_canvas_width, g_canvas_height);
107
+ }
107
108
 
108
109
  void Display::flush()
109
110
  {
@@ -132,10 +133,10 @@ void Display::flushRects(const DisplayFlushRect *rects, int count, bool)
132
133
  g_canvas.resetDirty();
133
134
  }
134
135
 
135
- bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raster, void *user)
136
+ static bool rasterRect(int x, int y, int w, int h, DisplayStreamRasterFn raster, void *user)
136
137
  {
137
138
  ensure_canvas();
138
- if (!raster || w <= 0 || h <= 0) return false;
139
+ if (!g_framebuffer || !raster || w <= 0 || h <= 0) return false;
139
140
  int x0 = std::max(0, x);
140
141
  int y0 = std::max(0, y);
141
142
  int x1 = std::min(g_canvas_width - 1, x + w - 1);
@@ -154,15 +155,32 @@ bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raste
154
155
  using native_t = gea::framework::graphics::pixel::native_t;
155
156
  std::vector<native_t> pixels(static_cast<std::size_t>(rw) * static_cast<std::size_t>(rh));
156
157
  raster(pixels.data(), rw, rh, x0, y0, user);
158
+ Display::rebindCanvasToFramebuffer();
157
159
  for (int row = 0; row < rh; row++) {
158
160
  std::copy_n(&pixels[static_cast<std::size_t>(row) * static_cast<std::size_t>(rw)], rw,
159
161
  &g_framebuffer[static_cast<std::size_t>((y0 + row) * g_canvas_width + x0)]);
160
162
  }
161
163
  g_canvas.markDirty(x0, y0, x1, y1);
164
+ return true;
165
+ }
166
+
167
+ bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raster, void *user)
168
+ {
169
+ if (!rasterRect(x, y, w, h, raster, user)) return false;
162
170
  flush();
163
171
  return true;
164
172
  }
165
173
 
174
+ void Display::flushRectsRasterized(const DisplayFlushRect *rects, int count, DisplayStreamRasterFn raster, void *user, bool allowPerChunkDrain)
175
+ {
176
+ if (!rects || count <= 0 || !raster) return;
177
+ for (int i = 0; i < count; i++) {
178
+ const auto &rect = rects[i];
179
+ rasterRect(rect.x0, rect.y0, rect.x1 - rect.x0 + 1, rect.y1 - rect.y0 + 1, raster, user);
180
+ }
181
+ flushRects(rects, count, allowPerChunkDrain);
182
+ }
183
+
166
184
  bool Display::present(const DisplayPresentCommand *commands, int command_count)
167
185
  {
168
186
  ensure_canvas();
@@ -325,6 +343,7 @@ DisplayFlushStageDetail Display::flushStageDetail() { return {}; }
325
343
  // Orientation is a device-panel concept; the iOS app rotates via UIKit, not by
326
344
  // rotating the framebuffer — so the framework rotation hook is a no-op here.
327
345
  void applyOrientation(gea::framework::display::DisplayOrientation) {}
346
+ bool autoRotateAllowed() { return false; }
328
347
 
329
348
  } // namespace gea::platform::display
330
349