Project

General

Profile

Download (7.08 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ c90aca24

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 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
28 806ca386 leszek
29 81b1577b Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30 e1e94682 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
31 86e99907 leszek
import org.distorted.library.type.Static3D;
32
33 c5369f1b leszek
/**
34
 * Class which represents the Screen.
35
 * <p>
36
 * User is able to render to it just like to a DistortedFramebuffer.
37
 */
38 040cd18c Leszek Koltunski
public class DistortedScreen extends DistortedFramebuffer
39 c5369f1b leszek
  {
40 86e99907 leszek
  ///// DEBUGGING ONLY /////////////////////////
41
  private static final int NUM_FRAMES  = 100;
42 4aa38649 Leszek Koltunski
  private static final int FPS_W       = 120;
43
  private static final int FPS_H       =  70;
44
45
  private boolean mShowFPS;
46 86e99907 leszek
47 e1e94682 Leszek Koltunski
  private MeshQuad fpsMesh;
48 86e99907 leszek
  private DistortedTexture fpsTexture;
49
  private DistortedEffects fpsEffects;
50
  private Canvas fpsCanvas;
51
  private Bitmap fpsBitmap;
52
  private Paint mPaint;
53 040cd18c Leszek Koltunski
  private String fpsString;
54 86e99907 leszek
  private long lastTime=0;
55
  private long[] durations;
56
  private int currDuration;
57 7bebb196 Leszek Koltunski
  private static Static3D mMoveVector = new Static3D(5,-5,0);
58
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove(mMoveVector);
59 040cd18c Leszek Koltunski
  ///// END DEBUGGING //////////////////////////
60 86e99907 leszek
61 9b94626c Leszek Koltunski
  private int mCurRenderedFBO;    // During the first FBO_QUEUE_SIZE frames, we blit the very first
62
  private int mToBeBlittedFBO;    // FBO one we have rendered. Then, we keep blitting the one we
63
  private boolean mFirstCircle;   // rendered FBO_QUEUE_SIZE ago.
64 9d845904 Leszek Koltunski
65 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
66 040cd18c Leszek Koltunski
// PUBLIC API
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
/**
69
 * Create a new Screen. Initially 1x1 in size.
70
 * <p>
71
 * Has to be followed by a 'resize()' to set the size.
72
 */
73
  public DistortedScreen()
74
    {
75 7602a827 Leszek Koltunski
    super(DistortedLibrary.FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
76 040cd18c Leszek Koltunski
    mShowFPS = false;
77 9b94626c Leszek Koltunski
    mCurRenderedFBO = 0;
78
    mToBeBlittedFBO = 0;
79
    mFirstCircle = true;
80 040cd18c Leszek Koltunski
    }
81 c5369f1b leszek
82 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
83 040cd18c Leszek Koltunski
/**
84
 * Draws all the attached children to this OutputSurface.
85
 * <p>
86
 * Must be called from a thread holding OpenGL Context.
87
 *
88
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
89
 * @return Number of objects rendered.
90
 */
91
  public int render(long time)
92
    {
93
    if( mShowFPS )
94
      {
95
      if( lastTime==0 ) lastTime = time;
96 26a4e5f6 leszek
97 040cd18c Leszek Koltunski
      currDuration++;
98
      if (currDuration >= NUM_FRAMES) currDuration = 0;
99
      durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
100
      durations[currDuration] = time - lastTime;
101
102
      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
103
104
      mPaint.setColor(0xffffffff);
105 4aa38649 Leszek Koltunski
      fpsCanvas.drawRect(0, 0, FPS_W, FPS_H, mPaint);
106 040cd18c Leszek Koltunski
      mPaint.setColor(0xff000000);
107 6bce98fc Leszek Koltunski
      fpsCanvas.drawText(fpsString, 0.5f*FPS_W, 0.75f*FPS_H, mPaint);
108 040cd18c Leszek Koltunski
      fpsTexture.setTexture(fpsBitmap);
109
110 ece89b28 Leszek Koltunski
      mMoveVector.set1(mHeight-FPS_H-5);
111 7bebb196 Leszek Koltunski
112 040cd18c Leszek Koltunski
      lastTime = time;
113
      }
114
115 9b94626c Leszek Koltunski
    int numrender = super.render(time,mCurRenderedFBO);
116 040cd18c Leszek Koltunski
117
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
118 2301cb2f Leszek Koltunski
119 7602a827 Leszek Koltunski
    // workaround for the Mali issues: blit the framebuffer we have computed DistortedLibrary.FBO_QUEUE_SIZE
120 2301cb2f Leszek Koltunski
    // frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
121
    // make it equal to 3.
122
    // This of course introduces a delay and uses more memory, but it does not appear to have any effect
123
    // on speed. Maybe a slight positive effect if any!
124 9b94626c Leszek Koltunski
    setAsInput(mToBeBlittedFBO,0);
125 2301cb2f Leszek Koltunski
126 040cd18c Leszek Koltunski
    GLES31.glColorMask(true,true,true,true);
127
    GLES31.glDepthMask(false);
128
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
129
    GLES31.glDisable(GLES31.GL_DEPTH_TEST);
130
    GLES31.glDisable(GLES31.GL_BLEND);
131
132 7602a827 Leszek Koltunski
    DistortedLibrary.blitPriv(this);
133 040cd18c Leszek Koltunski
134
    if( mShowFPS && fpsTexture.setAsInput())
135
      {
136 c90aca24 Leszek Koltunski
      DistortedLibrary.drawPriv(fpsEffects, fpsMesh, this, time);
137 040cd18c Leszek Koltunski
      }
138
139 7602a827 Leszek Koltunski
    if( ++mCurRenderedFBO>= DistortedLibrary.FBO_QUEUE_SIZE )
140 9b94626c Leszek Koltunski
      {
141
      mCurRenderedFBO = 0;
142
      if (mFirstCircle) mFirstCircle = false;
143
      }
144 7602a827 Leszek Koltunski
    if( !mFirstCircle && ++mToBeBlittedFBO>= DistortedLibrary.FBO_QUEUE_SIZE )
145 9b94626c Leszek Koltunski
      {
146
      mToBeBlittedFBO=0;
147
      }
148
149 040cd18c Leszek Koltunski
    return numrender+1;
150
    }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
/**
154
 * Make the library show Frames Per Second in the upper-left corner.
155
 * <p>
156
 */
157
  public void showFPS()
158 26a4e5f6 leszek
    {
159 01b9a241 Leszek Koltunski
    if( !mShowFPS )
160
      {
161
      mShowFPS = true;
162
      fpsString = "";
163 4aa38649 Leszek Koltunski
      fpsBitmap = Bitmap.createBitmap(FPS_W, FPS_H, Bitmap.Config.ARGB_8888);
164 e1e94682 Leszek Koltunski
      fpsMesh = new MeshQuad();
165 c90aca24 Leszek Koltunski
      fpsTexture = new DistortedTexture();
166 01b9a241 Leszek Koltunski
      fpsTexture.setTexture(fpsBitmap);
167
      fpsCanvas = new Canvas(fpsBitmap);
168 c90aca24 Leszek Koltunski
      fpsEffects = new DistortedEffects(FPS_W,FPS_H,0);
169 01b9a241 Leszek Koltunski
      fpsEffects.apply(mMoveEffect);
170
171
      mPaint = new Paint();
172
      mPaint.setAntiAlias(true);
173
      mPaint.setTextAlign(Paint.Align.CENTER);
174 4aa38649 Leszek Koltunski
      mPaint.setTextSize(0.7f * FPS_H);
175 01b9a241 Leszek Koltunski
176
      durations = new long[NUM_FRAMES + 1];
177
      currDuration = 0;
178
179 7bebb196 Leszek Koltunski
      for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;  // Assume FPS will be
180
      durations[NUM_FRAMES] = NUM_FRAMES * 16;               // close to 1000/16 ~ 60
181 01b9a241 Leszek Koltunski
      }
182 c5369f1b leszek
    }
183 9b94626c Leszek Koltunski
  }