| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 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 | |
| Sene Gales | 1673035f | 2015-09-30 14:41:29 +0100 | [diff] [blame] | 20 | #include <unordered_map> |
| 21 | |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 22 | #include <utils/Errors.h> |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 23 | #include <utils/LinearAllocator.h> |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 24 | |
| 25 | #include "Matrix.h" |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 26 | #include "OpenGLRenderer.h" |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 27 | #include "Rect.h" |
| Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 28 | |
| John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
| Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 31 | class SkBitmap; |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 36 | class ClipOp; |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 37 | class DrawOp; |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 38 | class SaveOp; |
| 39 | class SaveLayerOp; |
| 40 | class StateOp; |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 41 | |
| 42 | class DeferredDisplayState; |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 43 | |
| Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 44 | class Batch; |
| 45 | class DrawBatch; |
| 46 | class MergingDrawBatch; |
| 47 | |
| Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 48 | typedef const void* mergeid_t; |
| Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 49 | |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 50 | class DeferredDisplayState { |
| 51 | public: |
| Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 52 | static void* operator new(size_t size) = delete; |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 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 |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 64 | mat4 mMatrix; |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 65 | float mAlpha; |
| Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 66 | const RoundRectClipState* mRoundRectClipState; |
| Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 67 | const ProjectionPathMask* mProjectionPathMask; |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | class OpStatePair { |
| 71 | public: |
| 72 | OpStatePair() |
| Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 73 | : op(nullptr), state(nullptr) {} |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 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 | |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 82 | class DeferredDisplayList { |
| Andreas Gampe | edaecc1 | 2014-11-10 20:54:07 -0800 | [diff] [blame] | 83 | friend struct DeferStateStruct; // used to give access to allocator |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 84 | public: |
| Chris Craik | b45c6aa | 2015-09-28 15:41:27 -0700 | [diff] [blame] | 85 | DeferredDisplayList(const Rect& bounds) |
| 86 | : mBounds(bounds) { |
| Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 87 | clear(); |
| 88 | } |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 89 | ~DeferredDisplayList() { clear(); } |
| 90 | |
| 91 | enum OpBatchId { |
| Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 92 | kOpBatch_None = 0, // Don't batch |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 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 | |
| John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 104 | bool isEmpty() { return mBatches.empty(); } |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 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 | */ |
| Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 110 | void flush(OpenGLRenderer& renderer, Rect& dirty); |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 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); |
| Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 115 | void addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, int newSaveCount); |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Add a draw op into the DeferredDisplayList, reordering as needed (for performance) if |
| Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 119 | * disallowReorder is false, respecting draw order when overlaps occur. |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 120 | */ |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 121 | void addDrawOp(OpenGLRenderer& renderer, DrawOp* op); |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 122 | |
| 123 | private: |
| Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 124 | DeferredDisplayList(const DeferredDisplayList& other); // disallow copy |
| 125 | |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 126 | DeferredDisplayState* createState() { |
| 127 | return new (mAllocator) DeferredDisplayState(); |
| 128 | } |
| 129 | |
| 130 | void tryRecycleState(DeferredDisplayState* state) { |
| John Reck | b5bc454 | 2015-04-23 15:51:55 -0700 | [diff] [blame] | 131 | mAllocator.rewindIfLastAlloc(state); |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 134 | /** |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 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 | |
| Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 140 | void clear(); |
| 141 | |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 142 | void storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op); |
| Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 143 | void storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, int newSaveCount); |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 144 | |
| 145 | bool recordingComplexClip() const { return mComplexClipStackStart >= 0; } |
| 146 | |
| 147 | int getStateOpDeferFlags() const; |
| 148 | int getDrawOpDeferFlags() const; |
| 149 | |
| Chris Craik | f70119c | 2013-06-13 11:21:22 -0700 | [diff] [blame] | 150 | void discardDrawingBatches(const unsigned int maxIndex); |
| Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 151 | |
| 152 | // layer space bounds of rendering |
| 153 | Rect mBounds; |
| Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 154 | |
| Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 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 |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 159 | */ |
| John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 160 | std::vector<int> mSaveStack; |
| Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 161 | int mComplexClipStackStart; |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 162 | |
| John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 163 | std::vector<Batch*> mBatches; |
| Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 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 Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 171 | // Points to the first index that may contain a pure drawing batch |
| 172 | int mEarliestUnclearedIndex; |
| 173 | |
| Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 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 | */ |
| Sene Gales | 1673035f | 2015-09-30 14:41:29 +0100 | [diff] [blame] | 179 | std::unordered_map<mergeid_t, DrawBatch*> mMergingBatches[kOpBatch_Count]; |
| Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 180 | |
| 181 | LinearAllocator mAllocator; |
| Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
| Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Struct containing information that instructs the defer |
| 186 | */ |
| 187 | struct DeferInfo { |
| 188 | public: |
| 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 Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 202 | }; // namespace uirenderer |
| 203 | }; // namespace android |
| 204 | |
| 205 | #endif // ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H |