Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ 7a5e538a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
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
package org.distorted.library.main;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLES31;
28

    
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.mesh.MeshBase;
31
import org.distorted.library.mesh.MeshFlat;
32
import org.distorted.library.type.Static3D;
33

    
34
/**
35
 * Class which represents the Screen.
36
 * <p>
37
 * User is able to render to it just like to a DistortedFramebuffer.
38
 */
39
public class DistortedScreen extends DistortedFramebuffer
40
  {
41
  ///// DEBUGGING ONLY /////////////////////////
42
  private boolean mShowFPS;
43

    
44
  private static final int NUM_FRAMES  = 100;
45

    
46
  private MeshBase fpsMesh;
47
  private DistortedTexture fpsTexture;
48
  private DistortedEffects fpsEffects;
49
  private Canvas fpsCanvas;
50
  private Bitmap fpsBitmap;
51
  private Paint mPaint;
52
  private int fpsH, fpsW;
53
  private String fpsString;
54
  private long lastTime=0;
55
  private long[] durations;
56
  private int currDuration;
57
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
58
  ///// END DEBUGGING //////////////////////////
59

    
60
  private int mCurRenderedFBO;    // During the first FBO_QUEUE_SIZE frames, we blit the very first
61
  private int mToBeBlittedFBO;    // FBO one we have rendered. Then, we keep blitting the one we
62
  private boolean mFirstCircle;   // rendered FBO_QUEUE_SIZE ago.
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
// PUBLIC API
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
/**
68
 * Create a new Screen. Initially 1x1 in size.
69
 * <p>
70
 * Has to be followed by a 'resize()' to set the size.
71
 */
72
  public DistortedScreen()
73
    {
74
    super(Distorted.FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
75
    mShowFPS = false;
76
    mCurRenderedFBO = 0;
77
    mToBeBlittedFBO = 0;
78
    mFirstCircle = true;
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
/**
83
 * Draws all the attached children to this OutputSurface.
84
 * <p>
85
 * Must be called from a thread holding OpenGL Context.
86
 *
87
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
88
 * @return Number of objects rendered.
89
 */
90
  public int render(long time)
91
    {
92
    if( mShowFPS )
93
      {
94
      if( lastTime==0 ) lastTime = time;
95

    
96
      currDuration++;
97
      if (currDuration >= NUM_FRAMES) currDuration = 0;
98
      durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
99
      durations[currDuration] = time - lastTime;
100

    
101
      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
102

    
103
      mPaint.setColor(0xffffffff);
104
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
105
      mPaint.setColor(0xff000000);
106
      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
107
      fpsTexture.setTexture(fpsBitmap);
108

    
109
      lastTime = time;
110
      }
111

    
112
    int numrender = super.render(time,mCurRenderedFBO);
113

    
114
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
115

    
116
    // workaround for the Mali issues: blit the framebuffer we have computed Distorted.FBO_QUEUE_SIZE
117
    // frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
118
    // make it equal to 3.
119
    // This of course introduces a delay and uses more memory, but it does not appear to have any effect
120
    // on speed. Maybe a slight positive effect if any!
121
    setAsInput(mToBeBlittedFBO,0);
122

    
123
    GLES31.glColorMask(true,true,true,true);
124
    GLES31.glDepthMask(false);
125
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
126
    GLES31.glDisable(GLES31.GL_DEPTH_TEST);
127
    GLES31.glDisable(GLES31.GL_BLEND);
128

    
129
    DistortedEffects.blitPriv(this);
130

    
131
    if( mShowFPS && fpsTexture.setAsInput())
132
      {
133
      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time);
134
      }
135

    
136
    if( ++mCurRenderedFBO>=Distorted.FBO_QUEUE_SIZE )
137
      {
138
      mCurRenderedFBO = 0;
139
      if (mFirstCircle) mFirstCircle = false;
140
      }
141
    if( !mFirstCircle && ++mToBeBlittedFBO>=Distorted.FBO_QUEUE_SIZE )
142
      {
143
      mToBeBlittedFBO=0;
144
      }
145

    
146
    return numrender+1;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
/**
151
 * Make the library show Frames Per Second in the upper-left corner.
152
 * <p>
153
 */
154
  public void showFPS()
155
    {
156
    if( !mShowFPS )
157
      {
158
      mShowFPS = true;
159

    
160
      fpsW = 120;
161
      fpsH = 70;
162

    
163
      fpsString = "";
164
      fpsBitmap = Bitmap.createBitmap(fpsW, fpsH, Bitmap.Config.ARGB_8888);
165
      fpsMesh = new MeshFlat(1, 1);
166
      fpsTexture = new DistortedTexture(fpsW, fpsH);
167
      fpsTexture.setTexture(fpsBitmap);
168
      fpsCanvas = new Canvas(fpsBitmap);
169
      fpsEffects = new DistortedEffects();
170
      fpsEffects.apply(mMoveEffect);
171

    
172
      mPaint = new Paint();
173
      mPaint.setAntiAlias(true);
174
      mPaint.setTextAlign(Paint.Align.CENTER);
175
      mPaint.setTextSize(0.7f * fpsH);
176

    
177
      durations = new long[NUM_FRAMES + 1];
178
      currDuration = 0;
179

    
180
      for (int i = 0; i < NUM_FRAMES + 1; i++) durations[i] = 16;  // Assume FPS will be
181
      durations[NUM_FRAMES] = NUM_FRAMES * 16;              // close to 1000/16 ~ 60
182
      }
183
    }
184
  }
(10-10/17)