Skip to content

Commit 32f4200

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 1530ba2 + 07e0383 commit 32f4200

20 files changed

+916
-229
lines changed

core/src/processing/core/PApplet.java

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ static public class RendererChangeException extends RuntimeException { }
425425

426426

427427
/** Called with the activity is first created. */
428+
@SuppressWarnings("unchecked")
428429
@Override
429430
public void onCreate(Bundle savedInstanceState) {
430431
super.onCreate(savedInstanceState);
@@ -467,11 +468,32 @@ public void onCreate(Bundle savedInstanceState) {
467468
int sw = sketchWidth();
468469
int sh = sketchHeight();
469470

470-
if (sketchRenderer().equals(JAVA2D)) {
471-
surfaceView = new SketchSurfaceView(this, sw, sh);
472-
} else if (sketchRenderer().equals(P2D) || sketchRenderer().equals(P3D)) {
473-
surfaceView = new SketchSurfaceViewGL(this, sw, sh, sketchRenderer().equals(P3D));
471+
// Get renderer name and class
472+
String rendererName = sketchRenderer();
473+
Class<?> rendererClass = null;
474+
try {
475+
rendererClass = Class.forName(rendererName);
476+
} catch (ClassNotFoundException exception) {
477+
String message = String.format(
478+
"Error: Could not resolve renderer class name: %s", rendererName);
479+
throw new RuntimeException(message, exception);
480+
}
481+
482+
if (rendererName.equals(JAVA2D)) {
483+
// JAVA2D renderer
484+
surfaceView = new SketchSurfaceView(this, sw, sh,
485+
(Class<? extends PGraphicsAndroid2D>) rendererClass);
486+
} else if (PGraphicsOpenGL.class.isAssignableFrom(rendererClass)) {
487+
// P2D, P3D, and any other PGraphicsOpenGL-based renderer
488+
surfaceView = new SketchSurfaceViewGL(this, sw, sh,
489+
(Class<? extends PGraphicsOpenGL>) rendererClass);
490+
} else {
491+
// Anything else
492+
String message = String.format(
493+
"Error: Unsupported renderer class: %s", rendererName);
494+
throw new RuntimeException(message);
474495
}
496+
475497
// g = ((SketchSurfaceView) surfaceView).getGraphics();
476498

477499
// surfaceView.setLayoutParams(new LayoutParams(sketchWidth(), sketchHeight()));
@@ -677,13 +699,15 @@ public SurfaceView getSurfaceView() {
677699
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
678700

679701

680-
public class SketchSurfaceView extends SurfaceView
681-
implements /*SketchSurfaceView,*/ SurfaceHolder.Callback {
702+
public class SketchSurfaceView extends SurfaceView implements
703+
SurfaceHolder.Callback {
704+
682705
PGraphicsAndroid2D g2;
683706
SurfaceHolder surfaceHolder;
684707

685708

686-
public SketchSurfaceView(Context context, int wide, int high) {
709+
public SketchSurfaceView(Context context, int wide, int high,
710+
Class<? extends PGraphicsAndroid2D> clazz) {
687711
super(context);
688712

689713
// println("surface holder");
@@ -694,7 +718,20 @@ public SketchSurfaceView(Context context, int wide, int high) {
694718
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
695719

696720
// println("creating graphics");
697-
g2 = new PGraphicsAndroid2D();
721+
if (clazz.equals(PGraphicsAndroid2D.class)) {
722+
g2 = new PGraphicsAndroid2D();
723+
} else {
724+
try {
725+
Constructor<? extends PGraphicsAndroid2D> constructor =
726+
clazz.getConstructor();
727+
g2 = constructor.newInstance();
728+
} catch (Exception exception) {
729+
throw new RuntimeException(
730+
"Error: Failed to initialize custom Android2D renderer",
731+
exception);
732+
}
733+
}
734+
698735
// Set semi-arbitrary size; will be set properly when surfaceChanged() called
699736
g2.setSize(wide, high);
700737
// newGraphics.setSize(getWidth(), getHeight());
@@ -779,12 +816,13 @@ public boolean onKeyUp(int code, android.view.KeyEvent event) {
779816
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
780817

781818

782-
public class SketchSurfaceViewGL extends GLSurfaceView /*implements SketchSurfaceView*/ {
819+
public class SketchSurfaceViewGL extends GLSurfaceView {
783820
PGraphicsOpenGL g3;
784821
SurfaceHolder surfaceHolder;
785822

786823

787-
public SketchSurfaceViewGL(Context context, int wide, int high, boolean is3D) {
824+
public SketchSurfaceViewGL(Context context, int wide, int high,
825+
Class<? extends PGraphicsOpenGL> clazz) {
788826
super(context);
789827

790828
// Check if the system supports OpenGL ES 2.0.
@@ -805,11 +843,24 @@ public SketchSurfaceViewGL(Context context, int wide, int high, boolean is3D) {
805843
// null. This is required because PApplet.onResume events (which call
806844
// this.onResume() and thus require a valid renderer) are triggered
807845
// before surfaceChanged() is ever called.
808-
if (is3D) {
809-
g3 = new PGraphics3D();
810-
} else {
846+
847+
if (clazz.equals(PGraphics2D.class)) { // P2D
811848
g3 = new PGraphics2D();
849+
} else if (clazz.equals(PGraphics3D.class)) { // P3D
850+
g3 = new PGraphics3D();
851+
} else { // something that extends P2D, P3D, or PGraphicsOpenGL
852+
try {
853+
Constructor<? extends PGraphicsOpenGL> constructor =
854+
clazz.getConstructor();
855+
g3 = constructor.newInstance();
856+
} catch (Exception exception) {
857+
throw new RuntimeException(
858+
"Error: Failed to initialize custom OpenGL renderer",
859+
exception);
860+
}
812861
}
862+
863+
//set it up
813864
g3.setParent(PApplet.this);
814865
g3.setPrimary(true);
815866
// Set semi-arbitrary size; will be set properly when surfaceChanged() called
@@ -818,8 +869,9 @@ public SketchSurfaceViewGL(Context context, int wide, int high, boolean is3D) {
818869
// Tells the default EGLContextFactory and EGLConfigChooser to create an GLES2 context.
819870
setEGLContextClientVersion(2);
820871

821-
if (PGLES.ENABLE_MULTISAMPLING) {
822-
setEGLConfigChooser(((PGLES)g3.pgl).getConfigChooser());
872+
int quality = sketchQuality();
873+
if (1 < quality) {
874+
setEGLConfigChooser(((PGLES)g3.pgl).getConfigChooser(quality));
823875
}
824876

825877
// The renderer can be set only once.

core/src/processing/core/PGraphicsAndroid2D.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.zip.GZIPInputStream;
2727

2828
import processing.data.XML;
29+
import android.app.ActivityManager;
30+
import android.app.ActivityManager.MemoryInfo;
2931
import android.graphics.*;
3032
import android.graphics.Bitmap.Config;
3133
import android.graphics.Paint.Style;
@@ -135,6 +137,7 @@ public void setSize(int iwidth, int iheight) { // ignore
135137

136138
@Override
137139
protected void allocate() {
140+
if (bitmap != null) bitmap.recycle();
138141
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
139142
canvas = new Canvas(bitmap);
140143
// image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
@@ -996,6 +999,10 @@ public void noSmooth() {
996999
protected void imageImpl(PImage src,
9971000
float x1, float y1, float x2, float y2,
9981001
int u1, int v1, int u2, int v2) {
1002+
if (src.bitmap != null && src.bitmap.isRecycled()) {
1003+
// Let's make sure it is recreated
1004+
src.bitmap = null;
1005+
}
9991006

10001007
if (src.bitmap == null && src.format == ALPHA) {
10011008
// create an alpha bitmap for this feller
@@ -1019,15 +1026,19 @@ protected void imageImpl(PImage src,
10191026
if (src.bitmap == null ||
10201027
src.width != src.bitmap.getWidth() ||
10211028
src.height != src.bitmap.getHeight()) {
1029+
if (src.bitmap != null) src.bitmap.recycle();
10221030
src.bitmap = Bitmap.createBitmap(src.width, src.height, Config.ARGB_8888);
10231031
src.modified = true;
10241032
}
10251033
if (src.modified) {
10261034
//System.out.println("mutable, recycled = " + who.bitmap.isMutable() + ", " + who.bitmap.isRecycled());
10271035
if (!src.bitmap.isMutable()) {
1036+
src.bitmap.recycle();
10281037
src.bitmap = Bitmap.createBitmap(src.width, src.height, Config.ARGB_8888);
10291038
}
1030-
src.bitmap.setPixels(src.pixels, 0, src.width, 0, 0, src.width, src.height);
1039+
if (src.pixels != null) {
1040+
src.bitmap.setPixels(src.pixels, 0, src.width, 0, 0, src.width, src.height);
1041+
}
10311042
src.modified = false;
10321043
}
10331044

@@ -1043,6 +1054,17 @@ protected void imageImpl(PImage src,
10431054
//canvas.drawBitmap(who.bitmap, imageImplSrcRect, imageImplDstRect, fillPaint);
10441055
// System.out.println("drawing lower, tint = " + tint + " " + PApplet.hex(tintPaint.getColor()));
10451056
canvas.drawBitmap(src.bitmap, imageImplSrcRect, imageImplDstRect, tint ? tintPaint : null);
1057+
1058+
// If the OS things the memory is low, then recycles bitmaps automatically...
1059+
// but I don't think it is particularly efficient, as the bitmaps are stored
1060+
// in native heap for Android 10 and older.
1061+
MemoryInfo mi = new MemoryInfo();
1062+
ActivityManager activityManager = (ActivityManager) parent.getApplicationContext().getSystemService(android.content.Context.ACTIVITY_SERVICE);
1063+
activityManager.getMemoryInfo(mi);
1064+
if (mi.lowMemory) {
1065+
src.bitmap.recycle();
1066+
src.bitmap = null;
1067+
}
10461068
}
10471069

10481070

@@ -1996,11 +2018,13 @@ public void set(int x, int y, PImage src) {
19962018
} else { // src.bitmap != null
19972019
if (src.width != src.bitmap.getWidth() ||
19982020
src.height != src.bitmap.getHeight()) {
2021+
src.bitmap.recycle();
19992022
src.bitmap = Bitmap.createBitmap(src.width, src.height, Config.ARGB_8888);
20002023
src.modified = true;
20012024
}
20022025
if (src.modified) {
20032026
if (!src.bitmap.isMutable()) {
2027+
src.bitmap.recycle();
20042028
src.bitmap = Bitmap.createBitmap(src.width, src.height, Config.ARGB_8888);
20052029
}
20062030
src.bitmap.setPixels(src.pixels, 0, src.width, 0, 0, src.width, src.height);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Part of the Processing project - http://processing.org
3+
4+
Copyright (c) 2011-13 Ben Fry and Casey Reas
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License version 2.1 as published by the Free Software Foundation.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General
16+
Public License along with this library; if not, write to the
17+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18+
Boston, MA 02111-1307 USA
19+
*/
20+
21+
#ifdef GL_ES
22+
precision mediump float;
23+
precision mediump int;
24+
#endif
25+
26+
varying vec4 vertColor;
27+
varying vec4 backVertColor;
28+
29+
void main() {
30+
gl_FragColor = gl_FrontFacing ? vertColor : backVertColor;
31+
}

core/src/processing/opengl/LightVert.glsl

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ attribute vec4 emissive;
4343
attribute float shininess;
4444

4545
varying vec4 vertColor;
46+
varying vec4 backVertColor;
4647

4748
const float zero_float = 0.0;
4849
const float one_float = 1.0;
@@ -82,17 +83,17 @@ void main() {
8283

8384
// Normal vector in eye coordinates
8485
vec3 ecNormal = normalize(normalMatrix * normal);
85-
86-
if (dot(-one_float * ecVertex, ecNormal) < zero_float) {
87-
// If normal is away from camera, choose its opposite.
88-
// If we add backface culling, this will be backfacing
89-
ecNormal *= -one_float;
90-
}
86+
vec3 ecNormalInv = ecNormal * -one_float;
9187

9288
// Light calculations
9389
vec3 totalAmbient = vec3(0, 0, 0);
94-
vec3 totalDiffuse = vec3(0, 0, 0);
95-
vec3 totalSpecular = vec3(0, 0, 0);
90+
91+
vec3 totalFrontDiffuse = vec3(0, 0, 0);
92+
vec3 totalFrontSpecular = vec3(0, 0, 0);
93+
94+
vec3 totalBackDiffuse = vec3(0, 0, 0);
95+
vec3 totalBackSpecular = vec3(0, 0, 0);
96+
9697
for (int i = 0; i < 8; i++) {
9798
if (lightCount == i) break;
9899

@@ -118,24 +119,33 @@ void main() {
118119
: one_float;
119120

120121
if (any(greaterThan(lightAmbient[i], zero_vec3))) {
121-
totalAmbient += lightAmbient[i] * falloff;
122+
totalAmbient += lightAmbient[i] * falloff;
122123
}
123124

124125
if (any(greaterThan(lightDiffuse[i], zero_vec3))) {
125-
totalDiffuse += lightDiffuse[i] * falloff * spotf *
126-
lambertFactor(lightDir, ecNormal);
126+
totalFrontDiffuse += lightDiffuse[i] * falloff * spotf *
127+
lambertFactor(lightDir, ecNormal);
128+
totalBackDiffuse += lightDiffuse[i] * falloff * spotf *
129+
lambertFactor(lightDir, ecNormalInv);
127130
}
128131

129132
if (any(greaterThan(lightSpecular[i], zero_vec3))) {
130-
totalSpecular += lightSpecular[i] * falloff * spotf *
131-
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
133+
totalFrontSpecular += lightSpecular[i] * falloff * spotf *
134+
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
135+
totalBackSpecular += lightSpecular[i] * falloff * spotf *
136+
blinnPhongFactor(lightDir, ecVertex, ecNormalInv, shininess);
132137
}
133138
}
134139

135140
// Calculating final color as result of all lights (plus emissive term).
136141
// Transparency is determined exclusively by the diffuse component.
137-
vertColor = vec4(totalAmbient, 0) * ambient +
138-
vec4(totalDiffuse, 1) * color +
139-
vec4(totalSpecular, 0) * specular +
140-
vec4(emissive.rgb, 0);
142+
vertColor = vec4(totalAmbient, 0) * ambient +
143+
vec4(totalFrontDiffuse, 1) * color +
144+
vec4(totalFrontSpecular, 0) * specular +
145+
vec4(emissive.rgb, 0);
146+
147+
backVertColor = vec4(totalAmbient, 0) * ambient +
148+
vec4(totalBackDiffuse, 1) * color +
149+
vec4(totalBackSpecular, 0) * specular +
150+
vec4(emissive.rgb, 0);
141151
}

core/src/processing/opengl/PGL.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,6 @@ protected boolean contextIsCurrent(int other) {
800800

801801

802802
protected void enableTexturing(int target) {
803-
enable(target);
804803
if (target == TEXTURE_2D) {
805804
texturingTargets[0] = true;
806805
} else if (target == TEXTURE_RECTANGLE) {
@@ -810,7 +809,6 @@ protected void enableTexturing(int target) {
810809

811810

812811
protected void disableTexturing(int target) {
813-
disable(target);
814812
if (target == TEXTURE_2D) {
815813
texturingTargets[0] = false;
816814
} else if (target == TEXTURE_RECTANGLE) {

0 commit comments

Comments
 (0)