Skip to content

Commit 565f30c

Browse files
authored
Merge pull request #4075 from stalgiag/noMoreDirtyPixels
Get rid of _pixelsDirty and _pixelsTime
2 parents a023986 + 2224455 commit 565f30c

File tree

10 files changed

+15
-87
lines changed

10 files changed

+15
-87
lines changed

src/core/environment.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,6 @@ p5.prototype.pixelDensity = function(val) {
599599
if (typeof val === 'number') {
600600
if (val !== this._pixelDensity) {
601601
this._pixelDensity = val;
602-
this._pixelsDirty = true;
603602
}
604603
returnValue = this;
605604
this.resizeCanvas(this.width, this.height, true); // as a side effect, it will clear the canvas

src/core/main.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,6 @@ class p5 {
576576
hsl: [360, 100, 100, 1]
577577
};
578578

579-
this._pixelsDirty = true;
580-
581579
this._downKeys = {}; //Holds the key codes of currently pressed keys
582580
}
583581

src/core/p5.Renderer2D.js

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,13 @@ p5.Renderer2D.prototype.background = function(...args) {
6868
}
6969
}
7070
this.drawingContext.restore();
71-
72-
this._pixelsState._pixelsDirty = true;
7371
};
7472

7573
p5.Renderer2D.prototype.clear = function() {
7674
this.drawingContext.save();
7775
this.resetMatrix();
7876
this.drawingContext.clearRect(0, 0, this.width, this.height);
7977
this.drawingContext.restore();
80-
81-
this._pixelsState._pixelsDirty = true;
8278
};
8379

8480
p5.Renderer2D.prototype.fill = function(...args) {
@@ -180,8 +176,6 @@ p5.Renderer2D.prototype.image = function(
180176
throw e;
181177
}
182178
}
183-
184-
this._pixelsState._pixelsDirty = true;
185179
};
186180

187181
p5.Renderer2D.prototype._getTintedImageCanvas = function(img) {
@@ -258,15 +252,9 @@ p5.Renderer2D.prototype.blend = function(...args) {
258252

259253
// x,y are canvas-relative (pre-scaled by _pixelDensity)
260254
p5.Renderer2D.prototype._getPixel = function(x, y) {
261-
const pixelsState = this._pixelsState;
262255
let imageData, index;
263-
if (pixelsState._pixelsDirty) {
264-
imageData = this.drawingContext.getImageData(x, y, 1, 1).data;
265-
index = 0;
266-
} else {
267-
imageData = pixelsState.pixels;
268-
index = (Math.floor(x) + Math.floor(y) * this.canvas.width) * 4;
269-
}
256+
imageData = this.drawingContext.getImageData(x, y, 1, 1).data;
257+
index = 0;
270258
return [
271259
imageData[index + 0],
272260
imageData[index + 1],
@@ -277,8 +265,6 @@ p5.Renderer2D.prototype._getPixel = function(x, y) {
277265

278266
p5.Renderer2D.prototype.loadPixels = function() {
279267
const pixelsState = this._pixelsState; // if called by p5.Image
280-
if (!pixelsState._pixelsDirty) return;
281-
pixelsState._pixelsDirty = false;
282268

283269
const pd = pixelsState._pixelDensity;
284270
const w = this.width * pd;
@@ -304,7 +290,6 @@ p5.Renderer2D.prototype.set = function(x, y, imgOrCol) {
304290
);
305291
this.drawingContext.drawImage(imgOrCol.canvas, x, y);
306292
this.drawingContext.restore();
307-
pixelsState._pixelsDirty = true;
308293
} else {
309294
let r = 0,
310295
g = 0,
@@ -316,7 +301,7 @@ p5.Renderer2D.prototype.set = function(x, y, imgOrCol) {
316301
pixelsState._pixelDensity *
317302
(this.width * pixelsState._pixelDensity) +
318303
x * pixelsState._pixelDensity);
319-
if (!pixelsState.imageData || pixelsState._pixelsDirty) {
304+
if (!pixelsState.imageData) {
320305
pixelsState.loadPixels.call(pixelsState);
321306
}
322307
if (typeof imgOrCol === 'number') {
@@ -391,10 +376,6 @@ p5.Renderer2D.prototype.updatePixels = function(x, y, w, h) {
391376
}
392377

393378
this.drawingContext.putImageData(pixelsState.imageData, x, y, 0, 0, w, h);
394-
395-
if (x !== 0 || y !== 0 || w !== this.width || h !== this.height) {
396-
pixelsState._pixelsDirty = true;
397-
}
398379
};
399380

400381
//////////////////////////////////////////////
@@ -479,7 +460,6 @@ p5.Renderer2D.prototype.arc = function(x, y, w, h, start, stop, mode) {
479460
}
480461
ctx.closePath();
481462
ctx.fill();
482-
this._pixelsState._pixelsDirty = true;
483463
}
484464

485465
// Stroke curves
@@ -501,7 +481,6 @@ p5.Renderer2D.prototype.arc = function(x, y, w, h, start, stop, mode) {
501481
ctx.closePath();
502482
}
503483
ctx.stroke();
504-
this._pixelsState._pixelsDirty = true;
505484
}
506485
return this;
507486
};
@@ -544,11 +523,9 @@ p5.Renderer2D.prototype.ellipse = function(args) {
544523
ctx.closePath();
545524
if (doFill) {
546525
ctx.fill();
547-
this._pixelsState._pixelsDirty = true;
548526
}
549527
if (doStroke) {
550528
ctx.stroke();
551-
this._pixelsState._pixelsDirty = true;
552529
}
553530
};
554531

@@ -563,7 +540,6 @@ p5.Renderer2D.prototype.line = function(x1, y1, x2, y2) {
563540
ctx.moveTo(x1, y1);
564541
ctx.lineTo(x2, y2);
565542
ctx.stroke();
566-
this._pixelsState._pixelsDirty = true;
567543
return this;
568544
};
569545

@@ -588,7 +564,6 @@ p5.Renderer2D.prototype.point = function(x, y) {
588564
ctx.fillRect(x, y, 1, 1);
589565
}
590566
this._setFill(f);
591-
this._pixelsState._pixelsDirty = true;
592567
};
593568

594569
p5.Renderer2D.prototype.quad = function(x1, y1, x2, y2, x3, y3, x4, y4) {
@@ -616,7 +591,6 @@ p5.Renderer2D.prototype.quad = function(x1, y1, x2, y2, x3, y3, x4, y4) {
616591
if (doStroke) {
617592
ctx.stroke();
618593
}
619-
this._pixelsState._pixelsDirty = true;
620594
return this;
621595
};
622596

@@ -706,7 +680,6 @@ p5.Renderer2D.prototype.rect = function(args) {
706680
if (this._doStroke) {
707681
ctx.stroke();
708682
}
709-
this._pixelsState._pixelsDirty = true;
710683
return this;
711684
};
712685

@@ -736,11 +709,9 @@ p5.Renderer2D.prototype.triangle = function(args) {
736709
ctx.closePath();
737710
if (doFill) {
738711
ctx.fill();
739-
this._pixelsState._pixelsDirty = true;
740712
}
741713
if (doStroke) {
742714
ctx.stroke();
743-
this._pixelsState._pixelsDirty = true;
744715
}
745716
};
746717

@@ -1000,7 +971,6 @@ p5.Renderer2D.prototype.endShape = function(
1000971
vertices.pop();
1001972
}
1002973

1003-
this._pixelsState._pixelsDirty = true;
1004974
return this;
1005975
};
1006976
//////////////////////////////////////////////
@@ -1102,8 +1072,6 @@ p5.Renderer2D.prototype._doFillStrokeClose = function(closeShape) {
11021072
if (this._doStroke) {
11031073
this.drawingContext.stroke();
11041074
}
1105-
1106-
this._pixelsState._pixelsDirty = true;
11071075
};
11081076

11091077
//////////////////////////////////////////////
@@ -1200,8 +1168,6 @@ p5.Renderer2D.prototype._renderText = function(p, line, x, y, maxY) {
12001168
}
12011169

12021170
p.pop();
1203-
1204-
this._pixelsState._pixelsDirty = true;
12051171
return p;
12061172
};
12071173

src/dom/dom.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,8 +2224,6 @@ p5.MediaElement = function(elt, pInst) {
22242224
this._pixelsState = this;
22252225
this._pixelDensity = 1;
22262226
this._modified = false;
2227-
this._pixelsDirty = true;
2228-
this._pixelsTime = -1; // the time at which we last updated 'pixels'
22292227

22302228
/**
22312229
* Path to the media element source.
@@ -2877,25 +2875,16 @@ p5.MediaElement.prototype._ensureCanvas = function() {
28772875
this.canvas.height = this.elt.height;
28782876
this.width = this.canvas.width;
28792877
this.height = this.canvas.height;
2880-
this._pixelsDirty = true;
28812878
}
28822879

2883-
var currentTime = this.elt.currentTime;
2884-
if (this._pixelsDirty || this._pixelsTime !== currentTime) {
2885-
// only update the pixels array if it's dirty, or
2886-
// if the video time has changed.
2887-
this._pixelsTime = currentTime;
2888-
this._pixelsDirty = true;
2889-
2890-
this.drawingContext.drawImage(
2891-
this.elt,
2892-
0,
2893-
0,
2894-
this.canvas.width,
2895-
this.canvas.height
2896-
);
2897-
this.setModified(true);
2898-
}
2880+
this.drawingContext.drawImage(
2881+
this.elt,
2882+
0,
2883+
0,
2884+
this.canvas.width,
2885+
this.canvas.height
2886+
);
2887+
this.setModified(true);
28992888
}
29002889
};
29012890
p5.MediaElement.prototype.loadPixels = function() {

src/image/p5.Image.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ p5.Image = function(width, height) {
145145
this.drawingContext = this.canvas.getContext('2d');
146146
this._pixelsState = this;
147147
this._pixelDensity = 1;
148-
this._pixelsDirty = true;
149148
//Object for working with GIFs, defaults to null
150149
this.gifProperties = null;
151150
//For WebGL Texturing only: used to determine whether to reupload texture to GPU
@@ -242,7 +241,6 @@ p5.Image.prototype._animateGif = function(pInst) {
242241
const ind = props.displayIndex % props.numFrames;
243242
this.drawingContext.putImageData(props.frames[ind], 0, 0);
244243
props.displayIndex = ind;
245-
this._pixelsDirty = true;
246244
this.setModified(true);
247245
}
248246
}
@@ -547,7 +545,6 @@ p5.Image.prototype.resize = function(width, height) {
547545
}
548546

549547
this.setModified(true);
550-
this._pixelsDirty = true;
551548
};
552549

553550
/**

src/image/pixels.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ p5.prototype.copy = function(...args) {
255255
}
256256

257257
p5.prototype._copyHelper(this, srcImage, sx, sy, sw, sh, dx, dy, dw, dh);
258-
259-
this._pixelsDirty = true;
260258
};
261259

262260
p5.prototype._copyHelper = (

src/webgl/p5.RendererGL.Immediate.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,6 @@ p5.RendererGL.prototype._drawFillImmediateMode = function(
333333
0,
334334
this.immediateMode.vertices.length
335335
);
336-
337-
this._pixelsState._pixelsDirty = true;
338336
}
339337
// todo / optimizations? leave bound until another shader is set?
340338
shader.unbindShader();
@@ -373,8 +371,6 @@ p5.RendererGL.prototype._drawStrokeImmediateMode = function() {
373371
this._applyColorBlend(this.curStrokeColor);
374372
gl.drawArrays(gl.TRIANGLES, 0, this.immediateMode.lineVertices.length);
375373

376-
this._pixelsState._pixelsDirty = true;
377-
378374
shader.unbindShader();
379375
};
380376

src/webgl/p5.RendererGL.Retained.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ p5.RendererGL.prototype.drawBuffersScaled = function(
240240

241241
p5.RendererGL.prototype._drawArrays = function(drawMode, gId) {
242242
this.GL.drawArrays(drawMode, 0, this.gHash[gId].lineVertexCount);
243-
this._pixelsState._pixelsDirty = true;
244243
return this;
245244
};
246245

@@ -255,7 +254,6 @@ p5.RendererGL.prototype._drawElements = function(drawMode, gId) {
255254
// drawing vertices
256255
gl.drawArrays(drawMode || gl.TRIANGLES, 0, buffers.vertexCount);
257256
}
258-
this._pixelsState._pixelsDirty = true;
259257
};
260258

261259
p5.RendererGL.prototype._drawPoints = function(vertices, vertexBuffer) {
@@ -276,7 +274,6 @@ p5.RendererGL.prototype._drawPoints = function(vertices, vertexBuffer) {
276274
gl.drawArrays(gl.Points, 0, vertices.length);
277275

278276
pointShader.unbindShader();
279-
this._pixelsState._pixelsDirty = true;
280277
};
281278

282279
export default p5.RendererGL;

src/webgl/p5.RendererGL.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,6 @@ p5.RendererGL.prototype.background = function(...args) {
540540
this.GL.clearColor(_r, _g, _b, _a);
541541
this.GL.depthMask(true);
542542
this.GL.clear(this.GL.COLOR_BUFFER_BIT | this.GL.DEPTH_BUFFER_BIT);
543-
this._pixelsState._pixelsDirty = true;
544543
};
545544

546545
//////////////////////////////////////////////
@@ -739,21 +738,15 @@ p5.RendererGL.prototype.strokeWeight = function(w) {
739738

740739
// x,y are canvas-relative (pre-scaled by _pixelDensity)
741740
p5.RendererGL.prototype._getPixel = function(x, y) {
742-
const pixelsState = this._pixelsState;
743741
let imageData, index;
744-
if (pixelsState._pixelsDirty) {
745-
imageData = new Uint8Array(4);
746-
// prettier-ignore
747-
this.drawingContext.readPixels(
742+
imageData = new Uint8Array(4);
743+
// prettier-ignore
744+
this.drawingContext.readPixels(
748745
x, y, 1, 1,
749746
this.drawingContext.RGBA, this.drawingContext.UNSIGNED_BYTE,
750747
imageData
751748
);
752-
index = 0;
753-
} else {
754-
imageData = pixelsState.pixels;
755-
index = (Math.floor(x) + Math.floor(y) * this.canvas.width) * 4;
756-
}
749+
index = 0;
757750
return [
758751
imageData[index + 0],
759752
imageData[index + 1],
@@ -774,8 +767,6 @@ p5.RendererGL.prototype._getPixel = function(x, y) {
774767

775768
p5.RendererGL.prototype.loadPixels = function() {
776769
const pixelsState = this._pixelsState;
777-
if (!pixelsState._pixelsDirty) return;
778-
pixelsState._pixelsDirty = false;
779770

780771
//@todo_FES
781772
if (this._pInst._glAttributes.preserveDrawingBuffer !== true) {
@@ -831,7 +822,6 @@ p5.RendererGL.prototype.resize = function(w, h) {
831822

832823
//resize pixels buffer
833824
const pixelsState = this._pixelsState;
834-
pixelsState._pixelsDirty = true;
835825
if (typeof pixelsState.pixels !== 'undefined') {
836826
pixelsState._setProperty(
837827
'pixels',
@@ -858,7 +848,6 @@ p5.RendererGL.prototype.clear = function(...args) {
858848
const _a = args[3] || 0;
859849
this.GL.clearColor(_r, _g, _b, _a);
860850
this.GL.clear(this.GL.COLOR_BUFFER_BIT | this.GL.DEPTH_BUFFER_BIT);
861-
this._pixelsState._pixelsDirty = true;
862851
};
863852

864853
p5.RendererGL.prototype.applyMatrix = function(a, b, c, d, e, f) {

src/webgl/text.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,5 @@ p5.RendererGL.prototype._renderText = function(p, line, x, y, maxY) {
741741
p.pop();
742742
}
743743

744-
this._pixelsState._pixelsDirty = true;
745744
return p;
746745
};

0 commit comments

Comments
 (0)