Flutter Engine
embedder_test_compositor_software.cc
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/shell/platform/embedder/tests/embedder_test_compositor_software.h"
6 
7 #include "flutter/fml/logging.h"
8 #include "flutter/shell/platform/embedder/tests/embedder_assertions.h"
9 #include "third_party/skia/include/core/SkSurface.h"
10 
11 namespace flutter {
12 namespace testing {
13 
15  SkISize surface_size)
16  : EmbedderTestCompositor(surface_size, nullptr) {}
17 
19 
20 bool EmbedderTestCompositorSoftware::UpdateOffscrenComposition(
21  const FlutterLayer** layers,
22  size_t layers_count) {
23  last_composition_ = nullptr;
24 
25  const auto image_info = SkImageInfo::MakeN32Premul(surface_size_);
26 
27  auto surface = SkSurface::MakeRaster(image_info);
28 
29  if (!surface) {
30  FML_LOG(ERROR) << "Could not update the off-screen composition.";
31  return false;
32  }
33 
34  auto canvas = surface->getCanvas();
35 
36  // This has to be transparent because we are going to be compositing this
37  // sub-hierarchy onto the on-screen surface.
38  canvas->clear(SK_ColorTRANSPARENT);
39 
40  for (size_t i = 0; i < layers_count; ++i) {
41  const auto* layer = layers[i];
42 
43  sk_sp<SkImage> platform_renderered_contents;
44 
45  sk_sp<SkImage> layer_image;
46  SkIPoint canvas_offset = SkIPoint::Make(0, 0);
47 
48  switch (layer->type) {
50  layer_image =
51  reinterpret_cast<SkSurface*>(layer->backing_store->user_data)
52  ->makeImageSnapshot();
53 
54  break;
57  ? platform_view_renderer_callback_(*layer, nullptr)
58  : nullptr;
59  canvas_offset = SkIPoint::Make(layer->offset.x, layer->offset.y);
60  break;
61  };
62 
63  // If the layer is not a platform view but the engine did not specify an
64  // image for the backing store, it is an error.
65  if (!layer_image && layer->type != kFlutterLayerContentTypePlatformView) {
66  FML_LOG(ERROR) << "Could not snapshot layer in test compositor: "
67  << *layer;
68  return false;
69  }
70 
71  // The test could have just specified no contents to be rendered in place of
72  // a platform view. This is not an error.
73  if (layer_image) {
74  // The image rendered by Flutter already has the correct offset and
75  // transformation applied. The layers offset is meant for the platform.
76  canvas->drawImage(layer_image.get(), canvas_offset.x(),
77  canvas_offset.y());
78  }
79  }
80 
81  last_composition_ = surface->makeImageSnapshot();
82 
83  if (!last_composition_) {
84  FML_LOG(ERROR) << "Could not update the contents of the sub-composition.";
85  return false;
86  }
87 
89  auto last_composition_snapshot = last_composition_->makeRasterImage();
90  FML_CHECK(last_composition_snapshot);
91  auto callback = next_scene_callback_;
92  next_scene_callback_ = nullptr;
93  callback(std::move(last_composition_snapshot));
94  }
95 
96  return true;
97 }
98 
99 } // namespace testing
100 } // namespace flutter
PlatformViewRendererCallback platform_view_renderer_callback_
#define FML_LOG(severity)
Definition: logging.h:65
Indicates that the contents of this layer are determined by the embedder.
Definition: embedder.h:881
#define FML_CHECK(condition)
Definition: logging.h:68