forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedded_views.h
259 lines (215 loc) · 7.73 KB
/
embedded_views.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#ifndef FLUTTER_FLOW_EMBEDDED_VIEWS_H_
#define FLUTTER_FLOW_EMBEDDED_VIEWS_H_
#include <vector>
#include "flutter/fml/gpu_thread_merger.h"
#include "flutter/fml/memory/ref_counted.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkPoint.h"
#include "third_party/skia/include/core/SkRRect.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkSize.h"
#include "third_party/skia/include/core/SkSurface.h"
namespace flutter {
// TODO(chinmaygarde): Make these enum names match the style guide.
enum MutatorType { clip_rect, clip_rrect, clip_path, transform, opacity };
// Stores mutation information like clipping or transform.
//
// The `type` indicates the type of the mutation: clip_rect, transform and etc.
// Each `type` is paired with an object that supports the mutation. For example,
// if the `type` is clip_rect, `rect()` is used the represent the rect to be
// clipped. One mutation object must only contain one type of mutation.
class Mutator {
public:
Mutator(const Mutator& other) {
type_ = other.type_;
switch (other.type_) {
case clip_rect:
rect_ = other.rect_;
break;
case clip_rrect:
rrect_ = other.rrect_;
break;
case clip_path:
path_ = new SkPath(*other.path_);
break;
case transform:
matrix_ = other.matrix_;
break;
case opacity:
alpha_ = other.alpha_;
break;
default:
break;
}
}
explicit Mutator(const SkRect& rect) : type_(clip_rect), rect_(rect) {}
explicit Mutator(const SkRRect& rrect) : type_(clip_rrect), rrect_(rrect) {}
explicit Mutator(const SkPath& path)
: type_(clip_path), path_(new SkPath(path)) {}
explicit Mutator(const SkMatrix& matrix)
: type_(transform), matrix_(matrix) {}
explicit Mutator(const int& alpha) : type_(opacity), alpha_(alpha) {}
const MutatorType& GetType() const { return type_; }
const SkRect& GetRect() const { return rect_; }
const SkRRect& GetRRect() const { return rrect_; }
const SkPath& GetPath() const { return *path_; }
const SkMatrix& GetMatrix() const { return matrix_; }
const int& GetAlpha() const { return alpha_; }
float GetAlphaFloat() const { return (alpha_ / 255.0); }
bool operator==(const Mutator& other) const {
if (type_ != other.type_) {
return false;
}
switch (type_) {
case clip_rect:
return rect_ == other.rect_;
case clip_rrect:
return rrect_ == other.rrect_;
case clip_path:
return *path_ == *other.path_;
case transform:
return matrix_ == other.matrix_;
case opacity:
return alpha_ == other.alpha_;
}
return false;
}
bool operator!=(const Mutator& other) const { return !operator==(other); }
bool IsClipType() {
return type_ == clip_rect || type_ == clip_rrect || type_ == clip_path;
}
~Mutator() {
if (type_ == clip_path) {
delete path_;
}
};
private:
MutatorType type_;
union {
SkRect rect_;
SkRRect rrect_;
SkMatrix matrix_;
SkPath* path_;
int alpha_;
};
}; // Mutator
// A stack of mutators that can be applied to an embedded platform view.
//
// The stack may include mutators like transforms and clips, each mutator
// applies to all the mutators that are below it in the stack and to the
// embedded view.
//
// For example consider the following stack: [T1, T2, T3], where T1 is the top
// of the stack and T3 is the bottom of the stack. Applying this mutators stack
// to a platform view P1 will result in T1(T2(T2(P1))).
class MutatorsStack {
public:
MutatorsStack() = default;
void PushClipRect(const SkRect& rect);
void PushClipRRect(const SkRRect& rrect);
void PushClipPath(const SkPath& path);
void PushTransform(const SkMatrix& matrix);
void PushOpacity(const int& alpha);
// Removes the `Mutator` on the top of the stack
// and destroys it.
void Pop();
// Returns an iterator pointing to the top of the stack.
const std::vector<std::shared_ptr<Mutator>>::const_reverse_iterator Top()
const;
// Returns an iterator pointing to the bottom of the stack.
const std::vector<std::shared_ptr<Mutator>>::const_reverse_iterator Bottom()
const;
bool is_empty() const { return vector_.empty(); }
bool operator==(const MutatorsStack& other) const {
if (vector_.size() != other.vector_.size()) {
return false;
}
for (size_t i = 0; i < vector_.size(); i++) {
if (*vector_[i] != *other.vector_[i]) {
return false;
}
}
return true;
}
bool operator==(const std::vector<Mutator>& other) const {
if (vector_.size() != other.size()) {
return false;
}
for (size_t i = 0; i < vector_.size(); i++) {
if (*vector_[i] != other[i]) {
return false;
}
}
return true;
}
bool operator!=(const MutatorsStack& other) const {
return !operator==(other);
}
bool operator!=(const std::vector<Mutator>& other) const {
return !operator==(other);
}
private:
std::vector<std::shared_ptr<Mutator>> vector_;
}; // MutatorsStack
class EmbeddedViewParams {
public:
EmbeddedViewParams() = default;
EmbeddedViewParams(const EmbeddedViewParams& other) {
offsetPixels = other.offsetPixels;
sizePoints = other.sizePoints;
mutatorsStack = other.mutatorsStack;
};
SkPoint offsetPixels;
SkSize sizePoints;
MutatorsStack mutatorsStack;
bool operator==(const EmbeddedViewParams& other) const {
return offsetPixels == other.offsetPixels &&
sizePoints == other.sizePoints &&
mutatorsStack == other.mutatorsStack;
}
};
enum class PostPrerollResult { kResubmitFrame, kSuccess };
// Facilitates embedding of platform views within the flow layer tree.
//
// Used on iOS and on embedded platforms that provide a system compositor
// as part of the project arguments.
class ExternalViewEmbedder {
// TODO(cyanglaz): Make embedder own the `EmbeddedViewParams`.
public:
ExternalViewEmbedder() = default;
virtual ~ExternalViewEmbedder() = default;
// Usually, the root canvas is not owned by the view embedder. However, if
// the view embedder wants to provide a canvas to the rasterizer, it may
// return one here. This canvas takes priority over the canvas materialized
// from the on-screen render target.
virtual SkCanvas* GetRootCanvas() = 0;
// Call this in-lieu of |SubmitFrame| to clear pre-roll state and
// sets the stage for the next pre-roll.
virtual void CancelFrame() = 0;
virtual void BeginFrame(SkISize frame_size,
GrContext* context,
double device_pixel_ratio) = 0;
virtual void PrerollCompositeEmbeddedView(
int view_id,
std::unique_ptr<EmbeddedViewParams> params) = 0;
// This needs to get called after |Preroll| finishes on the layer tree.
// Returns kResubmitFrame if the frame needs to be processed again, this is
// after it does any requisite tasks needed to bring itself to a valid state.
// Returns kSuccess if the view embedder is already in a valid state.
virtual PostPrerollResult PostPrerollAction(
fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger) {
return PostPrerollResult::kSuccess;
}
virtual std::vector<SkCanvas*> GetCurrentCanvases() = 0;
// Must be called on the UI thread.
virtual SkCanvas* CompositeEmbeddedView(int view_id) = 0;
virtual bool SubmitFrame(GrContext* context);
FML_DISALLOW_COPY_AND_ASSIGN(ExternalViewEmbedder);
}; // ExternalViewEmbedder
} // namespace flutter
#endif // FLUTTER_FLOW_EMBEDDED_VIEWS_H_