DisplayList.h revision b565df13a9e5c7b1d7d93bdfa4a793752d66d3cc
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HWUI_DISPLAY_LIST_H
18#define ANDROID_HWUI_DISPLAY_LIST_H
19
20#include <SkCamera.h>
21#include <SkMatrix.h>
22
23#include <private/hwui/DrawGlInfo.h>
24
25#include <utils/KeyedVector.h>
26#include <utils/LinearAllocator.h>
27#include <utils/RefBase.h>
28#include <utils/SortedVector.h>
29#include <utils/String8.h>
30
31#include <cutils/compiler.h>
32
33#include <androidfw/ResourceTypes.h>
34
35#include "Debug.h"
36#include "CanvasProperty.h"
37#include "DeferredDisplayList.h"
38#include "Matrix.h"
39#include "RenderProperties.h"
40
41#include <vector>
42
43class SkBitmap;
44class SkPaint;
45class SkPath;
46class SkRegion;
47
48namespace android {
49namespace uirenderer {
50
51class DeferredDisplayList;
52class DisplayListOp;
53class DisplayListCanvas;
54class OpenGLRenderer;
55class Rect;
56class Layer;
57
58#if HWUI_NEW_OPS
59struct RecordedOp;
60struct RenderNodeOp;
61#else
62class DrawRenderNodeOp;
63#endif
64
65/**
66 * Holds data used in the playback a tree of DisplayLists.
67 */
68struct PlaybackStateStruct {
69protected:
70    PlaybackStateStruct(OpenGLRenderer& renderer, int replayFlags, LinearAllocator* allocator)
71            : mRenderer(renderer)
72            , mReplayFlags(replayFlags)
73            , mAllocator(allocator) {}
74
75public:
76    OpenGLRenderer& mRenderer;
77    const int mReplayFlags;
78
79    // Allocator with the lifetime of a single frame. replay uses an Allocator owned by the struct,
80    // while defer shares the DeferredDisplayList's Allocator
81    // TODO: move this allocator to be owned by object with clear frame lifecycle
82    LinearAllocator * const mAllocator;
83
84    SkPath* allocPathForFrame() {
85        return mRenderer.allocPathForFrame();
86    }
87};
88
89struct DeferStateStruct : public PlaybackStateStruct {
90    DeferStateStruct(DeferredDisplayList& deferredList, OpenGLRenderer& renderer, int replayFlags)
91            : PlaybackStateStruct(renderer, replayFlags, &(deferredList.mAllocator)),
92            mDeferredList(deferredList) {}
93
94    DeferredDisplayList& mDeferredList;
95};
96
97struct ReplayStateStruct : public PlaybackStateStruct {
98    ReplayStateStruct(OpenGLRenderer& renderer, Rect& dirty, int replayFlags)
99            : PlaybackStateStruct(renderer, replayFlags, &mReplayAllocator),
100            mDirty(dirty) {}
101
102    Rect& mDirty;
103    LinearAllocator mReplayAllocator;
104};
105
106/**
107 * Data structure that holds the list of commands used in display list stream
108 */
109class DisplayListData {
110    friend class DisplayListCanvas;
111    friend class RecordingCanvas;
112public:
113    struct Chunk {
114        // range of included ops in DLD::displayListOps
115        size_t beginOpIndex;
116        size_t endOpIndex;
117
118        // range of included children in DLD::mChildren
119        size_t beginChildIndex;
120        size_t endChildIndex;
121
122        // whether children with non-zero Z in the chunk should be reordered
123        bool reorderChildren;
124    };
125
126    DisplayListData();
127    ~DisplayListData();
128
129    // pointers to all ops within display list, pointing into allocator data
130    std::vector<DisplayListOp*> displayListOps;
131
132    // index of DisplayListOp restore, after which projected descendents should be drawn
133    int projectionReceiveIndex;
134
135    std::vector<const SkBitmap*> bitmapResources;
136    std::vector<const SkPath*> pathResources;
137    std::vector<const Res_png_9patch*> patchResources;
138
139    std::vector<std::unique_ptr<const SkPaint>> paints;
140    std::vector<std::unique_ptr<const SkRegion>> regions;
141    Vector<Functor*> functors;
142
143    const std::vector<Chunk>& getChunks() const {
144            return chunks;
145    }
146#if HWUI_NEW_OPS
147    const std::vector<RecordedOp*>& getOps() const {
148        return ops;
149    }
150#endif
151
152#if HWUI_NEW_OPS
153    size_t addChild(RenderNodeOp* childOp);
154    const std::vector<RenderNodeOp*>& children() { return mChildren; }
155#else
156    size_t addChild(DrawRenderNodeOp* childOp);
157    const std::vector<DrawRenderNodeOp*>& children() { return mChildren; }
158#endif
159
160    void ref(VirtualLightRefBase* prop) {
161        mReferenceHolders.push_back(prop);
162    }
163
164    size_t getUsedSize() {
165        return allocator.usedSize();
166    }
167    bool isEmpty() {
168        return !hasDrawOps;
169    }
170
171private:
172#if HWUI_NEW_OPS
173    std::vector<RecordedOp*> ops;
174#endif
175
176    std::vector< sp<VirtualLightRefBase> > mReferenceHolders;
177
178#if HWUI_NEW_OPS
179    std::vector<RenderNodeOp*> mChildren;
180#else
181    // list of children display lists for quick, non-drawing traversal
182    std::vector<DrawRenderNodeOp*> mChildren;
183#endif
184
185    std::vector<Chunk> chunks;
186
187    // allocator into which all ops were allocated
188    LinearAllocator allocator;
189    bool hasDrawOps;
190
191    void cleanupResources();
192};
193
194}; // namespace uirenderer
195}; // namespace android
196
197#endif // ANDROID_HWUI_OPENGL_RENDERER_H
198