Flutter Engine
 
Loading...
Searching...
No Matches
image_encoding_impl.h
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#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_ENCODING_IMPL_H_
6#define FLUTTER_LIB_UI_PAINTING_IMAGE_ENCODING_IMPL_H_
7
9#include "third_party/skia/include/core/SkCanvas.h"
10#include "third_party/skia/include/core/SkImage.h"
11#include "third_party/skia/include/core/SkSurface.h"
12#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
13#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
14
15namespace flutter {
16
17template <typename SyncSwitch>
19 const sk_sp<SkImage>& image,
20 const fml::WeakPtr<GrDirectContext>& resource_context,
21 const std::shared_ptr<const SyncSwitch>& is_gpu_disabled_sync_switch) {
22 sk_sp<SkSurface> surface;
23 SkImageInfo surface_info = SkImageInfo::MakeN32Premul(image->dimensions());
24
25 is_gpu_disabled_sync_switch->Execute(
26 typename SyncSwitch::Handlers()
27 .SetIfTrue([&surface, &surface_info] {
28 surface = SkSurfaces::Raster(surface_info);
29 })
30 .SetIfFalse([&surface, &surface_info, resource_context] {
31 if (resource_context) {
32 surface = SkSurfaces::RenderTarget(
33 resource_context.get(), skgpu::Budgeted::kNo, surface_info);
34 } else {
35 surface = SkSurfaces::Raster(surface_info);
36 }
37 }));
38
39 if (surface == nullptr || surface->getCanvas() == nullptr) {
40 FML_LOG(ERROR) << "Could not create a surface to copy the texture into.";
41 return nullptr;
42 }
43
44 surface->getCanvas()->drawImage(image, 0, 0);
45 if (resource_context) {
46 resource_context->flushAndSubmit();
47 }
48
49 auto snapshot = surface->makeImageSnapshot();
50
51 if (snapshot == nullptr) {
52 FML_LOG(ERROR) << "Could not snapshot image to encode.";
53 return nullptr;
54 }
55
56 return snapshot->makeRasterImage(resource_context.get());
57}
58
59} // namespace flutter
60
61#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_ENCODING_IMPL_H_
T * get() const
Definition weak_ptr.h:87
FlutterVulkanImage * image
VkSurfaceKHR surface
Definition main.cc:65
#define FML_LOG(severity)
Definition logging.h:101
sk_sp< SkImage > ConvertToRasterUsingResourceContext(const sk_sp< SkImage > &image, const fml::WeakPtr< GrDirectContext > &resource_context, const std::shared_ptr< const SyncSwitch > &is_gpu_disabled_sync_switch)