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 | |
| 20 | #include <utils/Errors.h> |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 21 | #include <utils/LinearAllocator.h> |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 22 | #include <utils/Vector.h> |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 23 | #include <utils/TinyHashMap.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 | |
| 29 | class SkBitmap; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 34 | class ClipOp; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 35 | class DrawOp; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 36 | class SaveOp; |
| 37 | class SaveLayerOp; |
| 38 | class StateOp; |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 39 | |
| 40 | class DeferredDisplayState; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 41 | class OpenGLRenderer; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 42 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 43 | class Batch; |
| 44 | class DrawBatch; |
| 45 | class MergingDrawBatch; |
| 46 | |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 47 | typedef const void* mergeid_t; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 48 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 49 | class DeferredDisplayState { |
| 50 | public: |
| 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 | |
| 69 | class OpStatePair { |
| 70 | public: |
| 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 Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 81 | class DeferredDisplayList { |
| 82 | public: |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 83 | DeferredDisplayList(const Rect& bounds, bool avoidOverdraw = true) : |
| 84 | mBounds(bounds), mAvoidOverdraw(avoidOverdraw) { |
| 85 | clear(); |
| 86 | } |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 87 | ~DeferredDisplayList() { clear(); } |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 88 | void reset(const Rect& bounds) { mBounds.set(bounds); } |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 89 | |
| 90 | enum OpBatchId { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 91 | kOpBatch_None = 0, // Don't batch |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 92 | 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 Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 109 | 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 Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 114 | void addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, int newSaveCount); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * 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] | 118 | * disallowReorder is false, respecting draw order when overlaps occur. |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 119 | */ |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 120 | void addDrawOp(OpenGLRenderer& renderer, DrawOp* op); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 121 | |
| 122 | private: |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 123 | DeferredDisplayState* createState() { |
| 124 | return new (mAllocator) DeferredDisplayState(); |
| 125 | } |
| 126 | |
| 127 | void tryRecycleState(DeferredDisplayState* state) { |
| 128 | mAllocator.rewindIfLastAlloc(state, sizeof(DeferredDisplayState)); |
| 129 | } |
| 130 | |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 131 | /** |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 132 | * Resets the batching back-pointers, creating a barrier in the operation stream so that no ops |
| 133 | * added in the future will be inserted into a batch that already exist. |
| 134 | */ |
| 135 | void resetBatchingState(); |
| 136 | |
Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 137 | void clear(); |
| 138 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 139 | void storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 140 | void storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, int newSaveCount); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 141 | |
| 142 | bool recordingComplexClip() const { return mComplexClipStackStart >= 0; } |
| 143 | |
| 144 | int getStateOpDeferFlags() const; |
| 145 | int getDrawOpDeferFlags() const; |
| 146 | |
Chris Craik | f70119c | 2013-06-13 11:21:22 -0700 | [diff] [blame] | 147 | void discardDrawingBatches(const unsigned int maxIndex); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 148 | |
| 149 | // layer space bounds of rendering |
| 150 | Rect mBounds; |
| 151 | const bool mAvoidOverdraw; |
| 152 | |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 153 | /** |
| 154 | * At defer time, stores the *defer time* savecount of save/saveLayer ops that were deferred, so |
| 155 | * that when an associated restoreToCount is deferred, it can be recorded as a |
| 156 | * RestoreToCountBatch |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 157 | */ |
| 158 | Vector<int> mSaveStack; |
| 159 | int mComplexClipStackStart; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 160 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 161 | Vector<Batch*> mBatches; |
| 162 | |
| 163 | // Maps batch ids to the most recent *non-merging* batch of that id |
| 164 | Batch* mBatchLookup[kOpBatch_Count]; |
| 165 | |
| 166 | // Points to the index after the most recent barrier |
| 167 | int mEarliestBatchIndex; |
| 168 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 169 | // Points to the first index that may contain a pure drawing batch |
| 170 | int mEarliestUnclearedIndex; |
| 171 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 172 | /** |
| 173 | * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen |
| 174 | * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not |
| 175 | * collide, which avoids the need to resolve mergeid collisions. |
| 176 | */ |
| 177 | TinyHashMap<mergeid_t, DrawBatch*> mMergingBatches[kOpBatch_Count]; |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 178 | |
| 179 | LinearAllocator mAllocator; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Struct containing information that instructs the defer |
| 184 | */ |
| 185 | struct DeferInfo { |
| 186 | public: |
| 187 | DeferInfo() : |
| 188 | batchId(DeferredDisplayList::kOpBatch_None), |
| 189 | mergeId((mergeid_t) -1), |
| 190 | mergeable(false), |
| 191 | opaqueOverBounds(false) { |
| 192 | }; |
| 193 | |
| 194 | int batchId; |
| 195 | mergeid_t mergeId; |
| 196 | bool mergeable; |
| 197 | bool opaqueOverBounds; // opaque over bounds in DeferredDisplayState - can skip ops below |
| 198 | }; |
| 199 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 200 | }; // namespace uirenderer |
| 201 | }; // namespace android |
| 202 | |
| 203 | #endif // ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H |