GLCanvas.java 29.1 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;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
6
import android.graphics.Matrix;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
7
import android.graphics.PixelFormat;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
8
import android.net.Uri;
9
import android.opengl.GLException;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
10
import android.opengl.GLSurfaceView;
11
import android.util.Base64;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
12
import android.util.DisplayMetrics;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
13 14
import android.util.Log;
import android.view.View;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
15 16
import android.view.ViewGroup;

17
import com.facebook.imagepipeline.core.ExecutorSupplier;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
18 19 20 21 22 23
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;
24 25
import com.facebook.react.uimanager.PointerEvents;
import com.facebook.react.uimanager.ReactPointerEventsView;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
26 27 28
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

135 136 137 138 139 140
    @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
141 142 143 144
    @Override
    public void onDrawFrame(GL10 gl) {
        runAll(mRunOnDraw);

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

148 149 150 151 152 153
        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
154 155
            return;
        }
156
        neverRendered = false;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
157

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
158 159 160 161 162 163 164 165 166 167
        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
168 169
            });
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
170 171

        if (shouldRenderNow) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
172 173
            this.render();
            deferredRendering = false;
174 175
            if (captureConfigs.size() > 0) {
                capture(); // FIXME: maybe we should schedule this?
176
            }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
177 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
    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<>();
    }

244 245 246 247 248 249 250 251 252
    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
253 254
    public void setNbContentTextures(int n) {
        this.nbContentTextures = n;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
255
        requestRender();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
256 257 258
    }

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

    public void setOpaque(boolean opaque) {
        if (opaque) {
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
267
            this.getHolder().setFormat(PixelFormat.RGB_888);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
268 269 270 271
        }
        else {
            this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        }
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
272
        this.requestRender();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
273 274 275 276 277 278 279 280 281
    }

    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;
282
        renderData = null;
283
        if (!haveRemainingToPreload()) syncContentBitmaps();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
284
        requestSyncData();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
285 286 287
    }


Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
288 289 290
    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
291
            imagesToPreload.add(resolveSrc(imagesToPreloadRA.getMap(i).getString("uri")));
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
292
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
293
        this.imagesToPreload = imagesToPreload;
294
        requestSyncData();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
295 296 297 298
    }

    // Sync methods

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

    public void requestSyncData () {
315
        execute(new Runnable() {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
316
            public void run() {
317
                // FIXME: maybe should set a flag so we don't do it twice??
318 319 320 321 322 323 324
                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
325 326 327 328
            }
        });
    }

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    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
348 349 350 351 352 353 354 355
    /**
     * 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
356 357 358 359 360 361 362 363 364 365
            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
366
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
367 368 369 370 371 372 373 374 375 376 377 378 379
        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
380 381 382 383 384 385 386 387 388 389
    }

    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
390
                contentTextures.add(new GLTexture(this));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
391 392 393 394 395 396 397
            }
        }
    }


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

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
406
    private void onImageLoad (Uri loaded) {
407 408 409 410 411 412 413
        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
414 415
    }

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    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
434

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
435
    public Uri srcResource (ReadableMap res) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
436 437 438 439
        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
440
        return resolveSrc(src);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
441 442
    }

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

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
446
        GLShader shader = getShader(data.shader);
447
        if (shader == null || !shader.ensureCompile()) return null;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
448 449 450 451 452 453 454 455 456
        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) {
457 458 459
            GLRenderData node = recSyncData(child, images);
            if (node == null) return null;
            contextChildren.add(node);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
460 461 462
        }

        for (GLData child: data.children) {
463 464 465
            GLRenderData node = recSyncData(child, images);
            if (node == null) return null;
            children.add(node);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
        }

        Map<String, Integer> uniformTypes = shader.getUniformTypes();

        int units = 0;
        ReadableMapKeySetIterator iterator = data.uniforms.keySetIterator();
        while (iterator.hasNextKey()) {
            String uniformName = iterator.nextKey();
            int type = uniformTypes.get(uniformName);

            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
482
                    GLTexture emptyTexture = new GLTexture(this);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
483 484 485 486 487 488 489 490
                    emptyTexture.setPixelsEmpty();
                    textures.put(uniformName, emptyTexture);
                }
                else {
                    ReadableMap value = dataUniforms.getMap(uniformName);
                    String t = value.getString("type");
                    if (t.equals("content")) {
                        int id = value.getInt("id");
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
491 492 493
                        if (id >= contentTextures.size()) {
                            resizeUniformContentTextures(id+1);
                        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
494 495 496 497
                        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
498
                        GLFBO fbo = getFBO(id);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
499 500 501
                        textures.put(uniformName, fbo.color.get(0));
                    }
                    else if (t.equals("uri")) {
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
502 503
                        final Uri src = srcResource(value);
                        if (src == null) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
504
                            shader.runtimeException("texture uniform '"+uniformName+"': Invalid uri format '"+value+"'");
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
505 506 507 508 509 510 511 512 513
                        }

                        GLImage image = images.get(src);
                        if (image == null) {
                            image = prevImages.get(src);
                            if (image != null)
                                images.put(src, image);
                        }
                        if (image == null) {
514
                            image = new GLImage(this, executorSupplier.forDecode(), new Runnable() {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
515 516 517
                                public void run() {
                                    onImageLoad(src);
                                }
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
518
                            });
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
519 520 521 522 523 524
                            image.setSrc(src);
                            images.put(src, image);
                        }
                        textures.put(uniformName, image.getTexture());
                    }
                    else {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
525
                        shader.runtimeException("texture uniform '" + uniformName + "': Unexpected type '" + type + "'");
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
                    }
                }
            }
            else {
                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()) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
551 552
                            shader.runtimeException(
                                    "uniform '"+uniformName+
553 554
                                            "': Invalid array size: "+arr.size()+
                                            ". Expected: "+arraySizeForType(type));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
555 556 557 558 559 560 561 562 563 564 565 566
                        }
                        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()) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
567 568
                            shader.runtimeException(
                                    "uniform '"+uniformName+
569 570
                                            "': Invalid array size: "+arr2.size()+
                                            ". Expected: "+arraySizeForType(type));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
571 572 573 574 575
                        }
                        uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2));
                        break;

                    default:
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
576 577
                        shader.runtimeException(
                                "uniform '"+uniformName+
578
                                        "': type not supported: "+type);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
579 580 581 582 583 584 585 586
                }

            }
        }

        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
587
            shader.runtimeException("Maximum number of texture reach. got " + units + " >= max " + maxTextureUnits);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
588 589 590 591
        }

        for (String uniformName: uniformTypes.keySet()) {
            if (!uniformsFloat.containsKey(uniformName) &&
592 593 594
                    !uniformsInteger.containsKey(uniformName) &&
                    !uniformsFloatBuffer.containsKey(uniformName) &&
                    !uniformsIntBuffer.containsKey(uniformName)) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
595
                shader.runtimeException("All defined uniforms must be provided. Missing '"+uniformName+"'");
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
596 597 598 599 600 601 602 603 604 605
            }
        }

        return new GLRenderData(
                shader,
                uniformsInteger,
                uniformsFloat,
                uniformsIntBuffer,
                uniformsFloatBuffer,
                textures,
606 607
                (int)(data.width * data.pixelRatio),
                (int)(data.height * data.pixelRatio),
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
608 609 610 611 612 613 614 615 616 617 618
                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++)
619
            buf.put((float) array.getDouble(i));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
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 652 653 654 655 656 657 658 659 660 661 662 663
        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);
        }
    }

664 665


666 667
    private boolean syncData () {
        if (data == null) return true;
668 669
        HashMap<Uri, GLImage> newImages = new HashMap<>();
        GLRenderData node = recSyncData(data, newImages);
670
        if (node == null) return false;
671
        Set<Uri> imagesGone = diff(this.images.keySet(), images.keySet());
672 673
        images = newImages;
        preloaded.removeAll(imagesGone);
674 675
        renderData = node;
        return true;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
676 677
    }

678
    private void recRender (GLRenderData renderData) {
679 680
        int w = renderData.width;
        int h = renderData.height;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
681 682 683 684 685 686 687
        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
688
            glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
689 690 691
            glViewport(0, 0, w, h);
        }
        else {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
692
            GLFBO fbo = getFBO(renderData.fboId);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
            fbo.setShape(w, h);
            fbo.bind();
        }

        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));
        }

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0, 6);
    }

725
    private void render () {
726 727
        GLRenderData rd = renderData;
        if (rd == null) return;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
728
        syncContentTextures();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
729

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
730 731 732
        int[] defaultFBOArr = new int[1];
        glGetIntegerv(GL_FRAMEBUFFER_BINDING, defaultFBOArr, 0);
        defaultFBO = defaultFBOArr[0];
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
733
        glEnable(GL_BLEND);
734
        recRender(rd);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
735
        glDisable(GL_BLEND);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
736
        glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
737
        glBindBuffer(GL_ARRAY_BUFFER, 0);
738 739 740 741 742

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

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
745
    private void dispatchOnProgress (double progress, int loaded, int total) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
746
        WritableMap event = Arguments.createMap();
747
        event.putDouble("progress", Double.isNaN(progress) ? 0.0 : progress);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
748
        event.putInt("loaded", loaded);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
749 750 751 752 753 754 755 756
        event.putInt("total", total);
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                getId(),
                "progress",
                event);
    }

757
    private void dispatchOnLoad () {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
758 759 760 761 762 763 764
        WritableMap event = Arguments.createMap();
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                getId(),
                "load",
                event);
    }
765

766
    public void requestCaptureFrame (CaptureConfig config) {
767
        this.requestRender();
768 769 770 771 772 773
        for (CaptureConfig existing : captureConfigs) {
            if (existing.equals(config)) {
                return;
            }
        }
        captureConfigs.add(config);
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 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 816 817
    }

    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;
    }

818 819 820 821 822 823
    static <A> Set<A> diff(Set<A> a, Set<A> b) {
        Set<A> d = new HashSet<>();
        d.addAll(a);
        d.removeAll(b);
        return d;
    }
824 825 826 827 828 829 830 831 832 833 834 835


    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
836
}