Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 33561629 authored by Angel Aguayo's avatar Angel Aguayo Committed by Android (Google) Code Review
Browse files

Merge "Added new Mesh activity to test performance."

parents 6cb8be7c 8be0e4c8
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -1163,5 +1163,13 @@
                <category android:name="com.android.test.hwui.TEST"/>
            </intent-filter>
        </activity>
        <activity android:name="MeshLargeActivity"
                             android:label="Mesh/LargeMesh"
                             android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="com.android.test.hwui.TEST"/>
        </intent-filter>
    </activity>
    </application>
</manifest>
+175 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.test.hwui;

import android.app.Activity;
import android.content.Context;
import android.graphics.BlendMode;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Mesh;
import android.graphics.MeshSpecification;
import android.graphics.MeshSpecification.Attribute;
import android.graphics.MeshSpecification.Varying;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;

public class MeshLargeActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new MeshView(this));
    }

    static class MeshView extends View {
        MeshView(Context c) {
            super(c);
            this.setOnTouchListener((v, event) -> {
                invalidate();
                return true;
            });
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            MeshSpecification meshSpec = createMeshSpecification();
            int numTriangles = 10000;
            // number of triangles plus first 2 vertices
            FloatBuffer vertexBuffer = FloatBuffer.allocate((numTriangles + 2) * 30);
            ShortBuffer indexBuffer = ShortBuffer.allocate(numTriangles * 3);

            float origin = 500.0f;
            int radius = 200;

            // origin
            vertexBuffer.put(0, origin);
            vertexBuffer.put(1, origin);
            for (int i = 0; i < 7; i++) {
                vertexBuffer.put(2 + (i * 4), 1.0f);
                vertexBuffer.put(2 + (i * 4) + 1, 1.0f);
                vertexBuffer.put(2 + (i * 4) + 2, 1.0f);
                vertexBuffer.put(2 + (i * 4) + 3, 1.0f);
            }

            // first point
            vertexBuffer.put(30, origin + radius);
            vertexBuffer.put(31, origin);
            for (int i = 0; i < 7; i++) {
                vertexBuffer.put(32 + (i * 4), 1.0f);
                vertexBuffer.put(32 + (i * 4) + 1, 1.0f);
                vertexBuffer.put(32 + (i * 4) + 2, 1.0f);
                vertexBuffer.put(32 + (i * 4) + 3, 1.0f);
            }

            int nVert = 2;
            int nInd = 0;
            for (int i = 2; i <= numTriangles + 1; i++) {
                double angle = 2 * Math.PI * i / numTriangles;
                double x = radius * Math.cos(angle);
                double y = radius * Math.sin(angle);
                // position
                vertexBuffer.put(i * 30, origin + (float) x);
                vertexBuffer.put(i * 30 + 1, origin + (float) y);

                // test through test7
                for (int j = 0; j < 7; j++) {
                    vertexBuffer.put((i * 30 + 2) + (j * 4), 1.0f);
                    vertexBuffer.put((i * 30 + 2) + (j * 4) + 1, 1.0f);
                    vertexBuffer.put((i * 30 + 2) + (j * 4) + 2, 1.0f);
                    vertexBuffer.put((i * 30 + 2) + (j * 4) + 3, 1.0f);
                }

                indexBuffer.put(nInd++, (short) 0);
                indexBuffer.put(nInd++, (short) (nVert - 1));
                indexBuffer.put(nInd++, (short) nVert);
                nVert++;
            }
            vertexBuffer.rewind();
            indexBuffer.rewind();
            Mesh mesh = Mesh.makeIndexed(
                    meshSpec, Mesh.TRIANGLES, vertexBuffer, numTriangles + 2, indexBuffer,
                    new Rect(0, 0, 1000, 1000)
            );
            mesh.setFloatUniform("test", 1.0f, 2.0f);
            Paint paint = new Paint();
            paint.setColor(Color.BLUE);

            canvas.drawMesh(mesh, BlendMode.SRC, paint);
        }

        private MeshSpecification createMeshSpecification() {
            String vs = "Varyings main(const Attributes attributes) { "
                    + "     Varyings varyings;"
                    + "     varyings.position = attributes.position;"
                    + "     return varyings;"
                    + "}";
            String fs = "uniform float2 test;"
                    + "float2 main(const Varyings varyings, out float4 color) {\n"
                    + "      color = vec4(1.0, 0.0, 0.0, 1.0);"
                    + "      return varyings.position;\n"
                    + "}";
            ArrayList<Attribute> attList = new ArrayList<>();
            attList.add(new Attribute(MeshSpecification.FLOAT2, 0, "position"));
            attList.add(new Attribute(
                    MeshSpecification.FLOAT4,
                    8,
                    "test"
            ));
            attList.add(new Attribute(
                    MeshSpecification.FLOAT4,
                    24,
                    "test2"
            ));
            attList.add(new Attribute(
                    MeshSpecification.FLOAT4,
                    40,
                    "test3"
            ));
            attList.add(new Attribute(
                    MeshSpecification.FLOAT4,
                    56,
                    "test4"
            ));
            attList.add(new Attribute(
                    MeshSpecification.FLOAT4,
                    72,
                    "test5"
            ));
            attList.add(new Attribute(
                    MeshSpecification.FLOAT4,
                    88,
                    "test6"
            ));
            attList.add(new Attribute(
                    MeshSpecification.FLOAT4,
                    104,
                    "test7"
            ));
            ArrayList<Varying> varyList = new ArrayList<>();
            return MeshSpecification.make(attList, 120, varyList, vs, fs);
        }
    }
}