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