DeferredDisplayList.h revision 6706b71d7cfb9a916fd482189c04a0d59c19c103
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_DEFERRED_DISPLAY_LIST_H
18#define ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H
19
20#include <unordered_map>
21
22#include <utils/Errors.h>
23#include <utils/LinearAllocator.h>
24#include <utils/Vector.h>
25
26#include "Matrix.h"
27#include "OpenGLRenderer.h"
28#include "Rect.h"
29
30class SkBitmap;
31
32namespace android {
33namespace uirenderer {
34
35class ClipOp;
36class DrawOp;
37class SaveOp;
38class SaveLayerOp;
39class StateOp;
40
41class DeferredDisplayState;
42
43class Batch;
44class DrawBatch;
45class MergingDrawBatch;
46
47typedef const void* mergeid_t;
48
49class DeferredDisplayState {
50public:
51    /** static void* operator new(size_t size); PURPOSELY OMITTED **/
52    static void* operator new(size_t size, LinearAllocator& allocator) {
53        return allocator.alloc(size);
54    }
55
56    // global op bounds, mapped by mMatrix to be in screen space coordinates, clipped
57    Rect mBounds;
58
59    // the below are set and used by the OpenGLRenderer at record and deferred playback
60    bool mClipValid;
61    Rect mClip;
62    int mClipSideFlags; // specifies which sides of the bounds are clipped, unclipped if cleared
63    bool mClipped;
64    mat4 mMatrix;
65    float mAlpha;
66    const RoundRectClipState* mRoundRectClipState;
67    const ProjectionPathMask* mProjectionPathMask;
68};
69
70class OpStatePair {
71public:
72    OpStatePair()
73            : op(nullptr), state(nullptr) {}
74    OpStatePair(DrawOp* newOp, const DeferredDisplayState* newState)
75            : op(newOp), state(newState) {}
76    OpStatePair(const OpStatePair& other)
77            : op(other.op), state(other.state) {}
78    DrawOp* op;
79    const DeferredDisplayState* state;
80};
81
82class DeferredDisplayList {
83    friend struct DeferStateStruct; // used to give access to allocator
84public:
85    DeferredDisplayList(const Rect& bounds, bool avoidOverdraw = true) :
86            mBounds(bounds), mAvoidOverdraw(avoidOverdraw) {
87        clear();
88    }
89    ~DeferredDisplayList() { clear(); }
90
91    enum OpBatchId {
92        kOpBatch_None = 0, // Don't batch
93        kOpBatch_Bitmap,
94        kOpBatch_Patch,
95        kOpBatch_AlphaVertices,
96        kOpBatch_Vertices,
97        kOpBatch_AlphaMaskTexture,
98        kOpBatch_Text,
99        kOpBatch_ColorText,
100
101        kOpBatch_Count, // Add other batch ids before this
102    };
103
104    bool isEmpty() { return mBatches.isEmpty(); }
105
106    /**
107     * Plays back all of the draw ops recorded into batches to the renderer.
108     * Adjusts the state of the renderer as necessary, and restores it when complete
109     */
110    void flush(OpenGLRenderer& renderer, Rect& dirty);
111
112    void addClip(OpenGLRenderer& renderer, ClipOp* op);
113    void addSaveLayer(OpenGLRenderer& renderer, SaveLayerOp* op, int newSaveCount);
114    void addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount);
115    void addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
116
117    /**
118     * Add a draw op into the DeferredDisplayList, reordering as needed (for performance) if
119     * disallowReorder is false, respecting draw order when overlaps occur.
120     */
121    void addDrawOp(OpenGLRenderer& renderer, DrawOp* op);
122
123private:
124    DeferredDisplayList(const DeferredDisplayList& other); // disallow copy
125
126    DeferredDisplayState* createState() {
127        return new (mAllocator) DeferredDisplayState();
128    }
129
130    void tryRecycleState(DeferredDisplayState* state) {
131        mAllocator.rewindIfLastAlloc(state);
132    }
133
134    /**
135     * Resets the batching back-pointers, creating a barrier in the operation stream so that no ops
136     * added in the future will be inserted into a batch that already exist.
137     */
138    void resetBatchingState();
139
140    void clear();
141
142    void storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op);
143    void storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
144
145    bool recordingComplexClip() const { return mComplexClipStackStart >= 0; }
146
147    int getStateOpDeferFlags() const;
148    int getDrawOpDeferFlags() const;
149
150    void discardDrawingBatches(const unsigned int maxIndex);
151
152    // layer space bounds of rendering
153    Rect mBounds;
154    const bool mAvoidOverdraw;
155
156    /**
157     * At defer time, stores the *defer time* savecount of save/saveLayer ops that were deferred, so
158     * that when an associated restoreToCount is deferred, it can be recorded as a
159     * RestoreToCountBatch
160     */
161    Vector<int> mSaveStack;
162    int mComplexClipStackStart;
163
164    Vector<Batch*> mBatches;
165
166    // Maps batch ids to the most recent *non-merging* batch of that id
167    Batch* mBatchLookup[kOpBatch_Count];
168
169    // Points to the index after the most recent barrier
170    int mEarliestBatchIndex;
171
172    // Points to the first index that may contain a pure drawing batch
173    int mEarliestUnclearedIndex;
174
175    /**
176     * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
177     * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
178     * collide, which avoids the need to resolve mergeid collisions.
179     */
180    std::unordered_map<mergeid_t, DrawBatch*> mMergingBatches[kOpBatch_Count];
181
182    LinearAllocator mAllocator;
183};
184
185/**
186 * Struct containing information that instructs the defer
187 */
188struct DeferInfo {
189public:
190    DeferInfo() :
191            batchId(DeferredDisplayList::kOpBatch_None),
192            mergeId((mergeid_t) -1),
193            mergeable(false),
194            opaqueOverBounds(false) {
195    };
196
197    int batchId;
198    mergeid_t mergeId;
199    bool mergeable;
200    bool opaqueOverBounds; // opaque over bounds in DeferredDisplayState - can skip ops below
201};
202
203}; // namespace uirenderer
204}; // namespace android
205
206#endif // ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H
207