(() => {
    "use strict";
    const MAX = Number(PluginManager.parameters("zz_render").maxScale || 4);
    let SS = 2;
    function computeSS() {
        const dpr = window.devicePixelRatio || 1;
        const rs = Graphics._realScale || 1;
        let s = rs * dpr;
        if (!isFinite(s) || s < 1) s = 1;
        if (s > MAX) s = MAX;
        return s;
    }
    const _app = Graphics._createPixiApp;
    Graphics._createPixiApp = function() {
        _app.call(this);
        if (this._app && this._app.renderer) { try { this._app.renderer.resolution = SS; } catch (e) {} }
    };
    Graphics._updateCanvas = function() {
        SS = computeSS();
        this._canvas.width = Math.round(this._width * SS);
        this._canvas.height = Math.round(this._height * SS);
        const c = this._canvas;
        c.style.position = "absolute"; c.style.margin = "auto"; c.style.zIndex = 1;
        c.style.top = 0; c.style.left = 0; c.style.right = 0; c.style.bottom = 0;
        c.style.width = (this._width * this._realScale) + "px";
        c.style.height = (this._height * this._realScale) + "px";
        if (this._app && this._app.renderer) {
            try { this._app.renderer.resolution = SS; this._app.renderer.resize(this._width, this._height); } catch (e) {}
        }
    };
    Object.defineProperty(Bitmap.prototype, "width", {configurable:true,
        get:function(){const i=this._canvas||this._image;return i?Math.round(i.width/(this._resolution||1)):0;}});
    Object.defineProperty(Bitmap.prototype, "height", {configurable:true,
        get:function(){const i=this._canvas||this._image;return i?Math.round(i.height/(this._resolution||1)):0;}});
    function makeHD(bm) {
        if (!bm || !bm._canvas) return bm;
        const res = SS;
        if (bm._resolution === res) return bm;
        const w = bm.width, h = bm.height;
        if (w <= 0 || h <= 0) return bm;
        bm._resolution = res;
        bm._canvas.width = Math.max(1, Math.round(w * res));
        bm._canvas.height = Math.max(1, Math.round(h * res));
        bm._context.setTransform(res, 0, 0, res, 0, 0);
        if (bm._baseTexture) {
            try { bm._baseTexture.setResolution(res); } catch (e) { bm._baseTexture.resolution = res; }
            bm._baseTexture.update();
        }
        return bm;
    }
    const _blt = Bitmap.prototype.blt;
    Bitmap.prototype.blt = function(s, sx, sy, sw, sh, dx, dy, dw, dh) {
        const r = s && s._resolution ? s._resolution : 1;
        _blt.call(this, s, sx*r, sy*r, sw*r, sh*r, dx, dy, dw, dh);
    };
    const _cc = Window_Base.prototype.createContents;
    Window_Base.prototype.createContents = function() {
        _cc.call(this);
        makeHD(this.contents);
        if (this.contentsBack) makeHD(this.contentsBack);
    };
})();
