Flutter Engine
embedder_external_view.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/embedder_external_view.h"
6 #include "flutter/fml/trace_event.h"
7 #include "flutter/shell/common/canvas_spy.h"
8 
9 namespace flutter {
10 
11 static SkISize TransformedSurfaceSize(const SkISize& size,
12  const SkMatrix& transformation) {
13  const auto source_rect = SkRect::MakeWH(size.width(), size.height());
14  const auto transformed_rect = transformation.mapRect(source_rect);
15  return SkISize::Make(transformed_rect.width(), transformed_rect.height());
16 }
17 
19  const SkISize& frame_size,
20  const SkMatrix& surface_transformation)
21  : EmbedderExternalView(frame_size, surface_transformation, {}, nullptr) {}
22 
24  const SkISize& frame_size,
25  const SkMatrix& surface_transformation,
26  ViewIdentifier view_identifier,
27  std::unique_ptr<EmbeddedViewParams> params)
28  : render_surface_size_(
29  TransformedSurfaceSize(frame_size, surface_transformation)),
30  surface_transformation_(surface_transformation),
31  view_identifier_(view_identifier),
32  embedded_view_params_(std::move(params)),
33  recorder_(std::make_unique<SkPictureRecorder>()),
34  canvas_spy_(std::make_unique<CanvasSpy>(
35  recorder_->beginRecording(frame_size.width(), frame_size.height()))) {
36 }
37 
39 
42  return {view_identifier_, render_surface_size_};
43 }
44 
46  return canvas_spy_->GetSpyingCanvas();
47 }
48 
50  return render_surface_size_;
51 }
52 
54  return !HasPlatformView();
55 }
56 
58  return view_identifier_.platform_view_id.has_value();
59 }
60 
62  return canvas_spy_->DidDrawIntoCanvas();
63 }
64 
66  const {
67  return view_identifier_;
68 }
69 
71  return embedded_view_params_.get();
72 }
73 
75  TRACE_EVENT0("flutter", "EmbedderExternalView::Render");
76 
78  << "Unnecessarily asked to render into a render target when there was "
79  "nothing to render.";
80 
81  auto picture = recorder_->finishRecordingAsPicture();
82  if (!picture) {
83  return false;
84  }
85 
86  auto surface = render_target.GetRenderSurface();
87  if (!surface) {
88  return false;
89  }
90 
91  FML_DCHECK(SkISize::Make(surface->width(), surface->height()) ==
92  render_surface_size_);
93 
94  auto canvas = surface->getCanvas();
95  if (!canvas) {
96  return false;
97  }
98 
99  canvas->setMatrix(surface_transformation_);
100  canvas->clear(SK_ColorTRANSPARENT);
101  canvas->drawPicture(picture);
102  canvas->flush();
103 
104  return true;
105 }
106 
107 } // namespace flutter
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
#define FML_DCHECK(condition)
Definition: logging.h:86
ViewIdentifier GetViewIdentifier() const
static SkISize TransformedSurfaceSize(const SkISize &size, const SkMatrix &transformation)
Definition: ref_ptr.h:252
const EmbeddedViewParams * GetEmbeddedViewParams() const
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
Describes a surface whose backing store is managed by the embedder. The type of surface depends on th...
RenderTargetDescriptor CreateRenderTargetDescriptor() const
sk_sp< SkSurface > GetRenderSurface() const
A render surface the rasterizer can use to draw into the backing store of this render target...
int32_t height
int32_t width
EmbedderExternalView(const SkISize &frame_size, const SkMatrix &surface_transformation)
std::optional< PlatformViewID > platform_view_id
bool Render(const EmbedderRenderTarget &render_target)