blob: fca3588aa3447e0dd7d3d439a030557c5786b8fa [file] [log] [blame]
Chris Craikc3566d02013-02-04 16:16:33 -08001/*
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>
Chris Craikc1c5f082013-09-11 16:23:37 -070021#include <utils/LinearAllocator.h>
Chris Craikc3566d02013-02-04 16:16:33 -080022#include <utils/Vector.h>
Chris Craikc1c5f082013-09-11 16:23:37 -070023#include <utils/TinyHashMap.h>
Chris Craikc3566d02013-02-04 16:16:33 -080024
25#include "Matrix.h"
Chris Craikc1c5f082013-09-11 16:23:37 -070026#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080027#include "Rect.h"
Chris Craik527a3aa2013-03-04 10:19:31 -080028
29class SkBitmap;
Chris Craikc3566d02013-02-04 16:16:33 -080030
31namespace android {
32namespace uirenderer {
33
Chris Craikff785832013-03-08 13:12:16 -080034class ClipOp;
Chris Craikc3566d02013-02-04 16:16:33 -080035class DrawOp;
Chris Craikff785832013-03-08 13:12:16 -080036class SaveOp;
37class SaveLayerOp;
38class StateOp;
Chris Craikc1c5f082013-09-11 16:23:37 -070039
40class DeferredDisplayState;
Chris Craikc3566d02013-02-04 16:16:33 -080041class OpenGLRenderer;
Chris Craikc3566d02013-02-04 16:16:33 -080042
Chris Craik527a3aa2013-03-04 10:19:31 -080043class Batch;
44class DrawBatch;
45class MergingDrawBatch;
46
Romain Guy7f6d6b02013-08-06 13:49:28 -070047typedef const void* mergeid_t;
Chris Craik527a3aa2013-03-04 10:19:31 -080048
Chris Craikc1c5f082013-09-11 16:23:37 -070049class 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 DrawModifiers mDrawModifiers;
66 float mAlpha;
67};
68
69class OpStatePair {
70public:
71 OpStatePair()
72 : op(NULL), state(NULL) {}
73 OpStatePair(DrawOp* newOp, const DeferredDisplayState* newState)
74 : op(newOp), state(newState) {}
75 OpStatePair(const OpStatePair& other)
76 : op(other.op), state(other.state) {}
77 DrawOp* op;
78 const DeferredDisplayState* state;
79};
80
Chris Craikc3566d02013-02-04 16:16:33 -080081class DeferredDisplayList {
Chris Craikf57776b2013-10-25 18:30:17 -070082 friend class DeferStateStruct; // used to give access to allocator
Chris Craikc3566d02013-02-04 16:16:33 -080083public:
Chris Craik28ce94a2013-05-31 11:38:03 -070084 DeferredDisplayList(const Rect& bounds, bool avoidOverdraw = true) :
85 mBounds(bounds), mAvoidOverdraw(avoidOverdraw) {
86 clear();
87 }
Chris Craikc3566d02013-02-04 16:16:33 -080088 ~DeferredDisplayList() { clear(); }
89
90 enum OpBatchId {
Chris Craik527a3aa2013-03-04 10:19:31 -080091 kOpBatch_None = 0, // Don't batch
Chris Craikc3566d02013-02-04 16:16:33 -080092 kOpBatch_Bitmap,
93 kOpBatch_Patch,
94 kOpBatch_AlphaVertices,
95 kOpBatch_Vertices,
96 kOpBatch_AlphaMaskTexture,
97 kOpBatch_Text,
98 kOpBatch_ColorText,
99
100 kOpBatch_Count, // Add other batch ids before this
101 };
102
103 bool isEmpty() { return mBatches.isEmpty(); }
104
105 /**
106 * Plays back all of the draw ops recorded into batches to the renderer.
107 * Adjusts the state of the renderer as necessary, and restores it when complete
108 */
Chris Craikff785832013-03-08 13:12:16 -0800109 status_t flush(OpenGLRenderer& renderer, Rect& dirty);
110
111 void addClip(OpenGLRenderer& renderer, ClipOp* op);
112 void addSaveLayer(OpenGLRenderer& renderer, SaveLayerOp* op, int newSaveCount);
113 void addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700114 void addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
Chris Craikc3566d02013-02-04 16:16:33 -0800115
116 /**
117 * Add a draw op into the DeferredDisplayList, reordering as needed (for performance) if
Chris Craik5e49b302013-07-30 19:05:20 -0700118 * disallowReorder is false, respecting draw order when overlaps occur.
Chris Craikc3566d02013-02-04 16:16:33 -0800119 */
Chris Craikff785832013-03-08 13:12:16 -0800120 void addDrawOp(OpenGLRenderer& renderer, DrawOp* op);
Chris Craikc3566d02013-02-04 16:16:33 -0800121
122private:
Chris Craikf57776b2013-10-25 18:30:17 -0700123 DeferredDisplayList(const DeferredDisplayList& other); // disallow copy
124
Chris Craikc1c5f082013-09-11 16:23:37 -0700125 DeferredDisplayState* createState() {
126 return new (mAllocator) DeferredDisplayState();
127 }
128
129 void tryRecycleState(DeferredDisplayState* state) {
130 mAllocator.rewindIfLastAlloc(state, sizeof(DeferredDisplayState));
131 }
132
Chris Craikd90144d2013-03-19 15:03:48 -0700133 /**
Chris Craikff785832013-03-08 13:12:16 -0800134 * Resets the batching back-pointers, creating a barrier in the operation stream so that no ops
135 * added in the future will be inserted into a batch that already exist.
136 */
137 void resetBatchingState();
138
Chris Craik1206b9b2013-04-04 14:46:24 -0700139 void clear();
140
Chris Craikff785832013-03-08 13:12:16 -0800141 void storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op);
Chris Craik7273daa2013-03-28 11:25:24 -0700142 void storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
Chris Craikff785832013-03-08 13:12:16 -0800143
144 bool recordingComplexClip() const { return mComplexClipStackStart >= 0; }
145
146 int getStateOpDeferFlags() const;
147 int getDrawOpDeferFlags() const;
148
Chris Craikf70119c2013-06-13 11:21:22 -0700149 void discardDrawingBatches(const unsigned int maxIndex);
Chris Craik28ce94a2013-05-31 11:38:03 -0700150
151 // layer space bounds of rendering
152 Rect mBounds;
153 const bool mAvoidOverdraw;
154
Chris Craikd90144d2013-03-19 15:03:48 -0700155 /**
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
Chris Craikff785832013-03-08 13:12:16 -0800159 */
160 Vector<int> mSaveStack;
161 int mComplexClipStackStart;
Chris Craikc3566d02013-02-04 16:16:33 -0800162
Chris Craik527a3aa2013-03-04 10:19:31 -0800163 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
Chris Craik28ce94a2013-05-31 11:38:03 -0700171 // Points to the first index that may contain a pure drawing batch
172 int mEarliestUnclearedIndex;
173
Chris Craik527a3aa2013-03-04 10:19:31 -0800174 /**
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];
Chris Craikc1c5f082013-09-11 16:23:37 -0700180
181 LinearAllocator mAllocator;
Chris Craikc3566d02013-02-04 16:16:33 -0800182};
183
Chris Craik28ce94a2013-05-31 11:38:03 -0700184/**
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
Chris Craikc3566d02013-02-04 16:16:33 -0800202}; // namespace uirenderer
203}; // namespace android
204
205#endif // ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H