diff --git a/src/effects/BrokenGlass.js b/src/effects/BrokenGlass.js index 955fd6d..7791745 100644 --- a/src/effects/BrokenGlass.js +++ b/src/effects/BrokenGlass.js @@ -20,8 +20,6 @@ import * as utils from '../utils.js'; // preferences process. They are used only in the creator function of the ShaderFactory // which is only called within GNOME Shell's process. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); -const St = await utils.importInShellOnly('gi://St'); -const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); const _ = await utils.importGettext(); @@ -55,11 +53,7 @@ export default class Effect { this.shaderFactory = new ShaderFactory(Effect.getNick(), (shader) => { // Create the texture in the first call. if (!this._shardTexture) { - const shardData = GdkPixbuf.Pixbuf.new_from_resource('/img/shards.png'); - this._shardTexture = new St.ImageContent(); - this._shardTexture.set_data(shardData.get_pixels(), Cogl.PixelFormat.RGB_888, - shardData.width, shardData.height, - shardData.rowstride); + this._shardTexture = utils.getImageResource('/img/shards.png'); } // Store all uniform locations. diff --git a/src/effects/Matrix.js b/src/effects/Matrix.js index ca800f8..45065ec 100644 --- a/src/effects/Matrix.js +++ b/src/effects/Matrix.js @@ -20,9 +20,6 @@ import * as utils from '../utils.js'; // preferences process. They are used only in the creator function of the ShaderFactory // which is only called within GNOME Shell's process. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); -const St = await utils.importInShellOnly('gi://St'); -const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); -const Cogl = await utils.importInShellOnly('gi://Cogl'); const _ = await utils.importGettext(); @@ -47,12 +44,7 @@ export default class Effect { this.shaderFactory = new ShaderFactory(Effect.getNick(), (shader) => { // Create the texture in the first call. if (!this._fontTexture) { - const fontData = GdkPixbuf.Pixbuf.new_from_resource('/img/matrixFont.png'); - this._fontTexture = new St.ImageContent(); - this._fontTexture.set_data( - fontData.get_pixels(), - fontData.has_alpha ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888, - fontData.width, fontData.height, fontData.rowstride); + this._fontTexture = utils.getImageResource('/img/matrixFont.png'); } // Store uniform locations of newly created shaders. diff --git a/src/effects/PaintBrush.js b/src/effects/PaintBrush.js index 94d7e2d..da9b7b1 100644 --- a/src/effects/PaintBrush.js +++ b/src/effects/PaintBrush.js @@ -20,8 +20,6 @@ import * as utils from '../utils.js'; // preferences process. They are used only in the creator function of the ShaderFactory // which is only called within GNOME Shell's process. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); -const St = await utils.importInShellOnly('gi://St'); -const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); const _ = await utils.importGettext(); @@ -45,11 +43,7 @@ export default class Effect { this.shaderFactory = new ShaderFactory(Effect.getNick(), (shader) => { // Create the texture in the first call. if (!this._brushTexture) { - const brushData = GdkPixbuf.Pixbuf.new_from_resource('/img/brush.png'); - this._brushTexture = new St.ImageContent(); - this._brushTexture.set_data(brushData.get_pixels(), - Cogl.PixelFormat.RGBA_8888_PRE, brushData.width, - brushData.height, brushData.rowstride); + this._brushTexture = utils.getImageResource('/img/brush.png', true); } // Store uniform locations of newly created shaders. diff --git a/src/effects/SnapOfDisintegration.js b/src/effects/SnapOfDisintegration.js index 86126a9..78fcd5e 100644 --- a/src/effects/SnapOfDisintegration.js +++ b/src/effects/SnapOfDisintegration.js @@ -20,8 +20,6 @@ import * as utils from '../utils.js'; // preferences process. They are used only in the creator function of the ShaderFactory // which is only called within GNOME Shell's process. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); -const St = await utils.importInShellOnly('gi://St'); -const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); const _ = await utils.importGettext(); @@ -49,10 +47,7 @@ export default class Effect { this.shaderFactory = new ShaderFactory(Effect.getNick(), (shader) => { // Create the texture in the first call. if (!this._dustTexture) { - const dustData = GdkPixbuf.Pixbuf.new_from_resource('/img/dust.png'); - this._dustTexture = new St.ImageContent(); - this._dustTexture.set_data(dustData.get_pixels(), Cogl.PixelFormat.RGB_888, - dustData.width, dustData.height, dustData.rowstride); + this._dustTexture = utils.getImageResource('/img/dust.png'); } // Store uniform locations of newly created shaders. diff --git a/src/effects/TRexAttack.js b/src/effects/TRexAttack.js index 111335c..04c68f9 100644 --- a/src/effects/TRexAttack.js +++ b/src/effects/TRexAttack.js @@ -20,8 +20,6 @@ import * as utils from '../utils.js'; // preferences process. They are used only in the creator function of the ShaderFactory // which is only called within GNOME Shell's process. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); -const St = await utils.importInShellOnly('gi://St'); -const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); const _ = await utils.importGettext(); @@ -45,10 +43,7 @@ export default class Effect { this.shaderFactory = new ShaderFactory(Effect.getNick(), (shader) => { // Create the texture in the first call. if (!this._clawTexture) { - const clawData = GdkPixbuf.Pixbuf.new_from_resource('/img/claws.png'); - this._clawTexture = new St.ImageContent(); - this._clawTexture.set_data(clawData.get_pixels(), Cogl.PixelFormat.RGB_888, - clawData.width, clawData.height, clawData.rowstride); + this._clawTexture = utils.getImageResource('/img/claws.png'); } // Store uniform locations of newly created shaders. diff --git a/src/utils.js b/src/utils.js index 73ad404..72b1de2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -20,8 +20,10 @@ import GLib from 'gi://GLib'; // We import some modules optionally. This file is used in the preferences process as well // as in the GNOME Shell process. Some modules are only available or required in one of // these processes. +const GdkPixbuf = await importInShellOnly('gi://GdkPixbuf'); const Clutter = await importInShellOnly('gi://Clutter'); const Cogl = await importInShellOnly('gi://Cogl'); +const St = await importInShellOnly('gi://St'); // We import the Config module. This is done differently in the GNOME Shell process and in // the preferences process. @@ -117,6 +119,30 @@ export function getStringResource(path) { return new TextDecoder().decode(data.get_data()); } +// Reads the contents of an image file contained in the global resources archive. The data +// is returned as a St.ImageContent. +export function getImageResource(path, premultiplied=false) { + const data = GdkPixbuf.Pixbuf.new_from_resource(path); + const texture = St.ImageContent.new_with_preferred_size(data.width, data.height); + const format = data.has_alpha ? (premultiplied ? Cogl.PixelFormat.RGBA_8888_PRE : Cogl.PixelFormat.RGBA_8888) : Cogl.PixelFormat.RGB_888; + + // https://gitlab.gnome.org/GNOME/gnome-shell/-/commit/44b84e458a22046fedb85701ea25ad08ecc0d43f + if (shellVersionIsAtLeast(48, 'beta')) { + texture.set_data( + global.stage.context.get_backend().get_cogl_context(), + data.get_pixels(), + format, + data.width, data.height, data.rowstride); + } else { + texture.set_data( + data.get_pixels(), + format, + data.width, data.height, data.rowstride); + } + + return texture; +} + // Executes a command asynchronously and returns the output from 'stdout' on success or // throw an error with output from 'stderr' on failure. If given, input will be passed to // 'stdin' and cancellable can be used to stop the process before it finishes.