| Flutter Impeller |

vulkan_screenshotter.mm
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
5 #include "flutter/impeller/golden_tests/vulkan_screenshotter.h"
6
7 #include "flutter/fml/synchronization/waitable_event.h"
8 #include "flutter/impeller/golden_tests/metal_screenshot.h"
9 #define GLFW_INCLUDE_NONE
10 #include "third_party/glfw/include/GLFW/glfw3.h"
11
12 namespace impeller {
13 namespace testing {
14
15 namespace {
16
17 using CGContextPtr = std::unique_ptr<std::remove_pointer<CGContextRef>::type,
18 decltype(&CGContextRelease)>;
19 using CGImagePtr = std::unique_ptr<std::remove_pointer<CGImageRef>::type,
20 decltype(&CGImageRelease)>;
21 using CGColorSpacePtr =
22 std::unique_ptr<std::remove_pointer<CGColorSpaceRef>::type,
23 decltype(&CGColorSpaceRelease)>;
24
25 std::unique_ptr<Screenshot> ReadTexture(
26 const std::shared_ptr<Context>& surface_context,
27 const std::shared_ptr<Texture>& texture) {
28 DeviceBufferDescriptor buffer_desc;
29 buffer_desc.storage_mode = StorageMode::kHostVisible;
30 buffer_desc.size =
31 texture->GetTextureDescriptor().GetByteSizeOfBaseMipLevel();
32 buffer_desc.readback = true;
33 std::shared_ptr<DeviceBuffer> device_buffer =
34 surface_context->GetResourceAllocator()->CreateBuffer(buffer_desc);
35 FML_CHECK(device_buffer);
36
37 auto command_buffer = surface_context->CreateCommandBuffer();
38 auto blit_pass = command_buffer->CreateBlitPass();
39 bool success = blit_pass->AddCopy(texture, device_buffer);
40 FML_CHECK(success);
41
42 success = blit_pass->EncodeCommands();
43 FML_CHECK(success);
44
45 fml::AutoResetWaitableEvent latch;
46 success =
47 surface_context->GetCommandQueue()
48 ->Submit({command_buffer},
49 [&latch](CommandBuffer::Status status) {
50 FML_CHECK(status == CommandBuffer::Status::kCompleted);
51 latch.Signal();
52 })
53 .ok();
54 FML_CHECK(success);
55 latch.Wait();
56 device_buffer->Invalidate();
57
58 // TODO(gaaclarke): Replace CoreImage requirement with something
59 // crossplatform.
60
61 CGColorSpacePtr color_space(CGColorSpaceCreateDeviceRGB(),
62 &CGColorSpaceRelease);
63 CGBitmapInfo bitmap_info =
64 texture->GetTextureDescriptor().format == PixelFormat::kB8G8R8A8UNormInt
65 ? static_cast<uint32_t>(kCGImageAlphaPremultipliedFirst) |
66 static_cast<uint32_t>(kCGBitmapByteOrder32Little)
67 : kCGImageAlphaPremultipliedLast;
68 CGContextPtr context(
69 CGBitmapContextCreate(
70 device_buffer->OnGetContents(), texture->GetSize().width,
71 texture->GetSize().height,
72 /*bitsPerComponent=*/8,
73 /*bytesPerRow=*/texture->GetTextureDescriptor().GetBytesPerRow(),
74 color_space.get(), bitmap_info),
75 &CGContextRelease);
76 FML_CHECK(context);
77 CGImagePtr image(CGBitmapContextCreateImage(context.get()), &CGImageRelease);
78 FML_CHECK(image);
79
80 return std::make_unique<MetalScreenshot>(image.release());
81 }
82 } // namespace
83
84 VulkanScreenshotter::VulkanScreenshotter(
85 const std::unique_ptr<PlaygroundImpl>& playground)
86 : playground_(playground) {
87 FML_CHECK(playground_);
88 }
89
90 std::unique_ptr<Screenshot> VulkanScreenshotter::MakeScreenshot(
91 AiksContext& aiks_context,
92 const std::shared_ptr<Texture> texture) {
93 return ReadTexture(aiks_context.GetContext(), texture);
94 }
95
96 } // namespace testing
97 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:153
impeller::AiksContext
Definition: aiks_context.h:20
impeller::AiksContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: aiks_context.cc:38
impeller::CommandBuffer::Status
Status
Definition: command_buffer.h:49
impeller::CommandBuffer::Status::kCompleted
@ kCompleted
impeller::testing::VulkanScreenshotter::VulkanScreenshotter
VulkanScreenshotter(const std::unique_ptr< PlaygroundImpl > &playground)
Definition: vulkan_screenshotter.mm:84
impeller::testing::VulkanScreenshotter::MakeScreenshot
std::unique_ptr< Screenshot > MakeScreenshot(AiksContext &aiks_context, const std::shared_ptr< Texture > texture) override
Definition: vulkan_screenshotter.mm:90
metal_screenshot.h
impeller
Definition: allocation.cc:12
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::PixelFormat::kB8G8R8A8UNormInt
@ kB8G8R8A8UNormInt
vulkan_screenshotter.h