| 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 | #include "flutter/testing/test_vulkan_surface.h" |
| 6 | #include <memory> |
| 7 | #include "flutter/fml/logging.h" |
| 8 | #include "flutter/testing/test_vulkan_context.h" |
| 9 | |
| 10 | #include "third_party/skia/include/core/SkColorSpace.h" |
| 11 | #include "third_party/skia/include/core/SkColorType.h" |
| 12 | #include "third_party/skia/include/core/SkSurface.h" |
| 13 | #include "third_party/skia/include/core/SkSurfaceProps.h" |
| 14 | #include "third_party/skia/include/gpu/GrBackendSurface.h" |
| 15 | #include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h" |
| 16 | |
| 17 | namespace flutter { |
| 18 | namespace testing { |
| 19 | |
| 20 | TestVulkanSurface::TestVulkanSurface(TestVulkanImage&& image) |
| 21 | : image_(std::move(image)){}; |
| 22 | |
| 23 | std::unique_ptr<TestVulkanSurface> TestVulkanSurface::Create( |
| 24 | const TestVulkanContext& context, |
| 25 | const SkISize& surface_size) { |
| 26 | auto image_result = context.CreateImage(size: surface_size); |
| 27 | |
| 28 | if (!image_result.has_value()) { |
| 29 | FML_LOG(ERROR) << "Could not create VkImage." ; |
| 30 | return nullptr; |
| 31 | } |
| 32 | |
| 33 | GrVkImageInfo image_info = { |
| 34 | .fImage = image_result.value().GetImage(), |
| 35 | .fImageTiling = VK_IMAGE_TILING_OPTIMAL, |
| 36 | .fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED, |
| 37 | .fFormat = VK_FORMAT_R8G8B8A8_UNORM, |
| 38 | .fImageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 39 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 40 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 41 | VK_IMAGE_USAGE_SAMPLED_BIT, |
| 42 | .fSampleCount = 1, |
| 43 | .fLevelCount = 1, |
| 44 | }; |
| 45 | GrBackendTexture backend_texture(surface_size.width(), // |
| 46 | surface_size.height(), // |
| 47 | image_info // |
| 48 | ); |
| 49 | |
| 50 | SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry); |
| 51 | |
| 52 | auto result = std::unique_ptr<TestVulkanSurface>( |
| 53 | new TestVulkanSurface(std::move(image_result.value()))); |
| 54 | result->surface_ = SkSurfaces::WrapBackendTexture( |
| 55 | context: context.GetGrDirectContext().get(), // context |
| 56 | backendTexture: backend_texture, // back-end texture |
| 57 | origin: kTopLeft_GrSurfaceOrigin, // surface origin |
| 58 | sampleCnt: 1, // sample count |
| 59 | colorType: kRGBA_8888_SkColorType, // color type |
| 60 | colorSpace: SkColorSpace::MakeSRGB(), // color space |
| 61 | surfaceProps: &surface_properties, // surface properties |
| 62 | textureReleaseProc: nullptr, // release proc |
| 63 | releaseContext: nullptr // release context |
| 64 | ); |
| 65 | |
| 66 | if (!result->surface_) { |
| 67 | FML_LOG(ERROR) |
| 68 | << "Could not wrap VkImage as an SkSurface Vulkan render texture." ; |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | bool TestVulkanSurface::IsValid() const { |
| 76 | return surface_ != nullptr; |
| 77 | } |
| 78 | |
| 79 | sk_sp<SkImage> TestVulkanSurface::GetSurfaceSnapshot() const { |
| 80 | if (!IsValid()) { |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
| 84 | if (!surface_) { |
| 85 | FML_LOG(ERROR) << "Aborting snapshot because of on-screen surface " |
| 86 | "acquisition failure." ; |
| 87 | return nullptr; |
| 88 | } |
| 89 | |
| 90 | auto device_snapshot = surface_->makeImageSnapshot(); |
| 91 | |
| 92 | if (!device_snapshot) { |
| 93 | FML_LOG(ERROR) << "Could not create the device snapshot while attempting " |
| 94 | "to snapshot the Vulkan surface." ; |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | auto host_snapshot = device_snapshot->makeRasterImage(); |
| 99 | |
| 100 | if (!host_snapshot) { |
| 101 | FML_LOG(ERROR) << "Could not create the host snapshot while attempting to " |
| 102 | "snapshot the Vulkan surface." ; |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | return host_snapshot; |
| 107 | } |
| 108 | |
| 109 | VkImage TestVulkanSurface::GetImage() { |
| 110 | return image_.GetImage(); |
| 111 | } |
| 112 | |
| 113 | } // namespace testing |
| 114 | } // namespace flutter |
| 115 | |