rasterizer.h 2.7 KB
Newer Older
M
Michael Goderbauer 已提交
1
// Copyright 2013 The Flutter Authors. All rights reserved.
A
Adam Barth 已提交
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef SHELL_COMMON_RASTERIZER_H_
#define SHELL_COMMON_RASTERIZER_H_
A
Adam Barth 已提交
7 8

#include <memory>
A
Adam Barth 已提交
9

10
#include "flutter/common/task_runners.h"
11
#include "flutter/flow/compositor_context.h"
12
#include "flutter/flow/layers/layer_tree.h"
13
#include "flutter/fml/closure.h"
14
#include "flutter/fml/memory/weak_ptr.h"
15
#include "flutter/fml/synchronization/waitable_event.h"
16
#include "flutter/lib/ui/snapshot_delegate.h"
17
#include "flutter/shell/common/surface.h"
18
#include "flutter/synchronization/pipeline.h"
A
Adam Barth 已提交
19 20 21

namespace shell {

22
class Rasterizer final : public blink::SnapshotDelegate {
A
Adam Barth 已提交
23
 public:
24
  Rasterizer(blink::TaskRunners task_runners);
25

26 27 28
  Rasterizer(blink::TaskRunners task_runners,
             std::unique_ptr<flow::CompositorContext> compositor_context);

29
  ~Rasterizer();
30

31
  void Setup(std::unique_ptr<Surface> surface);
32

33
  void Teardown();
34

35
  fml::WeakPtr<Rasterizer> GetWeakPtr() const;
A
Adam Barth 已提交
36

37 38
  fml::WeakPtr<blink::SnapshotDelegate> GetSnapshotDelegate() const;

39
  flow::LayerTree* GetLastLayerTree();
40

41
  void DrawLastLayerTree();
42

43
  flow::TextureRegistry* GetTextureRegistry();
44

45
  void Draw(fml::RefPtr<flutter::Pipeline<flow::LayerTree>> pipeline);
46

47 48 49 50 51
  enum class ScreenshotType {
    SkiaPicture,
    UncompressedImage,  // In kN32_SkColorType format
    CompressedImage,
  };
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66
  struct Screenshot {
    sk_sp<SkData> data;
    SkISize frame_size = SkISize::MakeEmpty();

    Screenshot() {}

    Screenshot(sk_sp<SkData> p_data, SkISize p_size)
        : data(std::move(p_data)), frame_size(p_size) {}
  };

  Screenshot ScreenshotLastLayerTree(ScreenshotType type, bool base64_encode);

  // Sets a callback that will be executed after the next frame is submitted to
  // the surface on the GPU task runner.
67
  void SetNextFrameCallback(fml::closure callback);
68

69 70 71 72
  flow::CompositorContext* compositor_context() {
    return compositor_context_.get();
  }

73 74 75
 private:
  blink::TaskRunners task_runners_;
  std::unique_ptr<Surface> surface_;
76
  std::unique_ptr<flow::CompositorContext> compositor_context_;
77
  std::unique_ptr<flow::LayerTree> last_layer_tree_;
78
  fml::closure next_frame_callback_;
79 80
  fml::WeakPtrFactory<Rasterizer> weak_factory_;

81 82 83 84
  // |blink::SnapshotDelegate|
  sk_sp<SkImage> MakeRasterSnapshot(sk_sp<SkPicture> picture,
                                    SkISize picture_size) override;

85 86 87 88 89 90
  void DoDraw(std::unique_ptr<flow::LayerTree> layer_tree);

  bool DrawToSurface(flow::LayerTree& layer_tree);

  void FireNextFrameCallbackIfPresent();

91
  FML_DISALLOW_COPY_AND_ASSIGN(Rasterizer);
A
Adam Barth 已提交
92 93 94 95
};

}  // namespace shell

96
#endif  // SHELL_COMMON_RASTERIZER_H_