Project

General

Profile

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

library / src / main / java / org / distorted / library / main / Distorted.java @ c1a38ba3

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
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.opengl.GLES31;
27

    
28
import org.distorted.library.effect.Effect;
29
import org.distorted.library.effect.FragmentEffect;
30
import org.distorted.library.effect.PostprocessEffect;
31
import org.distorted.library.effect.VertexEffect;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * A singleton class used to control various global settings.
36
 */
37
public class Distorted 
38
  {
39
  public static final int GLSL = 310;
40
  public static final String GLSL_VERSION= "#version 310 es\n";
41
  /**
42
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
43
   * backing up our DistortedTexture.
44
   * <p>
45
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
46
   * apply different effects. Used in the copy constructor.
47
   */
48
  public static final int CLONE_SURFACE = 0x1;
49
  /**
50
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
51
   * <p>
52
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
53
   */
54
  public static final int CLONE_MATRIX = 0x2;
55
  /**
56
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
57
   * <p>
58
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
59
   */
60
  public static final int CLONE_VERTEX  = 0x4;
61
  /**
62
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
63
   * <p>
64
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
65
   */
66
  public static final int CLONE_FRAGMENT= 0x8;
67
   /**
68
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
69
   * <p>
70
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
71
   */
72
  public static final int CLONE_POSTPROCESS= 0x10;
73
  /**
74
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
75
   * <p>
76
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
77
   * on the screen with (optionally) different Effects.
78
   */
79
  public static final int CLONE_CHILDREN= 0x20;
80

    
81
  /**
82
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
83
   * of FBO_QUEUE_SIZE FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
84
   * might be missing part of the Objects)
85
   *
86
   * This bug only exists on Mali driver r12. TODO: on other platforms, make this equal to 1.
87
   *
88
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
89
   */
90
  public static final int FBO_QUEUE_SIZE = 4;
91

    
92
  private static boolean mInitialized=false;
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
// private: hide this from Javadoc
96

    
97
  private Distorted()
98
    {
99

    
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
104
// PowerVR GE8100 compiler fails to compile OIT programs.
105

    
106
  static void detectBuggyDrivers()
107
    {
108
    String vendor  = GLES31.glGetString(GLES31.GL_VENDOR);
109
    String version = GLES31.glGetString(GLES31.GL_VERSION);
110
    String renderer= GLES31.glGetString(GLES31.GL_RENDERER);
111

    
112
    /*
113
    android.util.Log.e("DISTORTED", "GLSL Version "+GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
114
    android.util.Log.e("DISTORTED", "GL Version "  +GLES31.glGetString(GLES31.GL_VERSION));
115
    android.util.Log.e("DISTORTED", "GL Vendor "   +GLES31.glGetString(GLES31.GL_VENDOR));
116
    android.util.Log.e("DISTORTED", "GL Renderer " +GLES31.glGetString(GLES31.GL_RENDERER));
117
    */
118

    
119
    if( vendor.contains("ARM") )
120
      {
121
      if( version.contains("r12") )
122
        {
123
        android.util.Log.e("DISTORTED", "You are running this on a ARM Mali driver r12.\nThis is a buggy driver, please update to r22. Problems with flashing expected.");
124
        }
125
      }
126
    else if( vendor.contains("Imagination") )
127
      {
128
      if( renderer.contains("GE8") )
129
        {
130
        android.util.Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
131
        }
132
      }
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
/**
137
 * Have we called onCreate yet, ie have we initialized the library?
138
 * @return <code>true</code> if the library is initialized and ready for action.
139
 */
140
  public static boolean isInitialized()
141
    {
142
    return mInitialized;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
/**
147
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
148
 * I.e. best called from GLSurfaceView.onCreate().
149
 * <p>
150
 * Needs to be called from a thread holding the OpenGL context.
151
 *   
152
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
153
 */
154
  public static void onCreate(final Context context) throws Exception
155
    {
156
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
157
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
158
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
159

    
160
    mInitialized = true;
161

    
162
    detectBuggyDrivers();
163

    
164
    EffectMessageSender.startSending();
165

    
166
    final Resources resources = context.getResources();
167

    
168
    Exception exception=null;
169

    
170
    try
171
      {
172
      DistortedEffects.createPrograms(resources);
173
      }
174
    catch(Exception ex)
175
      {
176
      exception = ex;
177
      }
178

    
179
    try
180
      {
181
      DistortedEffects.createProgramsOIT(resources);
182
      }
183
    catch(Exception ex)
184
      {
185
      exception = ex;
186
      }
187

    
188
    try
189
      {
190
      PostprocessEffect.createPrograms();
191
      }
192
    catch(Exception ex)
193
      {
194
      exception = ex;
195
      }
196

    
197
    if( exception!=null)
198
      {
199
      throw exception;
200
      }
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
/**
205
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
206
 * Must be called from Activity.onPause().
207
 */
208
  public static void onPause()
209
    {
210
    DistortedObject.onPause();
211
    DistortedNode.onPause();
212
    DistortedEffects.onPause();
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
/**
217
 * Call this so that the Library can release its internal data structures.
218
 * Must be called from Activity.onDestroy(). 
219
 */
220
  public static void onDestroy()
221
    {
222
    DistortedObject.onDestroy();
223
    DistortedNode.onDestroy();
224
    DistortedEffects.onDestroy();
225
    DistortedMaster.onDestroy();
226
    DistortedOutputSurface.onDestroy();
227
    EffectQueue.onDestroy();
228
    Effect.onDestroy();
229
    VertexEffect.onDestroy();
230
    FragmentEffect.onDestroy();
231
    EffectMessageSender.stopSending();
232

    
233
    mInitialized = false;
234
    }
235
  }
(1-1/21)