blob: 5f37b7b0001fbc39026b3c0a9fa595340748ab7b [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/**
bsalomon16b99132015-08-13 14:55:50 -070045 * Base class for GrBatches that draw. These batches have a GrPipeline installed by GrDrawTarget.
46 */
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
67 const GrPipeline* pipeline() const {
68 SkASSERT(fPipelineInstalled);
69 return reinterpret_cast<const GrPipeline*>(fPipelineStorage.get());
70 }
71
72 bool installPipeline(const GrPipeline::CreateArgs&);
73
74 // TODO no GrPrimitiveProcessors yet read fragment position
75 bool willReadFragmentPosition() const { return false; }
76
bsalomon53469832015-08-18 09:20:09 -070077 uint32_t renderTargetUniqueID() const final {
78 SkASSERT(fPipelineInstalled);
robertphillips8abb3702016-08-31 14:04:06 -070079 return this->pipeline()->getRenderTarget()->uniqueID();
bsalomon53469832015-08-18 09:20:09 -070080 }
81
bsalomon6dea83f2015-12-03 12:58:06 -080082 GrRenderTarget* renderTarget() const final {
83 SkASSERT(fPipelineInstalled);
84 return this->pipeline()->getRenderTarget();
85 }
86
bsalomon53469832015-08-18 09:20:09 -070087 SkString dumpInfo() const override {
88 SkString string;
robertphillipse004bfc2015-11-16 09:06:59 -080089 string.appendf("RT: %d\n", this->renderTargetUniqueID());
bsalomon53469832015-08-18 09:20:09 -070090 string.append("ColorStages:\n");
bsalomonac856c92015-08-27 06:30:17 -070091 for (int i = 0; i < this->pipeline()->numColorFragmentProcessors(); i++) {
robertphillipse004bfc2015-11-16 09:06:59 -080092 string.appendf("\t\t%s\n\t\t%s\n",
93 this->pipeline()->getColorFragmentProcessor(i).name(),
94 this->pipeline()->getColorFragmentProcessor(i).dumpInfo().c_str());
bsalomon53469832015-08-18 09:20:09 -070095 }
96 string.append("CoverageStages:\n");
bsalomonac856c92015-08-27 06:30:17 -070097 for (int i = 0; i < this->pipeline()->numCoverageFragmentProcessors(); i++) {
robertphillipse004bfc2015-11-16 09:06:59 -080098 string.appendf("\t\t%s\n\t\t%s\n",
99 this->pipeline()->getCoverageFragmentProcessor(i).name(),
100 this->pipeline()->getCoverageFragmentProcessor(i).dumpInfo().c_str());
bsalomon53469832015-08-18 09:20:09 -0700101 }
bsalomon2047b782015-12-21 13:12:54 -0800102 string.appendf("XP: %s\n", this->pipeline()->getXferProcessor().name());
robertphillips44fbc792016-06-29 06:56:12 -0700103
104 bool scissorEnabled = this->pipeline()->getScissorState().enabled();
105 string.appendf("Scissor: ");
106 if (scissorEnabled) {
107 string.appendf("[L: %d, T: %d, R: %d, B: %d]\n",
108 this->pipeline()->getScissorState().rect().fLeft,
109 this->pipeline()->getScissorState().rect().fTop,
110 this->pipeline()->getScissorState().rect().fRight,
111 this->pipeline()->getScissorState().rect().fBottom);
112 } else {
113 string.appendf("<disabled>\n");
114 }
115 string.append(INHERITED::dumpInfo());
116
bsalomon53469832015-08-18 09:20:09 -0700117 return string;
118 }
119
ethannicholasff210322015-11-24 12:10:10 -0800120protected:
halcanary9d524f22016-03-29 09:03:52 -0700121 virtual void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800122 GrInitInvariantOutput* coverage,
123 GrBatchToXPOverrides* overrides) const = 0;
124
bsalomon16b99132015-08-13 14:55:50 -0700125private:
126 /**
127 * initBatchTracker is a hook for the some additional overrides / optimization possibilities
128 * from the GrXferProcessor.
129 */
ethannicholasff210322015-11-24 12:10:10 -0800130 virtual void initBatchTracker(const GrXPOverridesForBatch&) = 0;
bsalomon16b99132015-08-13 14:55:50 -0700131
bsalomon75398562015-08-17 12:55:38 -0700132protected:
bsalomon342bfc22016-04-01 06:06:20 -0700133 struct QueuedUpload {
134 QueuedUpload(DeferredUploadFn&& upload, GrBatchDrawToken token)
135 : fUpload(std::move(upload))
136 , fUploadBeforeToken(token) {}
137 DeferredUploadFn fUpload;
138 GrBatchDrawToken fUploadBeforeToken;
139 };
140 SkTArray<QueuedUpload> fInlineUploads;
bsalomon75398562015-08-17 12:55:38 -0700141
142private:
143 SkAlignedSTStorage<1, GrPipeline> fPipelineStorage;
144 bool fPipelineInstalled;
bsalomon16b99132015-08-13 14:55:50 -0700145 typedef GrBatch INHERITED;
146};
147
148#endif