robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 14 | struct GrCachedLayer; |
robertphillips | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 15 | class GrReplacements; |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 16 | class SkGpuDevice; |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 17 | struct SkRect; |
| 18 | |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 19 | class GrHoistedLayer { |
| 20 | public: |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 21 | const SkPicture* fPicture; // the picture that actually contains the layer |
| 22 | // (not necessarily the top-most picture) |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 23 | GrCachedLayer* fLayer; |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 24 | SkMatrix fInitialMat; |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 25 | SkMatrix fPreMat; |
| 26 | SkMatrix fLocalMat; |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 29 | // 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 |
| 34 | class GrLayerHoister { |
| 35 | public: |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 36 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 37 | /** 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 |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 41 | @param initialMat The CTM of the canvas into which the layers will be drawn |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 42 | @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 |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 46 | @param numSamples The number if MSAA samples required |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 47 | */ |
| 48 | static void FindLayersToAtlas(GrContext* context, |
| 49 | const SkPicture* topLevelPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 50 | const SkMatrix& initialMat, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 51 | const SkRect& query, |
| 52 | SkTDArray<GrHoistedLayer>* atlasedNeedRendering, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 53 | SkTDArray<GrHoistedLayer>* recycled, |
| 54 | int numSamples); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 55 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 56 | /** Find the layers in 'topLevelPicture' that need hoisting. Note that the discovered |
| 57 | layers can be inside nested sub-pictures. |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 58 | @param context Owner of the layer cache (the source of new layers) |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 59 | @param topLevelPicture The top-level picture that is about to be rendered |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 60 | @param initialMat The CTM of the canvas into which the layers will be drawn |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 61 | @param query The rectangle that is about to be drawn. |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 62 | @param needRendering Out parameter storing the layers that need rendering. |
| 63 | This should never include atlased layers. |
robertphillips | b5a9715 | 2014-09-30 11:33:02 -0700 | [diff] [blame] | 64 | @param recycled Out parameter storing layers that need hoisting but not rendering |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 65 | @param numSamples The number if MSAA samples required |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 66 | */ |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 67 | static void FindLayersToHoist(GrContext* context, |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 68 | const SkPicture* topLevelPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 69 | const SkMatrix& initialMat, |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 70 | const SkRect& query, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 71 | SkTDArray<GrHoistedLayer>* needRendering, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 72 | SkTDArray<GrHoistedLayer>* recycled, |
| 73 | int numSamples); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 74 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 75 | /** Draw the specified layers into the atlas. |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 76 | @param context Owner of the layer cache (and thus the layers) |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 77 | @param layers The layers to be drawn into the atlas |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 78 | */ |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 79 | static void DrawLayersToAtlas(GrContext* context, const SkTDArray<GrHoistedLayer>& layers); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 80 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 81 | /** 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 |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 84 | */ |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 85 | 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 | */ |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 91 | static void ConvertLayersToReplacements(const SkPicture* topLevelPicture, |
| 92 | const SkTDArray<GrHoistedLayer>& layers, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 93 | 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); |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 100 | |
| 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); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 106 | |
| 107 | private: |
| 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 |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 111 | @param info Layer info for a layer needing filtering prior to being composited |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 112 | */ |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 113 | static void FilterLayer(GrContext* context, SkGpuDevice* device, const GrHoistedLayer& info); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 114 | |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | #endif |