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/lib/ui/painting/picture_recorder.h"
6
7#include "flutter/lib/ui/painting/canvas.h"
8#include "flutter/lib/ui/painting/picture.h"
9#include "third_party/tonic/converter/dart_converter.h"
10#include "third_party/tonic/dart_args.h"
11#include "third_party/tonic/dart_binding_macros.h"
12#include "third_party/tonic/dart_library_natives.h"
13
14namespace flutter {
15
16static void PictureRecorder_constructor(Dart_NativeArguments args) {
17 UIDartState::ThrowIfUIOperationsProhibited();
18 DartCallConstructor(&PictureRecorder::Create, args);
19}
20
21IMPLEMENT_WRAPPERTYPEINFO(ui, PictureRecorder);
22
23#define FOR_EACH_BINDING(V) V(PictureRecorder, endRecording)
24
25FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
26
27void PictureRecorder::RegisterNatives(tonic::DartLibraryNatives* natives) {
28 natives->Register(
29 {{"PictureRecorder_constructor", PictureRecorder_constructor, 1, true},
30 FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
31}
32
33fml::RefPtr<PictureRecorder> PictureRecorder::Create() {
34 return fml::MakeRefCounted<PictureRecorder>();
35}
36
37PictureRecorder::PictureRecorder() {}
38
39PictureRecorder::~PictureRecorder() {}
40
41SkCanvas* PictureRecorder::BeginRecording(SkRect bounds) {
42 return picture_recorder_.beginRecording(bounds, &rtree_factory_);
43}
44
45fml::RefPtr<Picture> PictureRecorder::endRecording(Dart_Handle dart_picture) {
46 if (!canvas_) {
47 return nullptr;
48 }
49
50 fml::RefPtr<Picture> picture =
51 Picture::Create(dart_picture,
52 UIDartState::CreateGPUObject(
53 picture_recorder_.finishRecordingAsPicture()),
54 canvas_->external_allocation_size());
55
56 canvas_->Invalidate();
57 canvas_ = nullptr;
58 ClearDartWrapper();
59 return picture;
60}
61
62} // namespace flutter
63