bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 8 | #include "GrOpFlushState.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 9 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 10 | #include "GrContextPriv.h" |
Brian Salomon | 903da79 | 2016-12-16 14:24:46 -0500 | [diff] [blame] | 11 | #include "GrDrawOpAtlas.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 12 | #include "GrGpu.h" |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 13 | #include "GrResourceProvider.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 14 | #include "GrTexture.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 15 | |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 16 | ////////////////////////////////////////////////////////////////////////////// |
| 17 | |
Brian Salomon | 58f153c | 2018-10-18 21:51:15 -0400 | [diff] [blame] | 18 | GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider, |
| 19 | GrTokenTracker* tokenTracker, void* vertexSpace, void* indexSpace) |
| 20 | : fVertexPool(gpu, vertexSpace) |
| 21 | , fIndexPool(gpu, indexSpace) |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 22 | , fGpu(gpu) |
| 23 | , fResourceProvider(resourceProvider) |
Brian Salomon | 58f153c | 2018-10-18 21:51:15 -0400 | [diff] [blame] | 24 | , fTokenTracker(tokenTracker) {} |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 25 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 26 | const GrCaps& GrOpFlushState::caps() const { |
| 27 | return *fGpu->caps(); |
| 28 | } |
| 29 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 30 | GrGpuRTCommandBuffer* GrOpFlushState::rtCommandBuffer() { |
| 31 | return fCommandBuffer->asRTCommandBuffer(); |
| 32 | } |
| 33 | |
Brian Osman | e801210 | 2018-11-29 14:05:07 -0500 | [diff] [blame] | 34 | void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(const GrOp* op, const SkRect& opBounds) { |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 35 | SkASSERT(this->rtCommandBuffer()); |
Brian Osman | e801210 | 2018-11-29 14:05:07 -0500 | [diff] [blame] | 36 | while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) { |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 37 | GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 38 | while (fCurrUpload != fInlineUploads.end() && |
| 39 | fCurrUpload->fUploadBeforeToken == drawToken) { |
| 40 | this->rtCommandBuffer()->inlineUpload(this, fCurrUpload->fUpload); |
| 41 | ++fCurrUpload; |
| 42 | } |
| 43 | SkASSERT(fCurrDraw->fPipeline->proxy() == this->drawOpArgs().fProxy); |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 44 | this->rtCommandBuffer()->draw(*fCurrDraw->fGeometryProcessor, *fCurrDraw->fPipeline, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 45 | fCurrDraw->fFixedDynamicState, fCurrDraw->fDynamicStateArrays, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 46 | fCurrDraw->fMeshes, fCurrDraw->fMeshCnt, opBounds); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 47 | fTokenTracker->flushToken(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 48 | ++fCurrDraw; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void GrOpFlushState::preExecuteDraws() { |
| 53 | fVertexPool.unmap(); |
| 54 | fIndexPool.unmap(); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 55 | for (auto& upload : fASAPUploads) { |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 56 | this->doUpload(upload); |
| 57 | } |
| 58 | // Setup execution iterators. |
| 59 | fCurrDraw = fDraws.begin(); |
| 60 | fCurrUpload = fInlineUploads.begin(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void GrOpFlushState::reset() { |
| 64 | SkASSERT(fCurrDraw == fDraws.end()); |
| 65 | SkASSERT(fCurrUpload == fInlineUploads.end()); |
| 66 | fVertexPool.reset(); |
| 67 | fIndexPool.reset(); |
| 68 | fArena.reset(); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 69 | fASAPUploads.reset(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 70 | fInlineUploads.reset(); |
| 71 | fDraws.reset(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 72 | fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken(); |
| 73 | } |
| 74 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 75 | void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload) { |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 76 | GrDeferredTextureUploadWritePixelsFn wp = [this](GrTextureProxy* dstProxy, int left, int top, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 77 | int width, int height, |
| 78 | GrColorType srcColorType, const void* buffer, |
| 79 | size_t rowBytes) { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 80 | GrSurface* dstSurface = dstProxy->peekSurface(); |
Brian Salomon | 0ba9fa0 | 2018-06-01 11:55:08 -0400 | [diff] [blame] | 81 | if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface) && |
| 82 | fGpu->caps()->supportedWritePixelsColorType(dstSurface->config(), srcColorType) != srcColorType) { |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 83 | return false; |
| 84 | } |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 85 | return this->fGpu->writePixels(dstSurface, left, top, width, height, srcColorType, buffer, |
| 86 | rowBytes); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 87 | }; |
| 88 | upload(wp); |
| 89 | } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 90 | |
| 91 | GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) { |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 92 | return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken()) |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 93 | .fUploadBeforeToken; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) { |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 97 | fASAPUploads.append(&fArena, std::move(upload)); |
| 98 | return fTokenTracker->nextTokenToFlush(); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 101 | void GrOpFlushState::draw(sk_sp<const GrGeometryProcessor> gp, const GrPipeline* pipeline, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 102 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 103 | const GrPipeline::DynamicStateArrays* dynamicStateArrays, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 104 | const GrMesh meshes[], int meshCnt) { |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 105 | SkASSERT(fOpArgs); |
| 106 | SkASSERT(fOpArgs->fOp); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 107 | bool firstDraw = fDraws.begin() == fDraws.end(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 108 | auto& draw = fDraws.append(&fArena); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 109 | GrDeferredUploadToken token = fTokenTracker->issueDrawToken(); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 110 | if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) { |
| 111 | for (int i = 0; i < gp->numTextureSamplers(); ++i) { |
| 112 | fixedDynamicState->fPrimitiveProcessorTextures[i]->addPendingRead(); |
| 113 | } |
| 114 | } |
| 115 | if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) { |
| 116 | int n = gp->numTextureSamplers() * meshCnt; |
| 117 | for (int i = 0; i < n; ++i) { |
| 118 | dynamicStateArrays->fPrimitiveProcessorTextures[i]->addPendingRead(); |
| 119 | } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 120 | } |
| 121 | draw.fGeometryProcessor = std::move(gp); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 122 | draw.fPipeline = pipeline; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 123 | draw.fFixedDynamicState = fixedDynamicState; |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 124 | draw.fDynamicStateArrays = dynamicStateArrays; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 125 | draw.fMeshes = meshes; |
| 126 | draw.fMeshCnt = meshCnt; |
Brian Osman | e801210 | 2018-11-29 14:05:07 -0500 | [diff] [blame] | 127 | draw.fOp = fOpArgs->fOp; |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 128 | if (firstDraw) { |
| 129 | fBaseDrawToken = token; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer** buffer, |
| 134 | int* startVertex) { |
| 135 | return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex); |
| 136 | } |
| 137 | |
| 138 | uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex) { |
| 139 | return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex)); |
| 140 | } |
| 141 | |
| 142 | void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, |
| 143 | int fallbackVertexCount, const GrBuffer** buffer, |
| 144 | int* startVertex, int* actualVertexCount) { |
| 145 | return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer, |
| 146 | startVertex, actualVertexCount); |
| 147 | } |
| 148 | |
| 149 | uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, |
| 150 | const GrBuffer** buffer, int* startIndex, |
| 151 | int* actualIndexCount) { |
| 152 | return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast( |
| 153 | minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount)); |
| 154 | } |
| 155 | |
| 156 | void GrOpFlushState::putBackIndices(int indexCount) { |
| 157 | fIndexPool.putBack(indexCount * sizeof(uint16_t)); |
| 158 | } |
| 159 | |
| 160 | void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) { |
| 161 | fVertexPool.putBack(vertices * vertexStride); |
| 162 | } |
| 163 | |
| 164 | GrAppliedClip GrOpFlushState::detachAppliedClip() { |
| 165 | return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip(); |
| 166 | } |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 167 | |
| 168 | GrGlyphCache* GrOpFlushState::glyphCache() const { |
| 169 | return fGpu->getContext()->contextPriv().getGlyphCache(); |
| 170 | } |
| 171 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 172 | GrAtlasManager* GrOpFlushState::atlasManager() const { |
| 173 | return fGpu->getContext()->contextPriv().getAtlasManager(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 174 | } |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 175 | |
| 176 | ////////////////////////////////////////////////////////////////////////////// |
| 177 | |
| 178 | GrOpFlushState::Draw::~Draw() { |
| 179 | if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) { |
| 180 | for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) { |
| 181 | fFixedDynamicState->fPrimitiveProcessorTextures[i]->completedRead(); |
| 182 | } |
| 183 | } |
| 184 | if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) { |
| 185 | int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt; |
| 186 | const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures; |
| 187 | for (int i = 0; i < n; ++i) { |
| 188 | textures[i]->completedRead(); |
| 189 | } |
| 190 | } |
| 191 | } |