Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ImageReaderPlatformViewRenderTargetTest.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 io.flutter.plugin.platform;
6
7import static android.os.Looper.getMainLooper;
8import static io.flutter.Build.API_LEVELS;
9import static org.junit.Assert.*;
10import static org.mockito.ArgumentMatchers.*;
11import static org.mockito.Mockito.*;
12import static org.robolectric.Shadows.shadowOf;
13
14import android.annotation.TargetApi;
15import android.content.Context;
16import android.graphics.Canvas;
17import android.graphics.Color;
18import android.graphics.PorterDuff;
19import android.media.Image;
20import android.view.Surface;
21import android.view.View;
22import androidx.test.core.app.ApplicationProvider;
23import androidx.test.ext.junit.runners.AndroidJUnit4;
24import io.flutter.view.TextureRegistry.ImageTextureEntry;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27
28@TargetApi(API_LEVELS.API_29)
29@RunWith(AndroidJUnit4.class)
31 private final Context ctx = ApplicationProvider.getApplicationContext();
32
33 class TestImageTextureEntry implements ImageTextureEntry {
34 private Image lastPushedImage;
35
36 public long id() {
37 return 1;
38 }
39
40 public void release() {
41 if (this.lastPushedImage != null) {
42 this.lastPushedImage.close();
43 }
44 }
45
46 public void pushImage(Image image) {
47 if (this.lastPushedImage != null) {
48 this.lastPushedImage.close();
49 }
50 this.lastPushedImage = image;
51 }
52
53 public Image acquireLatestImage() {
54 Image r = this.lastPushedImage;
55 this.lastPushedImage = null;
56 return r;
57 }
58 }
59
60 @Test
62 final TestImageTextureEntry textureEntry = new TestImageTextureEntry();
63 final ImageReaderPlatformViewRenderTarget renderTarget =
65 // Custom view.
66 final View platformView =
67 new View(ctx) {
68 @Override
69 public void draw(Canvas canvas) {
70 super.draw(canvas);
71 canvas.drawColor(Color.RED);
72 }
73 };
74 final int size = 100;
75 platformView.measure(size, size);
76 platformView.layout(0, 0, size, size);
77 renderTarget.resize(size, size);
78
79 // We don't have an image in the texture entry.
80 assertNull(textureEntry.acquireLatestImage());
81
82 // Start rendering a frame.
83 final Surface s = renderTarget.getSurface();
84 assertNotNull(s);
85 final Canvas targetCanvas = s.lockHardwareCanvas();
86 assertNotNull(targetCanvas);
87
88 try {
89 // Fill the render target with transparent pixels. This is needed for platform views that
90 // expect a transparent background.
91 targetCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
92 // Override the canvas that this subtree of views will use to draw.
93 platformView.draw(targetCanvas);
94 } finally {
95 // Finish rendering a frame.
96 s.unlockCanvasAndPost(targetCanvas);
97 }
98
99 // Pump the UI thread task loop. This is needed so that the OnImageAvailable callback
100 // gets invoked (resulting in textureEntry.pushImage being invoked).
101 shadowOf(getMainLooper()).idle();
102
103 // An image was pushed into the texture entry and it has the correct dimensions.
104 Image pushedImage = textureEntry.acquireLatestImage();
105 assertNotNull(pushedImage);
106 assertEquals(pushedImage.getWidth(), size);
107 assertEquals(pushedImage.getHeight(), size);
108 }
109}
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
sk_sp< SkImage > image
Definition examples.cpp:29
struct MyStruct s