1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.test.hwui;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.graphics.BlendMode;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.Mesh;
25 import android.graphics.MeshSpecification;
26 import android.graphics.MeshSpecification.Attribute;
27 import android.graphics.MeshSpecification.Varying;
28 import android.graphics.Paint;
29 import android.graphics.RectF;
30 import android.os.Bundle;
31 import android.view.View;
32 
33 import java.nio.FloatBuffer;
34 import java.nio.ShortBuffer;
35 
36 public class MeshActivity extends Activity {
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40 
41         setContentView(new MeshView(this));
42     }
43 
44     static class MeshView extends View {
MeshView(Context c)45         MeshView(Context c) {
46             super(c);
47             this.setOnTouchListener((v, event) -> {
48                 invalidate();
49                 return true;
50             });
51         }
52 
53         @Override
onDraw(Canvas canvas)54         protected void onDraw(Canvas canvas) {
55             super.onDraw(canvas);
56 
57             MeshSpecification meshSpec = createMeshSpecification();
58             FloatBuffer vertexBuffer = FloatBuffer.allocate(6);
59             vertexBuffer.put(0, 100.0f);
60             vertexBuffer.put(1, 100.0f);
61             vertexBuffer.put(2, 400.0f);
62             vertexBuffer.put(3, 0.0f);
63             vertexBuffer.put(4, 0.0f);
64             vertexBuffer.put(5, 400.0f);
65             vertexBuffer.rewind();
66             Mesh mesh = new Mesh(
67                     meshSpec, Mesh.TRIANGLES, vertexBuffer, 3, new RectF(0, 0, 1000, 1000));
68 
69             canvas.drawMesh(mesh, BlendMode.COLOR, new Paint());
70 
71             int numTriangles = 100;
72             // number of triangles plus first 2 vertices
73             FloatBuffer iVertexBuffer = FloatBuffer.allocate(numTriangles * 2 + 4);
74             ShortBuffer indexBuffer = ShortBuffer.allocate(300);
75 
76             int radius = 200;
77             // origin
78             iVertexBuffer.put(0, 500.0f);
79             iVertexBuffer.put(1, 500.0f);
80 
81             // first point
82             iVertexBuffer.put(2, 500.0f + radius);
83             iVertexBuffer.put(3, 500.0f);
84             int nVert = 2;
85             int nInd = 0;
86             for (int i = 1; i <= numTriangles; i++) {
87                 double angle = (Math.PI * i) / numTriangles;
88                 double x = radius * Math.cos(angle);
89                 double y = radius * Math.sin(angle);
90                 iVertexBuffer.put((i + 1) * 2, 500 + (float) x);
91                 iVertexBuffer.put((i + 1) * 2 + 1, 500 + (float) y);
92 
93                 indexBuffer.put(nInd++, (short) 0);
94                 indexBuffer.put(nInd++, (short) (nVert - 1));
95                 indexBuffer.put(nInd++, (short) nVert);
96                 nVert++;
97             }
98             iVertexBuffer.rewind();
99             indexBuffer.rewind();
100             Mesh mesh2 = new Mesh(meshSpec, Mesh.TRIANGLES, iVertexBuffer, 102, indexBuffer,
101                     new RectF(0, 0, 1000, 1000));
102             Paint paint = new Paint();
103             paint.setColor(Color.RED);
104             canvas.drawMesh(mesh2, BlendMode.COLOR, paint);
105         }
106 
createMeshSpecification()107         private MeshSpecification createMeshSpecification() {
108             String vs = "Varyings main(const Attributes attributes) { "
109                     + "     Varyings varyings;"
110                     + "     varyings.position = attributes.position;"
111                     + "     return varyings;"
112                     + "}";
113             String fs = "float2 main(const Varyings varyings, out float4 color) {\n"
114                     + "      color = vec4(1.0, 0.0, 0.0, 1.0);"
115                     + "      return varyings.position;\n"
116                     + "}";
117             Attribute[] attList = new Attribute[]{
118                     new Attribute(MeshSpecification.TYPE_FLOAT2, 0, "position"),
119 
120             };
121             Varying[] varyList = new Varying[0];
122             return MeshSpecification.make(attList, 8, varyList, vs, fs);
123         }
124     }
125 }
126