DeferredDisplayList.h revision b45c6aa665624013ef3b207fffcfe265041f6bff
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 <utils/Errors.h>
21#include <utils/LinearAllocator.h>
22#include <utils/TinyHashMap.h>
23
24#include "Matrix.h"
25#include "OpenGLRenderer.h"
26#include "Rect.h"
27
28#include <vector>
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)
86            : mBounds(bounds) {
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.empty(); }
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
155    /**
156     * At defer time, stores the *defer time* savecount of save/saveLayer ops that were deferred, so
157     * that when an associated restoreToCount is deferred, it can be recorded as a
158     * RestoreToCountBatch
159     */
160    std::vector<int> mSaveStack;
161    int mComplexClipStackStart;
162
163    std::vector<Batch*> mBatches;
164
165    // Maps batch ids to the most recent *non-merging* batch of that id
166    Batch* mBatchLookup[kOpBatch_Count];
167
168    // Points to the index after the most recent barrier
169    int mEarliestBatchIndex;
170
171    // Points to the first index that may contain a pure drawing batch
172    int mEarliestUnclearedIndex;
173
174    /**
175     * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
176     * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
177     * collide, which avoids the need to resolve mergeid collisions.
178     */
179    TinyHashMap<mergeid_t, DrawBatch*> mMergingBatches[kOpBatch_Count];
180
181    LinearAllocator mAllocator;
182};
183
184/**
185 * Struct containing information that instructs the defer
186 */
187struct DeferInfo {
188public:
189    DeferInfo() :
190            batchId(DeferredDisplayList::kOpBatch_None),
191            mergeId((mergeid_t) -1),
192            mergeable(false),
193            opaqueOverBounds(false) {
194    };
195
196    int batchId;
197    mergeid_t mergeId;
198    bool mergeable;
199    bool opaqueOverBounds; // opaque over bounds in DeferredDisplayState - can skip ops below
200};
201
202}; // namespace uirenderer
203}; // namespace android
204
205#endif // ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H
206