GLCanvas.java 26.7 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;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
30 31 32 33 34 35
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
36
import java.util.HashSet;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
37 38 39 40
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
41
import java.util.Set;
42
import java.util.concurrent.Executor;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
43 44 45 46

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

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

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

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

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

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

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

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

84 85 86 87
        DisplayMetrics dm = reactContext.getResources().getDisplayMetrics();
        displayDensity = dm.density;
        pixelRatio = dm.density;

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

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

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

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

    public GLShader getShader (Integer id) {
        if (!shaders.containsKey(id)) {
            GLShaderData shaderData = rnglContext.getShader(id);
            if (shaderData == null) return null;
            shaders.put(id, new GLShader(shaderData));
        }
        return shaders.get(id);
    }

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

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

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

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

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

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

        if (shouldRenderNow) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
170 171
            this.render();
            deferredRendering = false;
172 173 174 175 176 177 178 179 180
            if (captureFrameRequested) {
                captureFrameRequested = false;
                Bitmap capture = createSnapshot();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                capture.compress(Bitmap.CompressFormat.PNG, 100, baos);
                String frame = "data:image/png;base64,"+
                        Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
                dispatchOnCaptureFrame(frame);
            }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
181 182 183
        }
    }

184 185 186 187 188 189 190 191 192
    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
193 194
    public void setNbContentTextures(int n) {
        this.nbContentTextures = n;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
195
        requestRender();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
196 197 198
    }

    public void setRenderId(int renderId) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
199
        if (nbContentTextures > 0) {
200
            if (!haveRemainingToPreload()) syncContentBitmaps();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
201 202
            requestRender();
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
203 204 205 206
    }

    public void setOpaque(boolean opaque) {
        if (opaque) {
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
207
            this.getHolder().setFormat(PixelFormat.RGB_888);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
208 209 210 211
        }
        else {
            this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        }
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
212
        this.requestRender();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
213 214 215 216 217 218 219 220 221
    }

    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;
222
        renderData = null;
223
        if (!haveRemainingToPreload()) syncContentBitmaps();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
224
        requestSyncData();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
225 226 227
    }


Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
228 229 230
    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
231
            imagesToPreload.add(resolveSrc(imagesToPreloadRA.getMap(i).getString("uri")));
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
232
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
233
        this.imagesToPreload = imagesToPreload;
234
        requestSyncData();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
235 236 237 238
    }

    // Sync methods

239 240
    @Override
    public void execute (final Runnable runnable) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254
        synchronized (mRunOnDraw) {
            mRunOnDraw.add(runnable);
            requestRender();
        }
    }
    private void runAll(Queue<Runnable> queue) {
        synchronized (queue) {
            while (!queue.isEmpty()) {
                queue.poll().run();
            }
        }
    }

    public void requestSyncData () {
255
        execute(new Runnable() {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
256
            public void run() {
257
                // FIXME: maybe should set a flag so we don't do it twice??
258
                if (!syncData())
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
259 260 261 262 263
                    requestSyncData();
            }
        });
    }

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    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
283 284 285 286 287 288 289 290
    /**
     * 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
291 292 293 294 295 296 297 298 299 300
            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
301
        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
302 303 304 305 306 307 308 309 310 311 312 313 314
        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
315 316 317 318 319 320 321 322 323 324
    }

    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
325
                contentTextures.add(new GLTexture(this));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
326 327 328 329 330 331 332
            }
        }
    }


    private int countPreloaded () {
        int nb = 0;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
333 334 335 336
        for (Uri toload: imagesToPreload) {
            if (preloaded.contains(toload)) {
                nb++;
            }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
337 338 339 340
        }
        return nb;
    }

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
341
    private void onImageLoad (Uri loaded) {
342 343 344 345 346 347 348
        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
349 350
    }

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    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
369

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
370
    public Uri srcResource (ReadableMap res) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
371 372 373 374
        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
375
        return resolveSrc(src);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
376 377
    }

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

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
381
        GLShader shader = getShader(data.shader);
382
        if (shader == null || !shader.ensureCompile()) return null;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
383 384 385 386 387 388 389 390 391
        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) {
392 393 394
            GLRenderData node = recSyncData(child, images);
            if (node == null) return null;
            contextChildren.add(node);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
395 396 397
        }

        for (GLData child: data.children) {
398 399 400
            GLRenderData node = recSyncData(child, images);
            if (node == null) return null;
            children.add(node);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        }

        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
417
                    GLTexture emptyTexture = new GLTexture(this);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
418 419 420 421 422 423 424 425
                    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
426 427 428
                        if (id >= contentTextures.size()) {
                            resizeUniformContentTextures(id+1);
                        }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
429 430 431 432
                        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
433
                        GLFBO fbo = getFBO(id);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
434 435 436
                        textures.put(uniformName, fbo.color.get(0));
                    }
                    else if (t.equals("uri")) {
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
437 438
                        final Uri src = srcResource(value);
                        if (src == null) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
439
                            shader.runtimeException("texture uniform '"+uniformName+"': Invalid uri format '"+value+"'");
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
440 441 442 443 444 445 446 447 448
                        }

                        GLImage image = images.get(src);
                        if (image == null) {
                            image = prevImages.get(src);
                            if (image != null)
                                images.put(src, image);
                        }
                        if (image == null) {
449
                            image = new GLImage(this, executorSupplier.forDecode(), new Runnable() {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
450 451 452
                                public void run() {
                                    onImageLoad(src);
                                }
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
453
                            });
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
454 455 456 457 458 459
                            image.setSrc(src);
                            images.put(src, image);
                        }
                        textures.put(uniformName, image.getTexture());
                    }
                    else {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
460
                        shader.runtimeException("texture uniform '" + uniformName + "': Unexpected type '" + type + "'");
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
                    }
                }
            }
            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
486 487
                            shader.runtimeException(
                                    "uniform '"+uniformName+
488 489
                                            "': Invalid array size: "+arr.size()+
                                            ". Expected: "+arraySizeForType(type));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
490 491 492 493 494 495 496 497 498 499 500 501
                        }
                        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
502 503
                            shader.runtimeException(
                                    "uniform '"+uniformName+
504 505
                                            "': Invalid array size: "+arr2.size()+
                                            ". Expected: "+arraySizeForType(type));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
506 507 508 509 510
                        }
                        uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2));
                        break;

                    default:
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
511 512
                        shader.runtimeException(
                                "uniform '"+uniformName+
513
                                        "': type not supported: "+type);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
514 515 516 517 518 519 520 521
                }

            }
        }

        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
522
            shader.runtimeException("Maximum number of texture reach. got " + units + " >= max " + maxTextureUnits);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
523 524 525 526
        }

        for (String uniformName: uniformTypes.keySet()) {
            if (!uniformsFloat.containsKey(uniformName) &&
527 528 529
                    !uniformsInteger.containsKey(uniformName) &&
                    !uniformsFloatBuffer.containsKey(uniformName) &&
                    !uniformsIntBuffer.containsKey(uniformName)) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
530
                shader.runtimeException("All defined uniforms must be provided. Missing '"+uniformName+"'");
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
531 532 533 534 535 536 537 538 539 540
            }
        }

        return new GLRenderData(
                shader,
                uniformsInteger,
                uniformsFloat,
                uniformsIntBuffer,
                uniformsFloatBuffer,
                textures,
541 542
                (int)(data.width * data.pixelRatio),
                (int)(data.height * data.pixelRatio),
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
543 544 545 546 547 548 549 550 551 552 553
                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++)
554
            buf.put((float) array.getDouble(i));
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
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 593 594 595 596 597 598
        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);
        }
    }

599 600


601 602
    private boolean syncData () {
        if (data == null) return true;
603 604
        HashMap<Uri, GLImage> newImages = new HashMap<>();
        GLRenderData node = recSyncData(data, newImages);
605
        if (node == null) return false;
606
        Set<Uri> imagesGone = diff(this.images.keySet(), images.keySet());
607 608
        images = newImages;
        preloaded.removeAll(imagesGone);
609 610
        renderData = node;
        return true;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
611 612
    }

613
    private void recRender (GLRenderData renderData) {
614 615
        int w = renderData.width;
        int h = renderData.height;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
616 617 618 619 620 621 622
        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
623
            glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
624 625 626
            glViewport(0, 0, w, h);
        }
        else {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
627
            GLFBO fbo = getFBO(renderData.fboId);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
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
            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);
    }

660
    private void render () {
661 662
        GLRenderData rd = renderData;
        if (rd == null) return;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
663
        syncContentTextures();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
664

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
665 666 667
        int[] defaultFBOArr = new int[1];
        glGetIntegerv(GL_FRAMEBUFFER_BINDING, defaultFBOArr, 0);
        defaultFBO = defaultFBOArr[0];
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
668
        glEnable(GL_BLEND);
669
        recRender(rd);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
670
        glDisable(GL_BLEND);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
671
        glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
672
        glBindBuffer(GL_ARRAY_BUFFER, 0);
673 674 675 676 677

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

680 681 682 683 684 685 686 687
    private void dispatchOnCaptureFrame (String frame) {
        WritableMap event = Arguments.createMap();
        event.putString("frame", frame);
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                getId(),
                "captureFrame",
                event);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
688 689
    }

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
690
    private void dispatchOnProgress (double progress, int loaded, int total) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
691
        WritableMap event = Arguments.createMap();
692
        event.putDouble("progress", Double.isNaN(progress) ? 0.0 : progress);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
693
        event.putInt("loaded", loaded);
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
694 695 696 697 698 699 700 701
        event.putInt("total", total);
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                getId(),
                "progress",
                event);
    }

702
    private void dispatchOnLoad () {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
703 704 705 706 707 708 709
        WritableMap event = Arguments.createMap();
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                getId(),
                "load",
                event);
    }
710 711 712 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 757

    public void requestCaptureFrame() {
        captureFrameRequested = true;
        this.requestRender();
    }

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

758 759 760 761 762 763
    static <A> Set<A> diff(Set<A> a, Set<A> b) {
        Set<A> d = new HashSet<>();
        d.addAll(a);
        d.removeAll(b);
        return d;
    }
764 765 766 767 768 769 770 771 772 773 774 775


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