blob: 30636a4413785fe8da600ada4b8643d134496c11 [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
Brian Salomon742e31d2016-12-07 17:06:19 -050029void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
cdalton397536c2016-03-25 12:15:03 -070030 const GrBuffer** buffer, int* startVertex) {
bsalomon75398562015-08-17 12:55:38 -070031 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
32}
33
Brian Salomon742e31d2016-12-07 17:06:19 -050034uint16_t* GrOpFlushState::makeIndexSpace(int indexCount,
cdalton397536c2016-03-25 12:15:03 -070035 const GrBuffer** buffer, int* startIndex) {
bsalomon75398562015-08-17 12:55:38 -070036 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
37}
Brian Salomon9bada542017-06-12 12:09:30 -040038
Brian Osman49b7b6f2017-06-20 14:43:58 -040039void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
40 int fallbackVertexCount, const GrBuffer** buffer,
41 int* startVertex, int* actualVertexCount) {
42 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
43 startVertex, actualVertexCount);
44}
45
46uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
47 const GrBuffer** buffer, int* startIndex,
48 int* actualIndexCount) {
49 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
50 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
51}
52
Brian Salomon9bada542017-06-12 12:09:30 -040053void GrOpFlushState::doUpload(GrDrawOp::DeferredUploadFn& upload) {
Robert Phillipsacaa6072017-07-28 10:54:53 -040054 GrDrawOp::WritePixelsFn wp = [this](GrTextureProxy* proxy,
55 int left, int top, int width,
Brian Salomon9bada542017-06-12 12:09:30 -040056 int height, GrPixelConfig config, const void* buffer,
57 size_t rowBytes) {
Robert Phillipsacaa6072017-07-28 10:54:53 -040058 GrSurface* surface = proxy->priv().peekSurface();
Brian Salomon9bada542017-06-12 12:09:30 -040059 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
60 GrGpu::WritePixelTempDrawInfo tempInfo;
Robert Phillips7294b852017-08-01 13:51:44 +000061 fGpu->getWritePixelsInfo(surface, width, height, proxy->config(),
Robert Phillipsacaa6072017-07-28 10:54:53 -040062 &drawPreference, &tempInfo);
Brian Salomon9bada542017-06-12 12:09:30 -040063 if (GrGpu::kNoDraw_DrawPreference == drawPreference) {
Robert Phillips7294b852017-08-01 13:51:44 +000064 return this->fGpu->writePixels(surface, left, top, width, height,
Robert Phillipsacaa6072017-07-28 10:54:53 -040065 config, buffer, rowBytes);
Brian Salomon9bada542017-06-12 12:09:30 -040066 }
67 GrSurfaceDesc desc;
Robert Phillipsacaa6072017-07-28 10:54:53 -040068 desc.fOrigin = proxy->origin();
Brian Salomon9bada542017-06-12 12:09:30 -040069 desc.fWidth = width;
70 desc.fHeight = height;
Robert Phillipsacaa6072017-07-28 10:54:53 -040071 desc.fConfig = proxy->config();
Brian Salomon9bada542017-06-12 12:09:30 -040072 sk_sp<GrTexture> temp(this->fResourceProvider->createApproxTexture(
73 desc, GrResourceProvider::kNoPendingIO_Flag));
74 if (!temp) {
75 return false;
76 }
Robert Phillips7294b852017-08-01 13:51:44 +000077 if (!fGpu->writePixels(temp.get(), 0, 0, width, height, desc.fConfig,
Robert Phillipsacaa6072017-07-28 10:54:53 -040078 buffer, rowBytes)) {
Brian Salomon9bada542017-06-12 12:09:30 -040079 return false;
80 }
Robert Phillips7294b852017-08-01 13:51:44 +000081 return fGpu->copySurface(surface, temp.get(),
Robert Phillipsacaa6072017-07-28 10:54:53 -040082 SkIRect::MakeWH(width, height), {left, top});
Brian Salomon9bada542017-06-12 12:09:30 -040083 };
84 upload(wp);
85}