|
| 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