GLCanvas.java 33.9 KB
Newer Older
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
1 2 3 4
package com.projectseptember.RNGL;

import static android.opengl.GLES20.*;

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
5
import android.graphics.Bitmap;
6
import android.graphics.Color;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
7
import android.graphics.Matrix;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
8
import android.graphics.PixelFormat;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
9
import android.net.Uri;
10
import android.opengl.GLException;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
11
import android.opengl.GLSurfaceView;
12
import android.util.Base64;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
13
import android.util.DisplayMetrics;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
14 15
import android.util.Log;
import android.view.View;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
16 17
import android.view.ViewGroup;

18
import com.facebook.imagepipeline.core.ExecutorSupplier;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
19 20 21 22 23 24
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableMap;
25 26
import com.facebook.react.uimanager.PointerEvents;
import com.facebook.react.uimanager.ReactPointerEventsView;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
27 28 29
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;

30
import java.io.ByteArrayOutputStream;
31
import java.io.FileOutputStream;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
32 33 34 35 36 37
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
38
import java.util.HashSet;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
39 40 41 42
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
43
import java.util.Set;
44
import java.util.concurrent.Executor;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
45 46 47 48

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

49 50
public class GLCanvas extends GLSurfaceView
        implements GLSurfaceView.Renderer, Executor, ReactPointerEventsView {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
51 52 53

    private ReactContext reactContext;
    private RNGLContext rnglContext;
54 55
    private boolean dirtyOnLoad = true;
    private boolean neverRendered = true;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
56 57
    private boolean deferredRendering = false;
    private GLRenderData renderData;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
58
    private int defaultFBO;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
59 60 61 62

    private int nbContentTextures;
    private boolean autoRedraw;
    private GLData data;
63
    private List<Uri> imagesToPreload = new ArrayList<>();
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
64
    private List<Uri> preloaded = new ArrayList<>();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
65

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
66
    private Map<Uri, GLImage> images = new HashMap<>();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
67
    private List<GLTexture> contentTextures = new ArrayList<>();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
68
    private List<Bitmap> contentBitmaps = new ArrayList<>();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
69

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
70 71
    private Map<Integer, GLShader> shaders;
    private Map<Integer, GLFBO> fbos;
72 73
    private ExecutorSupplier executorSupplier;
    private final Queue<Runnable> mRunOnDraw = new LinkedList<>();
74 75

    private List<CaptureConfig> captureConfigs = new ArrayList<>();
76 77 78
    private float pixelRatio;

    private float displayDensity;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
79

80
    public GLCanvas(ThemedReactContext context, ExecutorSupplier executorSupplier) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
81 82
        super(context);
        reactContext = context;
83
        this.executorSupplier = executorSupplier;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
84 85
        rnglContext = context.getNativeModule(RNGLContext.class);
        setEGLContextClientVersion(2);
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
86

87 88 89 90
        DisplayMetrics dm = reactContext.getResources().getDisplayMetrics();
        displayDensity = dm.density;
        pixelRatio = dm.density;

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
91
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
92
        getHolder().setFormat(PixelFormat.RGB_888);
93
        setZOrderOnTop(false);
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
94

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
95 96 97
        setRenderer(this);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
98

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
99 100 101 102 103
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        syncContentBitmaps();
        requestRender();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
104 105
    }

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
106 107
    public GLFBO getFBO (Integer id) {
        if (!fbos.containsKey(id)) {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
108
            fbos.put(id, new GLFBO(this));
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
109 110 111 112 113 114 115 116
        }
        return fbos.get(id);
    }

    public GLShader getShader (Integer id) {
        if (!shaders.containsKey(id)) {
            GLShaderData shaderData = rnglContext.getShader(id);
            if (shaderData == null) return null;
117
            shaders.put(id, new GLShader(shaderData, id, rnglContext));
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
118 119 120 121
        }
        return shaders.get(id);
    }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
122 123
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
124 125
        fbos = new HashMap<>();
        shaders = new HashMap<>();
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
126 127 128 129 130
        images = new HashMap<>();
        contentTextures = new ArrayList<>();
        contentBitmaps = new ArrayList<>();
        renderData = null;
        requestSyncData();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
131 132 133
    }

    @Override
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
134
    public void onSurfaceChanged(GL10 gl, int width, int height) {}
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
135

136 137 138 139 140 141
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        syncSize(w, h, pixelRatio);
    }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
142 143 144 145
    @Override
    public void onDrawFrame(GL10 gl) {
        runAll(mRunOnDraw);

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
146 147 148
        if (contentTextures.size() != this.nbContentTextures)
            resizeUniformContentTextures(nbContentTextures);

149 150 151 152 153 154
        if (haveRemainingToPreload()) {
            if (neverRendered) {
                neverRendered = false;
                glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
                glClear(GL_COLOR_BUFFER_BIT);
            }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
155 156
            return;
        }
157
        neverRendered = false;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
158

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
159 160 161 162 163 164 165 166 167 168
        final boolean shouldRenderNow = deferredRendering || autoRedraw || nbContentTextures == 0;
        if (nbContentTextures > 0) {
            reactContext.runOnUiQueueThread(new Runnable() {
                public void run() {
                    syncContentBitmaps();
                    if (!deferredRendering) {
                        deferredRendering = true;
                        requestRender();
                    }
                }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
169 170
            });
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
171 172

        if (shouldRenderNow) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
173 174
            this.render();
            deferredRendering = false;
175 176
            if (captureConfigs.size() > 0) {
                capture(); // FIXME: maybe we should schedule this?
177
            }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
178 179 180
        }
    }

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    private void capture () {
        Bitmap capture = createSnapshot();
        ReactContext reactContext = (ReactContext)getContext();
        RCTEventEmitter eventEmitter = reactContext.getJSModule(RCTEventEmitter.class);

        for (CaptureConfig config : captureConfigs) {
            String result = null, error = null;
            boolean isPng = config.type.equals("png");
            boolean isJpeg = !isPng && (config.type.equals("jpg")||config.type.equals("jpeg"));
            boolean isWebm = !isPng && !isJpeg && config.type.equals("webm");
            boolean isBase64 = config.format.equals("base64");
            boolean isFile = !isBase64 && config.format.equals("file");

            Bitmap.CompressFormat compressFormat =
                isPng ? Bitmap.CompressFormat.PNG :
                isJpeg ? Bitmap.CompressFormat.JPEG :
                isWebm ? Bitmap.CompressFormat.WEBP :
                null;

            int quality = (int)(100 * config.quality);

            if (compressFormat == null) {
                error = "Unsupported capture type '"+config.type+"'";
            }
            else if (isBase64) {
                try {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    capture.compress(compressFormat, quality, baos);
                    String frame = "data:image/png;base64,"+
                            Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
                    baos.close();
                    result = frame;
                }
                catch (Exception e) {
                    e.printStackTrace();
                    error = "Could not capture as base64: "+e.getMessage();
                }
            }
            else if (isFile) {
                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(config.filePath);
                    capture.compress(compressFormat, quality, fileOutputStream);
                    fileOutputStream.close();
                    result = "file://"+config.filePath;
                }
                catch (Exception e) {
                    e.printStackTrace();
                    error = "Could not write file: "+e.getMessage();
                }
            }
            else {
                error = "Unsupported capture format '"+config.format+"'";
            }

            WritableMap response = Arguments.createMap();
            response.putMap("config", config.toMap());
            if (error != null) response.putString("error", error);
            if (result != null) response.putString("result", result);
            eventEmitter.receiveEvent(getId(), "captureFrame", response);
        }

        captureConfigs = new ArrayList<>();
    }

245 246 247 248 249 250 251 252 253
    private boolean haveRemainingToPreload() {
        for (Uri uri: imagesToPreload) {
            if (!preloaded.contains(uri)) {
                return true;
            }
        }
        return false;
    }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
254 255
    public void setNbContentTextures(int n) {
        this.nbContentTextures = n;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
256
        requestRender();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
257 258 259
    }

    public void setRenderId(int renderId) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
260
        if (nbContentTextures > 0) {
261
            if (!haveRemainingToPreload()) syncContentBitmaps();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
262 263
            requestRender();
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
264 265
    }

266 267 268 269 270
    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);
        if (color == Color.TRANSPARENT) {
            this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
271 272
        }
        else {
273
            this.getHolder().setFormat(PixelFormat.RGB_888);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
274
        }
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
275
        this.requestRender();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
276 277 278 279 280 281 282 283 284
    }

    public void setAutoRedraw(boolean autoRedraw) {
        this.autoRedraw = autoRedraw;
        this.setRenderMode(autoRedraw ? GLSurfaceView.RENDERMODE_CONTINUOUSLY : GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    public void setData (GLData data) {
        this.data = data;
285
        renderData = null;
286
        if (!haveRemainingToPreload()) syncContentBitmaps();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
287
        requestSyncData();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
288 289 290
    }


Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
291 292 293
    public void setImagesToPreload (ReadableArray imagesToPreloadRA) {
        List<Uri> imagesToPreload = new ArrayList<>();
        for (int i=0; i<imagesToPreloadRA.size(); i++) {
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
294
            imagesToPreload.add(resolveSrc(imagesToPreloadRA.getMap(i).getString("uri")));
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
295
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
296
        this.imagesToPreload = imagesToPreload;
297
        requestSyncData();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
298 299 300 301
    }

    // Sync methods

302 303
    @Override
    public void execute (final Runnable runnable) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317
        synchronized (mRunOnDraw) {
            mRunOnDraw.add(runnable);
            requestRender();
        }
    }
    private void runAll(Queue<Runnable> queue) {
        synchronized (queue) {
            while (!queue.isEmpty()) {
                queue.poll().run();
            }
        }
    }

    public void requestSyncData () {
318
        execute(new Runnable() {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
319
            public void run() {
320
                // FIXME: maybe should set a flag so we don't do it twice??
321 322 323 324 325 326 327
                try {
                    if (!syncData())
                        requestSyncData();
                }
                catch (GLShaderCompilationFailed e) {
                    // This is ignored. It will be handled by RNGLContext.shaderFailedToCompile
                }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
328 329 330 331
            }
        });
    }

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    public static Bitmap captureView (View view) {
        int w = view.getWidth();
        int h = view.getHeight();
        if (w <= 0 || h <= 0)
            return Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
        Bitmap bitmap = view.getDrawingCache();
        if (bitmap == null)
            view.setDrawingCacheEnabled(true);
        bitmap = view.getDrawingCache();
        if (bitmap == null) {
            Log.e("GLCanvas", "view.getDrawingCache() is null. view="+view);
            return Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
        }
        Matrix matrix = new Matrix();
        matrix.postScale(1, -1);
        Bitmap reversed = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return reversed;
    }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
351 352 353 354 355 356 357 358
    /**
     * Snapshot the content views and save to contentBitmaps (must run in UI Thread)
     */
    public int syncContentBitmaps() {
        List<Bitmap> bitmaps = new ArrayList<>();
        ViewGroup parent = (ViewGroup) this.getParent();
        int count = parent == null ? 0 : parent.getChildCount() - 1;
        for (int i = 0; i < count; i++) {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
359 360 361 362 363 364 365 366 367 368
            View view = parent.getChildAt(i);
            if (view instanceof ViewGroup) {
                ViewGroup group = (ViewGroup) view;
                if (group.getChildCount() == 1) {
                    // If the content container only contain one other container,
                    // we will use it for rasterization. That way we screenshot without cropping.
                    view = group.getChildAt(0);
                }
            }
            bitmaps.add(captureView(view));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
369
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
370 371 372 373 374 375 376 377 378 379 380 381 382
        contentBitmaps = bitmaps;

        return count;
    }

    /**
     * Draw contentBitmaps to contentTextures (must run in GL Thread)
     */
    public int syncContentTextures() {
        int size = Math.min(contentTextures.size(), contentBitmaps.size());
        for (int i=0; i<size; i++)
            contentTextures.get(i).setPixels(contentBitmaps.get(i));
        return size;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
383 384 385 386 387 388 389 390 391 392
    }

    public void resizeUniformContentTextures (int n) {
        int length = contentTextures.size();
        if (length == n) return;
        if (n < length) {
            contentTextures = contentTextures.subList(0, n);
        }
        else {
            for (int i = contentTextures.size(); i < n; i++) {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
393
                contentTextures.add(new GLTexture(this));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
394 395 396 397 398 399 400
            }
        }
    }


    private int countPreloaded () {
        int nb = 0;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
401 402 403 404
        for (Uri toload: imagesToPreload) {
            if (preloaded.contains(toload)) {
                nb++;
            }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
405 406 407 408
        }
        return nb;
    }

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
409
    private void onImageLoad (Uri loaded) {
410 411 412 413 414 415 416
        preloaded.add(loaded);
        int count = countPreloaded();
        int total = imagesToPreload.size();
        double progress = ((double) count) / ((double) total);
        dispatchOnProgress(progress, count, total);
        dirtyOnLoad = true;
        requestSyncData();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
417 418
    }

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    public Uri resolveSrc (String src) {
        Uri uri = null;
        if (src != null) {
            try {
                uri = Uri.parse(src);
                // Verify scheme is set, so that relative uri (used by static resources) are not handled.
                if (uri.getScheme() == null) {
                    uri = null;
                }
            } catch (Exception e) {
                // ignore malformed uri, then attempt to extract resource ID.
            }
            if (uri == null) {
                uri = GLImage.getResourceDrawableUri(reactContext, src);
            }
        }
        return uri;
    }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
437

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
438
    public Uri srcResource (ReadableMap res) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
439 440 441 442
        String src = null;
        boolean isStatic = res.hasKey("isStatic") && res.getBoolean("isStatic");
        if (res.hasKey("path")) src = res.getString("path");
        if (src==null || isStatic) src = res.getString("uri");
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
443
        return resolveSrc(src);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
444 445
    }

446
    private GLRenderData recSyncData (GLData data, HashMap<Uri, GLImage> images) {
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
447
        Map<Uri, GLImage> prevImages = this.images;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
448

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
449
        GLShader shader = getShader(data.shader);
450
        if (shader == null || !shader.ensureCompile()) return null;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
451 452 453 454 455 456 457 458 459
        Map<String, Integer> uniformsInteger = new HashMap<>();
        Map<String, Float> uniformsFloat = new HashMap<>();
        Map<String, IntBuffer> uniformsIntBuffer = new HashMap<>();
        Map<String, FloatBuffer> uniformsFloatBuffer = new HashMap<>();
        Map<String,GLTexture> textures = new HashMap<>();
        List<GLRenderData> contextChildren = new ArrayList<>();
        List<GLRenderData> children = new ArrayList<>();

        for (GLData child: data.contextChildren) {
460 461 462
            GLRenderData node = recSyncData(child, images);
            if (node == null) return null;
            contextChildren.add(node);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
463 464 465
        }

        for (GLData child: data.children) {
466 467 468
            GLRenderData node = recSyncData(child, images);
            if (node == null) return null;
            children.add(node);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
469 470 471
        }

        Map<String, Integer> uniformTypes = shader.getUniformTypes();
472 473
        List<String> uniformNames = shader.getUniformNames();
        Map<String, Integer> uniformSizes = shader.getUniformSizes();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
474 475 476 477 478 479

        int units = 0;
        ReadableMapKeySetIterator iterator = data.uniforms.keySetIterator();
        while (iterator.hasNextKey()) {
            String uniformName = iterator.nextKey();
            int type = uniformTypes.get(uniformName);
480
            int size = uniformSizes.get(uniformName);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
481 482 483 484 485 486 487

            ReadableMap dataUniforms = data.uniforms;

            if (type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE) {
                uniformsInteger.put(uniformName, units++);

                if (dataUniforms.isNull(uniformName)) {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
488
                    GLTexture emptyTexture = new GLTexture(this);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
489 490 491 492
                    emptyTexture.setPixelsEmpty();
                    textures.put(uniformName, emptyTexture);
                }
                else {
493 494 495 496 497 498 499 500 501 502 503
                    ReadableMap value = null;
                    try {
                        value = dataUniforms.getMap(uniformName);
                    }
                    catch (Exception e) {
                        shader.runtimeException(
                        "texture uniform '"+uniformName+"': you cannot directly give require('./img.png') "+
                        "to gl-react, use resolveAssetSource(require('./img.png')) instead."
                        );
                        return null;
                    }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
504 505 506
                    String t = value.getString("type");
                    if (t.equals("content")) {
                        int id = value.getInt("id");
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
507 508 509
                        if (id >= contentTextures.size()) {
                            resizeUniformContentTextures(id+1);
                        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
510 511 512 513
                        textures.put(uniformName, contentTextures.get(id));
                    }
                    else if (t.equals("fbo")) {
                        int id = value.getInt("id");
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
514
                        GLFBO fbo = getFBO(id);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
515 516 517
                        textures.put(uniformName, fbo.color.get(0));
                    }
                    else if (t.equals("uri")) {
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
518 519
                        final Uri src = srcResource(value);
                        if (src == null) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
520
                            shader.runtimeException("texture uniform '"+uniformName+"': Invalid uri format '"+value+"'");
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
521 522 523 524 525 526 527 528 529
                        }

                        GLImage image = images.get(src);
                        if (image == null) {
                            image = prevImages.get(src);
                            if (image != null)
                                images.put(src, image);
                        }
                        if (image == null) {
530
                            image = new GLImage(this, executorSupplier.forDecode(), new Runnable() {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
531 532 533
                                public void run() {
                                    onImageLoad(src);
                                }
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
534
                            });
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
535 536 537 538 539 540
                            image.setSrc(src);
                            images.put(src, image);
                        }
                        textures.put(uniformName, image.getTexture());
                    }
                    else {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
541
                        shader.runtimeException("texture uniform '" + uniformName + "': Unexpected type '" + type + "'");
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
542 543 544 545
                    }
                }
            }
            else {
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
                if (size == 1) {
                    switch (type) {
                        case GL_INT:
                            uniformsInteger.put(uniformName, dataUniforms.getInt(uniformName));
                            break;

                        case GL_BOOL:
                            uniformsInteger.put(uniformName, dataUniforms.getBoolean(uniformName) ? 1 : 0);
                            break;

                        case GL_FLOAT:
                            uniformsFloat.put(uniformName, (float) dataUniforms.getDouble(uniformName));
                            break;

                        case GL_FLOAT_VEC2:
                        case GL_FLOAT_VEC3:
                        case GL_FLOAT_VEC4:
                        case GL_FLOAT_MAT2:
                        case GL_FLOAT_MAT3:
                        case GL_FLOAT_MAT4:
                            ReadableArray arr = dataUniforms.getArray(uniformName);
                            if (arraySizeForType(type) != arr.size()) {
                                shader.runtimeException(
                                        "uniform '"+uniformName+
                                                "': Invalid array size: "+arr.size()+
                                                ". Expected: "+arraySizeForType(type));
                            }
                            uniformsFloatBuffer.put(uniformName, parseAsFloatArray(arr));
                            break;

                        case GL_INT_VEC2:
                        case GL_INT_VEC3:
                        case GL_INT_VEC4:
                        case GL_BOOL_VEC2:
                        case GL_BOOL_VEC3:
                        case GL_BOOL_VEC4:
                            ReadableArray arr2 = dataUniforms.getArray(uniformName);
                            if (arraySizeForType(type) != arr2.size()) {
                                shader.runtimeException(
                                        "uniform '"+uniformName+
                                                "': Invalid array size: "+arr2.size()+
                                                ". Expected: "+arraySizeForType(type));
                            }
                            uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2));
                            break;

                        default:
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
593 594
                            shader.runtimeException(
                                    "uniform '"+uniformName+
595 596 597 598 599 600
                                            "': type not supported: "+type);
                    }
                }
                else {
                    ReadableArray array = dataUniforms.getArray(uniformName);
                    if (size != array.size()) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
601 602
                        shader.runtimeException(
                                "uniform '"+uniformName+
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
                                        "': Invalid array size: "+array.size()+
                                        ". Expected: "+size);
                    }
                    for (int i=0; i<size; i++) {
                        String name = uniformName+"["+i+"]";
                        switch (type) {
                            case GL_INT:
                                uniformsInteger.put(name, array.getInt(i));
                                break;

                            case GL_BOOL:
                                uniformsInteger.put(name, array.getBoolean(i) ? 1 : 0);
                                break;

                            case GL_FLOAT:
                                uniformsFloat.put(name, (float) array.getDouble(i));
                                break;

                            case GL_FLOAT_VEC2:
                            case GL_FLOAT_VEC3:
                            case GL_FLOAT_VEC4:
                            case GL_FLOAT_MAT2:
                            case GL_FLOAT_MAT3:
                            case GL_FLOAT_MAT4:
                                ReadableArray arr = array.getArray(i);
                                if (arraySizeForType(type) != arr.size()) {
                                    shader.runtimeException(
                                            "uniform '"+name+
                                                    "': Invalid array size: "+arr.size()+
                                                    ". Expected: "+arraySizeForType(type));
                                }
                                uniformsFloatBuffer.put(name, parseAsFloatArray(arr));
                                break;

                            case GL_INT_VEC2:
                            case GL_INT_VEC3:
                            case GL_INT_VEC4:
                            case GL_BOOL_VEC2:
                            case GL_BOOL_VEC3:
                            case GL_BOOL_VEC4:
                                ReadableArray arr2 = array.getArray(i);
                                if (arraySizeForType(type) != arr2.size()) {
                                    shader.runtimeException(
                                            "uniform '"+name+
                                                    "': Invalid array size: "+arr2.size()+
                                                    ". Expected: "+arraySizeForType(type));
                                }
                                uniformsIntBuffer.put(name, parseAsIntArray(arr2));
                                break;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
652

653 654 655 656 657 658 659
                            default:
                                shader.runtimeException(
                                        "uniform '"+name+
                                                "': type not supported: "+type);
                        }
                    }
                }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
660 661 662 663 664 665
            }
        }

        int[] maxTextureUnits = new int[1];
        glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, maxTextureUnits, 0);
        if (units > maxTextureUnits[0]) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
666
            shader.runtimeException("Maximum number of texture reach. got " + units + " >= max " + maxTextureUnits);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
667 668
        }

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
        for (String uniformName: uniformNames) {
            int size = uniformSizes.get(uniformName);
            if (size == 1) {
                if (!uniformsFloat.containsKey(uniformName) &&
                        !uniformsInteger.containsKey(uniformName) &&
                        !uniformsFloatBuffer.containsKey(uniformName) &&
                        !uniformsIntBuffer.containsKey(uniformName)) {
                    shader.runtimeException("All defined uniforms must be provided. Missing '" + uniformName + "'");
                }
            }
            else {
                for (int i=0; i<size; i++) {
                    String name = uniformName+"["+i+"]";
                    if (!uniformsFloat.containsKey(name) &&
                            !uniformsInteger.containsKey(name) &&
                            !uniformsFloatBuffer.containsKey(name) &&
                            !uniformsIntBuffer.containsKey(name)) {
                        shader.runtimeException("All defined uniforms must be provided. Missing '" + name + "'");
                    }
                }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
689 690 691 692 693 694 695 696 697 698
            }
        }

        return new GLRenderData(
                shader,
                uniformsInteger,
                uniformsFloat,
                uniformsIntBuffer,
                uniformsFloatBuffer,
                textures,
699 700
                (int)(data.width * data.pixelRatio),
                (int)(data.height * data.pixelRatio),
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
701 702 703 704 705 706 707 708 709 710 711
                data.fboId,
                contextChildren,
                children);
    }

    private FloatBuffer parseAsFloatArray(ReadableArray array) {
        int size = array.size();
        FloatBuffer buf = ByteBuffer.allocateDirect(size * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        for (int i=0; i<size; i++)
712
            buf.put((float) array.getDouble(i));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
        buf.position(0);
        return buf;
    }

    private IntBuffer parseAsIntArray(ReadableArray array) {
        int size = array.size();
        IntBuffer buf = ByteBuffer.allocateDirect(size * 4)
                .order(ByteOrder.nativeOrder())
                .asIntBuffer();
        for (int i=0; i<size; i++)
            buf.put(array.getInt(i));
        buf.position(0);
        return buf;
    }

    private int arraySizeForType(int type) {
        switch (type) {
            case GL_FLOAT_VEC2:
            case GL_INT_VEC2:
            case GL_BOOL_VEC2:
                return 2;

            case GL_FLOAT_VEC3:
            case GL_INT_VEC3:
            case GL_BOOL_VEC3:
                return 3;

            case GL_FLOAT_VEC4:
            case GL_INT_VEC4:
            case GL_BOOL_VEC4:
            case GL_FLOAT_MAT2:
                return 4;

            case GL_FLOAT_MAT3:
                return 9;

            case GL_FLOAT_MAT4:
                return 16;

            default:
                throw new Error("Invalid array type: "+type);
        }
    }

757 758


759 760
    private boolean syncData () {
        if (data == null) return true;
761 762
        HashMap<Uri, GLImage> newImages = new HashMap<>();
        GLRenderData node = recSyncData(data, newImages);
763
        if (node == null) return false;
764
        Set<Uri> imagesGone = diff(this.images.keySet(), images.keySet());
765 766
        images = newImages;
        preloaded.removeAll(imagesGone);
767 768
        renderData = node;
        return true;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
769 770
    }

771
    private void recRender (GLRenderData renderData) {
772 773
        int w = renderData.width;
        int h = renderData.height;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
774 775 776 777 778 779 780
        for (GLRenderData child: renderData.contextChildren)
            recRender(child);

        for (GLRenderData child: renderData.children)
            recRender(child);

        if (renderData.fboId == -1) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
781
            glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
782
            glViewport(0, 0, w, h);
783
            glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
784 785
        }
        else {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
786
            GLFBO fbo = getFBO(renderData.fboId);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
787 788
            fbo.setShape(w, h);
            fbo.bind();
789
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
        }

        renderData.shader.bind();

        for (String uniformName: renderData.textures.keySet()) {
            GLTexture texture = renderData.textures.get(uniformName);
            int unit = renderData.uniformsInteger.get(uniformName);
            texture.bind(unit);
        }

        Map<String, Integer> uniformTypes = renderData.shader.getUniformTypes();
        for (String uniformName: renderData.uniformsInteger.keySet()) {
            renderData.shader.setUniform(uniformName, renderData.uniformsInteger.get(uniformName));
        }
        for (String uniformName: renderData.uniformsFloat.keySet()) {
            renderData.shader.setUniform(uniformName, renderData.uniformsFloat.get(uniformName));
        }
        for (String uniformName: renderData.uniformsFloatBuffer.keySet()) {
            renderData.shader.setUniform(uniformName, renderData.uniformsFloatBuffer.get(uniformName), uniformTypes.get(uniformName));
        }
        for (String uniformName: renderData.uniformsIntBuffer.keySet()) {
            renderData.shader.setUniform(uniformName, renderData.uniformsIntBuffer.get(uniformName), uniformTypes.get(uniformName));
        }

        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
816
        glDrawArrays(GL_TRIANGLES, 0, 3);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
817 818
    }

819
    private void render () {
820 821
        GLRenderData rd = renderData;
        if (rd == null) return;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
822
        syncContentTextures();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
823

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
824 825 826
        int[] defaultFBOArr = new int[1];
        glGetIntegerv(GL_FRAMEBUFFER_BINDING, defaultFBOArr, 0);
        defaultFBO = defaultFBOArr[0];
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
827
        glEnable(GL_BLEND);
828
        recRender(rd);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
829
        glDisable(GL_BLEND);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
830
        glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
831
        glBindBuffer(GL_ARRAY_BUFFER, 0);
832 833 834 835 836

        if (dirtyOnLoad && !haveRemainingToPreload()) {
            dirtyOnLoad = false;
            dispatchOnLoad();
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
837 838
    }

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
839
    private void dispatchOnProgress (double progress, int loaded, int total) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
840
        WritableMap event = Arguments.createMap();
841
        event.putDouble("progress", Double.isNaN(progress) ? 0.0 : progress);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
842
        event.putInt("loaded", loaded);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
843 844 845 846 847 848 849 850
        event.putInt("total", total);
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                getId(),
                "progress",
                event);
    }

851
    private void dispatchOnLoad () {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
852 853 854 855 856 857 858
        WritableMap event = Arguments.createMap();
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                getId(),
                "load",
                event);
    }
859

860
    public void requestCaptureFrame (CaptureConfig config) {
861
        this.requestRender();
862 863 864 865 866 867
        for (CaptureConfig existing : captureConfigs) {
            if (existing.equals(config)) {
                return;
            }
        }
        captureConfigs.add(config);
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    }

    private Bitmap createSnapshot () {
        return createSnapshot(0, 0, getWidth(), getHeight());
    }

    private Bitmap createSnapshot (int x, int y, int w, int h) {
        int bitmapBuffer[] = new int[w * h];
        int bitmapSource[] = new int[w * h];
        IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
        intBuffer.position(0);

        try {
            glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, intBuffer);
            int offset1, offset2;
            for (int i = 0; i < h; i++) {
                offset1 = i * w;
                offset2 = (h - i - 1) * w;
                for (int j = 0; j < w; j++) {
                    int texturePixel = bitmapBuffer[offset1 + j];
                    int blue = (texturePixel >> 16) & 0xff;
                    int red = (texturePixel << 16) & 0x00ff0000;
                    int pixel = (texturePixel & 0xff00ff00) | red | blue;
                    bitmapSource[offset2 + j] = pixel;
                }
            }
        } catch (GLException e) {
            return null;
        }

        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
    }

    private PointerEvents mPointerEvents = PointerEvents.AUTO;

    @Override
    public PointerEvents getPointerEvents() {
        return mPointerEvents;
    }

    void setPointerEvents(PointerEvents pointerEvents) {
        mPointerEvents = pointerEvents;
    }

912 913 914 915 916 917
    static <A> Set<A> diff(Set<A> a, Set<A> b) {
        Set<A> d = new HashSet<>();
        d.addAll(a);
        d.removeAll(b);
        return d;
    }
918 919 920 921 922 923 924 925 926 927 928 929


    public void setPixelRatio(float pixelRatio) {
        this.pixelRatio = pixelRatio;
        syncSize(this.getWidth(), this.getHeight(), pixelRatio);
    }

    private void syncSize (int w, int h, float pixelRatio) {
        int width  = (int) (w * pixelRatio / displayDensity);
        int height = (int) (h * pixelRatio / displayDensity);
        getHolder().setFixedSize(width, height);
    }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
930
}