🔧 Fix memory leaks of more effects
This commit is contained in:
+124
-80
@@ -25,8 +25,13 @@ const utils = Me.imports.src.utils;
|
||||
// This effect looks a bit like the transporter effect from TNG. //
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// The shader class for this effect is registered further down in this file.
|
||||
let Shader = null;
|
||||
// The shader class for this effect is registered further down in this file. When this
|
||||
// effect is used for the first time, an instance of this shader class is created. Once
|
||||
// the effect is finished, the shader will be stored in the freeShaders array and will
|
||||
// then be reused if a new shader is requested. ShaderClass which will be used whenever
|
||||
// this effect is used.
|
||||
let ShaderClass = null;
|
||||
let freeShaders = [];
|
||||
|
||||
// The effect class is completely static. It can be used to get some metadata (like the
|
||||
// effect's name or supported GNOME Shell versions), to initialize the respective page of
|
||||
@@ -75,9 +80,21 @@ var EnergizeB = class EnergizeB {
|
||||
|
||||
// ---------------------------------------------------------------- API for extension.js
|
||||
|
||||
// This is called from extension.js whenever a window is closed with this effect.
|
||||
static createShader(actor, settings, forOpening) {
|
||||
return new Shader(settings, forOpening);
|
||||
// This is called from extension.js whenever a window is opened or closed with this
|
||||
// effect. It returns an instance of the shader class, trying to reuse previously
|
||||
// created shaders.
|
||||
static getShader(actor, settings, forOpening) {
|
||||
let shader;
|
||||
|
||||
if (freeShaders.length == 0) {
|
||||
shader = new ShaderClass();
|
||||
} else {
|
||||
shader = freeShaders.pop();
|
||||
}
|
||||
|
||||
shader.setUniforms(actor, settings, forOpening);
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
// The tweakTransition() is called from extension.js to tweak a window's open / close
|
||||
@@ -97,6 +114,12 @@ var EnergizeB = class EnergizeB {
|
||||
'scale-y': {from: 1.0, to: 1.0, mode: 3}
|
||||
};
|
||||
}
|
||||
|
||||
// This is called from extension.js if the extension is disabled. This should free all
|
||||
// static resources.
|
||||
static cleanUp() {
|
||||
freeShaders = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -108,112 +131,132 @@ var EnergizeB = class EnergizeB {
|
||||
|
||||
if (utils.isInShellProcess()) {
|
||||
|
||||
const Clutter = imports.gi.Clutter;
|
||||
const shaderSnippets = Me.imports.src.shaderSnippets;
|
||||
const {Clutter, Shell} = imports.gi;
|
||||
const shaderSnippets = Me.imports.src.shaderSnippets;
|
||||
|
||||
Shader = GObject.registerClass({}, class Shader extends Clutter.ShaderEffect {
|
||||
_init(settings, forOpening) {
|
||||
super._init({shader_type: Clutter.ShaderType.FRAGMENT_SHADER});
|
||||
ShaderClass = GObject.registerClass({}, class ShaderClass extends Shell.GLSLEffect {
|
||||
// This is called when the effect is used for the first time. This can be used to
|
||||
// store all required uniform locations.
|
||||
_init() {
|
||||
super._init();
|
||||
|
||||
const color = Clutter.Color.from_string(settings.get_string('energize-b-color'))[1];
|
||||
this._uForOpening = this.get_uniform_location('uForOpening');
|
||||
this._uColor = this.get_uniform_location('uColor');
|
||||
this._uScale = this.get_uniform_location('uScale');
|
||||
}
|
||||
|
||||
// If we are currently performing integration test, the animation uses a fixed seed.
|
||||
const testMode = settings.get_boolean('test-mode');
|
||||
// This is called each time the effect is used. This can be used to retrieve the
|
||||
// configuration from the settings and update all uniforms accordingly.
|
||||
setUniforms(actor, settings, forOpening) {
|
||||
const c = Clutter.Color.from_string(settings.get_string('energize-b-color'))[1];
|
||||
|
||||
this.set_shader_source(`
|
||||
// clang-format off
|
||||
this.set_uniform_float(this._uForOpening, 1, [forOpening]);
|
||||
this.set_uniform_float(this._uColor, 3, [c.red / 255, c.green / 255, c.blue / 255]);
|
||||
this.set_uniform_float(this._uScale, 1, [settings.get_double('energize-b-scale')]);
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// Inject some common shader snippets.
|
||||
${shaderSnippets.standardUniforms()}
|
||||
${shaderSnippets.noise()}
|
||||
${shaderSnippets.edgeMask()}
|
||||
// This is called by extension.js when the shader is not used anymore. We will store
|
||||
// this instance of the shader so that it can be re-used in th future.
|
||||
free() {
|
||||
freeShaders.push(this);
|
||||
}
|
||||
|
||||
const vec2 SEED = vec2(${testMode ? 0 : Math.random()},
|
||||
${testMode ? 0 : Math.random()});
|
||||
const float SHOWER_TIME = 0.3;
|
||||
const float SHOWER_WIDTH = 0.3;
|
||||
const float STREAK_TIME = 0.6;
|
||||
const float EDGE_FADE = 50;
|
||||
const float SCALE = ${settings.get_double('energize-b-scale')};
|
||||
// This is called by the constructor. This is means it's only called when the effect
|
||||
// is used for the first time.
|
||||
vfunc_build_pipeline() {
|
||||
const declarations = `
|
||||
// Inject some common shader snippets.
|
||||
${shaderSnippets.standardUniforms()}
|
||||
${shaderSnippets.noise()}
|
||||
${shaderSnippets.edgeMask()}
|
||||
|
||||
// This method returns four values:
|
||||
// result.x: A mask for the particles which lead the shower.
|
||||
// result.y: A mask for the streaks which follow the shower particles.
|
||||
// result.z: A mask for the final "atom" particles.
|
||||
// result.w: The opacity of the fading window.
|
||||
vec4 getMasks() {
|
||||
float showerProgress = uProgress/SHOWER_TIME;
|
||||
float streakProgress = clamp((uProgress-SHOWER_TIME)/STREAK_TIME, 0, 1);
|
||||
float fadeProgress = clamp((uProgress-SHOWER_TIME)/(1.0 - SHOWER_TIME), 0, 1);
|
||||
uniform bool uForOpening;
|
||||
uniform vec3 uColor;
|
||||
uniform float uScale;
|
||||
|
||||
// Gradient from top to bottom.
|
||||
float t = cogl_tex_coord_in[0].t;
|
||||
const float SHOWER_TIME = 0.3;
|
||||
const float SHOWER_WIDTH = 0.3;
|
||||
const float STREAK_TIME = 0.6;
|
||||
const float EDGE_FADE = 50;
|
||||
|
||||
// A smooth gradient which moves to the bottom within the showerProgress.
|
||||
float showerMask = smoothstep(1, 0, abs(showerProgress - t - SHOWER_WIDTH) / SHOWER_WIDTH);
|
||||
// This method returns four values:
|
||||
// result.x: A mask for the particles which lead the shower.
|
||||
// result.y: A mask for the streaks which follow the shower particles.
|
||||
// result.z: A mask for the final "atom" particles.
|
||||
// result.w: The opacity of the fading window.
|
||||
vec4 getMasks() {
|
||||
float showerProgress = uProgress/SHOWER_TIME;
|
||||
float streakProgress = clamp((uProgress-SHOWER_TIME)/STREAK_TIME, 0, 1);
|
||||
float fadeProgress = clamp((uProgress-SHOWER_TIME)/(1.0 - SHOWER_TIME), 0, 1);
|
||||
|
||||
// This is 1 above the streak mask.
|
||||
float streakMask = (showerProgress - t - SHOWER_WIDTH) > 0 ? 1 : 0;
|
||||
// Gradient from top to bottom.
|
||||
float t = cogl_tex_coord_in[0].t;
|
||||
|
||||
// Compute mask for the "atom" particles.
|
||||
float atomMask = getRelativeEdgeMask(0.2);
|
||||
atomMask = max(0, atomMask - showerMask);
|
||||
atomMask *= streakMask;
|
||||
atomMask *= sqrt(1-fadeProgress*fadeProgress);
|
||||
// A smooth gradient which moves to the bottom within the showerProgress.
|
||||
float showerMask = smoothstep(1, 0, abs(showerProgress - t - SHOWER_WIDTH) / SHOWER_WIDTH);
|
||||
|
||||
// Make some particles visible in the streaks.
|
||||
showerMask += 0.05 * streakMask;
|
||||
// This is 1 above the streak mask.
|
||||
float streakMask = (showerProgress - t - SHOWER_WIDTH) > 0 ? 1 : 0;
|
||||
|
||||
// Add shower mask to streak mask.
|
||||
streakMask = max(streakMask, showerMask);
|
||||
// Compute mask for the "atom" particles.
|
||||
float atomMask = getRelativeEdgeMask(0.2);
|
||||
atomMask = max(0, atomMask - showerMask);
|
||||
atomMask *= streakMask;
|
||||
atomMask *= sqrt(1-fadeProgress*fadeProgress);
|
||||
|
||||
// Fade-out the masks at the window edges.
|
||||
float edgeFade = getAbsoluteEdgeMask(EDGE_FADE);
|
||||
streakMask *= edgeFade;
|
||||
showerMask *= edgeFade;
|
||||
|
||||
// Fade-out the masks from top to bottom.
|
||||
float fade = smoothstep(0.0, 1.0, 1.0 + t - 2.0 * streakProgress);
|
||||
streakMask *= fade;
|
||||
showerMask *= fade;
|
||||
// Make some particles visible in the streaks.
|
||||
showerMask += 0.05 * streakMask;
|
||||
|
||||
// Compute fading window opacity.
|
||||
float windowMask = pow(1.0 - fadeProgress, 2.0);
|
||||
// Add shower mask to streak mask.
|
||||
streakMask = max(streakMask, showerMask);
|
||||
|
||||
#if ${forOpening ? '1' : '0'}
|
||||
windowMask = 1.0 - windowMask;
|
||||
#endif
|
||||
// Fade-out the masks at the window edges.
|
||||
float edgeFade = getAbsoluteEdgeMask(EDGE_FADE);
|
||||
streakMask *= edgeFade;
|
||||
showerMask *= edgeFade;
|
||||
|
||||
// Fade-out the masks from top to bottom.
|
||||
float fade = smoothstep(0.0, 1.0, 1.0 + t - 2.0 * streakProgress);
|
||||
streakMask *= fade;
|
||||
showerMask *= fade;
|
||||
|
||||
return vec4(showerMask, streakMask, atomMask, windowMask);
|
||||
}
|
||||
// Compute fading window opacity.
|
||||
float windowMask = pow(1.0 - fadeProgress, 2.0);
|
||||
|
||||
void main() {
|
||||
|
||||
if (uForOpening) {
|
||||
windowMask = 1.0 - windowMask;
|
||||
}
|
||||
|
||||
return vec4(showerMask, streakMask, atomMask, windowMask);
|
||||
}
|
||||
`;
|
||||
|
||||
const code = `
|
||||
vec4 masks = getMasks();
|
||||
vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st);
|
||||
vec3 effectColor = vec3(${color.red / 255},
|
||||
${color.green / 255},
|
||||
${color.blue / 255});
|
||||
|
||||
// Dissolve window to effect color / transparency.
|
||||
cogl_color_out = mix(vec4(effectColor, 1.0) * windowColor.a, windowColor, 0.5 * masks.w + 0.5) * masks.w;
|
||||
cogl_color_out = mix(vec4(uColor, 1.0) * windowColor.a, windowColor, 0.5 * masks.w + 0.5) * masks.w;
|
||||
|
||||
// Add leading shower particles.
|
||||
vec2 showerUV = cogl_tex_coord_in[0].st + vec2(0, -0.7*uProgress/SHOWER_TIME);
|
||||
showerUV *= 0.02 * vec2(uSizeX, uSizeY) / SCALE;
|
||||
showerUV *= 0.02 * vec2(uSizeX, uSizeY) / uScale;
|
||||
float shower = pow(simplex2D(showerUV), 10.0);
|
||||
cogl_color_out.rgb += effectColor * shower * masks.x;
|
||||
cogl_color_out.rgb += uColor * shower * masks.x;
|
||||
|
||||
// Add trailing streak lines.
|
||||
vec2 streakUV = cogl_tex_coord_in[0].st + vec2(0, -uProgress/SHOWER_TIME);
|
||||
streakUV *= vec2(0.05 * uSizeX, 0.001 * uSizeY) / SCALE;
|
||||
streakUV *= vec2(0.05 * uSizeX, 0.001 * uSizeY) / uScale;
|
||||
float streaks = simplex2DFractal(streakUV) * 0.5;
|
||||
cogl_color_out.rgb += effectColor * streaks * masks.y;
|
||||
cogl_color_out.rgb += uColor * streaks * masks.y;
|
||||
|
||||
// Add glimmering atoms.
|
||||
vec2 atomUV = cogl_tex_coord_in[0].st + vec2(0, -0.025*uProgress/SHOWER_TIME);
|
||||
atomUV *= 0.2 * vec2(uSizeX, uSizeY) / SCALE;
|
||||
atomUV *= 0.2 * vec2(uSizeX, uSizeY) / uScale;
|
||||
float atoms = pow((simplex3D(vec3(atomUV, uTime))), 5.0);
|
||||
cogl_color_out.rgb += effectColor * atoms * masks.z;
|
||||
cogl_color_out.rgb += uColor * atoms * masks.z;
|
||||
|
||||
// These are pretty useful for understanding how this works.
|
||||
// cogl_color_out = vec4(masks.rgb, 1.0);
|
||||
@@ -224,8 +267,9 @@ if (utils.isInShellProcess()) {
|
||||
// cogl_color_out = vec4(vec3(shower), 1.0);
|
||||
// cogl_color_out = vec4(vec3(streaks), 1.0);
|
||||
// cogl_color_out = vec4(vec3(atoms), 1.0);
|
||||
}
|
||||
`);
|
||||
};
|
||||
`;
|
||||
|
||||
this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, declarations, code, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user