1 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
9b94626c
|
Leszek Koltunski
|
// Copyright 2016 Leszek Koltunski //
|
3 |
c5369f1b
|
leszek
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
c5369f1b
|
leszek
|
// //
|
6 |
46b572b5
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
c5369f1b
|
leszek
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
c5369f1b
|
leszek
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
b7074bc6
|
Leszek Koltunski
|
import android.opengl.GLES30;
|
28 |
806ca386
|
leszek
|
|
29 |
81b1577b
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectMove;
|
30 |
1b059065
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectScale;
|
31 |
e1e94682
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshQuad;
|
32 |
86e99907
|
leszek
|
import org.distorted.library.type.Static3D;
|
33 |
|
|
|
34 |
c5369f1b
|
leszek
|
/**
|
35 |
|
|
* Class which represents the Screen.
|
36 |
|
|
* <p>
|
37 |
|
|
* User is able to render to it just like to a DistortedFramebuffer.
|
38 |
|
|
*/
|
39 |
040cd18c
|
Leszek Koltunski
|
public class DistortedScreen extends DistortedFramebuffer
|
40 |
c5369f1b
|
leszek
|
{
|
41 |
86e99907
|
leszek
|
///// DEBUGGING ONLY /////////////////////////
|
42 |
|
|
private static final int NUM_FRAMES = 100;
|
43 |
b8f8ef5c
|
Leszek Koltunski
|
private static final int DEBUG_W = 150;
|
44 |
|
|
private static final int DEBUG_H = 70;
|
45 |
4aa38649
|
Leszek Koltunski
|
|
46 |
b8f8ef5c
|
Leszek Koltunski
|
private static final int DEBUG_MODE_NONE = 0;
|
47 |
|
|
private static final int DEBUG_MODE_FPS = 1;
|
48 |
|
|
private static final int DEBUG_MODE_FRAME= 2;
|
49 |
86e99907
|
leszek
|
|
50 |
b8f8ef5c
|
Leszek Koltunski
|
private int mDebugMode;
|
51 |
|
|
private boolean mDebugAllocated;
|
52 |
|
|
|
53 |
|
|
private MeshQuad debugMesh;
|
54 |
|
|
private DistortedTexture debugTexture;
|
55 |
|
|
private DistortedEffects debugEffects;
|
56 |
|
|
private Canvas debugCanvas;
|
57 |
|
|
private Bitmap debugBitmap;
|
58 |
86e99907
|
leszek
|
private Paint mPaint;
|
59 |
b8f8ef5c
|
Leszek Koltunski
|
private String debugString;
|
60 |
86e99907
|
leszek
|
private long lastTime=0;
|
61 |
|
|
private long[] durations;
|
62 |
|
|
private int currDuration;
|
63 |
b8f8ef5c
|
Leszek Koltunski
|
private int frameNumber;
|
64 |
c0f5af7b
|
Leszek Koltunski
|
private static Static3D mMoveVector = new Static3D(0,0,0);
|
65 |
7bebb196
|
Leszek Koltunski
|
private static MatrixEffectMove mMoveEffect = new MatrixEffectMove(mMoveVector);
|
66 |
040cd18c
|
Leszek Koltunski
|
///// END DEBUGGING //////////////////////////
|
67 |
86e99907
|
leszek
|
|
68 |
5f35f1cb
|
Leszek Koltunski
|
private int mQueueSize;
|
69 |
9b94626c
|
Leszek Koltunski
|
private int mCurRenderedFBO; // During the first FBO_QUEUE_SIZE frames, we blit the very first
|
70 |
|
|
private int mToBeBlittedFBO; // FBO one we have rendered. Then, we keep blitting the one we
|
71 |
|
|
private boolean mFirstCircle; // rendered FBO_QUEUE_SIZE ago.
|
72 |
9d845904
|
Leszek Koltunski
|
|
73 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74 |
040cd18c
|
Leszek Koltunski
|
// PUBLIC API
|
75 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76 |
|
|
/**
|
77 |
|
|
* Create a new Screen. Initially 1x1 in size.
|
78 |
|
|
* <p>
|
79 |
9ecac8cd
|
Leszek Koltunski
|
* Has to be followed by a 'resizeFBO()' to set the size.
|
80 |
040cd18c
|
Leszek Koltunski
|
*/
|
81 |
|
|
public DistortedScreen()
|
82 |
|
|
{
|
83 |
5f35f1cb
|
Leszek Koltunski
|
super(DistortedLibrary.WAIT_FOR_FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
|
84 |
b8f8ef5c
|
Leszek Koltunski
|
mDebugMode = DEBUG_MODE_NONE;
|
85 |
9b94626c
|
Leszek Koltunski
|
mCurRenderedFBO = 0;
|
86 |
|
|
mToBeBlittedFBO = 0;
|
87 |
|
|
mFirstCircle = true;
|
88 |
b8f8ef5c
|
Leszek Koltunski
|
mDebugAllocated = false;
|
89 |
5f35f1cb
|
Leszek Koltunski
|
mQueueSize = -1;
|
90 |
040cd18c
|
Leszek Koltunski
|
}
|
91 |
c5369f1b
|
leszek
|
|
92 |
b8f8ef5c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93 |
|
|
|
94 |
|
|
private void allocateDebug()
|
95 |
|
|
{
|
96 |
|
|
if( !mDebugAllocated )
|
97 |
|
|
{
|
98 |
|
|
mDebugAllocated = true;
|
99 |
|
|
|
100 |
|
|
debugString = "";
|
101 |
|
|
debugBitmap = Bitmap.createBitmap(DEBUG_W, DEBUG_H, Bitmap.Config.ARGB_8888);
|
102 |
|
|
debugMesh = new MeshQuad();
|
103 |
|
|
debugTexture = new DistortedTexture();
|
104 |
|
|
debugTexture.setTexture(debugBitmap);
|
105 |
|
|
debugCanvas = new Canvas(debugBitmap);
|
106 |
|
|
debugEffects = new DistortedEffects();
|
107 |
|
|
debugEffects.apply( new MatrixEffectScale( new Static3D(DEBUG_W,DEBUG_H,1) ) );
|
108 |
|
|
debugEffects.apply(mMoveEffect);
|
109 |
|
|
|
110 |
|
|
mPaint = new Paint();
|
111 |
|
|
mPaint.setAntiAlias(true);
|
112 |
|
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
113 |
|
|
mPaint.setTextSize(0.7f * DEBUG_H);
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
26a4e5f6
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118 |
040cd18c
|
Leszek Koltunski
|
/**
|
119 |
|
|
* Draws all the attached children to this OutputSurface.
|
120 |
|
|
* <p>
|
121 |
|
|
* Must be called from a thread holding OpenGL Context.
|
122 |
|
|
*
|
123 |
|
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
124 |
|
|
* @return Number of objects rendered.
|
125 |
|
|
*/
|
126 |
|
|
public int render(long time)
|
127 |
|
|
{
|
128 |
b8f8ef5c
|
Leszek Koltunski
|
if( mDebugMode!=DEBUG_MODE_NONE )
|
129 |
040cd18c
|
Leszek Koltunski
|
{
|
130 |
|
|
if( lastTime==0 ) lastTime = time;
|
131 |
26a4e5f6
|
leszek
|
|
132 |
b8f8ef5c
|
Leszek Koltunski
|
if( mDebugMode==DEBUG_MODE_FPS )
|
133 |
|
|
{
|
134 |
|
|
currDuration++;
|
135 |
|
|
if (currDuration >= NUM_FRAMES) currDuration = 0;
|
136 |
|
|
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
|
137 |
|
|
durations[currDuration] = time - lastTime;
|
138 |
|
|
|
139 |
|
|
debugString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
|
140 |
|
|
}
|
141 |
|
|
else if( mDebugMode==DEBUG_MODE_FRAME )
|
142 |
|
|
{
|
143 |
|
|
debugString = "" + frameNumber;
|
144 |
|
|
frameNumber++;
|
145 |
|
|
}
|
146 |
040cd18c
|
Leszek Koltunski
|
|
147 |
|
|
mPaint.setColor(0xffffffff);
|
148 |
b8f8ef5c
|
Leszek Koltunski
|
debugCanvas.drawRect(0, 0, DEBUG_W, DEBUG_H, mPaint);
|
149 |
040cd18c
|
Leszek Koltunski
|
mPaint.setColor(0xff000000);
|
150 |
b8f8ef5c
|
Leszek Koltunski
|
debugCanvas.drawText(debugString, 0.5f*DEBUG_W, 0.75f*DEBUG_H, mPaint);
|
151 |
|
|
debugTexture.setTexture(debugBitmap);
|
152 |
040cd18c
|
Leszek Koltunski
|
|
153 |
b8f8ef5c
|
Leszek Koltunski
|
mMoveVector.set( (-mWidth+DEBUG_W)/2 +5, (mHeight-DEBUG_H)/2 -5, 0);
|
154 |
7bebb196
|
Leszek Koltunski
|
|
155 |
040cd18c
|
Leszek Koltunski
|
lastTime = time;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
9b94626c
|
Leszek Koltunski
|
int numrender = super.render(time,mCurRenderedFBO);
|
159 |
040cd18c
|
Leszek Koltunski
|
|
160 |
b7074bc6
|
Leszek Koltunski
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
|
161 |
2301cb2f
|
Leszek Koltunski
|
|
162 |
7602a827
|
Leszek Koltunski
|
// workaround for the Mali issues: blit the framebuffer we have computed DistortedLibrary.FBO_QUEUE_SIZE
|
163 |
2301cb2f
|
Leszek Koltunski
|
// frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
|
164 |
|
|
// make it equal to 3.
|
165 |
|
|
// This of course introduces a delay and uses more memory, but it does not appear to have any effect
|
166 |
|
|
// on speed. Maybe a slight positive effect if any!
|
167 |
9b94626c
|
Leszek Koltunski
|
setAsInput(mToBeBlittedFBO,0);
|
168 |
2301cb2f
|
Leszek Koltunski
|
|
169 |
b7074bc6
|
Leszek Koltunski
|
GLES30.glColorMask(true,true,true,true);
|
170 |
|
|
GLES30.glDepthMask(false);
|
171 |
|
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
172 |
|
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
173 |
|
|
GLES30.glDisable(GLES30.GL_BLEND);
|
174 |
040cd18c
|
Leszek Koltunski
|
|
175 |
7602a827
|
Leszek Koltunski
|
DistortedLibrary.blitPriv(this);
|
176 |
040cd18c
|
Leszek Koltunski
|
|
177 |
b8f8ef5c
|
Leszek Koltunski
|
if( mDebugMode!=DEBUG_MODE_NONE && debugTexture.setAsInput())
|
178 |
040cd18c
|
Leszek Koltunski
|
{
|
179 |
b8f8ef5c
|
Leszek Koltunski
|
DistortedLibrary.drawPriv(debugEffects, debugMesh, this, time);
|
180 |
040cd18c
|
Leszek Koltunski
|
}
|
181 |
|
|
|
182 |
5f35f1cb
|
Leszek Koltunski
|
if( mQueueSize<=0 )
|
183 |
|
|
{
|
184 |
|
|
mQueueSize = DistortedLibrary.getQueueSize();
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
if( ++mCurRenderedFBO>=mQueueSize )
|
188 |
9b94626c
|
Leszek Koltunski
|
{
|
189 |
|
|
mCurRenderedFBO = 0;
|
190 |
|
|
if (mFirstCircle) mFirstCircle = false;
|
191 |
|
|
}
|
192 |
5f35f1cb
|
Leszek Koltunski
|
if( !mFirstCircle && ++mToBeBlittedFBO>=mQueueSize )
|
193 |
9b94626c
|
Leszek Koltunski
|
{
|
194 |
|
|
mToBeBlittedFBO=0;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
040cd18c
|
Leszek Koltunski
|
return numrender+1;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
201 |
|
|
/**
|
202 |
|
|
* Make the library show Frames Per Second in the upper-left corner.
|
203 |
|
|
* <p>
|
204 |
|
|
*/
|
205 |
|
|
public void showFPS()
|
206 |
26a4e5f6
|
leszek
|
{
|
207 |
b8f8ef5c
|
Leszek Koltunski
|
if( mDebugMode!=DEBUG_MODE_FPS )
|
208 |
01b9a241
|
Leszek Koltunski
|
{
|
209 |
b8f8ef5c
|
Leszek Koltunski
|
allocateDebug();
|
210 |
|
|
mDebugMode = DEBUG_MODE_FPS;
|
211 |
01b9a241
|
Leszek Koltunski
|
|
212 |
|
|
durations = new long[NUM_FRAMES + 1];
|
213 |
|
|
currDuration = 0;
|
214 |
|
|
|
215 |
7bebb196
|
Leszek Koltunski
|
for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16; // Assume FPS will be
|
216 |
|
|
durations[NUM_FRAMES] = NUM_FRAMES * 16; // close to 1000/16 ~ 60
|
217 |
01b9a241
|
Leszek Koltunski
|
}
|
218 |
c5369f1b
|
leszek
|
}
|
219 |
b8f8ef5c
|
Leszek Koltunski
|
|
220 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
221 |
|
|
/**
|
222 |
|
|
* Make the library show current frame number in the upper-left corner.
|
223 |
|
|
* <p>
|
224 |
|
|
*/
|
225 |
|
|
public void showFrameNumber()
|
226 |
|
|
{
|
227 |
|
|
if( mDebugMode!=DEBUG_MODE_FRAME )
|
228 |
|
|
{
|
229 |
|
|
allocateDebug();
|
230 |
|
|
mDebugMode = DEBUG_MODE_FRAME;
|
231 |
|
|
frameNumber = 0;
|
232 |
|
|
}
|
233 |
|
|
}
|
234 |
9b94626c
|
Leszek Koltunski
|
}
|