blob: c92dca346a9b9c9c82fe3ffe1ebfd3cd7b8e9481 [file] [log] [blame]
bsalomon16b99132015-08-13 14:55:50 -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
8#ifndef GrDrawBatch_DEFINED
9#define GrDrawBatch_DEFINED
10
bsalomon342bfc22016-04-01 06:06:20 -070011#include <functional>
bsalomon16b99132015-08-13 14:55:50 -070012#include "GrBatch.h"
13#include "GrPipeline.h"
14
15struct GrInitInvariantOutput;
16
17/**
bsalomon75398562015-08-17 12:55:38 -070018 * GrDrawBatches are flushed in two phases (preDraw, and draw). In preDraw uploads to GrGpuResources
19 * and draws are determined and scheduled. They are issued in the draw phase. GrBatchToken is used
20 * to sequence the uploads relative to each other and to draws.
21 **/
22
bsalomon342bfc22016-04-01 06:06:20 -070023class GrBatchDrawToken {
bsalomon75398562015-08-17 12:55:38 -070024public:
bsalomon342bfc22016-04-01 06:06:20 -070025 static GrBatchDrawToken AlreadyFlushedToken() { return GrBatchDrawToken(0); }
bsalomon75398562015-08-17 12:55:38 -070026
bsalomon342bfc22016-04-01 06:06:20 -070027 GrBatchDrawToken(const GrBatchDrawToken& that) : fSequenceNumber(that.fSequenceNumber) {}
28 GrBatchDrawToken& operator =(const GrBatchDrawToken& that) {
29 fSequenceNumber = that.fSequenceNumber;
30 return *this;
31 }
32 bool operator==(const GrBatchDrawToken& that) const {
33 return fSequenceNumber == that.fSequenceNumber;
34 }
35 bool operator!=(const GrBatchDrawToken& that) const { return !(*this == that); }
bsalomon75398562015-08-17 12:55:38 -070036
37private:
bsalomon342bfc22016-04-01 06:06:20 -070038 GrBatchDrawToken();
39 explicit GrBatchDrawToken(uint64_t sequenceNumber) : fSequenceNumber(sequenceNumber) {}
40 friend class GrBatchFlushState;
41 uint64_t fSequenceNumber;
bsalomon75398562015-08-17 12:55:38 -070042};
43
44/**
Robert Phillipsf2361d22016-10-25 14:20:06 -040045 * Base class for GrBatches that draw. These batches have a GrPipeline installed by GrOpList.
bsalomon16b99132015-08-13 14:55:50 -070046 */
47class GrDrawBatch : public GrBatch {
48public:
jvanverthc3d706f2016-04-20 10:33:27 -070049 /** Method that performs an upload on behalf of a DeferredUploadFn. */
bsalomon342bfc22016-04-01 06:06:20 -070050 using WritePixelsFn = std::function<bool(GrSurface* texture,
51 int left, int top, int width, int height,
jvanverthc3d706f2016-04-20 10:33:27 -070052 GrPixelConfig config, const void* buffer,
bsalomon342bfc22016-04-01 06:06:20 -070053 size_t rowBytes)>;
54 /** See comments before GrDrawBatch::Target definition on how deferred uploaders work. */
jvanverthc3d706f2016-04-20 10:33:27 -070055 using DeferredUploadFn = std::function<void(WritePixelsFn&)>;
bsalomon342bfc22016-04-01 06:06:20 -070056
bsalomon75398562015-08-17 12:55:38 -070057 class Target;
58
reed1b55a962015-09-17 20:16:13 -070059 GrDrawBatch(uint32_t classID);
bsalomon16b99132015-08-13 14:55:50 -070060 ~GrDrawBatch() override;
61
ethannicholasff210322015-11-24 12:10:10 -080062 /**
63 * Fills in a structure informing the XP of overrides to its normal behavior.
64 */
65 void getPipelineOptimizations(GrPipelineOptimizations* override) const;
bsalomon16b99132015-08-13 14:55:50 -070066
bsalomon16b99132015-08-13 14:55:50 -070067 bool installPipeline(const GrPipeline::CreateArgs&);
68
69 // TODO no GrPrimitiveProcessors yet read fragment position
70 bool willReadFragmentPosition() const { return false; }
71
Robert Phillips294870f2016-11-11 12:38:40 -050072 // TODO: this needs to be updated to return GrSurfaceProxy::UniqueID
73 // This is a bit more exciting than the other call sites since it uses the pipeline
74 GrGpuResource::UniqueID renderTargetUniqueID() const final {
robertphillips8abb3702016-08-31 14:04:06 -070075 return this->pipeline()->getRenderTarget()->uniqueID();
bsalomon53469832015-08-18 09:20:09 -070076 }
77
Brian Salomon7c3e7182016-12-01 09:35:30 -050078protected:
79 static SkString DumpPipelineInfo(const GrPipeline& pipeline) {
bsalomon53469832015-08-18 09:20:09 -070080 SkString string;
Brian Salomon7c3e7182016-12-01 09:35:30 -050081 string.appendf("RT: %d\n", pipeline.getRenderTarget()->uniqueID().asUInt());
bsalomon53469832015-08-18 09:20:09 -070082 string.append("ColorStages:\n");
Brian Salomon7c3e7182016-12-01 09:35:30 -050083 for (int i = 0; i < pipeline.numColorFragmentProcessors(); i++) {
robertphillipse004bfc2015-11-16 09:06:59 -080084 string.appendf("\t\t%s\n\t\t%s\n",
Brian Salomon7c3e7182016-12-01 09:35:30 -050085 pipeline.getColorFragmentProcessor(i).name(),
86 pipeline.getColorFragmentProcessor(i).dumpInfo().c_str());
bsalomon53469832015-08-18 09:20:09 -070087 }
88 string.append("CoverageStages:\n");
Brian Salomon7c3e7182016-12-01 09:35:30 -050089 for (int i = 0; i < pipeline.numCoverageFragmentProcessors(); i++) {
robertphillipse004bfc2015-11-16 09:06:59 -080090 string.appendf("\t\t%s\n\t\t%s\n",
Brian Salomon7c3e7182016-12-01 09:35:30 -050091 pipeline.getCoverageFragmentProcessor(i).name(),
92 pipeline.getCoverageFragmentProcessor(i).dumpInfo().c_str());
bsalomon53469832015-08-18 09:20:09 -070093 }
Brian Salomon7c3e7182016-12-01 09:35:30 -050094 string.appendf("XP: %s\n", pipeline.getXferProcessor().name());
robertphillips44fbc792016-06-29 06:56:12 -070095
Brian Salomon7c3e7182016-12-01 09:35:30 -050096 bool scissorEnabled = pipeline.getScissorState().enabled();
robertphillips44fbc792016-06-29 06:56:12 -070097 string.appendf("Scissor: ");
98 if (scissorEnabled) {
99 string.appendf("[L: %d, T: %d, R: %d, B: %d]\n",
Brian Salomon7c3e7182016-12-01 09:35:30 -0500100 pipeline.getScissorState().rect().fLeft,
101 pipeline.getScissorState().rect().fTop,
102 pipeline.getScissorState().rect().fRight,
103 pipeline.getScissorState().rect().fBottom);
robertphillips44fbc792016-06-29 06:56:12 -0700104 } else {
105 string.appendf("<disabled>\n");
106 }
bsalomon53469832015-08-18 09:20:09 -0700107 return string;
108 }
109
Brian Salomon2d38b222016-11-29 17:09:20 -0500110 const GrPipeline* pipeline() const {
111 SkASSERT(fPipelineInstalled);
112 return reinterpret_cast<const GrPipeline*>(fPipelineStorage.get());
113 }
114
halcanary9d524f22016-03-29 09:03:52 -0700115 virtual void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800116 GrInitInvariantOutput* coverage,
117 GrBatchToXPOverrides* overrides) const = 0;
118
bsalomon16b99132015-08-13 14:55:50 -0700119private:
120 /**
121 * initBatchTracker is a hook for the some additional overrides / optimization possibilities
122 * from the GrXferProcessor.
123 */
ethannicholasff210322015-11-24 12:10:10 -0800124 virtual void initBatchTracker(const GrXPOverridesForBatch&) = 0;
bsalomon16b99132015-08-13 14:55:50 -0700125
bsalomon75398562015-08-17 12:55:38 -0700126protected:
bsalomon342bfc22016-04-01 06:06:20 -0700127 struct QueuedUpload {
128 QueuedUpload(DeferredUploadFn&& upload, GrBatchDrawToken token)
129 : fUpload(std::move(upload))
130 , fUploadBeforeToken(token) {}
131 DeferredUploadFn fUpload;
132 GrBatchDrawToken fUploadBeforeToken;
133 };
134 SkTArray<QueuedUpload> fInlineUploads;
bsalomon75398562015-08-17 12:55:38 -0700135
136private:
137 SkAlignedSTStorage<1, GrPipeline> fPipelineStorage;
138 bool fPipelineInstalled;
bsalomon16b99132015-08-13 14:55:50 -0700139 typedef GrBatch INHERITED;
140};
141
142#endif