bsalomon | 16b9913 | 2015-08-13 14:55:50 -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 | |
| 8 | #ifndef GrDrawBatch_DEFINED |
| 9 | #define GrDrawBatch_DEFINED |
| 10 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 11 | #include <functional> |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 12 | #include "GrBatch.h" |
| 13 | #include "GrPipeline.h" |
| 14 | |
| 15 | struct GrInitInvariantOutput; |
| 16 | |
| 17 | /** |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 18 | * 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 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 23 | class GrBatchDrawToken { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 24 | public: |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 25 | static GrBatchDrawToken AlreadyFlushedToken() { return GrBatchDrawToken(0); } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 26 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 27 | 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); } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 36 | |
| 37 | private: |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 38 | GrBatchDrawToken(); |
| 39 | explicit GrBatchDrawToken(uint64_t sequenceNumber) : fSequenceNumber(sequenceNumber) {} |
| 40 | friend class GrBatchFlushState; |
| 41 | uint64_t fSequenceNumber; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /** |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 45 | * Base class for GrBatches that draw. These batches have a GrPipeline installed by GrOpList. |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 46 | */ |
| 47 | class GrDrawBatch : public GrBatch { |
| 48 | public: |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 49 | /** Method that performs an upload on behalf of a DeferredUploadFn. */ |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 50 | using WritePixelsFn = std::function<bool(GrSurface* texture, |
| 51 | int left, int top, int width, int height, |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 52 | GrPixelConfig config, const void* buffer, |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 53 | size_t rowBytes)>; |
| 54 | /** See comments before GrDrawBatch::Target definition on how deferred uploaders work. */ |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 55 | using DeferredUploadFn = std::function<void(WritePixelsFn&)>; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 56 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 57 | class Target; |
| 58 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 59 | GrDrawBatch(uint32_t classID); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 60 | ~GrDrawBatch() override; |
| 61 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Fills in a structure informing the XP of overrides to its normal behavior. |
| 64 | */ |
| 65 | void getPipelineOptimizations(GrPipelineOptimizations* override) const; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 66 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 67 | bool installPipeline(const GrPipeline::CreateArgs&); |
| 68 | |
| 69 | // TODO no GrPrimitiveProcessors yet read fragment position |
| 70 | bool willReadFragmentPosition() const { return false; } |
| 71 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 72 | // 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 { |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 75 | return this->pipeline()->getRenderTarget()->uniqueID(); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 78 | protected: |
| 79 | static SkString DumpPipelineInfo(const GrPipeline& pipeline) { |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 80 | SkString string; |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 81 | string.appendf("RT: %d\n", pipeline.getRenderTarget()->uniqueID().asUInt()); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 82 | string.append("ColorStages:\n"); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 83 | for (int i = 0; i < pipeline.numColorFragmentProcessors(); i++) { |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 84 | string.appendf("\t\t%s\n\t\t%s\n", |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 85 | pipeline.getColorFragmentProcessor(i).name(), |
| 86 | pipeline.getColorFragmentProcessor(i).dumpInfo().c_str()); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 87 | } |
| 88 | string.append("CoverageStages:\n"); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 89 | for (int i = 0; i < pipeline.numCoverageFragmentProcessors(); i++) { |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 90 | string.appendf("\t\t%s\n\t\t%s\n", |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 91 | pipeline.getCoverageFragmentProcessor(i).name(), |
| 92 | pipeline.getCoverageFragmentProcessor(i).dumpInfo().c_str()); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 93 | } |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 94 | string.appendf("XP: %s\n", pipeline.getXferProcessor().name()); |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 95 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 96 | bool scissorEnabled = pipeline.getScissorState().enabled(); |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 97 | string.appendf("Scissor: "); |
| 98 | if (scissorEnabled) { |
| 99 | string.appendf("[L: %d, T: %d, R: %d, B: %d]\n", |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame^] | 100 | pipeline.getScissorState().rect().fLeft, |
| 101 | pipeline.getScissorState().rect().fTop, |
| 102 | pipeline.getScissorState().rect().fRight, |
| 103 | pipeline.getScissorState().rect().fBottom); |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 104 | } else { |
| 105 | string.appendf("<disabled>\n"); |
| 106 | } |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 107 | return string; |
| 108 | } |
| 109 | |
Brian Salomon | 2d38b22 | 2016-11-29 17:09:20 -0500 | [diff] [blame] | 110 | const GrPipeline* pipeline() const { |
| 111 | SkASSERT(fPipelineInstalled); |
| 112 | return reinterpret_cast<const GrPipeline*>(fPipelineStorage.get()); |
| 113 | } |
| 114 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 115 | virtual void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 116 | GrInitInvariantOutput* coverage, |
| 117 | GrBatchToXPOverrides* overrides) const = 0; |
| 118 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 119 | private: |
| 120 | /** |
| 121 | * initBatchTracker is a hook for the some additional overrides / optimization possibilities |
| 122 | * from the GrXferProcessor. |
| 123 | */ |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 124 | virtual void initBatchTracker(const GrXPOverridesForBatch&) = 0; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 125 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 126 | protected: |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 127 | 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; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | SkAlignedSTStorage<1, GrPipeline> fPipelineStorage; |
| 138 | bool fPipelineInstalled; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 139 | typedef GrBatch INHERITED; |
| 140 | }; |
| 141 | |
| 142 | #endif |