1 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
2e569ff6
|
Leszek Koltunski
|
// Copyright 2016 Leszek Koltunski leszek@koltunski.pl //
|
3 |
c5369f1b
|
leszek
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
c5369f1b
|
leszek
|
// //
|
6 |
2e569ff6
|
Leszek Koltunski
|
// 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 as published by the Free Software Foundation; either //
|
9 |
|
|
// version 2.1 of the License, or (at your option) any later version. //
|
10 |
c5369f1b
|
leszek
|
// //
|
11 |
2e569ff6
|
Leszek Koltunski
|
// This library is distributed in the hope that it will be useful, //
|
12 |
c5369f1b
|
leszek
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
2e569ff6
|
Leszek Koltunski
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
14 |
|
|
// Lesser General Public License for more details. //
|
15 |
c5369f1b
|
leszek
|
// //
|
16 |
2e569ff6
|
Leszek Koltunski
|
// You should have received a copy of the GNU Lesser General Public //
|
17 |
|
|
// License along with this library; if not, write to the Free Software //
|
18 |
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
|
21 |
fe82a979
|
Leszek Koltunski
|
package org.distorted.library.main;
|
22 |
c5369f1b
|
leszek
|
|
23 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
24 |
806ca386
|
leszek
|
|
25 |
86e99907
|
leszek
|
import android.graphics.Bitmap;
|
26 |
|
|
import android.graphics.Canvas;
|
27 |
|
|
import android.graphics.Paint;
|
28 |
b7074bc6
|
Leszek Koltunski
|
import android.opengl.GLES30;
|
29 |
806ca386
|
leszek
|
|
30 |
81b1577b
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectMove;
|
31 |
1b059065
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectScale;
|
32 |
e1e94682
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshQuad;
|
33 |
86e99907
|
leszek
|
import org.distorted.library.type.Static3D;
|
34 |
|
|
|
35 |
c5369f1b
|
leszek
|
/**
|
36 |
|
|
* Class which represents the Screen.
|
37 |
|
|
* <p>
|
38 |
|
|
* User is able to render to it just like to a DistortedFramebuffer.
|
39 |
|
|
*/
|
40 |
040cd18c
|
Leszek Koltunski
|
public class DistortedScreen extends DistortedFramebuffer
|
41 |
c5369f1b
|
leszek
|
{
|
42 |
86e99907
|
leszek
|
///// DEBUGGING ONLY /////////////////////////
|
43 |
a889148a
|
Leszek Koltunski
|
private static final int NUM_FRAMES = 80;
|
44 |
aa6e92f2
|
Leszek Koltunski
|
private static final float DEBUG_SCR_FRAC = 0.15f;
|
45 |
|
|
private static final float DEBUG_FRAC = 0.5f;
|
46 |
4aa38649
|
Leszek Koltunski
|
|
47 |
b8f8ef5c
|
Leszek Koltunski
|
private static final int DEBUG_MODE_NONE = 0;
|
48 |
|
|
private static final int DEBUG_MODE_FPS = 1;
|
49 |
|
|
private static final int DEBUG_MODE_FRAME= 2;
|
50 |
86e99907
|
leszek
|
|
51 |
b8f8ef5c
|
Leszek Koltunski
|
private int mDebugMode;
|
52 |
|
|
private boolean mDebugAllocated;
|
53 |
|
|
|
54 |
|
|
private MeshQuad debugMesh;
|
55 |
|
|
private DistortedTexture debugTexture;
|
56 |
|
|
private DistortedEffects debugEffects;
|
57 |
|
|
private Canvas debugCanvas;
|
58 |
|
|
private Bitmap debugBitmap;
|
59 |
86e99907
|
leszek
|
private Paint mPaint;
|
60 |
b8f8ef5c
|
Leszek Koltunski
|
private String debugString;
|
61 |
86e99907
|
leszek
|
private long lastTime=0;
|
62 |
|
|
private long[] durations;
|
63 |
|
|
private int currDuration;
|
64 |
b8f8ef5c
|
Leszek Koltunski
|
private int frameNumber;
|
65 |
b6947445
|
Leszek Koltunski
|
private static final Static3D mMoveVector = new Static3D(0,0,0);
|
66 |
|
|
private static final MatrixEffectMove mMoveEffect = new MatrixEffectMove(mMoveVector);
|
67 |
040cd18c
|
Leszek Koltunski
|
///// END DEBUGGING //////////////////////////
|
68 |
86e99907
|
leszek
|
|
69 |
5f35f1cb
|
Leszek Koltunski
|
private int mQueueSize;
|
70 |
9b94626c
|
Leszek Koltunski
|
private int mCurRenderedFBO; // During the first FBO_QUEUE_SIZE frames, we blit the very first
|
71 |
|
|
private int mToBeBlittedFBO; // FBO one we have rendered. Then, we keep blitting the one we
|
72 |
|
|
private boolean mFirstCircle; // rendered FBO_QUEUE_SIZE ago.
|
73 |
9d845904
|
Leszek Koltunski
|
|
74 |
c840136b
|
Leszek Koltunski
|
private int mDebugWidth, mDebugHeight, mDebugGap, mDebugTextColor, mDebugBackColor;
|
75 |
|
|
|
76 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77 |
040cd18c
|
Leszek Koltunski
|
// PUBLIC API
|
78 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79 |
|
|
/**
|
80 |
|
|
* Create a new Screen. Initially 1x1 in size.
|
81 |
|
|
* <p>
|
82 |
9ecac8cd
|
Leszek Koltunski
|
* Has to be followed by a 'resizeFBO()' to set the size.
|
83 |
040cd18c
|
Leszek Koltunski
|
*/
|
84 |
|
|
public DistortedScreen()
|
85 |
|
|
{
|
86 |
9ec374e8
|
Leszek Koltunski
|
super(DistortedLibrary.WAIT_FOR_FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, STORAGE_PRIVATE,1,1);
|
87 |
b8f8ef5c
|
Leszek Koltunski
|
mDebugMode = DEBUG_MODE_NONE;
|
88 |
9b94626c
|
Leszek Koltunski
|
mCurRenderedFBO = 0;
|
89 |
|
|
mToBeBlittedFBO = 0;
|
90 |
|
|
mFirstCircle = true;
|
91 |
b8f8ef5c
|
Leszek Koltunski
|
mDebugAllocated = false;
|
92 |
5f35f1cb
|
Leszek Koltunski
|
mQueueSize = -1;
|
93 |
040cd18c
|
Leszek Koltunski
|
}
|
94 |
c5369f1b
|
leszek
|
|
95 |
26a4e5f6
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96 |
040cd18c
|
Leszek Koltunski
|
/**
|
97 |
|
|
* Draws all the attached children to this OutputSurface.
|
98 |
|
|
* <p>
|
99 |
|
|
* Must be called from a thread holding OpenGL Context.
|
100 |
|
|
*
|
101 |
|
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
102 |
|
|
* @return Number of objects rendered.
|
103 |
|
|
*/
|
104 |
|
|
public int render(long time)
|
105 |
|
|
{
|
106 |
b8f8ef5c
|
Leszek Koltunski
|
if( mDebugMode!=DEBUG_MODE_NONE )
|
107 |
040cd18c
|
Leszek Koltunski
|
{
|
108 |
|
|
if( lastTime==0 ) lastTime = time;
|
109 |
26a4e5f6
|
leszek
|
|
110 |
b8f8ef5c
|
Leszek Koltunski
|
if( mDebugMode==DEBUG_MODE_FPS )
|
111 |
|
|
{
|
112 |
|
|
currDuration++;
|
113 |
|
|
if (currDuration >= NUM_FRAMES) currDuration = 0;
|
114 |
|
|
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
|
115 |
|
|
durations[currDuration] = time - lastTime;
|
116 |
|
|
|
117 |
|
|
debugString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
|
118 |
|
|
}
|
119 |
|
|
else if( mDebugMode==DEBUG_MODE_FRAME )
|
120 |
|
|
{
|
121 |
|
|
debugString = "" + frameNumber;
|
122 |
|
|
frameNumber++;
|
123 |
|
|
}
|
124 |
040cd18c
|
Leszek Koltunski
|
|
125 |
aa6e92f2
|
Leszek Koltunski
|
if( !mDebugAllocated )
|
126 |
c840136b
|
Leszek Koltunski
|
{
|
127 |
|
|
mDebugAllocated = true;
|
128 |
|
|
debugString = "";
|
129 |
|
|
|
130 |
b0ac5b29
|
Leszek Koltunski
|
if( mDebugWidth<=0 || mDebugHeight<=0 )
|
131 |
|
|
{
|
132 |
|
|
mDebugWidth = (int)(mWidth*DEBUG_SCR_FRAC);
|
133 |
|
|
mDebugHeight= (int)(mWidth*DEBUG_SCR_FRAC*DEBUG_FRAC);
|
134 |
a889148a
|
Leszek Koltunski
|
|
135 |
|
|
if( mDebugWidth<=0 || mDebugHeight<=0 )
|
136 |
|
|
{
|
137 |
|
|
int width = 100;
|
138 |
|
|
mDebugWidth = (int)(width*DEBUG_SCR_FRAC);
|
139 |
|
|
mDebugHeight= (int)(width*DEBUG_SCR_FRAC*DEBUG_FRAC);
|
140 |
|
|
}
|
141 |
b0ac5b29
|
Leszek Koltunski
|
}
|
142 |
|
|
|
143 |
c840136b
|
Leszek Koltunski
|
debugBitmap = Bitmap.createBitmap( mDebugWidth, mDebugHeight, Bitmap.Config.ARGB_8888);
|
144 |
|
|
debugMesh = new MeshQuad();
|
145 |
|
|
debugTexture = new DistortedTexture();
|
146 |
|
|
debugTexture.setTexture(debugBitmap);
|
147 |
|
|
debugCanvas = new Canvas(debugBitmap);
|
148 |
|
|
debugEffects = new DistortedEffects();
|
149 |
|
|
debugEffects.apply( new MatrixEffectScale( new Static3D(mDebugWidth,mDebugHeight,1) ) );
|
150 |
|
|
debugEffects.apply(mMoveEffect);
|
151 |
|
|
|
152 |
|
|
mPaint = new Paint();
|
153 |
|
|
mPaint.setAntiAlias(true);
|
154 |
|
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
155 |
|
|
mPaint.setTextSize(0.7f*mDebugHeight);
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
mPaint.setColor(mDebugBackColor);
|
159 |
|
|
debugCanvas.drawRect(0, 0, mDebugWidth, mDebugHeight, mPaint);
|
160 |
|
|
mPaint.setColor(mDebugTextColor);
|
161 |
|
|
debugCanvas.drawText(debugString, 0.5f*mDebugWidth, 0.75f*mDebugHeight, mPaint);
|
162 |
b8f8ef5c
|
Leszek Koltunski
|
debugTexture.setTexture(debugBitmap);
|
163 |
040cd18c
|
Leszek Koltunski
|
|
164 |
c840136b
|
Leszek Koltunski
|
mMoveVector.set( (-mWidth+mDebugWidth)*0.5f +mDebugGap, (mHeight-mDebugHeight)*0.5f -mDebugGap, 0);
|
165 |
7bebb196
|
Leszek Koltunski
|
|
166 |
040cd18c
|
Leszek Koltunski
|
lastTime = time;
|
167 |
|
|
}
|
168 |
|
|
|
169 |
9b94626c
|
Leszek Koltunski
|
int numrender = super.render(time,mCurRenderedFBO);
|
170 |
040cd18c
|
Leszek Koltunski
|
|
171 |
b7074bc6
|
Leszek Koltunski
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
|
172 |
2301cb2f
|
Leszek Koltunski
|
|
173 |
7602a827
|
Leszek Koltunski
|
// workaround for the Mali issues: blit the framebuffer we have computed DistortedLibrary.FBO_QUEUE_SIZE
|
174 |
2301cb2f
|
Leszek Koltunski
|
// frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
|
175 |
|
|
// make it equal to 3.
|
176 |
|
|
// This of course introduces a delay and uses more memory, but it does not appear to have any effect
|
177 |
|
|
// on speed. Maybe a slight positive effect if any!
|
178 |
9b94626c
|
Leszek Koltunski
|
setAsInput(mToBeBlittedFBO,0);
|
179 |
2301cb2f
|
Leszek Koltunski
|
|
180 |
b7074bc6
|
Leszek Koltunski
|
GLES30.glColorMask(true,true,true,true);
|
181 |
|
|
GLES30.glDepthMask(false);
|
182 |
|
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
183 |
|
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
184 |
|
|
GLES30.glDisable(GLES30.GL_BLEND);
|
185 |
040cd18c
|
Leszek Koltunski
|
|
186 |
7602a827
|
Leszek Koltunski
|
DistortedLibrary.blitPriv(this);
|
187 |
040cd18c
|
Leszek Koltunski
|
|
188 |
b8f8ef5c
|
Leszek Koltunski
|
if( mDebugMode!=DEBUG_MODE_NONE && debugTexture.setAsInput())
|
189 |
040cd18c
|
Leszek Koltunski
|
{
|
190 |
835b197e
|
Leszek Koltunski
|
DistortedLibrary.drawPriv(debugEffects, debugMesh, this, time,0);
|
191 |
040cd18c
|
Leszek Koltunski
|
}
|
192 |
|
|
|
193 |
5f35f1cb
|
Leszek Koltunski
|
if( mQueueSize<=0 )
|
194 |
|
|
{
|
195 |
|
|
mQueueSize = DistortedLibrary.getQueueSize();
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
if( ++mCurRenderedFBO>=mQueueSize )
|
199 |
9b94626c
|
Leszek Koltunski
|
{
|
200 |
|
|
mCurRenderedFBO = 0;
|
201 |
|
|
if (mFirstCircle) mFirstCircle = false;
|
202 |
|
|
}
|
203 |
5f35f1cb
|
Leszek Koltunski
|
if( !mFirstCircle && ++mToBeBlittedFBO>=mQueueSize )
|
204 |
9b94626c
|
Leszek Koltunski
|
{
|
205 |
|
|
mToBeBlittedFBO=0;
|
206 |
|
|
}
|
207 |
b48a9939
|
Leszek Koltunski
|
/*
|
208 |
fca614bf
|
Leszek Koltunski
|
int err = GLES30.glGetError();
|
209 |
|
|
|
210 |
|
|
if( err!=GLES30.GL_NO_ERROR )
|
211 |
|
|
{
|
212 |
8c57d77b
|
Leszek Koltunski
|
DistortedLibrary.logMessage("DistortedScreen: OpenGL error "+err);
|
213 |
fca614bf
|
Leszek Koltunski
|
}
|
214 |
b48a9939
|
Leszek Koltunski
|
*/
|
215 |
040cd18c
|
Leszek Koltunski
|
return numrender+1;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219 |
|
|
/**
|
220 |
|
|
* Make the library show Frames Per Second in the upper-left corner.
|
221 |
|
|
* <p>
|
222 |
|
|
*/
|
223 |
|
|
public void showFPS()
|
224 |
26a4e5f6
|
leszek
|
{
|
225 |
c840136b
|
Leszek Koltunski
|
int width = (int)(mWidth*DEBUG_SCR_FRAC);
|
226 |
|
|
int height = (int)(mWidth*DEBUG_SCR_FRAC*DEBUG_FRAC);
|
227 |
|
|
int gap = 5;
|
228 |
|
|
int textColor = 0xffffffff;
|
229 |
|
|
int backColor = 0xff000000;
|
230 |
|
|
|
231 |
|
|
showFPS(width,height,gap,textColor,backColor);
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235 |
|
|
/**
|
236 |
|
|
* Make the library show Frames Per Second in the upper-left corner.
|
237 |
|
|
* Set appropriate params.
|
238 |
|
|
* <p>
|
239 |
|
|
*/
|
240 |
|
|
public void showFPS(int width, int height, int gap, int textColor, int backColor)
|
241 |
|
|
{
|
242 |
|
|
mDebugWidth = width;
|
243 |
|
|
mDebugHeight = height;
|
244 |
|
|
mDebugGap = gap;
|
245 |
|
|
mDebugBackColor = backColor;
|
246 |
|
|
mDebugTextColor = textColor;
|
247 |
|
|
|
248 |
b8f8ef5c
|
Leszek Koltunski
|
if( mDebugMode!=DEBUG_MODE_FPS )
|
249 |
01b9a241
|
Leszek Koltunski
|
{
|
250 |
b8f8ef5c
|
Leszek Koltunski
|
mDebugMode = DEBUG_MODE_FPS;
|
251 |
01b9a241
|
Leszek Koltunski
|
|
252 |
|
|
durations = new long[NUM_FRAMES + 1];
|
253 |
|
|
currDuration = 0;
|
254 |
|
|
|
255 |
7bebb196
|
Leszek Koltunski
|
for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16; // Assume FPS will be
|
256 |
|
|
durations[NUM_FRAMES] = NUM_FRAMES * 16; // close to 1000/16 ~ 60
|
257 |
01b9a241
|
Leszek Koltunski
|
}
|
258 |
c5369f1b
|
leszek
|
}
|
259 |
b8f8ef5c
|
Leszek Koltunski
|
|
260 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261 |
|
|
/**
|
262 |
|
|
* Make the library show current frame number in the upper-left corner.
|
263 |
|
|
* <p>
|
264 |
|
|
*/
|
265 |
|
|
public void showFrameNumber()
|
266 |
|
|
{
|
267 |
|
|
if( mDebugMode!=DEBUG_MODE_FRAME )
|
268 |
|
|
{
|
269 |
|
|
mDebugMode = DEBUG_MODE_FRAME;
|
270 |
|
|
frameNumber = 0;
|
271 |
|
|
}
|
272 |
|
|
}
|
273 |
9b94626c
|
Leszek Koltunski
|
}
|