Flutter Engine
embedder_test_compositor_gl.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_gl.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  sk_sp<GrDirectContext> context)
17  : EmbedderTestCompositor(surface_size, context) {}
18 
20 
21 bool EmbedderTestCompositorGL::UpdateOffscrenComposition(
22  const FlutterLayer** layers,
23  size_t layers_count) {
24  last_composition_ = nullptr;
25 
26  const auto image_info = SkImageInfo::MakeN32Premul(surface_size_);
27 
28  auto surface =
29  SkSurface::MakeRenderTarget(context_.get(), // context
30  SkBudgeted::kNo, // budgeted
31  image_info, // image info
32  1, // sample count
33  kTopLeft_GrSurfaceOrigin, // surface origin
34  nullptr, // surface properties
35  false // create mipmaps
36  );
37 
38  if (!surface) {
39  FML_LOG(ERROR) << "Could not update the off-screen composition.";
40  return false;
41  }
42 
43  auto canvas = surface->getCanvas();
44 
45  // This has to be transparent because we are going to be compositing this
46  // sub-hierarchy onto the on-screen surface.
47  canvas->clear(SK_ColorTRANSPARENT);
48 
49  for (size_t i = 0; i < layers_count; ++i) {
50  const auto* layer = layers[i];
51 
52  sk_sp<SkImage> platform_renderered_contents;
53 
54  sk_sp<SkImage> layer_image;
55  SkIPoint canvas_offset = SkIPoint::Make(0, 0);
56 
57  switch (layer->type) {
59  layer_image =
60  reinterpret_cast<SkSurface*>(layer->backing_store->user_data)
61  ->makeImageSnapshot();
62 
63  break;
65  layer_image =
68  : nullptr;
69  canvas_offset = SkIPoint::Make(layer->offset.x, layer->offset.y);
70  break;
71  };
72 
73  // If the layer is not a platform view but the engine did not specify an
74  // image for the backing store, it is an error.
75  if (!layer_image && layer->type != kFlutterLayerContentTypePlatformView) {
76  FML_LOG(ERROR) << "Could not snapshot layer in test compositor: "
77  << *layer;
78  return false;
79  }
80 
81  // The test could have just specified no contents to be rendered in place of
82  // a platform view. This is not an error.
83  if (layer_image) {
84  // The image rendered by Flutter already has the correct offset and
85  // transformation applied. The layers offset is meant for the platform.
86  canvas->drawImage(layer_image.get(), canvas_offset.x(),
87  canvas_offset.y());
88  }
89  }
90 
91  last_composition_ = surface->makeImageSnapshot();
92 
93  if (!last_composition_) {
94  FML_LOG(ERROR) << "Could not update the contents of the sub-composition.";
95  return false;
96  }
97 
99  auto last_composition_snapshot = last_composition_->makeRasterImage();
100  FML_CHECK(last_composition_snapshot);
101  auto callback = next_scene_callback_;
102  next_scene_callback_ = nullptr;
103  callback(std::move(last_composition_snapshot));
104  }
105 
106  return true;
107 }
108 
109 } // namespace testing
110 } // namespace flutter
EmbedderTestCompositorGL(SkISize surface_size, sk_sp< GrDirectContext > context)
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:877
#define FML_CHECK(condition)
Definition: logging.h:68