DeferredDisplayList.h revision f236d7ffadd918a2a50f7cbd04349ad2222644b7
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 <unordered_map> 21 22#include <utils/Errors.h> 23#include <utils/LinearAllocator.h> 24 25#include "Matrix.h" 26#include "OpenGLRenderer.h" 27#include "Rect.h" 28 29#include <vector> 30 31class SkBitmap; 32 33namespace android { 34namespace uirenderer { 35 36class ClipOp; 37class DrawOp; 38class SaveOp; 39class SaveLayerOp; 40class StateOp; 41 42class DeferredDisplayState; 43 44class Batch; 45class DrawBatch; 46class MergingDrawBatch; 47 48typedef const void* mergeid_t; 49 50class DeferredDisplayState { 51public: 52 /** static void* operator new(size_t size); PURPOSELY OMITTED **/ 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 64 bool mClipped; 65 mat4 mMatrix; 66 float mAlpha; 67 const RoundRectClipState* mRoundRectClipState; 68 const ProjectionPathMask* mProjectionPathMask; 69}; 70 71class OpStatePair { 72public: 73 OpStatePair() 74 : op(nullptr), state(nullptr) {} 75 OpStatePair(DrawOp* newOp, const DeferredDisplayState* newState) 76 : op(newOp), state(newState) {} 77 OpStatePair(const OpStatePair& other) 78 : op(other.op), state(other.state) {} 79 DrawOp* op; 80 const DeferredDisplayState* state; 81}; 82 83class DeferredDisplayList { 84 friend struct DeferStateStruct; // used to give access to allocator 85public: 86 DeferredDisplayList(const Rect& bounds, bool avoidOverdraw = true) : 87 mBounds(bounds), mAvoidOverdraw(avoidOverdraw) { 88 clear(); 89 } 90 ~DeferredDisplayList() { clear(); } 91 92 enum OpBatchId { 93 kOpBatch_None = 0, // Don't batch 94 kOpBatch_Bitmap, 95 kOpBatch_Patch, 96 kOpBatch_AlphaVertices, 97 kOpBatch_Vertices, 98 kOpBatch_AlphaMaskTexture, 99 kOpBatch_Text, 100 kOpBatch_ColorText, 101 102 kOpBatch_Count, // Add other batch ids before this 103 }; 104 105 bool isEmpty() { return mBatches.empty(); } 106 107 /** 108 * Plays back all of the draw ops recorded into batches to the renderer. 109 * Adjusts the state of the renderer as necessary, and restores it when complete 110 */ 111 void flush(OpenGLRenderer& renderer, Rect& dirty); 112 113 void addClip(OpenGLRenderer& renderer, ClipOp* op); 114 void addSaveLayer(OpenGLRenderer& renderer, SaveLayerOp* op, int newSaveCount); 115 void addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount); 116 void addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, int newSaveCount); 117 118 /** 119 * Add a draw op into the DeferredDisplayList, reordering as needed (for performance) if 120 * disallowReorder is false, respecting draw order when overlaps occur. 121 */ 122 void addDrawOp(OpenGLRenderer& renderer, DrawOp* op); 123 124private: 125 DeferredDisplayList(const DeferredDisplayList& other); // disallow copy 126 127 DeferredDisplayState* createState() { 128 return new (mAllocator) DeferredDisplayState(); 129 } 130 131 void tryRecycleState(DeferredDisplayState* state) { 132 mAllocator.rewindIfLastAlloc(state); 133 } 134 135 /** 136 * Resets the batching back-pointers, creating a barrier in the operation stream so that no ops 137 * added in the future will be inserted into a batch that already exist. 138 */ 139 void resetBatchingState(); 140 141 void clear(); 142 143 void storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op); 144 void storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, int newSaveCount); 145 146 bool recordingComplexClip() const { return mComplexClipStackStart >= 0; } 147 148 int getStateOpDeferFlags() const; 149 int getDrawOpDeferFlags() const; 150 151 void discardDrawingBatches(const unsigned int maxIndex); 152 153 // layer space bounds of rendering 154 Rect mBounds; 155 const bool mAvoidOverdraw; 156 157 /** 158 * At defer time, stores the *defer time* savecount of save/saveLayer ops that were deferred, so 159 * that when an associated restoreToCount is deferred, it can be recorded as a 160 * RestoreToCountBatch 161 */ 162 std::vector<int> mSaveStack; 163 int mComplexClipStackStart; 164 165 std::vector<Batch*> mBatches; 166 167 // Maps batch ids to the most recent *non-merging* batch of that id 168 Batch* mBatchLookup[kOpBatch_Count]; 169 170 // Points to the index after the most recent barrier 171 int mEarliestBatchIndex; 172 173 // Points to the first index that may contain a pure drawing batch 174 int mEarliestUnclearedIndex; 175 176 /** 177 * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen 178 * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not 179 * collide, which avoids the need to resolve mergeid collisions. 180 */ 181 std::unordered_map<mergeid_t, DrawBatch*> mMergingBatches[kOpBatch_Count]; 182 183 LinearAllocator mAllocator; 184}; 185 186/** 187 * Struct containing information that instructs the defer 188 */ 189struct DeferInfo { 190public: 191 DeferInfo() : 192 batchId(DeferredDisplayList::kOpBatch_None), 193 mergeId((mergeid_t) -1), 194 mergeable(false), 195 opaqueOverBounds(false) { 196 }; 197 198 int batchId; 199 mergeid_t mergeId; 200 bool mergeable; 201 bool opaqueOverBounds; // opaque over bounds in DeferredDisplayState - can skip ops below 202}; 203 204}; // namespace uirenderer 205}; // namespace android 206 207#endif // ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H 208