Flutter Engine
The Flutter Engine
opacity_layer.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/flow/layers/opacity_layer.h"
6
7#include "flutter/flow/layers/cacheable_layer.h"
8#include "flutter/flow/raster_cache_util.h"
10
11namespace flutter {
12
13// the opacity_layer couldn't cache itself, so the cache_threshold is the
14// max_int
16 : CacheableContainerLayer(std::numeric_limits<int>::max(), true),
17 alpha_(alpha),
18 offset_(offset) {}
19
20void OpacityLayer::Diff(DiffContext* context, const Layer* old_layer) {
21 DiffContext::AutoSubtreeRestore subtree(context);
22 auto* prev = static_cast<const OpacityLayer*>(old_layer);
23 if (!context->IsSubtreeDirty()) {
25 if (alpha_ != prev->alpha_ || offset_ != prev->offset_) {
26 context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(old_layer));
27 }
28 }
29 context->PushTransform(SkMatrix::Translate(offset_.fX, offset_.fY));
30 if (context->has_raster_cache()) {
32 }
33 DiffChildren(context, prev);
34 context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
35}
36
38 auto mutator = context->state_stack.save();
39 mutator.translate(offset_);
40 mutator.applyOpacity(SkRect(), DlColor::toOpacity(alpha_));
41
42#if !SLIMPELLER
43 AutoCache auto_cache = AutoCache(layer_raster_cache_item_.get(), context,
44 context->state_stack.transform_3x3());
45#endif // !SLIMPELLER
46
49
51 // We store the inheritance ability of our children for |Paint|
54 0);
55
56 // Now we let our parent layers know that we, too, can inherit opacity
57 // regardless of what our children are capable of
59
60 set_paint_bounds(paint_bounds().makeOffset(offset_.fX, offset_.fY));
61
62#if !SLIMPELLER
64 // For opacity layer, we can use raster_cache children only when the
65 // children can't accept opacity so if the children_can_accept_opacity we
66 // should tell the AutoCache object don't do raster_cache.
67 auto_cache.ShouldNotBeCached();
68 }
69#endif // !SLIMPELLER
70}
71
72void OpacityLayer::Paint(PaintContext& context) const {
73 FML_DCHECK(needs_painting(context));
74
75 auto mutator = context.state_stack.save();
76 mutator.translate(offset_.fX, offset_.fY);
77
78#if !SLIMPELLER
79 if (context.raster_cache) {
80 mutator.integralTransform();
81 }
82#endif // !SLIMPELLER
83
84 mutator.applyOpacity(child_paint_bounds(), opacity());
85
86#if !SLIMPELLER
89 if (layer_raster_cache_item_->Draw(context,
90 context.state_stack.fill(paint))) {
91 return;
92 }
93 }
94#endif // !SLIMPELLER
95
96 PaintChildren(context);
97}
98
99} // namespace flutter
static float prev(float f)
uint8_t SkAlpha
Definition: SkColor.h:26
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
std::unique_ptr< LayerRasterCacheItem > layer_raster_cache_item_
void Preroll(PrerollContext *context) override
void PaintChildren(PaintContext &context) const override
virtual void DiffChildren(DiffContext *context, const ContainerLayer *old_layer)
const SkRect & child_paint_bounds() const
void PushTransform(const SkMatrix &transform)
Definition: diff_context.cc:49
void WillPaintWithIntegralTransform()
Definition: diff_context.h:89
void SetLayerPaintRegion(const Layer *layer, const PaintRegion &region)
void MarkSubtreeDirty(const PaintRegion &previous_paint_region=PaintRegion())
PaintRegion CurrentSubtreeRegion() const
bool has_raster_cache() const
Definition: diff_context.h:167
PaintRegion GetOldLayerPaintRegion(const Layer *layer) const
bool IsSubtreeDirty() const
Definition: diff_context.h:108
void translate(SkScalar tx, SkScalar ty)
SkMatrix transform_3x3() const
void fill(MutatorsStack *mutators)
static constexpr int kCallerCanApplyOpacity
static AutoPrerollSaveLayerState Create(PrerollContext *preroll_context, bool save_layer_is_active=true, bool layer_itself_performs_readback=false)
Definition: layer.cc:40
const SkRect & paint_bounds() const
Definition: layer.h:210
bool needs_painting(PaintContext &context) const
Definition: layer.h:232
void set_paint_bounds(const SkRect &paint_bounds)
Definition: layer.h:223
void Diff(DiffContext *context, const Layer *old_layer) override
SkScalar opacity() const
Definition: opacity_layer.h:48
bool children_can_accept_opacity() const
Definition: opacity_layer.h:41
void Preroll(PrerollContext *context) override
void Paint(PaintContext &context) const override
void set_children_can_accept_opacity(bool value)
Definition: opacity_layer.h:44
OpacityLayer(SkAlpha alpha, const SkPoint &offset)
const Paint & paint
Definition: color_source.cc:38
#define FML_DCHECK(condition)
Definition: logging.h:103
static float max(float r, float g, float b)
Definition: hsl.cpp:49
Definition: ref_ptr.h:256
SeparatedVector2 offset
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
static constexpr SkScalar toOpacity(uint8_t alpha)
Definition: dl_color.h:18
LayerStateStack & state_stack
Definition: layer.h:101
LayerStateStack & state_stack
Definition: layer.h:59
int renderable_state_flags
Definition: layer.h:83