DeferredDisplayList.h revision 0e26fc471d0a9084f6ad2b97eebadfea9763b855
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
25#include "Matrix.h"
26#include "OpenGLRenderer.h"
27#include "Rect.h"
28
29#include <vector>
30
31class SkBitmap;
32
33namespace android {
34namespace uirenderer {
35
36class ClipOp;
37class DrawOp;
38class SaveOp;
39class SaveLayerOp;
40class StateOp;
41
42class DeferredDisplayState;
43
44class Batch;
45class DrawBatch;
46class MergingDrawBatch;
47
48typedef const void* mergeid_t;
49
50class DeferredDisplayState {
51public:
52    /** static void* operator new(size_t size); PURPOSELY OMITTED **/
53    static void* operator new(size_t size, LinearAllocator& allocator) {
54        return allocator.alloc(size);
55    }
56
57    // global op bounds, mapped by mMatrix to be in screen space coordinates, clipped
58    Rect mBounds;
59
60    // the below are set and used by the OpenGLRenderer at record and deferred playback
61    bool mClipValid;
62    Rect mClip;
63    int mClipSideFlags; // specifies which sides of the bounds are clipped, unclipped if cleared
64    bool mClipped;
65    mat4 mMatrix;
66    float mAlpha;
67    const RoundRectClipState* mRoundRectClipState;
68    const ProjectionPathMask* mProjectionPathMask;
69};
70
71class OpStatePair {
72public:
73    OpStatePair()
74            : op(nullptr), state(nullptr) {}
75    OpStatePair(DrawOp* newOp, const DeferredDisplayState* newState)
76            : op(newOp), state(newState) {}
77    OpStatePair(const OpStatePair& other)
78            : op(other.op), state(other.state) {}
79    DrawOp* op;
80    const DeferredDisplayState* state;
81};
82
83class DeferredDisplayList {
84    friend struct DeferStateStruct; // used to give access to allocator
85public:
86    DeferredDisplayList(const Rect& bounds)
87            : mBounds(bounds) {
88        clear();
89    }
90    ~DeferredDisplayList() { clear(); }
91
92    enum OpBatchId {
93        kOpBatch_None = 0, // Don't batch
94        kOpBatch_Bitmap,
95        kOpBatch_Patch,
96        kOpBatch_AlphaVertices,
97        kOpBatch_Vertices,
98        kOpBatch_AlphaMaskTexture,
99        kOpBatch_Text,
100        kOpBatch_ColorText,
101
102        kOpBatch_Count, // Add other batch ids before this
103    };
104
105    bool isEmpty() { return mBatches.empty(); }
106
107    /**
108     * Plays back all of the draw ops recorded into batches to the renderer.
109     * Adjusts the state of the renderer as necessary, and restores it when complete
110     */
111    void flush(OpenGLRenderer& renderer, Rect& dirty);
112
113    void addClip(OpenGLRenderer& renderer, ClipOp* op);
114    void addSaveLayer(OpenGLRenderer& renderer, SaveLayerOp* op, int newSaveCount);
115    void addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount);
116    void addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
117
118    /**
119     * Add a draw op into the DeferredDisplayList, reordering as needed (for performance) if
120     * disallowReorder is false, respecting draw order when overlaps occur.
121     */
122    void addDrawOp(OpenGLRenderer& renderer, DrawOp* op);
123
124private:
125    DeferredDisplayList(const DeferredDisplayList& other); // disallow copy
126
127    DeferredDisplayState* createState() {
128        return new (mAllocator) DeferredDisplayState();
129    }
130
131    void tryRecycleState(DeferredDisplayState* state) {
132        mAllocator.rewindIfLastAlloc(state);
133    }
134
135    /**
136     * Resets the batching back-pointers, creating a barrier in the operation stream so that no ops
137     * added in the future will be inserted into a batch that already exist.
138     */
139    void resetBatchingState();
140
141    void clear();
142
143    void storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op);
144    void storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
145
146    bool recordingComplexClip() const { return mComplexClipStackStart >= 0; }
147
148    int getStateOpDeferFlags() const;
149    int getDrawOpDeferFlags() const;
150
151    void discardDrawingBatches(const unsigned int maxIndex);
152
153    // layer space bounds of rendering
154    Rect mBounds;
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    std::vector<int> mSaveStack;
162    int mComplexClipStackStart;
163
164    std::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