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 SkLayerInfo_DEFINED
9#define SkLayerInfo_DEFINED
10
11#include "SkBigPicture.h"
12#include "SkTArray.h"
13
14// This class stores information about the saveLayer/restore pairs found
15// within an SkPicture. It is used by Ganesh to perform layer hoisting.
16class SkLayerInfo : public SkBigPicture::AccelData {
17public:
18    // Information about a given saveLayer/restore block in an SkPicture
19    class BlockInfo {
20    public:
21        BlockInfo() : fPicture(nullptr), fPaint(nullptr), fKey(nullptr), fKeySize(0) {}
22        ~BlockInfo() {
23            SkSafeUnref(fPicture);
24            delete fPaint;
25            delete[] fKey;
26        }
27
28        // The picture owning the layer. If the owning picture is the top-most
29        // one (i.e., the picture for which this SkLayerInfo was created) then
30        // this pointer is nullptr. If it is a nested picture then the pointer
31        // is non-nullptr and owns a ref on the picture.
32        const SkPicture* fPicture;
33        // The device space bounds of this layer.
34        SkRect fBounds;
35        // If not-empty, the optional bounds parameter passed in to the saveLayer
36        // call.
37        SkRect fSrcBounds;
38        // The pre-matrix begins as the identity and accumulates the transforms
39        // of the containing SkPictures (if any). This matrix state has to be
40        // part of the initial matrix during replay so that it will be
41        // preserved across setMatrix calls.
42        SkMatrix fPreMat;
43        // The matrix state (in the leaf picture) in which this layer's draws
44        // must occur. It will/can be overridden by setMatrix calls in the
45        // layer itself. It does not include the translation needed to map the
46        // layer's top-left point to the origin (which must be part of the
47        // initial matrix).
48        SkMatrix fLocalMat;
49        // The paint to use on restore. Can be nullptr since it is optional.
50        const SkPaint* fPaint;
51        // The index of this saveLayer in the picture.
52        size_t  fSaveLayerOpID;
53        // The index of the matching restore in the picture.
54        size_t  fRestoreOpID;
55        // True if this saveLayer has at least one other saveLayer nested within it.
56        // False otherwise.
57        bool    fHasNestedLayers;
58        // True if this saveLayer is nested within another. False otherwise.
59        bool    fIsNested;
60        // The variable length key for this saveLayer block. It stores the
61        // thread of drawPicture and saveLayer operation indices that lead to this
62        // saveLayer (including its own op index). The BlockInfo owns this memory.
63        int* fKey;
64        int  fKeySize;  // # of ints
65    };
66
67    SkLayerInfo() {}
68
69    BlockInfo& addBlock() { return fBlocks.push_back(); }
70
71    int numBlocks() const { return fBlocks.count(); }
72
73    const BlockInfo& block(int index) const {
74        SkASSERT(index < fBlocks.count());
75
76        return fBlocks[index];
77    }
78
79private:
80    SkTArray<BlockInfo, true> fBlocks;
81
82    typedef SkBigPicture::AccelData INHERITED;
83};
84
85#endif // SkLayerInfo_DEFINED
86