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 5part of engine; 6 7/// A tree of [Layer]s that, together with a [Size] compose a frame. 8class LayerTree { 9 /// The root of the layer tree. 10 Layer rootLayer; 11 12 /// The size (in physical pixels) of the frame to paint this layer tree into. 13 ui.Size frameSize; 14 15 /// Performs a preroll phase before painting the layer tree. 16 /// 17 /// In this phase, the paint boundary for each layer is computed and 18 /// pictures are registered with the raster cache as potential candidates 19 /// to raster. If [ignoreRasterCache] is `true`, then there will be no 20 /// attempt to register pictures to cache. 21 void preroll(Frame frame, {bool ignoreRasterCache = false}) { 22 final PrerollContext context = 23 PrerollContext(ignoreRasterCache ? null : frame.rasterCache); 24 rootLayer.preroll(context, Matrix4.identity()); 25 } 26 27 /// Paints the layer tree into the given [frame]. 28 /// 29 /// If [ignoreRasterCache] is `true`, then the raster cache will 30 /// not be used. 31 void paint(Frame frame, {bool ignoreRasterCache = false}) { 32 final PaintContext context = PaintContext( 33 frame.canvas, ignoreRasterCache ? null : frame.rasterCache); 34 if (rootLayer.needsPainting) { 35 rootLayer.paint(context); 36 } 37 } 38} 39 40/// A single frame to be rendered. 41class Frame { 42 /// The canvas to render this frame to. 43 final SkCanvas canvas; 44 45 /// A cache of pre-rastered pictures. 46 final RasterCache rasterCache; 47 48 Frame(this.canvas, this.rasterCache); 49 50 /// Rasterize the given layer tree into this frame. 51 bool raster(LayerTree layerTree, {bool ignoreRasterCache = false}) { 52 layerTree.preroll(this, ignoreRasterCache: ignoreRasterCache); 53 layerTree.paint(this, ignoreRasterCache: ignoreRasterCache); 54 return true; 55 } 56} 57 58/// The state of the compositor, which is persisted between frames. 59class CompositorContext { 60 /// A cache of pictures, which is shared between successive frames. 61 RasterCache rasterCache; 62 63 /// Acquire a frame using this compositor's settings. 64 Frame acquireFrame(SkCanvas canvas) { 65 return Frame(canvas, rasterCache); 66 } 67} 68