blob: f30c53c0386e8bec8374b0f477c3fdb63c16fe6d [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:
robertphillips30d2cc62014-09-24 08:52:18 -070036
robertphillipsfd61ed02014-10-28 07:21:44 -070037 /** Find the layers in 'topLevelPicture' that can be atlased. Note that the discovered
38 layers can be inside nested sub-pictures.
39 @param context Owner of the layer cache (the source of new layers)
40 @param topLevelPicture The top-level picture that is about to be rendered
robertphillips30d78412014-11-24 09:49:17 -080041 @param initialMat The CTM of the canvas into which the layers will be drawn
robertphillipsfd61ed02014-10-28 07:21:44 -070042 @param query The rectangle that is about to be drawn.
43 @param atlasedNeedRendering Out parameter storing the layers that
44 should be hoisted to the atlas
45 @param recycled Out parameter storing layers that are atlased but do not need rendering
robertphillipsa63f32e2014-11-10 08:10:42 -080046 @param numSamples The number if MSAA samples required
robertphillipsfd61ed02014-10-28 07:21:44 -070047 */
48 static void FindLayersToAtlas(GrContext* context,
49 const SkPicture* topLevelPicture,
robertphillips30d78412014-11-24 09:49:17 -080050 const SkMatrix& initialMat,
robertphillipsfd61ed02014-10-28 07:21:44 -070051 const SkRect& query,
52 SkTDArray<GrHoistedLayer>* atlasedNeedRendering,
robertphillipsa63f32e2014-11-10 08:10:42 -080053 SkTDArray<GrHoistedLayer>* recycled,
54 int numSamples);
robertphillipsfd61ed02014-10-28 07:21:44 -070055
robertphillips30d2cc62014-09-24 08:52:18 -070056 /** Find the layers in 'topLevelPicture' that need hoisting. Note that the discovered
57 layers can be inside nested sub-pictures.
robertphillipsd61ef012014-10-08 05:17:02 -070058 @param context Owner of the layer cache (the source of new layers)
robertphillips30d2cc62014-09-24 08:52:18 -070059 @param topLevelPicture The top-level picture that is about to be rendered
robertphillips30d78412014-11-24 09:49:17 -080060 @param initialMat The CTM of the canvas into which the layers will be drawn
robertphillips30d2cc62014-09-24 08:52:18 -070061 @param query The rectangle that is about to be drawn.
robertphillipsfd61ed02014-10-28 07:21:44 -070062 @param needRendering Out parameter storing the layers that need rendering.
63 This should never include atlased layers.
robertphillipsb5a97152014-09-30 11:33:02 -070064 @param recycled Out parameter storing layers that need hoisting but not rendering
robertphillipsa63f32e2014-11-10 08:10:42 -080065 @param numSamples The number if MSAA samples required
robertphillips98d709b2014-09-02 10:20:50 -070066 */
robertphillipsfd61ed02014-10-28 07:21:44 -070067 static void FindLayersToHoist(GrContext* context,
robertphillipsd61ef012014-10-08 05:17:02 -070068 const SkPicture* topLevelPicture,
robertphillips30d78412014-11-24 09:49:17 -080069 const SkMatrix& initialMat,
robertphillips98d709b2014-09-02 10:20:50 -070070 const SkRect& query,
robertphillipsfd61ed02014-10-28 07:21:44 -070071 SkTDArray<GrHoistedLayer>* needRendering,
robertphillipsa63f32e2014-11-10 08:10:42 -080072 SkTDArray<GrHoistedLayer>* recycled,
73 int numSamples);
robertphillips98d709b2014-09-02 10:20:50 -070074
robertphillipsfd61ed02014-10-28 07:21:44 -070075 /** Draw the specified layers into the atlas.
robertphillips9e6835d2014-10-22 05:33:52 -070076 @param context Owner of the layer cache (and thus the layers)
robertphillipsfd61ed02014-10-28 07:21:44 -070077 @param layers The layers to be drawn into the atlas
robertphillips98d709b2014-09-02 10:20:50 -070078 */
robertphillipsfd61ed02014-10-28 07:21:44 -070079 static void DrawLayersToAtlas(GrContext* context, const SkTDArray<GrHoistedLayer>& layers);
robertphillips98d709b2014-09-02 10:20:50 -070080
robertphillipsfd61ed02014-10-28 07:21:44 -070081 /** Draw the specified layers into their own individual textures.
82 @param context Owner of the layer cache (and thus the layers)
83 @param layers The layers to be drawn
robertphillips98d709b2014-09-02 10:20:50 -070084 */
robertphillipsfd61ed02014-10-28 07:21:44 -070085 static void DrawLayers(GrContext* context, const SkTDArray<GrHoistedLayer>& layers);
86
87 /** Convert all the layers in 'layers' into replacement objects in 'replacements'.
88 @param layers The hoisted layers
89 @param replacements Replacement object that will be used for a replacement draw
90 */
robertphillips01d6e5f2014-12-01 09:09:27 -080091 static void ConvertLayersToReplacements(const SkPicture* topLevelPicture,
92 const SkTDArray<GrHoistedLayer>& layers,
robertphillipsfd61ed02014-10-28 07:21:44 -070093 GrReplacements* replacements);
94
95 /** Unlock a group of layers in the layer cache.
96 @param context Owner of the layer cache (and thus the layers)
97 @param layers Unneeded layers in the atlas
98 */
99 static void UnlockLayers(GrContext* context, const SkTDArray<GrHoistedLayer>& layers);
robertphillips4ab5a902014-10-29 13:56:02 -0700100
101 /** Forceably remove all cached layers and release the atlas. Useful for debugging and timing.
102 This is only functional when GR_CACHE_HOISTED_LAYERS is set to 1 in GrLayerCache.h
103 @param context Owner of the layer cache (and thus the layers)
104 */
105 static void PurgeCache(GrContext* context);
robertphillips7b9e8a42014-12-11 08:20:31 -0800106
107private:
108 /** Update the GrTexture in 'layer' with its filtered version
109 @param context Owner of the layer cache (and thus the layers)
110 @param device Required by the filtering code
robertphillips478dd722014-12-16 08:25:55 -0800111 @param info Layer info for a layer needing filtering prior to being composited
robertphillips7b9e8a42014-12-11 08:20:31 -0800112 */
robertphillips478dd722014-12-16 08:25:55 -0800113 static void FilterLayer(GrContext* context, SkGpuDevice* device, const GrHoistedLayer& info);
robertphillips7b9e8a42014-12-11 08:20:31 -0800114
robertphillips98d709b2014-09-02 10:20:50 -0700115};
116
117#endif