blob: 390942abe47b1a21609e9b09b4ea4a39082faa9d [file] [log] [blame]
bsalomon75398562015-08-17 12:55:38 -07001/*
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 Salomon742e31d2016-12-07 17:06:19 -05008#include "GrOpFlushState.h"
bsalomon75398562015-08-17 12:55:38 -07009
Brian Salomon903da792016-12-16 14:24:46 -050010#include "GrDrawOpAtlas.h"
Robert Phillips646e4292017-06-13 12:44:56 -040011#include "GrGpu.h"
Brian Salomon9bada542017-06-12 12:09:30 -040012#include "GrResourceProvider.h"
Robert Phillips646e4292017-06-13 12:44:56 -040013#include "GrTexture.h"
bsalomon75398562015-08-17 12:55:38 -070014
Brian Salomon742e31d2016-12-07 17:06:19 -050015GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider)
Brian Salomon54d212e2017-03-21 14:22:38 -040016 : fGpu(gpu)
17 , fResourceProvider(resourceProvider)
18 , fCommandBuffer(nullptr)
19 , fVertexPool(gpu)
20 , fIndexPool(gpu)
21 , fLastIssuedToken(GrDrawOpUploadToken::AlreadyFlushedToken())
22 , fLastFlushedToken(0)
23 , fOpArgs(nullptr) {}
bsalomon75398562015-08-17 12:55:38 -070024
Robert Phillips646e4292017-06-13 12:44:56 -040025const GrCaps& GrOpFlushState::caps() const {
26 return *fGpu->caps();
27}
28
Greg Daniel500d58b2017-08-24 15:59:33 -040029GrGpuRTCommandBuffer* GrOpFlushState::rtCommandBuffer() {
30 return fCommandBuffer->asRTCommandBuffer();
31}
32
Brian Salomon742e31d2016-12-07 17:06:19 -050033void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
cdalton397536c2016-03-25 12:15:03 -070034 const GrBuffer** buffer, int* startVertex) {
bsalomon75398562015-08-17 12:55:38 -070035 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
36}
37
Brian Salomon742e31d2016-12-07 17:06:19 -050038uint16_t* GrOpFlushState::makeIndexSpace(int indexCount,
cdalton397536c2016-03-25 12:15:03 -070039 const GrBuffer** buffer, int* startIndex) {
bsalomon75398562015-08-17 12:55:38 -070040 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
41}
Brian Salomon9bada542017-06-12 12:09:30 -040042
Brian Osman49b7b6f2017-06-20 14:43:58 -040043void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
44 int fallbackVertexCount, const GrBuffer** buffer,
45 int* startVertex, int* actualVertexCount) {
46 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
47 startVertex, actualVertexCount);
48}
49
50uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
51 const GrBuffer** buffer, int* startIndex,
52 int* actualIndexCount) {
53 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
54 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
55}
56
Brian Salomon9bada542017-06-12 12:09:30 -040057void GrOpFlushState::doUpload(GrDrawOp::DeferredUploadFn& upload) {
Robert Phillipsacaa6072017-07-28 10:54:53 -040058 GrDrawOp::WritePixelsFn wp = [this](GrTextureProxy* proxy,
59 int left, int top, int width,
Brian Salomon9bada542017-06-12 12:09:30 -040060 int height, GrPixelConfig config, const void* buffer,
61 size_t rowBytes) {
Robert Phillipsacaa6072017-07-28 10:54:53 -040062 GrSurface* surface = proxy->priv().peekSurface();
Brian Salomon9bada542017-06-12 12:09:30 -040063 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
64 GrGpu::WritePixelTempDrawInfo tempInfo;
Robert Phillips7294b852017-08-01 13:51:44 +000065 fGpu->getWritePixelsInfo(surface, width, height, proxy->config(),
Robert Phillipsacaa6072017-07-28 10:54:53 -040066 &drawPreference, &tempInfo);
Brian Salomon9bada542017-06-12 12:09:30 -040067 if (GrGpu::kNoDraw_DrawPreference == drawPreference) {
Robert Phillips7294b852017-08-01 13:51:44 +000068 return this->fGpu->writePixels(surface, left, top, width, height,
Robert Phillipsacaa6072017-07-28 10:54:53 -040069 config, buffer, rowBytes);
Brian Salomon9bada542017-06-12 12:09:30 -040070 }
71 GrSurfaceDesc desc;
Robert Phillipsacaa6072017-07-28 10:54:53 -040072 desc.fOrigin = proxy->origin();
Brian Salomon9bada542017-06-12 12:09:30 -040073 desc.fWidth = width;
74 desc.fHeight = height;
Robert Phillipsacaa6072017-07-28 10:54:53 -040075 desc.fConfig = proxy->config();
Brian Salomon9bada542017-06-12 12:09:30 -040076 sk_sp<GrTexture> temp(this->fResourceProvider->createApproxTexture(
77 desc, GrResourceProvider::kNoPendingIO_Flag));
78 if (!temp) {
79 return false;
80 }
Robert Phillips7294b852017-08-01 13:51:44 +000081 if (!fGpu->writePixels(temp.get(), 0, 0, width, height, desc.fConfig,
Robert Phillipsacaa6072017-07-28 10:54:53 -040082 buffer, rowBytes)) {
Brian Salomon9bada542017-06-12 12:09:30 -040083 return false;
84 }
Robert Phillips7294b852017-08-01 13:51:44 +000085 return fGpu->copySurface(surface, temp.get(),
Robert Phillipsacaa6072017-07-28 10:54:53 -040086 SkIRect::MakeWH(width, height), {left, top});
Brian Salomon9bada542017-06-12 12:09:30 -040087 };
88 upload(wp);
89}