blob: 13cac5173ab98aec8e5ca1233b4ec78b5a31802d [file] [log] [blame]
robertphillips98d709b2014-09-02 10:20:50 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrLayerHoister_DEFINED
9#define GrLayerHoister_DEFINED
10
11#include "SkPicture.h"
12#include "SkTDArray.h"
13
robertphillips98d709b2014-09-02 10:20:50 -070014struct GrCachedLayer;
robertphillips1c4c5282014-09-18 12:03:15 -070015class GrReplacements;
robertphillips7b9e8a42014-12-11 08:20:31 -080016class SkGpuDevice;
robertphillips98d709b2014-09-02 10:20:50 -070017struct SkRect;
18
robertphillipsd61ef012014-10-08 05:17:02 -070019class GrHoistedLayer {
20public:
robertphillips01d6e5f2014-12-01 09:09:27 -080021 const SkPicture* fPicture; // the picture that actually contains the layer
22 // (not necessarily the top-most picture)
robertphillipsd61ef012014-10-08 05:17:02 -070023 GrCachedLayer* fLayer;
robertphillips01d6e5f2014-12-01 09:09:27 -080024 SkMatrix fInitialMat;
robertphillips9e6835d2014-10-22 05:33:52 -070025 SkMatrix fPreMat;
26 SkMatrix fLocalMat;
robertphillipsd61ef012014-10-08 05:17:02 -070027};
28
robertphillips98d709b2014-09-02 10:20:50 -070029// This class collects the layer hoisting functionality in one place.
30// For each picture rendering:
31// FindLayersToHoist should be called once to collect the required layers
32// DrawLayers should be called once to render them
33// UnlockLayers should be called once to allow the texture resources to be recycled
34class GrLayerHoister {
35public:
robertphillips60029a52015-11-09 13:51:06 -080036 /** Attempt to reattach layers that may have been atlased in the past
37 */
38 static void Begin(GrContext* context);
39
40 /** Release cache resources
41 */
42 static void End(GrContext* context);
robertphillips30d2cc62014-09-24 08:52:18 -070043
robertphillipsfd61ed02014-10-28 07:21:44 -070044 /** Find the layers in 'topLevelPicture' that can be atlased. Note that the discovered
45 layers can be inside nested sub-pictures.
46 @param context Owner of the layer cache (the source of new layers)
47 @param topLevelPicture The top-level picture that is about to be rendered
robertphillips30d78412014-11-24 09:49:17 -080048 @param initialMat The CTM of the canvas into which the layers will be drawn
robertphillipsfd61ed02014-10-28 07:21:44 -070049 @param query The rectangle that is about to be drawn.
50 @param atlasedNeedRendering Out parameter storing the layers that
51 should be hoisted to the atlas
52 @param recycled Out parameter storing layers that are atlased but do not need rendering
robertphillipsa63f32e2014-11-10 08:10:42 -080053 @param numSamples The number if MSAA samples required
robertphillipsfd61ed02014-10-28 07:21:44 -070054 */
55 static void FindLayersToAtlas(GrContext* context,
56 const SkPicture* topLevelPicture,
robertphillips30d78412014-11-24 09:49:17 -080057 const SkMatrix& initialMat,
robertphillipsfd61ed02014-10-28 07:21:44 -070058 const SkRect& query,
59 SkTDArray<GrHoistedLayer>* atlasedNeedRendering,
robertphillipsa63f32e2014-11-10 08:10:42 -080060 SkTDArray<GrHoistedLayer>* recycled,
61 int numSamples);
robertphillipsfd61ed02014-10-28 07:21:44 -070062
robertphillips30d2cc62014-09-24 08:52:18 -070063 /** Find the layers in 'topLevelPicture' that need hoisting. Note that the discovered
64 layers can be inside nested sub-pictures.
robertphillipsd61ef012014-10-08 05:17:02 -070065 @param context Owner of the layer cache (the source of new layers)
robertphillips30d2cc62014-09-24 08:52:18 -070066 @param topLevelPicture The top-level picture that is about to be rendered
robertphillips30d78412014-11-24 09:49:17 -080067 @param initialMat The CTM of the canvas into which the layers will be drawn
robertphillips30d2cc62014-09-24 08:52:18 -070068 @param query The rectangle that is about to be drawn.
robertphillipsfd61ed02014-10-28 07:21:44 -070069 @param needRendering Out parameter storing the layers that need rendering.
70 This should never include atlased layers.
robertphillipsb5a97152014-09-30 11:33:02 -070071 @param recycled Out parameter storing layers that need hoisting but not rendering
robertphillipsa63f32e2014-11-10 08:10:42 -080072 @param numSamples The number if MSAA samples required
robertphillips98d709b2014-09-02 10:20:50 -070073 */
robertphillipsfd61ed02014-10-28 07:21:44 -070074 static void FindLayersToHoist(GrContext* context,
robertphillipsd61ef012014-10-08 05:17:02 -070075 const SkPicture* topLevelPicture,
robertphillips30d78412014-11-24 09:49:17 -080076 const SkMatrix& initialMat,
robertphillips98d709b2014-09-02 10:20:50 -070077 const SkRect& query,
robertphillipsfd61ed02014-10-28 07:21:44 -070078 SkTDArray<GrHoistedLayer>* needRendering,
robertphillipsa63f32e2014-11-10 08:10:42 -080079 SkTDArray<GrHoistedLayer>* recycled,
80 int numSamples);
robertphillips98d709b2014-09-02 10:20:50 -070081
robertphillipsfd61ed02014-10-28 07:21:44 -070082 /** Draw the specified layers into the atlas.
robertphillips9e6835d2014-10-22 05:33:52 -070083 @param context Owner of the layer cache (and thus the layers)
robertphillipsfd61ed02014-10-28 07:21:44 -070084 @param layers The layers to be drawn into the atlas
robertphillips98d709b2014-09-02 10:20:50 -070085 */
robertphillipsfd61ed02014-10-28 07:21:44 -070086 static void DrawLayersToAtlas(GrContext* context, const SkTDArray<GrHoistedLayer>& layers);
robertphillips98d709b2014-09-02 10:20:50 -070087
robertphillipsfd61ed02014-10-28 07:21:44 -070088 /** Draw the specified layers into their own individual textures.
89 @param context Owner of the layer cache (and thus the layers)
90 @param layers The layers to be drawn
robertphillips98d709b2014-09-02 10:20:50 -070091 */
robertphillipsfd61ed02014-10-28 07:21:44 -070092 static void DrawLayers(GrContext* context, const SkTDArray<GrHoistedLayer>& layers);
93
94 /** Convert all the layers in 'layers' into replacement objects in 'replacements'.
95 @param layers The hoisted layers
96 @param replacements Replacement object that will be used for a replacement draw
97 */
robertphillips01d6e5f2014-12-01 09:09:27 -080098 static void ConvertLayersToReplacements(const SkPicture* topLevelPicture,
99 const SkTDArray<GrHoistedLayer>& layers,
robertphillipsfd61ed02014-10-28 07:21:44 -0700100 GrReplacements* replacements);
101
102 /** Unlock a group of layers in the layer cache.
103 @param context Owner of the layer cache (and thus the layers)
104 @param layers Unneeded layers in the atlas
105 */
106 static void UnlockLayers(GrContext* context, const SkTDArray<GrHoistedLayer>& layers);
robertphillips4ab5a902014-10-29 13:56:02 -0700107
108 /** Forceably remove all cached layers and release the atlas. Useful for debugging and timing.
109 This is only functional when GR_CACHE_HOISTED_LAYERS is set to 1 in GrLayerCache.h
110 @param context Owner of the layer cache (and thus the layers)
111 */
112 static void PurgeCache(GrContext* context);
robertphillips7b9e8a42014-12-11 08:20:31 -0800113
114private:
115 /** Update the GrTexture in 'layer' with its filtered version
116 @param context Owner of the layer cache (and thus the layers)
117 @param device Required by the filtering code
robertphillips478dd722014-12-16 08:25:55 -0800118 @param info Layer info for a layer needing filtering prior to being composited
robertphillips7b9e8a42014-12-11 08:20:31 -0800119 */
robertphillips478dd722014-12-16 08:25:55 -0800120 static void FilterLayer(GrContext* context, SkGpuDevice* device, const GrHoistedLayer& info);
robertphillips7b9e8a42014-12-11 08:20:31 -0800121
robertphillips98d709b2014-09-02 10:20:50 -0700122};
123
124#endif