bsalomon | 5346983 | 2015-08-18 09:20:09 -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 | |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 8 | #ifndef GrClearOp_DEFINED |
| 9 | #define GrClearOp_DEFINED |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 10 | |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 11 | #include "GrFixedClip.h" |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 12 | #include "GrGpu.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 13 | #include "GrGpuCommandBuffer.h" |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 14 | #include "GrOp.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 15 | #include "GrOpFlushState.h" |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 16 | #include "GrRenderTarget.h" |
| 17 | |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 18 | class GrClearOp final : public GrOp { |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 19 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 20 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 21 | |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 22 | // MDB TODO: replace the renderTargetContext with just the renderTargetProxy. |
| 23 | // For now, we need the renderTargetContext for its accessRenderTarget powers. |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 24 | static std::unique_ptr<GrClearOp> Make(const GrFixedClip& clip, GrColor color, |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 25 | GrRenderTargetContext* rtc) { |
| 26 | const SkIRect rtRect = SkIRect::MakeWH(rtc->width(), rtc->height()); |
| 27 | if (clip.scissorEnabled() && !SkIRect::Intersects(clip.scissorRect(), rtRect)) { |
| 28 | return nullptr; |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 29 | } |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 30 | |
| 31 | // MDB TODO: remove this. In this hybrid state we need to be sure the RT is instantiable |
| 32 | // so it can carry the IO refs. In the future we will just get the proxy and |
| 33 | // it carry the IO refs. |
| 34 | if (!rtc->accessRenderTarget()) { |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | return std::unique_ptr<GrClearOp>(new GrClearOp(clip, color, rtc)); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 41 | // MDB TODO: replace the renderTargetContext with just the renderTargetProxy. |
| 42 | static std::unique_ptr<GrClearOp> Make(const SkIRect& rect, GrColor color, |
| 43 | GrRenderTargetContext* rtc, |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 44 | bool fullScreen) { |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 45 | SkASSERT(fullScreen || !rect.isEmpty()); |
| 46 | |
| 47 | // MDB TODO: remove this. See above comment. |
| 48 | if (!rtc->accessRenderTarget()) { |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | return std::unique_ptr<GrClearOp>(new GrClearOp(rect, color, rtc, fullScreen)); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 53 | } |
| 54 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 55 | const char* name() const override { return "Clear"; } |
| 56 | |
Brian Salomon | 9dc2a9a | 2015-08-18 12:46:51 -0400 | [diff] [blame] | 57 | SkString dumpInfo() const override { |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 58 | SkString string; |
| 59 | string.appendf("rtID: %d proxyID: %d Scissor [", |
| 60 | fRenderTarget.get()->uniqueID().asUInt(), |
| 61 | fProxyUniqueID.asUInt()); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 62 | if (fClip.scissorEnabled()) { |
| 63 | const SkIRect& r = fClip.scissorRect(); |
| 64 | string.appendf("L: %d, T: %d, R: %d, B: %d", r.fLeft, r.fTop, r.fRight, r.fBottom); |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 65 | } else { |
| 66 | string.append("disabled"); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 67 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 68 | string.appendf("], Color: 0x%08x ", fColor); |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 69 | string.append(INHERITED::dumpInfo()); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 70 | return string; |
| 71 | } |
| 72 | |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame] | 73 | void setColor(GrColor color) { fColor = color; } |
| 74 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 75 | private: |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 76 | GrClearOp(const GrFixedClip& clip, GrColor color, GrRenderTargetContext* rtc) |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 77 | : INHERITED(ClassID()) |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 78 | , fClip(clip) |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 79 | , fColor(color) |
| 80 | , fProxyUniqueID(rtc->asSurfaceProxy()->uniqueID()) { |
| 81 | |
| 82 | GrSurfaceProxy* proxy = rtc->asSurfaceProxy(); |
| 83 | const SkIRect rtRect = SkIRect::MakeWH(proxy->width(), proxy->height()); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 84 | if (fClip.scissorEnabled()) { |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 85 | // Don't let scissors extend outside the RT. This may improve op combining. |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 86 | if (!fClip.intersect(rtRect)) { |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 87 | SkASSERT(0); // should be caught upstream |
| 88 | fClip = GrFixedClip(SkIRect::MakeEmpty()); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 89 | } |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 90 | |
| 91 | if (GrResourceProvider::IsFunctionallyExact(proxy) && fClip.scissorRect() == rtRect) { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 92 | fClip.disableScissor(); |
| 93 | } |
| 94 | } |
| 95 | this->setBounds(SkRect::Make(fClip.scissorEnabled() ? fClip.scissorRect() : rtRect), |
| 96 | HasAABloat::kNo, IsZeroArea::kNo); |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 97 | fRenderTarget.reset(rtc->accessRenderTarget()); |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 100 | GrClearOp(const SkIRect& rect, GrColor color, GrRenderTargetContext* rtc, bool fullScreen) |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 101 | : INHERITED(ClassID()) |
| 102 | , fClip(GrFixedClip(rect)) |
| 103 | , fColor(color) |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 104 | , fProxyUniqueID(rtc->asSurfaceProxy()->uniqueID()) { |
| 105 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 106 | if (fullScreen) { |
| 107 | fClip.disableScissor(); |
| 108 | } |
| 109 | this->setBounds(SkRect::Make(rect), HasAABloat::kNo, IsZeroArea::kNo); |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 110 | fRenderTarget.reset(rtc->accessRenderTarget()); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 113 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 114 | // This could be much more complicated. Currently we look at cases where the new clear |
| 115 | // contains the old clear, or when the new clear is a subset of the old clear and is the |
| 116 | // same color. |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 117 | GrClearOp* cb = t->cast<GrClearOp>(); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 118 | SkASSERT(cb->fRenderTarget == fRenderTarget); |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 119 | SkASSERT(cb->fProxyUniqueID == fProxyUniqueID); |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 120 | if (fClip.windowRectsState() != cb->fClip.windowRectsState()) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 121 | return false; |
| 122 | } |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 123 | if (cb->contains(this)) { |
| 124 | fClip = cb->fClip; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 125 | this->replaceBounds(*t); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 126 | fColor = cb->fColor; |
| 127 | return true; |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 128 | } else if (cb->fColor == fColor && this->contains(cb)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 129 | return true; |
| 130 | } |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 134 | bool contains(const GrClearOp* that) const { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 135 | // The constructor ensures that scissor gets disabled on any clip that fills the entire RT. |
| 136 | return !fClip.scissorEnabled() || |
| 137 | (that->fClip.scissorEnabled() && |
| 138 | fClip.scissorRect().contains(that->fClip.scissorRect())); |
| 139 | } |
| 140 | |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 141 | void onPrepare(GrOpFlushState*) override {} |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 142 | |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 143 | void onExecute(GrOpFlushState* state) override { |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 144 | // MDB TODO: instantiate the renderTarget from the proxy in here |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 145 | state->commandBuffer()->clear(fRenderTarget.get(), fClip, fColor); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 146 | } |
| 147 | |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 148 | GrFixedClip fClip; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 149 | GrColor fColor; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 150 | |
| 151 | // MDB TODO: remove this. When the renderTargetProxy carries the refs this will be redundant. |
| 152 | GrSurfaceProxy::UniqueID fProxyUniqueID; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 153 | GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 154 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 155 | typedef GrOp INHERITED; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 158 | #endif |