MCPcopy Index your code
hub / github.com/CapSoftware/Cap / renderRgbaFrameCanvas2D

Function renderRgbaFrameCanvas2D

apps/desktop/src/utils/socket.ts:688–764  ·  view source on GitHub ↗
(buffer: ArrayBuffer, receivedAt: number)

Source from the content-addressed store, hash-verified

686 }
687
688 function renderRgbaFrameCanvas2D(buffer: ArrayBuffer, receivedAt: number) {
689 if (!directCanvas || !directCtx) return;
690 if (buffer.byteLength < 24) return;
691
692 const metadataOffset = buffer.byteLength - 24;
693 const meta = new DataView(buffer, metadataOffset, 24);
694 const strideBytes = meta.getUint32(0, true);
695 const height = meta.getUint32(4, true);
696 const width = meta.getUint32(8, true);
697
698 if (width <= 0 || height <= 0) return;
699
700 const expectedRowBytes = width * 4;
701 const needsStrideCorrection = strideBytes !== expectedRowBytes;
702 const availableLength = strideBytes * height;
703
704 if (
705 strideBytes === 0 ||
706 strideBytes < expectedRowBytes ||
707 buffer.byteLength - 24 < availableLength
708 ) {
709 return;
710 }
711
712 if (!needsStrideCorrection) {
713 const frameData = new Uint8ClampedArray(
714 buffer,
715 0,
716 expectedRowBytes * height,
717 );
718 const renderStart = performance.now();
719
720 if (directCanvas.width !== width || directCanvas.height !== height) {
721 directCanvas.width = width;
722 directCanvas.height = height;
723 }
724
725 if (
726 !cachedDirectImageData ||
727 cachedDirectWidth !== width ||
728 cachedDirectHeight !== height
729 ) {
730 cachedDirectImageData = new ImageData(width, height);
731 cachedDirectWidth = width;
732 cachedDirectHeight = height;
733 }
734 cachedDirectImageData.data.set(frameData);
735 directCtx.putImageData(cachedDirectImageData, 0, 0);
736
737 storeRenderedFrame(
738 cachedDirectImageData.data,
739 width,
740 height,
741 width * 4,
742 false,
743 );
744 recordRender(
745 performance.now() - renderStart,

Callers 1

Calls 3

storeRenderedFrameFunction · 0.85
recordRenderFunction · 0.85
ensureStrideWorkerFunction · 0.85

Tested by

no test coverage detected