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 #ifndef FLUTTER_FLOW_LAYERS_LAYER_H_ 6 #define FLUTTER_FLOW_LAYERS_LAYER_H_ 7 8 #include <memory> 9 #include <vector> 10 11 #include "flutter/flow/embedded_views.h" 12 #include "flutter/flow/instrumentation.h" 13 #include "flutter/flow/raster_cache.h" 14 #include "flutter/flow/texture.h" 15 #include "flutter/fml/build_config.h" 16 #include "flutter/fml/compiler_specific.h" 17 #include "flutter/fml/logging.h" 18 #include "flutter/fml/macros.h" 19 #include "flutter/fml/trace_event.h" 20 #include "third_party/skia/include/core/SkCanvas.h" 21 #include "third_party/skia/include/core/SkColor.h" 22 #include "third_party/skia/include/core/SkColorFilter.h" 23 #include "third_party/skia/include/core/SkMatrix.h" 24 #include "third_party/skia/include/core/SkPath.h" 25 #include "third_party/skia/include/core/SkPicture.h" 26 #include "third_party/skia/include/core/SkRRect.h" 27 #include "third_party/skia/include/core/SkRect.h" 28 #include "third_party/skia/include/utils/SkNWayCanvas.h" 29 30 #if defined(OS_FUCHSIA) 31 32 #include "flutter/flow/scene_update_context.h" //nogncheck 33 #include "lib/ui/scenic/cpp/resources.h" //nogncheck 34 #include "lib/ui/scenic/cpp/session.h" //nogncheck 35 36 #endif // defined(OS_FUCHSIA) 37 38 namespace flutter { 39 40 static constexpr SkRect kGiantRect = SkRect::MakeLTRB(-1E9F, -1E9F, 1E9F, 1E9F); 41 42 // This should be an exact copy of the Clip enum in painting.dart. 43 enum Clip { none, hardEdge, antiAlias, antiAliasWithSaveLayer }; 44 45 class ContainerLayer; 46 47 struct PrerollContext { 48 RasterCache* raster_cache; 49 GrContext* gr_context; 50 ExternalViewEmbedder* view_embedder; 51 MutatorsStack& mutators_stack; 52 SkColorSpace* dst_color_space; 53 SkRect cull_rect; 54 55 // The following allows us to paint in the end of subtree preroll 56 const Stopwatch& raster_time; 57 const Stopwatch& ui_time; 58 TextureRegistry& texture_registry; 59 const bool checkerboard_offscreen_layers; 60 float total_elevation = 0.0f; 61 }; 62 63 // Represents a single composited layer. Created on the UI thread but then 64 // subquently used on the Rasterizer thread. 65 class Layer { 66 public: 67 Layer(); 68 virtual ~Layer(); 69 70 virtual void Preroll(PrerollContext* context, const SkMatrix& matrix); 71 72 struct PaintContext { 73 // When splitting the scene into multiple canvases (e.g when embedding 74 // a platform view on iOS) during the paint traversal we apply the non leaf 75 // flow layers to all canvases, and leaf layers just to the "current" 76 // canvas. Applying the non leaf layers to all canvases ensures that when 77 // we switch a canvas (when painting a PlatformViewLayer) the next canvas 78 // has the exact same state as the current canvas. 79 // The internal_nodes_canvas is a SkNWayCanvas which is used by non leaf 80 // and applies the operations to all canvases. 81 // The leaf_nodes_canvas is the "current" canvas and is used by leaf 82 // layers. 83 SkCanvas* internal_nodes_canvas; 84 SkCanvas* leaf_nodes_canvas; 85 GrContext* gr_context; 86 ExternalViewEmbedder* view_embedder; 87 const Stopwatch& raster_time; 88 const Stopwatch& ui_time; 89 TextureRegistry& texture_registry; 90 const RasterCache* raster_cache; 91 const bool checkerboard_offscreen_layers; 92 }; 93 94 // Calls SkCanvas::saveLayer and restores the layer upon destruction. Also 95 // draws a checkerboard over the layer if that is enabled in the PaintContext. 96 class AutoSaveLayer { 97 public: 98 FML_WARN_UNUSED_RESULT static AutoSaveLayer Create( 99 const PaintContext& paint_context, 100 const SkRect& bounds, 101 const SkPaint* paint); 102 103 FML_WARN_UNUSED_RESULT static AutoSaveLayer Create( 104 const PaintContext& paint_context, 105 const SkCanvas::SaveLayerRec& layer_rec); 106 107 ~AutoSaveLayer(); 108 109 private: 110 AutoSaveLayer(const PaintContext& paint_context, 111 const SkRect& bounds, 112 const SkPaint* paint); 113 114 AutoSaveLayer(const PaintContext& paint_context, 115 const SkCanvas::SaveLayerRec& layer_rec); 116 117 const PaintContext& paint_context_; 118 const SkRect bounds_; 119 }; 120 121 virtual void Paint(PaintContext& context) const = 0; 122 IsContainer()123 virtual bool IsContainer() { return false; } 124 125 #if defined(OS_FUCHSIA) 126 // Updates the system composited scene. 127 virtual void UpdateScene(SceneUpdateContext& context); 128 #endif 129 parent()130 ContainerLayer* parent() const { return parent_; } 131 set_parent(ContainerLayer * parent)132 void set_parent(ContainerLayer* parent) { parent_ = parent; } 133 needs_system_composite()134 bool needs_system_composite() const { return needs_system_composite_; } set_needs_system_composite(bool value)135 void set_needs_system_composite(bool value) { 136 needs_system_composite_ = value; 137 } 138 paint_bounds()139 const SkRect& paint_bounds() const { return paint_bounds_; } 140 141 // This must be set by the time Preroll() returns otherwise the layer will 142 // be assumed to have empty paint bounds (paints no content). set_paint_bounds(const SkRect & paint_bounds)143 void set_paint_bounds(const SkRect& paint_bounds) { 144 paint_bounds_ = paint_bounds; 145 } 146 needs_painting()147 bool needs_painting() const { return !paint_bounds_.isEmpty(); } 148 unique_id()149 uint64_t unique_id() const { return unique_id_; } 150 151 private: 152 ContainerLayer* parent_; 153 bool needs_system_composite_; 154 SkRect paint_bounds_; 155 uint64_t unique_id_; 156 157 static uint64_t NextUniqueID(); 158 159 FML_DISALLOW_COPY_AND_ASSIGN(Layer); 160 }; 161 162 } // namespace flutter 163 164 #endif // FLUTTER_FLOW_LAYERS_LAYER_H_ 165