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 | |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 18 | GrOpFlushState::GrOpFlushState(GrGpu* gpu, |
| 19 | GrResourceProvider* resourceProvider, |
| 20 | GrTokenTracker* tokenTracker) |
| 21 | : fVertexPool(gpu) |
| 22 | , fIndexPool(gpu) |
| 23 | , fGpu(gpu) |
| 24 | , fResourceProvider(resourceProvider) |
| 25 | , fTokenTracker(tokenTracker) { |
| 26 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 27 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 28 | const GrCaps& GrOpFlushState::caps() const { |
| 29 | return *fGpu->caps(); |
| 30 | } |
| 31 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 32 | GrGpuRTCommandBuffer* GrOpFlushState::rtCommandBuffer() { |
| 33 | return fCommandBuffer->asRTCommandBuffer(); |
| 34 | } |
| 35 | |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 36 | void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(uint32_t opID, const SkRect& opBounds) { |
| 37 | SkASSERT(this->rtCommandBuffer()); |
| 38 | while (fCurrDraw != fDraws.end() && fCurrDraw->fOpID == opID) { |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 39 | GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 40 | while (fCurrUpload != fInlineUploads.end() && |
| 41 | fCurrUpload->fUploadBeforeToken == drawToken) { |
| 42 | this->rtCommandBuffer()->inlineUpload(this, fCurrUpload->fUpload); |
| 43 | ++fCurrUpload; |
| 44 | } |
| 45 | SkASSERT(fCurrDraw->fPipeline->proxy() == this->drawOpArgs().fProxy); |
| 46 | this->rtCommandBuffer()->draw(*fCurrDraw->fPipeline, *fCurrDraw->fGeometryProcessor, |
| 47 | fMeshes.begin() + fCurrMesh, nullptr, fCurrDraw->fMeshCnt, |
| 48 | opBounds); |
| 49 | fCurrMesh += fCurrDraw->fMeshCnt; |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 50 | fTokenTracker->flushToken(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 51 | ++fCurrDraw; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void GrOpFlushState::preExecuteDraws() { |
| 56 | fVertexPool.unmap(); |
| 57 | fIndexPool.unmap(); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 58 | for (auto& upload : fASAPUploads) { |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 59 | this->doUpload(upload); |
| 60 | } |
| 61 | // Setup execution iterators. |
| 62 | fCurrDraw = fDraws.begin(); |
| 63 | fCurrUpload = fInlineUploads.begin(); |
| 64 | fCurrMesh = 0; |
| 65 | } |
| 66 | |
| 67 | void GrOpFlushState::reset() { |
| 68 | SkASSERT(fCurrDraw == fDraws.end()); |
| 69 | SkASSERT(fCurrUpload == fInlineUploads.end()); |
| 70 | fVertexPool.reset(); |
| 71 | fIndexPool.reset(); |
| 72 | fArena.reset(); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 73 | fASAPUploads.reset(); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 74 | fInlineUploads.reset(); |
| 75 | fDraws.reset(); |
| 76 | fMeshes.reset(); |
| 77 | fCurrMesh = 0; |
| 78 | fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken(); |
| 79 | } |
| 80 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 81 | void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload) { |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 82 | GrDeferredTextureUploadWritePixelsFn wp = [this](GrTextureProxy* dstProxy, int left, int top, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 83 | int width, int height, |
| 84 | GrColorType srcColorType, const void* buffer, |
| 85 | size_t rowBytes) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 86 | // We don't allow srgb conversions via op flush state uploads. |
| 87 | static constexpr auto kSRGBConversion = GrSRGBConversion::kNone; |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 88 | GrSurface* dstSurface = dstProxy->priv().peekSurface(); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 89 | GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference; |
| 90 | GrGpu::WritePixelTempDrawInfo tempInfo; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 91 | if (!fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height, srcColorType, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 92 | kSRGBConversion, &drawPreference, &tempInfo)) { |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 93 | return false; |
| 94 | } |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 95 | if (GrGpu::kNoDraw_DrawPreference == drawPreference) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 96 | return this->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height, |
| 97 | srcColorType, buffer, rowBytes); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 98 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 99 | // TODO: Shouldn't we be bailing here if a draw is really required instead of a copy? |
| 100 | // e.g. if (tempInfo.fSwizzle != "RGBA") fail. |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 101 | GrSurfaceDesc desc; |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 102 | desc.fWidth = width; |
| 103 | desc.fHeight = height; |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 104 | desc.fConfig = dstProxy->config(); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 105 | sk_sp<GrTexture> temp(this->fResourceProvider->createApproxTexture( |
| 106 | desc, GrResourceProvider::kNoPendingIO_Flag)); |
| 107 | if (!temp) { |
| 108 | return false; |
| 109 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 110 | if (!fGpu->writePixels(temp.get(), dstProxy->origin(), 0, 0, width, height, |
| 111 | tempInfo.fWriteColorType, buffer, rowBytes)) { |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 112 | return false; |
| 113 | } |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 114 | return fGpu->copySurface(dstSurface, dstProxy->origin(), temp.get(), dstProxy->origin(), |
Robert Phillips | acaa607 | 2017-07-28 10:54:53 -0400 | [diff] [blame] | 115 | SkIRect::MakeWH(width, height), {left, top}); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 116 | }; |
| 117 | upload(wp); |
| 118 | } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 119 | |
| 120 | GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) { |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 121 | return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken()) |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 122 | .fUploadBeforeToken; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) { |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 126 | fASAPUploads.append(&fArena, std::move(upload)); |
| 127 | return fTokenTracker->nextTokenToFlush(); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void GrOpFlushState::draw(const GrGeometryProcessor* gp, const GrPipeline* pipeline, |
| 131 | const GrMesh& mesh) { |
| 132 | SkASSERT(fOpArgs); |
| 133 | SkASSERT(fOpArgs->fOp); |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 134 | fMeshes.push_back(mesh); |
| 135 | bool firstDraw = fDraws.begin() == fDraws.end(); |
| 136 | if (!firstDraw) { |
| 137 | Draw& lastDraw = *fDraws.begin(); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 138 | // If the last draw shares a geometry processor and pipeline and there are no intervening |
| 139 | // uploads, add this mesh to it. |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 140 | if (lastDraw.fGeometryProcessor == gp && lastDraw.fPipeline == pipeline) { |
| 141 | if (fInlineUploads.begin() == fInlineUploads.end() || |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 142 | fInlineUploads.tail()->fUploadBeforeToken != fTokenTracker->nextDrawToken()) { |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 143 | ++lastDraw.fMeshCnt; |
| 144 | return; |
| 145 | } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 146 | } |
| 147 | } |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 148 | auto& draw = fDraws.append(&fArena); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 149 | GrDeferredUploadToken token = fTokenTracker->issueDrawToken(); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 150 | |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 151 | draw.fGeometryProcessor.reset(gp); |
| 152 | draw.fPipeline = pipeline; |
| 153 | draw.fMeshCnt = 1; |
| 154 | draw.fOpID = fOpArgs->fOp->uniqueID(); |
| 155 | if (firstDraw) { |
| 156 | fBaseDrawToken = token; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer** buffer, |
| 161 | int* startVertex) { |
| 162 | return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex); |
| 163 | } |
| 164 | |
| 165 | uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex) { |
| 166 | return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex)); |
| 167 | } |
| 168 | |
| 169 | void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, |
| 170 | int fallbackVertexCount, const GrBuffer** buffer, |
| 171 | int* startVertex, int* actualVertexCount) { |
| 172 | return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer, |
| 173 | startVertex, actualVertexCount); |
| 174 | } |
| 175 | |
| 176 | uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, |
| 177 | const GrBuffer** buffer, int* startIndex, |
| 178 | int* actualIndexCount) { |
| 179 | return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast( |
| 180 | minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount)); |
| 181 | } |
| 182 | |
| 183 | void GrOpFlushState::putBackIndices(int indexCount) { |
| 184 | fIndexPool.putBack(indexCount * sizeof(uint16_t)); |
| 185 | } |
| 186 | |
| 187 | void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) { |
| 188 | fVertexPool.putBack(vertices * vertexStride); |
| 189 | } |
| 190 | |
| 191 | GrAppliedClip GrOpFlushState::detachAppliedClip() { |
| 192 | return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip(); |
| 193 | } |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 194 | |
| 195 | GrGlyphCache* GrOpFlushState::glyphCache() const { |
| 196 | return fGpu->getContext()->contextPriv().getGlyphCache(); |
| 197 | } |
| 198 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 199 | GrAtlasManager* GrOpFlushState::atlasManager() const { |
| 200 | return fGpu->getContext()->contextPriv().getAtlasManager(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 201 | } |