Shared PGraphics instances - drawing

NOTE: PGraphics.beginDraw(), PGraphics.endDraw() alternatives

how would i go about drawing to a shared PGraphics instance in the context each window that obtains the PGraphics instance could just invoke .background(0); and erase the entire pgraphics drawing image

as the only option i see right know is to completely wrap the PGraphics to provide a containable drawing region such that, for example, fill(0) will only fill from x1, y2, to x2, y2 instead of from 0, 0, width, height

or even interfere with, and possibly maliciously overlay other windows should they be in possession of there known coordinates and width/size

basically how would i design this such that there is seperation between all intances and their windows, eg window X can only write to 50 by 100 of the 500 by 500 canvas, and window K can only write to 10 by 30 of the 500 by 500 window canvas, and as such, invoking background(0) would ONLY fill the 10 by 30 region that is assigned, and 0,0 would correspond to 10,10 and 20,20 to 30,30

specifically

how would i go about implementing double buffering in the context of a window manager?

as tipically, based on osdev.org, it is implemented as a pointer that is (assumably) changed for each window being drawn as so that, for example, that window’s 0,0 points to the correct location in memory

but in java, pointers do not exist, and as such, i assume that to correctly implement this, a new buffer MUST be created for every window and then copied to the main buffer on each window draw

in which, the creation and destruction of a drawing context (eg via OpenGL) is expensive and as such

if i have for example 30 windows open my performance will drop to about 5FPS

when with no windows it is 60FPS

due to the overhead of the 60 drawing context creations and destructions, multiplied by 60 frames per second

One solution is to constrain parameters within primitives calls. Example (for a given container): ellipse(max(a, posX), max(b, posY), min(n, containerWidth), min(m, containerHeight);. You could have containers override some kind of superclass that transmutes a primitives call to a constrained version, given the constraints of the calling container.

Replace calls to fill() with rect() to mimic the effect on a given area.

Also consider splitting window rendering across threads with PThreading (limited to 2D drawing though).

im not sure how that would work, as the basic idea is to get it to mimic clip()

eg a possible solution may be

PGraphics graphics, g2;

void settings() {
  size(400, 400, P3D);
}

void setup() {
  background(0);
  graphics = createGraphics(500,500, P3D);
  g2 = createGraphics(500,500, P3D);
}

void draw() {
  g2.beginDraw();
  g2.clip(0,0,50,50);
  g2.background(0, 128, 128);
  g2.clip(0,0,25,25);
  g2.background(0);
  g2.clip(35,35,65,65);
  g2.background(0, 228, 128);
  g2.clip(0,0,50,50);
  g2.endDraw();
  graphics.beginDraw();
  graphics.image(g2, 0, 0);
  graphics.endDraw();
  image(graphics, 0, 0);
}

however this would require additional work such as what if a user intentionally unclips the clipped region, eg manager clips to 50,50,100,100, user application clips to 30,60,40,70, as with

  g2.clip(50,50,100,100);
// USER START
// clip to out of bounds region
// user may clip thinking 0 is the actual top left corner
// when, in reality, 50 is the top left corner
  g2.clip(0,0,30,30);
  g2.background(0, 228, 128);
// USER END
  g2.clip(0,0,50,50);

as with the following example

PGraphics graphics, g2;

void settings() {
  size(400, 400, P3D);
}

void setup() {
  background(0);
  graphics = createGraphics(500,500, P3D);
  g2 = createGraphics(500,500, P3D);
}

void draw() {
  g2.beginDraw();
  g2.clip(50,50,100,100);
  g2.background(0, 128, 128);
  g2.clip(60,60,75,75);
  g2.background(0);
  g2.clip(0,0,25,25);
  g2.background(0, 228, 128);
  g2.clip(50,50,100,100);
  g2.endDraw();
  graphics.beginDraw();
  graphics.image(g2, 0, 0);
  graphics.endDraw();
  image(graphics, 0, 0);
}