Search in sources :

Example 1 with ReadableArray

use of com.facebook.react.bridge.ReadableArray in project gl-react-native by ProjectSeptemberInc.

the class GLCanvas method recSyncData.

private GLRenderData recSyncData(GLData data, HashMap<Uri, GLImage> images) {
    Map<Uri, GLImage> prevImages = this.images;
    GLShader shader = getShader(data.shader);
    if (shader == null || !shader.ensureCompile())
        return null;
    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) {
        GLRenderData node = recSyncData(child, images);
        if (node == null)
            return null;
        contextChildren.add(node);
    }
    for (GLData child : data.children) {
        GLRenderData node = recSyncData(child, images);
        if (node == null)
            return null;
        children.add(node);
    }
    Map<String, Integer> uniformTypes = shader.getUniformTypes();
    List<String> uniformNames = shader.getUniformNames();
    Map<String, Integer> uniformSizes = shader.getUniformSizes();
    int units = 0;
    ReadableMapKeySetIterator iterator = data.uniforms.keySetIterator();
    while (iterator.hasNextKey()) {
        String uniformName = iterator.nextKey();
        int type = uniformTypes.get(uniformName);
        int size = uniformSizes.get(uniformName);
        ReadableMap dataUniforms = data.uniforms;
        if (type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE) {
            uniformsInteger.put(uniformName, units++);
            if (dataUniforms.isNull(uniformName)) {
                GLTexture emptyTexture = new GLTexture(this);
                emptyTexture.setPixelsEmpty();
                textures.put(uniformName, emptyTexture);
            } else {
                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;
                }
                String t = value.getString("type");
                if (t.equals("content")) {
                    int id = value.getInt("id");
                    if (id >= contentTextures.size()) {
                        resizeUniformContentTextures(id + 1);
                    }
                    textures.put(uniformName, contentTextures.get(id));
                } else if (t.equals("fbo")) {
                    int id = value.getInt("id");
                    GLFBO fbo = getFBO(id);
                    textures.put(uniformName, fbo.color.get(0));
                } else if (t.equals("uri")) {
                    final Uri src = srcResource(value);
                    if (src == null) {
                        shader.runtimeException("texture uniform '" + uniformName + "': Invalid uri format '" + value + "'");
                    }
                    GLImage image = images.get(src);
                    if (image == null) {
                        image = prevImages.get(src);
                        if (image != null)
                            images.put(src, image);
                    }
                    if (image == null) {
                        image = new GLImage(this, executorSupplier.forDecode(), new Runnable() {

                            public void run() {
                                onImageLoad(src);
                            }
                        });
                        image.setSrc(src);
                        images.put(src, image);
                    }
                    textures.put(uniformName, image.getTexture());
                } else {
                    shader.runtimeException("texture uniform '" + uniformName + "': Unexpected type '" + type + "'");
                }
            }
        } else {
            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:
                        shader.runtimeException("uniform '" + uniformName + "': type not supported: " + type);
                }
            } else {
                ReadableArray array = dataUniforms.getArray(uniformName);
                if (size != array.size()) {
                    shader.runtimeException("uniform '" + uniformName + "': 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;
                        default:
                            shader.runtimeException("uniform '" + name + "': type not supported: " + type);
                    }
                }
            }
        }
    }
    int[] maxTextureUnits = new int[1];
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, maxTextureUnits, 0);
    if (units > maxTextureUnits[0]) {
        shader.runtimeException("Maximum number of texture reach. got " + units + " >= max " + maxTextureUnits);
    }
    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 + "'");
                }
            }
        }
    }
    return new GLRenderData(shader, uniformsInteger, uniformsFloat, uniformsIntBuffer, uniformsFloatBuffer, textures, (int) (data.width * data.pixelRatio), (int) (data.height * data.pixelRatio), data.fboId, contextChildren, children);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) Uri(android.net.Uri) ReadableMap(com.facebook.react.bridge.ReadableMap) ReadableArray(com.facebook.react.bridge.ReadableArray) GLException(android.opengl.GLException) ReadableMapKeySetIterator(com.facebook.react.bridge.ReadableMapKeySetIterator) IntBuffer(java.nio.IntBuffer)

Example 2 with ReadableArray

use of com.facebook.react.bridge.ReadableArray in project native-navigation by airbnb.

the class DefaultNavigationImplementation method prepareOptionsMenu.

public void prepareOptionsMenu(final ReactInterface component, ReactToolbar toolbar, ActionBar bar, Menu menu, ReadableMap prev, ReadableMap next) {
    Log.d(TAG, "prepareOptionsMenu");
    if (arrayHasChanged("rightButtons", prev, next)) {
        if (next.hasKey("rightButtons")) {
            ReadableArray buttons = next.getArray("rightButtons");
            menu.clear();
            toolbar.setRightButtons(menu, buttons, component);
        } else {
            menu.clear();
        }
    }
    if (stringHasChanged("rightTitle", prev, next)) {
        if (next.hasKey("rightTitle")) {
            String rightTitle = next.getString("rightTitle");
            MenuItem item = menu.add(rightTitle);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
            item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    component.emitEvent("onRightPress", null);
                    return true;
                }
            });
        }
    }
    if (mapHasChanged("rightImage", prev, next)) {
    // TODO(lmr):
    }
}
Also used : ReadableArray(com.facebook.react.bridge.ReadableArray)

Example 3 with ReadableArray

use of com.facebook.react.bridge.ReadableArray in project react-native-image-picker by marcshilling.

the class ButtonsHelper method getCustomButtons.

@NonNull
private static LinkedList<Item> getCustomButtons(@NonNull final ReadableMap options) {
    LinkedList<Item> result = new LinkedList<>();
    if (!options.hasKey("customButtons")) {
        return result;
    }
    final ReadableArray customButtons = options.getArray("customButtons");
    for (int i = 0; i < customButtons.size(); i++) {
        final ReadableMap button = customButtons.getMap(i);
        final String title = button.getString("title");
        final String action = button.getString("name");
        result.add(new Item(title, action));
    }
    return result;
}
Also used : ReadableArray(com.facebook.react.bridge.ReadableArray) ReadableMap(com.facebook.react.bridge.ReadableMap) LinkedList(java.util.LinkedList) NonNull(android.support.annotation.NonNull)

Aggregations

ReadableArray (com.facebook.react.bridge.ReadableArray)3 ReadableMap (com.facebook.react.bridge.ReadableMap)2 Uri (android.net.Uri)1 GLException (android.opengl.GLException)1 NonNull (android.support.annotation.NonNull)1 ReadableMapKeySetIterator (com.facebook.react.bridge.ReadableMapKeySetIterator)1 FloatBuffer (java.nio.FloatBuffer)1 IntBuffer (java.nio.IntBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1