UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

185 lines (184 loc) 7.83 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResourceManager = exports.Frame = exports.Painter = exports.COORD = void 0; const CamOverlayDrawingAPI_1 = require("../CamOverlayDrawingAPI"); const ResourceManager_1 = require("./ResourceManager"); exports.ResourceManager = ResourceManager_1.default; const Frame_1 = require("./Frame"); Object.defineProperty(exports, "Frame", { enumerable: true, get: function () { return Frame_1.Frame; } }); exports.COORD = { top_left: [-1, -1], center_left: [-1, 0], bottom_left: [-1, 1], top_center: [0, -1], center: [0, 0], bottom_center: [0, 1], top_right: [1, -1], center_right: [1, 0], bottom_right: [1, 1], }; class Painter extends Frame_1.Frame { constructor(opt, coopt) { super(opt); this.refreshLayers = true; this.layers = []; this.coAlignment = exports.COORD[opt.coAlignment]; this.screenWidth = opt.screenWidth; this.screenHeight = opt.screenHeight; this.cod = new CamOverlayDrawingAPI_1.CamOverlayDrawingAPI(coopt); this.rm = new ResourceManager_1.default(this.cod); } get camOverlayDrawingAPI() { return this.cod; } get resourceManager() { return this.rm; } connect() { this.cod.on('open', () => { this.rm.clear(); this.layers = []; this.refreshLayers = true; this.emit('open'); }); this.cod.on('close', () => { this.emit('close'); }); this.cod.on('error', (err) => { console.error('Painter:', err); }); this.cod.connect(); } disconnect() { this.cod.disconnect(); } isConnected() { return this.cod.isConnected(); } registerImage(moniker, fileName) { this.rm.registerImage(moniker, fileName); } registerFont(moniker, fileName) { this.rm.registerFont(moniker, fileName); } setScreenSize(sw, sh) { this.screenWidth = sw; this.screenHeight = sh; } setCoAlignment(coAlignment) { this.coAlignment = exports.COORD[coAlignment]; } layoutChanged() { this.refreshLayers = true; } display(scale = 1.0) { return __awaiter(this, void 0, void 0, function* () { if (this.enabled) { if (this.refreshLayers) { this.refreshLayers = false; yield this.prepareLayers(); } let cairo; let surface; let lastCachedLayer; for (let i = 0; i < this.layers.length; i++) { const layer = this.layers[i]; if (layer.cairoCache !== undefined && layer.surfaceCache !== undefined && surface === undefined && cairo === undefined) { lastCachedLayer = layer; continue; } if (surface === undefined || cairo === undefined) { [surface, cairo] = yield this.prepareSurface(scale, lastCachedLayer === null || lastCachedLayer === void 0 ? void 0 : lastCachedLayer.surfaceCache, lastCachedLayer === null || lastCachedLayer === void 0 ? void 0 : lastCachedLayer.cairoCache); } yield this.displayImage(this.cod, this.rm, cairo, -this.posX, -this.posY, scale, layer.layer); if (i < this.layers.length - 1) { const [surfaceToCache, cairoToCache] = yield this.prepareSurface(scale, surface, cairo); layer.surfaceCache = surfaceToCache; layer.cairoCache = cairoToCache; } } if (surface !== undefined && cairo !== undefined) { yield this.cod.showCairoImage(surface, this.positionConvertor(this.coAlignment[0], this.screenWidth, this.posX, scale * this.width), this.positionConvertor(this.coAlignment[1], this.screenHeight, this.posY, scale * this.height)); yield this.cleanupSurface(surface, cairo); } } }); } hide() { return __awaiter(this, void 0, void 0, function* () { yield this.cod.removeImage(); }); } invalidateLayer(layer) { return __awaiter(this, void 0, void 0, function* () { const layerIdx = this.layers.findIndex((l) => l.layer === layer); if (layerIdx === -1) { return; } for (let i = layerIdx; i < this.layers.length; i++) { const layer = this.layers[i]; if (layer.surfaceCache !== undefined && layer.cairoCache !== undefined) { yield this.cleanupSurface(layer.surfaceCache, layer.cairoCache); layer.surfaceCache = undefined; layer.cairoCache = undefined; } } }); } prepareLayers() { return __awaiter(this, void 0, void 0, function* () { for (const layer of this.layers) { if (layer.surfaceCache !== undefined && layer.cairoCache !== undefined) { yield this.cleanupSurface(layer.surfaceCache, layer.cairoCache); } } const uniqueLayers = this.getLayers(); const sortedLayers = Array.from(uniqueLayers).sort((a, b) => a - b); this.layers = sortedLayers.map((layer) => { return { layer }; }); }); } prepareSurface(scale, cachedSurface, cachedCairo) { return __awaiter(this, void 0, void 0, function* () { const surface = (yield this.cod.cairo('cairo_image_surface_create', 'CAIRO_FORMAT_ARGB32', Math.floor(this.width * scale), Math.floor(this.height * scale))); const cairo = (yield this.cod.cairo('cairo_create', surface.var)); if (cachedSurface !== undefined && cachedCairo !== undefined) { yield this.cod.cairo('cairo_set_source_surface', cairo.var, cachedSurface, 0, 0); yield this.cod.cairo('cairo_paint', cairo.var); } return [surface.var, cairo.var]; }); } cleanupSurface(surface, cairo) { return __awaiter(this, void 0, void 0, function* () { yield this.cod.cairo('cairo_surface_destroy', surface); yield this.cod.cairo('cairo_destroy', cairo); }); } positionConvertor(alignment, screenSize, position, graphicsSize) { switch (alignment) { case -1: return alignment + (2.0 * position) / screenSize; case 0: return alignment - (2.0 * (position + graphicsSize / 2)) / screenSize; case 1: return alignment - (2.0 * (position + graphicsSize)) / screenSize; default: throw new Error('Invalid graphics alignment.'); } } } exports.Painter = Painter;