Project

General

Profile

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

library / src / main / java / org / distorted / library / main / Distorted.java @ 973e99d7

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 30beb34f Leszek Koltunski
import android.app.ActivityManager;
23 7845dc66 Leszek Koltunski
import android.content.Context;
24 30beb34f Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 d6e94c84 Leszek Koltunski
import android.content.res.Resources;
26 edea9cf3 Leszek Koltunski
import android.opengl.GLES31;
27 310e14fb leszek
28 26a4e5f6 leszek
import org.distorted.library.effect.Effect;
29 7cd24173 leszek
import org.distorted.library.effect.FragmentEffect;
30 aa2f0486 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffect;
31 7cd24173 leszek
import org.distorted.library.effect.VertexEffect;
32 ed06301f Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
33 6a06a912 Leszek Koltunski
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
/**
36
 * A singleton class used to control various global settings.
37
 */
38
public class Distorted 
39 39cbf9dc Leszek Koltunski
  {
40 c25273e0 Leszek Koltunski
  public static final int GLSL = 310;
41
  public static final String GLSL_VERSION= "#version 310 es\n";
42 6a06a912 Leszek Koltunski
  /**
43 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
44
   * backing up our DistortedTexture.
45 6a06a912 Leszek Koltunski
   * <p>
46 cacc63de Leszek Koltunski
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
47 6a06a912 Leszek Koltunski
   * apply different effects. Used in the copy constructor.
48
   */
49 29a06526 Leszek Koltunski
  public static final int CLONE_SURFACE = 0x1;
50 6a06a912 Leszek Koltunski
  /**
51 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
52 6a06a912 Leszek Koltunski
   * <p>
53 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
54 6a06a912 Leszek Koltunski
   */
55 015642fb Leszek Koltunski
  public static final int CLONE_MATRIX = 0x2;
56 6a06a912 Leszek Koltunski
  /**
57 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
58 6a06a912 Leszek Koltunski
   * <p>
59 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
60 6a06a912 Leszek Koltunski
   */
61
  public static final int CLONE_VERTEX  = 0x4;
62
  /**
63 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
64 6a06a912 Leszek Koltunski
   * <p>
65 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
66 6a06a912 Leszek Koltunski
   */
67
  public static final int CLONE_FRAGMENT= 0x8;
68 d6e94c84 Leszek Koltunski
   /**
69
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
70
   * <p>
71
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
72
   */
73
  public static final int CLONE_POSTPROCESS= 0x10;
74 6a06a912 Leszek Koltunski
  /**
75 a09ada4c Leszek Koltunski
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
76 6a06a912 Leszek Koltunski
   * <p>
77 cacc63de Leszek Koltunski
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
78
   * on the screen with (optionally) different Effects.
79 6a06a912 Leszek Koltunski
   */
80 d6e94c84 Leszek Koltunski
  public static final int CLONE_CHILDREN= 0x20;
81 b3618cb5 Leszek Koltunski
82 586b5fa1 Leszek Koltunski
  /**
83
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
84
   * of FBO_QUEUE_SIZE FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
85
   * might be missing part of the Objects)
86 6672d895 Leszek Koltunski
   *
87 12f9e4bb Leszek Koltunski
   * This bug only exists on Mali driver r12.
88 6672d895 Leszek Koltunski
   *
89
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
90 586b5fa1 Leszek Koltunski
   */
91 973e99d7 Leszek Koltunski
  static final int FBO_QUEUE_SIZE = 4;
92 86c352e0 Leszek Koltunski
  /**
93 a13dde77 Leszek Koltunski
   * Number of Main program variants (ATM 3: MAIN, MAIN OIT, PREPROCESS)
94 86c352e0 Leszek Koltunski
   */
95 973e99d7 Leszek Koltunski
  static final int MAIN_VARIANTS = 3;
96 586b5fa1 Leszek Koltunski
97 55c14a19 Leszek Koltunski
  private static boolean mInitialized=false;
98 6a06a912 Leszek Koltunski
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100 c638c1b0 Leszek Koltunski
// private: hide this from Javadoc
101 6a06a912 Leszek Koltunski
102
  private Distorted()
103
    {
104 8eccf334 Leszek Koltunski
105 6a06a912 Leszek Koltunski
    }
106
107 edea9cf3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
109
// PowerVR GE8100 compiler fails to compile OIT programs.
110
111
  static void detectBuggyDrivers()
112
    {
113
    String vendor  = GLES31.glGetString(GLES31.GL_VENDOR);
114
    String version = GLES31.glGetString(GLES31.GL_VERSION);
115
    String renderer= GLES31.glGetString(GLES31.GL_RENDERER);
116
117
    /*
118
    android.util.Log.e("DISTORTED", "GLSL Version "+GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
119
    android.util.Log.e("DISTORTED", "GL Version "  +GLES31.glGetString(GLES31.GL_VERSION));
120
    android.util.Log.e("DISTORTED", "GL Vendor "   +GLES31.glGetString(GLES31.GL_VENDOR));
121
    android.util.Log.e("DISTORTED", "GL Renderer " +GLES31.glGetString(GLES31.GL_RENDERER));
122
    */
123
124
    if( vendor.contains("ARM") )
125
      {
126
      if( version.contains("r12") )
127
        {
128 c1a38ba3 Leszek Koltunski
        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.");
129 edea9cf3 Leszek Koltunski
        }
130
      }
131
    else if( vendor.contains("Imagination") )
132
      {
133
      if( renderer.contains("GE8") )
134
        {
135 c1a38ba3 Leszek Koltunski
        android.util.Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
136 edea9cf3 Leszek Koltunski
        }
137
      }
138
    }
139
140 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
141 310e14fb leszek
/**
142
 * Have we called onCreate yet, ie have we initialized the library?
143 1dfc9074 leszek
 * @return <code>true</code> if the library is initialized and ready for action.
144 310e14fb leszek
 */
145
  public static boolean isInitialized()
146 421c2728 Leszek Koltunski
    {
147 55c14a19 Leszek Koltunski
    return mInitialized;
148 421c2728 Leszek Koltunski
    }
149
150 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/**
152
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
153 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
154 6a06a912 Leszek Koltunski
 * <p>
155 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
156 6a06a912 Leszek Koltunski
 *   
157 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
158 6a06a912 Leszek Koltunski
 */
159 edea9cf3 Leszek Koltunski
  public static void onCreate(final Context context) throws Exception
160 d6e94c84 Leszek Koltunski
    {
161 30beb34f Leszek Koltunski
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
162
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
163
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
164 edea9cf3 Leszek Koltunski
165
    mInitialized = true;
166
167
    detectBuggyDrivers();
168
169 0e924ba5 Leszek Koltunski
    EffectMessageSender.startSending();
170
171 d6e94c84 Leszek Koltunski
    final Resources resources = context.getResources();
172 c90b9e01 Leszek Koltunski
173 edea9cf3 Leszek Koltunski
    Exception exception=null;
174
175
    try
176
      {
177
      DistortedEffects.createPrograms(resources);
178
      }
179
    catch(Exception ex)
180
      {
181
      exception = ex;
182
      }
183
184
    try
185
      {
186
      DistortedEffects.createProgramsOIT(resources);
187
      }
188
    catch(Exception ex)
189
      {
190
      exception = ex;
191
      }
192
193 a13dde77 Leszek Koltunski
    try
194
      {
195
      EffectQueuePostprocess.createPrograms(resources);
196
      }
197
    catch(Exception ex)
198
      {
199
      exception = ex;
200
      }
201
202 edea9cf3 Leszek Koltunski
    try
203
      {
204
      PostprocessEffect.createPrograms();
205
      }
206
    catch(Exception ex)
207
      {
208
      exception = ex;
209
      }
210
211
    if( exception!=null)
212
      {
213
      throw exception;
214
      }
215 6a06a912 Leszek Koltunski
    }
216
217 05ecc6fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
/**
219 78e89fb5 Leszek Koltunski
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
220 05ecc6fe Leszek Koltunski
 * Must be called from Activity.onPause().
221
 */
222
  public static void onPause()
223
    {
224 226144d0 leszek
    DistortedObject.onPause();
225 78e89fb5 Leszek Koltunski
    DistortedEffects.onPause();
226 05ecc6fe Leszek Koltunski
    }
227
228 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229
/**
230
 * Call this so that the Library can release its internal data structures.
231
 * Must be called from Activity.onDestroy(). 
232
 */
233
  public static void onDestroy()
234
    {
235 226144d0 leszek
    DistortedObject.onDestroy();
236 a09ada4c Leszek Koltunski
    DistortedNode.onDestroy();
237 421c2728 Leszek Koltunski
    DistortedEffects.onDestroy();
238 efe3d8fe leszek
    DistortedMaster.onDestroy();
239 ce154014 Leszek Koltunski
    DistortedOutputSurface.onDestroy();
240 42571056 Leszek Koltunski
    EffectQueue.onDestroy();
241 26a4e5f6 leszek
    Effect.onDestroy();
242 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
243 f8686932 Leszek Koltunski
244 55c14a19 Leszek Koltunski
    mInitialized = false;
245 6a06a912 Leszek Koltunski
    }
246 f8f6d457 leszek
  }