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 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 22 | static std::unique_ptr<GrClearOp> Make(const GrFixedClip& clip, GrColor color, |
| 23 | GrRenderTarget* rt) { |
| 24 | std::unique_ptr<GrClearOp> op(new GrClearOp(clip, color, rt)); |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 25 | if (!op->fRenderTarget) { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 26 | return nullptr; // The clip did not contain any pixels within the render target. |
| 27 | } |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 28 | return op; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 31 | static std::unique_ptr<GrClearOp> Make(const SkIRect& rect, GrColor color, GrRenderTarget* rt, |
| 32 | bool fullScreen) { |
| 33 | return std::unique_ptr<GrClearOp>(new GrClearOp(rect, color, rt, fullScreen)); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 36 | const char* name() const override { return "Clear"; } |
| 37 | |
Brian Salomon | 9dc2a9a | 2015-08-18 12:46:51 -0400 | [diff] [blame] | 38 | SkString dumpInfo() const override { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 39 | SkString string("Scissor ["); |
| 40 | if (fClip.scissorEnabled()) { |
| 41 | const SkIRect& r = fClip.scissorRect(); |
| 42 | string.appendf("L: %d, T: %d, R: %d, B: %d", r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 43 | } |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 44 | string.appendf("], Color: 0x%08x, RT: %d", fColor, |
| 45 | fRenderTarget.get()->uniqueID().asUInt()); |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 46 | string.append(INHERITED::dumpInfo()); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 47 | return string; |
| 48 | } |
| 49 | |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame] | 50 | void setColor(GrColor color) { fColor = color; } |
| 51 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 52 | private: |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 53 | GrClearOp(const GrFixedClip& clip, GrColor color, GrRenderTarget* rt) |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 54 | : INHERITED(ClassID()) |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 55 | , fClip(clip) |
| 56 | , fColor(color) { |
| 57 | SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height()); |
| 58 | if (fClip.scissorEnabled()) { |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 59 | // Don't let scissors extend outside the RT. This may improve op combining. |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 60 | if (!fClip.intersect(rtRect)) { |
| 61 | return; |
| 62 | } |
| 63 | if (fClip.scissorRect() == rtRect) { |
| 64 | fClip.disableScissor(); |
| 65 | } |
| 66 | } |
| 67 | this->setBounds(SkRect::Make(fClip.scissorEnabled() ? fClip.scissorRect() : rtRect), |
| 68 | HasAABloat::kNo, IsZeroArea::kNo); |
| 69 | fRenderTarget.reset(rt); |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 72 | GrClearOp(const SkIRect& rect, GrColor color, GrRenderTarget* rt, bool fullScreen) |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 73 | : INHERITED(ClassID()) |
| 74 | , fClip(GrFixedClip(rect)) |
| 75 | , fColor(color) |
| 76 | , fRenderTarget(rt) { |
| 77 | if (fullScreen) { |
| 78 | fClip.disableScissor(); |
| 79 | } |
| 80 | this->setBounds(SkRect::Make(rect), HasAABloat::kNo, IsZeroArea::kNo); |
| 81 | } |
| 82 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 83 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 84 | // This could be much more complicated. Currently we look at cases where the new clear |
| 85 | // contains the old clear, or when the new clear is a subset of the old clear and is the |
| 86 | // same color. |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 87 | GrClearOp* cb = t->cast<GrClearOp>(); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 88 | SkASSERT(cb->fRenderTarget == fRenderTarget); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 89 | if (!fClip.windowRectsState().cheapEqualTo(cb->fClip.windowRectsState())) { |
| 90 | return false; |
| 91 | } |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 92 | if (cb->contains(this)) { |
| 93 | fClip = cb->fClip; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 94 | this->replaceBounds(*t); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 95 | fColor = cb->fColor; |
| 96 | return true; |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 97 | } else if (cb->fColor == fColor && this->contains(cb)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 98 | return true; |
| 99 | } |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 103 | bool contains(const GrClearOp* that) const { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 104 | // The constructor ensures that scissor gets disabled on any clip that fills the entire RT. |
| 105 | return !fClip.scissorEnabled() || |
| 106 | (that->fClip.scissorEnabled() && |
| 107 | fClip.scissorRect().contains(that->fClip.scissorRect())); |
| 108 | } |
| 109 | |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 110 | void onPrepare(GrOpFlushState*) override {} |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 111 | |
Brian Salomon | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 112 | void onExecute(GrOpFlushState* state, const SkRect& /*bounds*/) override { |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 113 | state->commandBuffer()->clear(fRenderTarget.get(), fClip, fColor); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 114 | } |
| 115 | |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 116 | GrFixedClip fClip; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 117 | GrColor fColor; |
| 118 | GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 119 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 120 | typedef GrOp INHERITED; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 123 | #endif |