blob: 15703ea3feef4ddee2aa46014c518078bca2a990 [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
Sene Gales16730352015-09-30 14:41:29 +010020#include <unordered_map>
21
Chris Craikc3566d02013-02-04 16:16:33 -080022#include <utils/Errors.h>
Chris Craikc1c5f082013-09-11 16:23:37 -070023#include <utils/LinearAllocator.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
John Reck272a6852015-07-29 16:48:58 -070029#include <vector>
30
Chris Craik527a3aa2013-03-04 10:19:31 -080031class SkBitmap;
Chris Craikc3566d02013-02-04 16:16:33 -080032
33namespace android {
34namespace uirenderer {
35
Chris Craikff785832013-03-08 13:12:16 -080036class ClipOp;
Chris Craikc3566d02013-02-04 16:16:33 -080037class DrawOp;
Chris Craikff785832013-03-08 13:12:16 -080038class SaveOp;
39class SaveLayerOp;
40class StateOp;
Chris Craikc1c5f082013-09-11 16:23:37 -070041
42class DeferredDisplayState;
Chris Craikc3566d02013-02-04 16:16:33 -080043
Chris Craik527a3aa2013-03-04 10:19:31 -080044class Batch;
45class DrawBatch;
46class MergingDrawBatch;
47
Romain Guy7f6d6b02013-08-06 13:49:28 -070048typedef const void* mergeid_t;
Chris Craik527a3aa2013-03-04 10:19:31 -080049
Chris Craikc1c5f082013-09-11 16:23:37 -070050class DeferredDisplayState {
51public:
Chris Craikc1c5f082013-09-11 16:23:37 -070052 // global op bounds, mapped by mMatrix to be in screen space coordinates, clipped
53 Rect mBounds;
54
55 // the below are set and used by the OpenGLRenderer at record and deferred playback
56 bool mClipValid;
57 Rect mClip;
58 int mClipSideFlags; // specifies which sides of the bounds are clipped, unclipped if cleared
Chris Craikc1c5f082013-09-11 16:23:37 -070059 mat4 mMatrix;
Chris Craikc1c5f082013-09-11 16:23:37 -070060 float mAlpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -070061 const RoundRectClipState* mRoundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -070062 const ProjectionPathMask* mProjectionPathMask;
Chris Craikc1c5f082013-09-11 16:23:37 -070063};
64
65class OpStatePair {
66public:
67 OpStatePair()
Chris Craike84a2082014-12-22 14:28:49 -080068 : op(nullptr), state(nullptr) {}
Chris Craikc1c5f082013-09-11 16:23:37 -070069 OpStatePair(DrawOp* newOp, const DeferredDisplayState* newState)
70 : op(newOp), state(newState) {}
71 OpStatePair(const OpStatePair& other)
72 : op(other.op), state(other.state) {}
73 DrawOp* op;
74 const DeferredDisplayState* state;
75};
76
Chris Craikc3566d02013-02-04 16:16:33 -080077class DeferredDisplayList {
Andreas Gampeedaecc12014-11-10 20:54:07 -080078 friend struct DeferStateStruct; // used to give access to allocator
Chris Craikc3566d02013-02-04 16:16:33 -080079public:
Chih-Hung Hsieh05160d72016-07-21 18:13:31 -070080 explicit DeferredDisplayList(const Rect& bounds)
Chris Craikb45c6aa2015-09-28 15:41:27 -070081 : mBounds(bounds) {
Chris Craik28ce94a2013-05-31 11:38:03 -070082 clear();
83 }
Chris Craikc3566d02013-02-04 16:16:33 -080084 ~DeferredDisplayList() { clear(); }
85
86 enum OpBatchId {
Chris Craik527a3aa2013-03-04 10:19:31 -080087 kOpBatch_None = 0, // Don't batch
Chris Craikc3566d02013-02-04 16:16:33 -080088 kOpBatch_Bitmap,
89 kOpBatch_Patch,
90 kOpBatch_AlphaVertices,
91 kOpBatch_Vertices,
92 kOpBatch_AlphaMaskTexture,
93 kOpBatch_Text,
94 kOpBatch_ColorText,
95
96 kOpBatch_Count, // Add other batch ids before this
97 };
98
John Reck272a6852015-07-29 16:48:58 -070099 bool isEmpty() { return mBatches.empty(); }
Chris Craikc3566d02013-02-04 16:16:33 -0800100
101 /**
102 * Plays back all of the draw ops recorded into batches to the renderer.
103 * Adjusts the state of the renderer as necessary, and restores it when complete
104 */
Tom Hudson107843d2014-09-08 11:26:26 -0400105 void flush(OpenGLRenderer& renderer, Rect& dirty);
Chris Craikff785832013-03-08 13:12:16 -0800106
107 void addClip(OpenGLRenderer& renderer, ClipOp* op);
108 void addSaveLayer(OpenGLRenderer& renderer, SaveLayerOp* op, int newSaveCount);
109 void addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700110 void addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
Chris Craikc3566d02013-02-04 16:16:33 -0800111
112 /**
113 * Add a draw op into the DeferredDisplayList, reordering as needed (for performance) if
Chris Craik5e49b302013-07-30 19:05:20 -0700114 * disallowReorder is false, respecting draw order when overlaps occur.
Chris Craikc3566d02013-02-04 16:16:33 -0800115 */
Chris Craikff785832013-03-08 13:12:16 -0800116 void addDrawOp(OpenGLRenderer& renderer, DrawOp* op);
Chris Craikc3566d02013-02-04 16:16:33 -0800117
118private:
Chris Craikf57776b2013-10-25 18:30:17 -0700119 DeferredDisplayList(const DeferredDisplayList& other); // disallow copy
120
Chris Craikc1c5f082013-09-11 16:23:37 -0700121 DeferredDisplayState* createState() {
John Reck7df9ff22016-02-10 16:08:08 -0800122 return mAllocator.create_trivial<DeferredDisplayState>();
Chris Craikc1c5f082013-09-11 16:23:37 -0700123 }
124
125 void tryRecycleState(DeferredDisplayState* state) {
John Reckb5bc4542015-04-23 15:51:55 -0700126 mAllocator.rewindIfLastAlloc(state);
Chris Craikc1c5f082013-09-11 16:23:37 -0700127 }
128
Chris Craikd90144d2013-03-19 15:03:48 -0700129 /**
Chris Craikff785832013-03-08 13:12:16 -0800130 * Resets the batching back-pointers, creating a barrier in the operation stream so that no ops
131 * added in the future will be inserted into a batch that already exist.
132 */
133 void resetBatchingState();
134
Chris Craik1206b9b2013-04-04 14:46:24 -0700135 void clear();
136
Chris Craikff785832013-03-08 13:12:16 -0800137 void storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op);
Chris Craik7273daa2013-03-28 11:25:24 -0700138 void storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
Chris Craikff785832013-03-08 13:12:16 -0800139
140 bool recordingComplexClip() const { return mComplexClipStackStart >= 0; }
141
142 int getStateOpDeferFlags() const;
143 int getDrawOpDeferFlags() const;
144
Chris Craikf70119c2013-06-13 11:21:22 -0700145 void discardDrawingBatches(const unsigned int maxIndex);
Chris Craik28ce94a2013-05-31 11:38:03 -0700146
147 // layer space bounds of rendering
148 Rect mBounds;
Chris Craik28ce94a2013-05-31 11:38:03 -0700149
Chris Craikd90144d2013-03-19 15:03:48 -0700150 /**
151 * At defer time, stores the *defer time* savecount of save/saveLayer ops that were deferred, so
152 * that when an associated restoreToCount is deferred, it can be recorded as a
153 * RestoreToCountBatch
Chris Craikff785832013-03-08 13:12:16 -0800154 */
John Reck272a6852015-07-29 16:48:58 -0700155 std::vector<int> mSaveStack;
Chris Craikff785832013-03-08 13:12:16 -0800156 int mComplexClipStackStart;
Chris Craikc3566d02013-02-04 16:16:33 -0800157
John Reck272a6852015-07-29 16:48:58 -0700158 std::vector<Batch*> mBatches;
Chris Craik527a3aa2013-03-04 10:19:31 -0800159
160 // Maps batch ids to the most recent *non-merging* batch of that id
161 Batch* mBatchLookup[kOpBatch_Count];
162
163 // Points to the index after the most recent barrier
164 int mEarliestBatchIndex;
165
Chris Craik28ce94a2013-05-31 11:38:03 -0700166 // Points to the first index that may contain a pure drawing batch
167 int mEarliestUnclearedIndex;
168
Chris Craik527a3aa2013-03-04 10:19:31 -0800169 /**
170 * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
171 * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
172 * collide, which avoids the need to resolve mergeid collisions.
173 */
Sene Gales16730352015-09-30 14:41:29 +0100174 std::unordered_map<mergeid_t, DrawBatch*> mMergingBatches[kOpBatch_Count];
Chris Craikc1c5f082013-09-11 16:23:37 -0700175
176 LinearAllocator mAllocator;
Chris Craikc3566d02013-02-04 16:16:33 -0800177};
178
Chris Craik28ce94a2013-05-31 11:38:03 -0700179/**
180 * Struct containing information that instructs the defer
181 */
182struct DeferInfo {
183public:
184 DeferInfo() :
185 batchId(DeferredDisplayList::kOpBatch_None),
186 mergeId((mergeid_t) -1),
187 mergeable(false),
188 opaqueOverBounds(false) {
189 };
190
191 int batchId;
192 mergeid_t mergeId;
193 bool mergeable;
194 bool opaqueOverBounds; // opaque over bounds in DeferredDisplayState - can skip ops below
195};
196
Chris Craikc3566d02013-02-04 16:16:33 -0800197}; // namespace uirenderer
198}; // namespace android
199
200#endif // ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H