Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9a2180d

Browse files
committed
simple think
1 parent 72beac3 commit 9a2180d

File tree

5 files changed

+198
-87
lines changed

5 files changed

+198
-87
lines changed

flow/layers/display_list_layer.cc

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,22 @@ void DisplayListLayer::Paint(PaintContext& context) const {
149149
context.layer_snapshot_store->Add(snapshot_data);
150150
}
151151

152-
if (context.builder) {
153-
// AutoCachePaint save_paint(context);
154-
// int restore_count = context.leaf_nodes_builder->getSaveCount();
155-
// if (save_paint.dl_paint() != nullptr) {
156-
// context.leaf_nodes_builder->saveLayer(&paint_bounds(),
157-
// save_paint.dl_paint());
158-
// }
159-
context.builder->drawDisplayList(display_list_.skia_object());
160-
// context.leaf_nodes_builder->restoreToCount(restore_count);
161-
} else {
162-
display_list()->RenderTo(context.canvas, context.inherited_opacity);
163-
}
152+
auto saved_layer = context.state_stack.saveWithDisplayList(
153+
&paint_bounds(), display_list_.skia_object());
154+
155+
// if (context.builder) {
156+
157+
// // AutoCachePaint save_paint(context);
158+
// // int restore_count = context.leaf_nodes_builder->getSaveCount();
159+
// // if (save_paint.dl_paint() != nullptr) {
160+
// // context.leaf_nodes_builder->saveLayer(&paint_bounds(),
161+
// // save_paint.dl_paint());
162+
// // }
163+
// // context.builder->drawDisplayList(display_list_.skia_object());
164+
// // context.leaf_nodes_builder->restoreToCount(restore_count);
165+
// } else {
166+
// display_list()->RenderTo(context.canvas, context.inherited_opacity);
167+
// }
164168
}
165169

166170
} // namespace flutter

flow/layers/layer.h

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,59 @@ struct PaintContext {
156156
DisplayListBuilder* builder = nullptr;
157157
};
158158

159+
class AutoCachePaint {
160+
public:
161+
explicit AutoCachePaint(PaintContext& context) : context_(context) {
162+
needs_paint_ = context.inherited_opacity < SK_Scalar1;
163+
if (needs_paint_) {
164+
sk_paint_.setAlphaf(context.inherited_opacity);
165+
dl_paint_.setAlpha(SkScalarRoundToInt(context.inherited_opacity * 255));
166+
context.inherited_opacity = SK_Scalar1;
167+
}
168+
}
169+
170+
~AutoCachePaint() { context_.inherited_opacity = sk_paint_.getAlphaf(); }
171+
172+
void setImageFilter(const DlImageFilter* filter) {
173+
sk_paint_.setImageFilter(!filter ? nullptr : filter->skia_object());
174+
dl_paint_.setImageFilter(filter);
175+
update_needs_paint();
176+
}
177+
178+
void setColorFilter(const DlColorFilter* filter) {
179+
sk_paint_.setColorFilter(!filter ? nullptr : filter->skia_object());
180+
dl_paint_.setColorFilter(filter);
181+
update_needs_paint();
182+
}
183+
184+
void setBlendMode(DlBlendMode mode) {
185+
sk_paint_.setBlendMode(ToSk(mode));
186+
dl_paint_.setBlendMode(mode);
187+
update_needs_paint();
188+
}
189+
190+
void setAlpha(float alpha) {
191+
sk_paint_.setAlphaf(alpha);
192+
dl_paint_.setAlpha(SkScalarRoundToInt(alpha * 255));
193+
update_needs_paint();
194+
}
195+
196+
const SkPaint* sk_paint() { return needs_paint_ ? &sk_paint_ : nullptr; }
197+
const DlPaint* dl_paint() { return needs_paint_ ? &dl_paint_ : nullptr; }
198+
199+
private:
200+
PaintContext& context_;
201+
SkPaint sk_paint_;
202+
DlPaint dl_paint_;
203+
bool needs_paint_;
204+
205+
void update_needs_paint() {
206+
needs_paint_ = sk_paint_.getImageFilter() != nullptr ||
207+
sk_paint_.getColorFilter() != nullptr ||
208+
!sk_paint_.isSrcOver() || sk_paint_.getAlphaf() < SK_Scalar1;
209+
}
210+
};
211+
159212
// Represents a single composited layer. Created on the UI thread but then
160213
// subquently used on the Rasterizer thread.
161214
class Layer {
@@ -214,54 +267,6 @@ class Layer {
214267
bool prev_surface_needs_readback_;
215268
};
216269

217-
class AutoCachePaint {
218-
public:
219-
explicit AutoCachePaint(PaintContext& context) : context_(context) {
220-
needs_paint_ = context.inherited_opacity < SK_Scalar1;
221-
if (needs_paint_) {
222-
sk_paint_.setAlphaf(context.inherited_opacity);
223-
dl_paint_.setAlpha(SkScalarRoundToInt(context.inherited_opacity * 255));
224-
context.inherited_opacity = SK_Scalar1;
225-
}
226-
}
227-
228-
~AutoCachePaint() { context_.inherited_opacity = sk_paint_.getAlphaf(); }
229-
230-
void setImageFilter(const DlImageFilter* filter) {
231-
sk_paint_.setImageFilter(!filter ? nullptr : filter->skia_object());
232-
dl_paint_.setImageFilter(filter);
233-
update_needs_paint();
234-
}
235-
236-
void setColorFilter(const DlColorFilter* filter) {
237-
sk_paint_.setColorFilter(!filter ? nullptr : filter->skia_object());
238-
dl_paint_.setColorFilter(filter);
239-
update_needs_paint();
240-
}
241-
242-
void setBlendMode(DlBlendMode mode) {
243-
sk_paint_.setBlendMode(ToSk(mode));
244-
dl_paint_.setBlendMode(mode);
245-
update_needs_paint();
246-
}
247-
248-
const SkPaint* sk_paint() { return needs_paint_ ? &sk_paint_ : nullptr; }
249-
const DlPaint* dl_paint() { return needs_paint_ ? &dl_paint_ : nullptr; }
250-
251-
private:
252-
PaintContext& context_;
253-
SkPaint sk_paint_;
254-
DlPaint dl_paint_;
255-
bool needs_paint_;
256-
257-
void update_needs_paint() {
258-
needs_paint_ = sk_paint_.getImageFilter() != nullptr ||
259-
sk_paint_.getColorFilter() != nullptr ||
260-
!sk_paint_.isSrcOver() ||
261-
sk_paint_.getAlphaf() < SK_Scalar1;
262-
}
263-
};
264-
265270
virtual void Paint(PaintContext& context) const = 0;
266271

267272
virtual void PaintChildren(PaintContext& context) const { FML_DCHECK(false); }

flow/layers/layer_state_stack.cc

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
// found in the LICENSE file.
44

55
#include "flutter/flow/layers/layer_state_stack.h"
6+
#include "flutter/display_list/display_list_builder.h"
7+
#include "flutter/display_list/display_list_paint.h"
8+
#include "flutter/flow/layers/layer.h"
69
#include "flutter/flow/paint_utils.h"
7-
10+
#include "flutter/flow/raster_cache.h"
11+
#include "include/core/SkPath.h"
12+
#include "include/core/SkScalar.h"
813
namespace flutter {
914

1015
using AutoRestore = LayerStateStack::AutoRestore;
@@ -122,6 +127,17 @@ AutoRestore LayerStateStack::saveWithBackdropFilter(
122127
return ret;
123128
}
124129

130+
AutoRestore LayerStateStack::saveWithDisplayList(
131+
const SkRect* bounds,
132+
const sk_sp<DisplayList> display_list,
133+
bool checkerboard) {
134+
auto ret = LayerStateStack::AutoRestore(this);
135+
state_stack_.emplace_back(
136+
std::make_unique<DisplayListEntry>(display_list, bounds, checkerboard));
137+
state_stack_.back()->apply(&outstanding_, canvas_, builder_);
138+
return ret;
139+
}
140+
125141
void LayerStateStack::translate(SkScalar tx, SkScalar ty) {
126142
state_stack_.emplace_back(std::make_unique<TranslateEntry>(tx, ty));
127143
state_stack_.back()->apply(&outstanding_, canvas_, builder_);
@@ -182,14 +198,22 @@ void LayerStateStack::SaveEntry::restore(RenderingAttributes* attributes,
182198
do_checkerboard(canvas, builder);
183199
}
184200

201+
185202
void LayerStateStack::SaveLayerEntry::apply(RenderingAttributes* attributes,
186203
SkCanvas* canvas,
187204
DisplayListBuilder* builder) const {
188205
if (canvas) {
189-
canvas->saveLayer(save_bounds(), nullptr);
206+
if (attributes->opacity < SK_Scalar1) {
207+
sk_paint_.setAlphaf(attributes->opacity);
208+
}
209+
canvas->saveLayer(save_bounds(), &sk_paint_);
190210
}
191211
if (builder) {
192-
builder->saveLayer(save_bounds(), nullptr);
212+
if (attributes->opacity < SK_Scalar1) {
213+
dl_paint_.setAlpha(SkScalarRoundToInt(attributes->opacity * 255));
214+
attributes->opacity = SK_Scalar1;
215+
}
216+
builder->saveLayer(save_bounds(), &dl_paint_);
193217
}
194218
}
195219

@@ -206,6 +230,10 @@ void LayerStateStack::SaveLayerEntry::do_checkerboard(
206230
}
207231
}
208232

233+
void LayerStateStack::SaveLayerEntry::restore(RenderingAttributes *attributes, SkCanvas *canvas, DisplayListBuilder *builder) const {
234+
attributes->opacity = sk_paint_.getAlphaf();
235+
}
236+
209237
void LayerStateStack::OpacityEntry::apply(RenderingAttributes* attributes,
210238
SkCanvas* canvas,
211239
DisplayListBuilder* builder) const {
@@ -255,17 +283,16 @@ void LayerStateStack::ColorFilterEntry::apply(
255283
SkCanvas* canvas,
256284
DisplayListBuilder* builder) const {
257285
if (canvas) {
258-
SkPaint paint;
259-
paint.setColorFilter(filter_ ? filter_->skia_object() : nullptr);
260-
canvas->saveLayer(save_bounds(), &paint);
286+
sk_paint_.setColorFilter(filter_ ? filter_->skia_object() : nullptr);
261287
}
262288
if (builder) {
263-
DlPaint paint;
264-
paint.setColorFilter(filter_);
265-
builder->saveLayer(save_bounds(), &paint);
289+
dl_paint_.setColorFilter(filter_);
266290
}
291+
SaveLayerEntry::apply(attributes, canvas, builder);
267292
}
268293

294+
295+
269296
void LayerStateStack::BackdropFilterEntry::apply(
270297
RenderingAttributes* attributes,
271298
SkCanvas* canvas,
@@ -376,4 +403,22 @@ void LayerStateStack::ClipPathEntry::apply(RenderingAttributes* attributes,
376403
}
377404
}
378405

406+
void LayerStateStack::DisplayListEntry::apply(
407+
RenderingAttributes* attributes,
408+
SkCanvas* canvas,
409+
DisplayListBuilder* builder) const {
410+
// if (canvas) {
411+
// display_list_.get()->RenderTo(canvas, paint->sk_paint()
412+
// ? paint->sk_paint()->getAlphaf()
413+
// : SK_Scalar1);
414+
// }
415+
if (builder) {
416+
if (attributes->opacity < SK_Scalar1) {
417+
auto paint = DlPaint().setAlpha(attributes->opacity);
418+
builder->saveLayer(save_bounds(), &paint);
419+
}
420+
builder->drawDisplayList(display_list_);
421+
}
422+
}
423+
379424
} // namespace flutter

0 commit comments

Comments
 (0)