Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a855fa4

Browse files
committed
Add test for external textures on macOS
1 parent 0d79389 commit a855fa4

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

shell/platform/darwin/macos/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ executable("flutter_desktop_darwin_unittests") {
154154

155155
if (shell_enable_metal) {
156156
sources += [
157+
"framework/Source/FlutterEmbedderExternalTextureUnittests.mm",
157158
"framework/Source/FlutterMetalRendererTest.mm",
158159
"framework/Source/FlutterMetalSurfaceManagerTest.mm",
159160
]
@@ -169,6 +170,7 @@ executable("flutter_desktop_darwin_unittests") {
169170
":flutter_desktop_darwin_fixtures",
170171
":flutter_framework_source",
171172
"//flutter/shell/platform/darwin/common:framework_shared",
173+
"//flutter/shell/platform/darwin/graphics",
172174
"//flutter/shell/platform/embedder:embedder_as_internal_library",
173175
"//flutter/shell/platform/embedder:embedder_test_utils",
174176
"//flutter/testing",
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
5+
#import <Foundation/Foundation.h>
6+
#import <Metal/Metal.h>
7+
8+
#include <memory>
9+
#include <vector>
10+
11+
#import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h"
12+
#import "flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h"
13+
#include "flutter/shell/platform/embedder/embedder.h"
14+
#include "flutter/shell/platform/embedder/embedder_external_texture_metal.h"
15+
#import "flutter/testing/testing.h"
16+
#include "third_party/googletest/googletest/include/gtest/gtest.h"
17+
#include "third_party/skia/include/core/SkImage.h"
18+
#include "third_party/skia/include/core/SkSamplingOptions.h"
19+
#include "third_party/skia/include/core/SkSurface.h"
20+
21+
namespace flutter::testing {
22+
23+
TEST(FlutterEmbedderExternalTextureUnittests, TestTextureResolution) {
24+
// constants.
25+
const size_t width = 100;
26+
const size_t height = 100;
27+
const int64_t texture_id = 1;
28+
29+
// setup the surface.
30+
FlutterDarwinContextMetal* darwinContextMetal =
31+
[[FlutterDarwinContextMetal alloc] initWithDefaultMTLDevice];
32+
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
33+
GrDirectContext* grContext = darwinContextMetal.mainContext.get();
34+
sk_sp<SkSurface> gpuSurface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
35+
36+
// create a texture.
37+
MTLTextureDescriptor* textureDescriptor = [[MTLTextureDescriptor alloc] init];
38+
textureDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm;
39+
textureDescriptor.width = width;
40+
textureDescriptor.height = height;
41+
textureDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
42+
id<MTLTexture> mtlTexture =
43+
[darwinContextMetal.device newTextureWithDescriptor:textureDescriptor];
44+
45+
// callback to resolve the texture.
46+
EmbedderExternalTextureMetal::ExternalTextureCallback callback = [&](int64_t texture_id, size_t w,
47+
size_t h) {
48+
EXPECT_TRUE(w == width);
49+
EXPECT_TRUE(h == height);
50+
51+
FlutterMetalExternalTexture* texture = new FlutterMetalExternalTexture();
52+
texture->struct_size = sizeof(FlutterMetalExternalTexture);
53+
texture->num_textures = 1;
54+
texture->height = h;
55+
texture->width = w;
56+
texture->pixel_format = FlutterMetalExternalTexturePixelFormat::kRGBA;
57+
58+
std::vector<FlutterMetalTextureHandle> textures = {
59+
(__bridge FlutterMetalTextureHandle)mtlTexture,
60+
};
61+
62+
texture->textures = textures.data();
63+
64+
return std::unique_ptr<FlutterMetalExternalTexture>(texture);
65+
};
66+
67+
// render the texture.
68+
std::unique_ptr<flutter::Texture> texture =
69+
std::make_unique<EmbedderExternalTextureMetal>(texture_id, callback);
70+
SkRect bounds = SkRect::MakeWH(info.width(), info.height());
71+
SkSamplingOptions sampling = SkSamplingOptions(SkFilterMode::kNearest);
72+
texture->Paint(*gpuSurface->getCanvas(), bounds, /*freeze=*/false, grContext, sampling);
73+
74+
ASSERT_TRUE(mtlTexture != nil);
75+
76+
gpuSurface->makeImageSnapshot();
77+
}
78+
79+
} // namespace flutter::testing

0 commit comments

Comments
 (0)