1 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
9b94626c
|
Leszek Koltunski
|
// Copyright 2016 Leszek Koltunski //
|
3 |
c5369f1b
|
leszek
|
// //
|
4 |
|
|
// This file is part of Distorted. //
|
5 |
|
|
// //
|
6 |
|
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
|
|
// it under the terms of the GNU General Public License as published by //
|
8 |
|
|
// the Free Software Foundation, either version 2 of the License, or //
|
9 |
|
|
// (at your option) any later version. //
|
10 |
|
|
// //
|
11 |
|
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14 |
|
|
// GNU General Public License for more details. //
|
15 |
|
|
// //
|
16 |
|
|
// You should have received a copy of the GNU General Public License //
|
17 |
|
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
fe82a979
|
Leszek Koltunski
|
package org.distorted.library.main;
|
21 |
c5369f1b
|
leszek
|
|
22 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23 |
806ca386
|
leszek
|
|
24 |
86e99907
|
leszek
|
import android.graphics.Bitmap;
|
25 |
|
|
import android.graphics.Canvas;
|
26 |
|
|
import android.graphics.Paint;
|
27 |
e6519ac8
|
Leszek Koltunski
|
import android.opengl.GLES31;
|
28 |
806ca386
|
leszek
|
|
29 |
81b1577b
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectMove;
|
30 |
86e99907
|
leszek
|
import org.distorted.library.type.Static3D;
|
31 |
|
|
|
32 |
c5369f1b
|
leszek
|
/**
|
33 |
|
|
* Class which represents the Screen.
|
34 |
|
|
* <p>
|
35 |
|
|
* User is able to render to it just like to a DistortedFramebuffer.
|
36 |
|
|
*/
|
37 |
040cd18c
|
Leszek Koltunski
|
public class DistortedScreen extends DistortedFramebuffer
|
38 |
c5369f1b
|
leszek
|
{
|
39 |
86e99907
|
leszek
|
///// DEBUGGING ONLY /////////////////////////
|
40 |
040cd18c
|
Leszek Koltunski
|
private boolean mShowFPS;
|
41 |
|
|
|
42 |
86e99907
|
leszek
|
private static final int NUM_FRAMES = 100;
|
43 |
|
|
|
44 |
|
|
private MeshObject fpsMesh;
|
45 |
|
|
private DistortedTexture fpsTexture;
|
46 |
|
|
private DistortedEffects fpsEffects;
|
47 |
|
|
private Canvas fpsCanvas;
|
48 |
|
|
private Bitmap fpsBitmap;
|
49 |
|
|
private Paint mPaint;
|
50 |
|
|
private int fpsH, fpsW;
|
51 |
040cd18c
|
Leszek Koltunski
|
private String fpsString;
|
52 |
86e99907
|
leszek
|
private long lastTime=0;
|
53 |
|
|
private long[] durations;
|
54 |
|
|
private int currDuration;
|
55 |
81b1577b
|
Leszek Koltunski
|
private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
|
56 |
040cd18c
|
Leszek Koltunski
|
///// END DEBUGGING //////////////////////////
|
57 |
86e99907
|
leszek
|
|
58 |
9b94626c
|
Leszek Koltunski
|
private int mCurRenderedFBO; // During the first FBO_QUEUE_SIZE frames, we blit the very first
|
59 |
|
|
private int mToBeBlittedFBO; // FBO one we have rendered. Then, we keep blitting the one we
|
60 |
|
|
private boolean mFirstCircle; // rendered FBO_QUEUE_SIZE ago.
|
61 |
9d845904
|
Leszek Koltunski
|
|
62 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63 |
040cd18c
|
Leszek Koltunski
|
// PUBLIC API
|
64 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65 |
|
|
/**
|
66 |
|
|
* Create a new Screen. Initially 1x1 in size.
|
67 |
|
|
* <p>
|
68 |
|
|
* Has to be followed by a 'resize()' to set the size.
|
69 |
|
|
*/
|
70 |
|
|
public DistortedScreen()
|
71 |
|
|
{
|
72 |
f4d6114c
|
Leszek Koltunski
|
super(Distorted.FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
|
73 |
040cd18c
|
Leszek Koltunski
|
mShowFPS = false;
|
74 |
9b94626c
|
Leszek Koltunski
|
mCurRenderedFBO = 0;
|
75 |
|
|
mToBeBlittedFBO = 0;
|
76 |
|
|
mFirstCircle = true;
|
77 |
040cd18c
|
Leszek Koltunski
|
}
|
78 |
c5369f1b
|
leszek
|
|
79 |
26a4e5f6
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80 |
040cd18c
|
Leszek Koltunski
|
/**
|
81 |
|
|
* Draws all the attached children to this OutputSurface.
|
82 |
|
|
* <p>
|
83 |
|
|
* Must be called from a thread holding OpenGL Context.
|
84 |
|
|
*
|
85 |
|
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
86 |
|
|
* @return Number of objects rendered.
|
87 |
|
|
*/
|
88 |
|
|
public int render(long time)
|
89 |
|
|
{
|
90 |
|
|
if( mShowFPS )
|
91 |
|
|
{
|
92 |
|
|
if( lastTime==0 ) lastTime = time;
|
93 |
26a4e5f6
|
leszek
|
|
94 |
040cd18c
|
Leszek Koltunski
|
currDuration++;
|
95 |
|
|
if (currDuration >= NUM_FRAMES) currDuration = 0;
|
96 |
|
|
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
|
97 |
|
|
durations[currDuration] = time - lastTime;
|
98 |
|
|
|
99 |
|
|
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
|
100 |
|
|
|
101 |
|
|
mPaint.setColor(0xffffffff);
|
102 |
|
|
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
|
103 |
|
|
mPaint.setColor(0xff000000);
|
104 |
|
|
fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
|
105 |
|
|
fpsTexture.setTexture(fpsBitmap);
|
106 |
|
|
|
107 |
|
|
lastTime = time;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
9b94626c
|
Leszek Koltunski
|
int numrender = super.render(time,mCurRenderedFBO);
|
111 |
040cd18c
|
Leszek Koltunski
|
|
112 |
|
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
|
113 |
2301cb2f
|
Leszek Koltunski
|
|
114 |
|
|
// workaround for the Mali issues: blit the framebuffer we have computed Distorted.FBO_QUEUE_SIZE
|
115 |
|
|
// frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
|
116 |
|
|
// make it equal to 3.
|
117 |
|
|
// This of course introduces a delay and uses more memory, but it does not appear to have any effect
|
118 |
|
|
// on speed. Maybe a slight positive effect if any!
|
119 |
9b94626c
|
Leszek Koltunski
|
setAsInput(mToBeBlittedFBO,0);
|
120 |
2301cb2f
|
Leszek Koltunski
|
|
121 |
040cd18c
|
Leszek Koltunski
|
GLES31.glColorMask(true,true,true,true);
|
122 |
|
|
GLES31.glDepthMask(false);
|
123 |
|
|
GLES31.glDisable(GLES31.GL_STENCIL_TEST);
|
124 |
|
|
GLES31.glDisable(GLES31.GL_DEPTH_TEST);
|
125 |
|
|
GLES31.glDisable(GLES31.GL_BLEND);
|
126 |
|
|
|
127 |
|
|
DistortedEffects.blitPriv(this);
|
128 |
|
|
|
129 |
|
|
if( mShowFPS && fpsTexture.setAsInput())
|
130 |
|
|
{
|
131 |
|
|
fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time, 0);
|
132 |
|
|
}
|
133 |
|
|
|
134 |
9b94626c
|
Leszek Koltunski
|
if( ++mCurRenderedFBO>=Distorted.FBO_QUEUE_SIZE )
|
135 |
|
|
{
|
136 |
|
|
mCurRenderedFBO = 0;
|
137 |
|
|
if (mFirstCircle) mFirstCircle = false;
|
138 |
|
|
}
|
139 |
|
|
if( !mFirstCircle && ++mToBeBlittedFBO>=Distorted.FBO_QUEUE_SIZE )
|
140 |
|
|
{
|
141 |
|
|
mToBeBlittedFBO=0;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
040cd18c
|
Leszek Koltunski
|
return numrender+1;
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148 |
|
|
/**
|
149 |
|
|
* Make the library show Frames Per Second in the upper-left corner.
|
150 |
|
|
* <p>
|
151 |
|
|
*/
|
152 |
|
|
public void showFPS()
|
153 |
26a4e5f6
|
leszek
|
{
|
154 |
01b9a241
|
Leszek Koltunski
|
if( !mShowFPS )
|
155 |
|
|
{
|
156 |
|
|
mShowFPS = true;
|
157 |
|
|
|
158 |
|
|
fpsW = 120;
|
159 |
|
|
fpsH = 70;
|
160 |
|
|
|
161 |
|
|
fpsString = "";
|
162 |
|
|
fpsBitmap = Bitmap.createBitmap(fpsW, fpsH, Bitmap.Config.ARGB_8888);
|
163 |
|
|
fpsMesh = new MeshFlat(1, 1);
|
164 |
|
|
fpsTexture = new DistortedTexture(fpsW, fpsH);
|
165 |
|
|
fpsTexture.setTexture(fpsBitmap);
|
166 |
|
|
fpsCanvas = new Canvas(fpsBitmap);
|
167 |
|
|
fpsEffects = new DistortedEffects();
|
168 |
|
|
fpsEffects.apply(mMoveEffect);
|
169 |
|
|
|
170 |
|
|
mPaint = new Paint();
|
171 |
|
|
mPaint.setAntiAlias(true);
|
172 |
|
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
173 |
|
|
mPaint.setTextSize(0.7f * fpsH);
|
174 |
|
|
|
175 |
|
|
durations = new long[NUM_FRAMES + 1];
|
176 |
|
|
currDuration = 0;
|
177 |
|
|
|
178 |
|
|
for (int i = 0; i < NUM_FRAMES + 1; i++) durations[i] = 16; // Assume FPS will be
|
179 |
|
|
durations[NUM_FRAMES] = NUM_FRAMES * 16; // close to 1000/16 ~ 60
|
180 |
|
|
}
|
181 |
c5369f1b
|
leszek
|
}
|
182 |
9b94626c
|
Leszek Koltunski
|
}
|