Flutter Engine
The Flutter Engine
TexturePlatformViewFactory.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package dev.flutter.scenarios;
6
7import static io.flutter.Build.API_LEVELS;
8
9import android.annotation.TargetApi;
10import android.content.Context;
11import android.graphics.Canvas;
12import android.graphics.Color;
13import android.graphics.Paint;
14import android.graphics.SurfaceTexture;
15import android.view.Choreographer;
16import android.view.TextureView;
17import android.view.View;
18import androidx.annotation.NonNull;
19import androidx.annotation.Nullable;
20import io.flutter.Log;
21import io.flutter.plugin.common.MessageCodec;
22import io.flutter.plugin.common.StringCodec;
23import io.flutter.plugin.platform.PlatformView;
24import io.flutter.plugin.platform.PlatformViewFactory;
25import java.nio.ByteBuffer;
26
27@TargetApi(API_LEVELS.API_23)
30 super(
32 @Nullable
33 @Override
34 public ByteBuffer encodeMessage(@Nullable Object o) {
35 if (o instanceof String) {
36 return StringCodec.INSTANCE.encodeMessage((String) o);
37 }
38 return null;
39 }
40
41 @Nullable
42 @Override
43 public Object decodeMessage(@Nullable ByteBuffer byteBuffer) {
44 return StringCodec.INSTANCE.decodeMessage(byteBuffer);
45 }
46 });
47 }
48
49 @SuppressWarnings("unchecked")
50 @Override
51 @NonNull
52 public PlatformView create(@NonNull Context context, int id, @Nullable Object args) {
53 return new TexturePlatformView(context);
54 }
55
56 private static class TexturePlatformView implements PlatformView {
57 static String TAG = "TexturePlatformView";
58
59 final TextureView textureView;
60
61 @SuppressWarnings("unchecked")
62 TexturePlatformView(@NonNull final Context context) {
63 textureView = new TextureView(context);
64 textureView.setSurfaceTextureListener(
65 new TextureView.SurfaceTextureListener() {
66 @Override
67 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
68 Log.i(TAG, "onSurfaceTextureAvailable");
69 final Canvas canvas = textureView.lockCanvas();
70 canvas.drawColor(Color.WHITE);
71
72 final Paint paint = new Paint();
73 paint.setColor(Color.GREEN);
74 canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 20, paint);
75 textureView.unlockCanvasAndPost(canvas);
76 Choreographer.getInstance()
77 .postFrameCallbackDelayed(
78 new Choreographer.FrameCallback() {
79 @Override
80 public void doFrame(long frameTimeNanos) {
81 textureView.invalidate();
82 }
83 },
84 500);
85 }
86
87 @Override
88 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
89 Log.i(TAG, "onSurfaceTextureDestroyed");
90 return true;
91 }
92
93 @Override
94 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
95 Log.i(TAG, "onSurfaceTextureSizeChanged");
96 }
97
98 @Override
99 public void onSurfaceTextureUpdated(SurfaceTexture surface) {
100 Log.i(TAG, "onSurfaceTextureUpdated");
101 }
102 });
103 }
104
105 @Override
106 @NonNull
107 public View getView() {
108 return textureView;
109 }
110
111 @Override
112 public void dispose() {}
113 }
114}
PlatformView create(@NonNull Context context, int id, @Nullable Object args)
static void i(@NonNull String tag, @NonNull String message)
Definition: Log.java:52
static final StringCodec INSTANCE
ByteBuffer encodeMessage(@Nullable String message)
String decodeMessage(@Nullable ByteBuffer message)
VkSurfaceKHR surface
Definition: main.cc:49
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
void Log(const char *format,...) SK_PRINTF_LIKE(1
Definition: TestRunner.cpp:137
#define TAG()
int32_t height
int32_t width