Flutter Engine
The Flutter Engine
MainActivity.java
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8package org.skia.jetskidemo;
9
10import androidx.annotation.NonNull;
11
12import android.app.Activity;
13import android.graphics.Bitmap;
14import android.os.Bundle;
15import android.util.Log;
16import android.view.SurfaceHolder;
17import android.view.SurfaceView;
18import android.view.View;
19import android.view.ViewGroup;
20import android.widget.ImageView;
21import android.widget.LinearLayout;
22
23import org.skia.jetski.*;
24import org.skia.jetski.util.SkottieView;
25import org.skia.jetski.util.SurfaceRenderer;
26
27public class MainActivity extends Activity {
28 private ImageView bitmapImage;
29 private Surface threadedSurface;
30
31 static {
32 System.loadLibrary("jetski");
33 }
34
35 @Override
36 protected void onCreate(Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
38 setContentView(R.layout.activity_main);
39
40 Paint p = new Paint();
41 p.setColor(new Color(0, 1, 0, 1));
42
43 /*
44 * Draw into a Java Bitmap through software using Skia's native API.
45 * Load the Bitmap into an ImageView.
46 * Applies Matrix transformations to canvas
47 */
48 Bitmap.Config conf = Bitmap.Config.ARGB_8888;
49 Bitmap bmp = Bitmap.createBitmap(400, 400, conf);
50 Surface bitmapSurface = new Surface(bmp);
51 Canvas canvas = bitmapSurface.getCanvas();
52
53 canvas.drawRect(0, 0, 100, 100, p);
54
55 float[] m = {1, 0, 0, 100,
56 0, 1, 0, 100,
57 0, 0, 1, 0,
58 0, 0, 0, 1};
59 p.setColor(new Color(0, 0, 1, 1));
60 canvas.save();
61 canvas.concat(m);
62 canvas.drawRect(0, 0, 100, 100, p);
63 canvas.restore();
64
65 Image snapshot = bitmapSurface.makeImageSnapshot();
66 canvas.drawImage(snapshot, 0, 200);
67
68 try {
69 Image image = Image.fromStream(getResources().openRawResource(R.raw.brickwork_texture));
70 // TODO: Canvas.scale
71 canvas.concat(new Matrix().scale(10, 10));
72 canvas.drawImage(image, 20, 0, SamplingOptions.CATMULLROM());
73 } catch (Exception e) {
74 Log.e("JetSki Demo", "Could not load Image resource: " + R.raw.brickwork_texture);
75 }
76 bitmapImage = findViewById(R.id.bitmapImage);
77 bitmapImage.setImageBitmap(bmp);
78
79 /*
80 * Draw into a SurfaceView's surface with GL
81 * The ThreadedSurface is handled by JetSki through native code
82 */
83 SurfaceView surfaceView = findViewById(R.id.threadedSurface);
84 surfaceView.getHolder().addCallback(new ThreadedSurfaceHandler());
85
86 /*
87 * Draw into a SurfaceView's surface with GL
88 * The thread is handled using a util RenderThread provided by JetSki
89 */
90 SurfaceView runtimeEffectView = findViewById(R.id.runtimeEffect);
91 runtimeEffectView.getHolder().addCallback(new DemoRuntimeShaderRenderer());
92
93 /*
94 * SkottieView added programmatically to view hierarchy
95 */
96 SkottieView skottieView = new SkottieView(this, R.raw.im_thirsty, new Color(1, 1, 1, 1));
97 skottieView.setLayoutParams(new ViewGroup.LayoutParams(400, 400));
98 skottieView.setOnClickListener((View v) -> {
100 s.pause();
101 });
102 LinearLayout skottieContainer = findViewById(R.id.skottie_container);
103 skottieContainer.addView(skottieView);
104 }
105
106 private class ThreadedSurfaceHandler implements SurfaceHolder.Callback {
107 @Override
108 public void surfaceCreated(@NonNull SurfaceHolder holder) {}
109
110 @Override
111 public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
112 threadedSurface = Surface.createThreadedSurface(holder.getSurface());
113 Paint p = new Paint();
114 p.setColor(new Color(1, 1, 0, 1));
115 p.setStrokeWidth(15);
116 p.setStrokeCap(Paint.Cap.ROUND);
117 p.setStrokeJoin(Paint.Join.ROUND);
118 p.setStrokeMiter(4);
119// ImageFilter filter = ImageFilter.distantLitDiffuse(.5f, .5f, .5f, new Color(1, 0, 0, 1), 1, 1, null);
120// ImageFilter filter = ImageFilter.blur(10, 10, TileMode.DECAL, null);
121 ImageFilter filter = ImageFilter.dropShadow(10, 10, 10, 10, new Color(1, 0, 0, 1), null);
122// ImageFilter filter2 = ImageFilter.blend(BlendMode.DIFFERENCE, null, filter);
123 p.setImageFilter(filter);
124 PathBuilder pathBuilder = new PathBuilder();
125 pathBuilder.moveTo(20, 20);
126 pathBuilder.quadTo(180, 60, 180, 180);
127 pathBuilder.close();
128 pathBuilder.moveTo(180, 60);
129 pathBuilder.quadTo(180, 180, 60, 180);
130 Path path = pathBuilder.makePath();
131 threadedSurface.getCanvas().drawPath(path, p);
132 threadedSurface.flushAndSubmit();
133 }
134
135 @Override
136 public void surfaceDestroyed(@NonNull SurfaceHolder holder) {}
137 }
138
140 private RuntimeShaderBuilder mBuilder = new RuntimeShaderBuilder(SkSLShader);
141
142 private static final String SkSLShader =
143 "uniform half u_time; " +
144 "uniform half u_w; " +
145 "uniform half u_h; " +
146
147 "float f(vec3 p) { " +
148 " p.z -= u_time * 10.; " +
149 " float a = p.z * .1; " +
150 " p.xy *= mat2(cos(a), sin(a), -sin(a), cos(a)); " +
151 " return .1 - length(cos(p.xy) + sin(p.yz)); " +
152 "} " +
153
154 "half4 main(vec2 fragcoord) { " +
155 " vec3 d = .5 - fragcoord.xy1 / u_h; " +
156 " vec3 p=vec3(0); " +
157 " for (int i = 0; i < 32; i++) p += f(p) * d; " +
158 " return ((sin(p) + vec3(2, 5, 9)) / length(p)).xyz1;" +
159 "}";
160
161 @Override
163
164 @Override
165 protected void onRenderFrame(Canvas canvas, long ms) {
166 final int w = canvas.getWidth();
167 final int h = canvas.getHeight();
168
169 Paint p = new Paint();
170 p.setShader(mBuilder.setUniform("u_time", ms/1000.0f)
171 .setUniform("u_w", w)
172 .setUniform("u_h", h)
173 .makeShader());
174
175 canvas.drawRect(0, 0, w, h, p);
176 }
177 }
178}
void drawImage(Image image, float x, float y)
Definition: Canvas.java:116
void drawRect(float left, float top, float right, float bottom, Paint paint)
Definition: Canvas.java:95
void concat(Matrix m)
Definition: Canvas.java:49
void drawPath(Path path, Paint paint)
Definition: Canvas.java:125
static ImageFilter dropShadow(float dx, float dy, float sigmaX, float sigmaY, Color c, @Nullable ImageFilter input)
static Image fromStream(InputStream encodedStream)
Definition: Image.java:33
void quadTo(float x1, float y1, float x2, float y2)
void moveTo(float x, float y)
RuntimeShaderBuilder setUniform(String name, float val)
static SamplingOptions CATMULLROM()
static Surface createThreadedSurface(android.view.Surface surface)
Definition: Surface.java:39
void onCreate(Bundle savedInstanceState)
VkSurfaceKHR surface
Definition: main.cc:49
struct MyStruct s
uint32_t uint32_t * format
#define R(r)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkShader > Color(SkColor)
void Log(const char *format,...) SK_PRINTF_LIKE(1
Definition: TestRunner.cpp:137
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
std::function< void(MTLRenderPipelineDescriptor *)> Callback
SK_API sk_sp< PrecompileColorFilter > Matrix()
SkScalar w
SkScalar h
int32_t height
int32_t width
const Scalar scale