blob: 16f1ddd7f40db1687af359f74d1329fec1f10cae [file] [log] [blame]
bsalomon53469832015-08-18 09:20:09 -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 GrClearBatch_DEFINED
9#define GrClearBatch_DEFINED
10
11#include "GrBatch.h"
12#include "GrBatchFlushState.h"
csmartdalton29df7602016-08-31 11:55:52 -070013#include "GrFixedClip.h"
bsalomon53469832015-08-18 09:20:09 -070014#include "GrGpu.h"
egdaniel9cb63402016-06-23 08:37:05 -070015#include "GrGpuCommandBuffer.h"
bsalomon53469832015-08-18 09:20:09 -070016#include "GrRenderTarget.h"
17
18class GrClearBatch final : public GrBatch {
19public:
reed1b55a962015-09-17 20:16:13 -070020 DEFINE_BATCH_CLASS_ID
21
csmartdalton29df7602016-08-31 11:55:52 -070022 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;
bsalomon53469832015-08-18 09:20:09 -070028 }
29
30 const char* name() const override { return "Clear"; }
31
robertphillips8abb3702016-08-31 14:04:06 -070032 uint32_t renderTargetUniqueID() const override { return fRenderTarget.get()->uniqueID(); }
bsalomon6dea83f2015-12-03 12:58:06 -080033 GrRenderTarget* renderTarget() const override { return fRenderTarget.get(); }
bsalomon53469832015-08-18 09:20:09 -070034
Brian Salomon9dc2a9a2015-08-18 12:46:51 -040035 SkString dumpInfo() const override {
csmartdalton29df7602016-08-31 11:55:52 -070036 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 }
robertphillips8abb3702016-08-31 14:04:06 -070041 string.appendf("], Color: 0x%08x, RT: %d", fColor, fRenderTarget.get()->uniqueID());
robertphillips44fbc792016-06-29 06:56:12 -070042 string.append(INHERITED::dumpInfo());
bsalomon53469832015-08-18 09:20:09 -070043 return string;
44 }
45
bsalomonfd8d0132016-08-11 11:25:33 -070046 void setColor(GrColor color) { fColor = color; }
47
bsalomon53469832015-08-18 09:20:09 -070048private:
csmartdalton29df7602016-08-31 11:55:52 -070049 GrClearBatch(const GrFixedClip& clip, GrColor color, GrRenderTarget* rt)
robertphillips9199a9f2016-07-13 07:48:43 -070050 : INHERITED(ClassID())
csmartdalton29df7602016-08-31 11:55:52 -070051 , 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);
robertphillips9199a9f2016-07-13 07:48:43 -070066 }
67
bsalomon53469832015-08-18 09:20:09 -070068 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomone63ffef2016-02-05 07:17:34 -080069 // 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);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070074 if (!fClip.windowRectsState().cheapEqualTo(cb->fClip.windowRectsState())) {
75 return false;
76 }
csmartdalton29df7602016-08-31 11:55:52 -070077 if (cb->contains(this)) {
78 fClip = cb->fClip;
bsalomon88cf17d2016-07-08 06:40:56 -070079 this->replaceBounds(*t);
bsalomone63ffef2016-02-05 07:17:34 -080080 fColor = cb->fColor;
81 return true;
csmartdalton29df7602016-08-31 11:55:52 -070082 } else if (cb->fColor == fColor && this->contains(cb)) {
bsalomone63ffef2016-02-05 07:17:34 -080083 return true;
84 }
bsalomon53469832015-08-18 09:20:09 -070085 return false;
86 }
87
csmartdalton29df7602016-08-31 11:55:52 -070088 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
bsalomon53469832015-08-18 09:20:09 -070095 void onPrepare(GrBatchFlushState*) override {}
96
97 void onDraw(GrBatchFlushState* state) override {
csmartdalton29df7602016-08-31 11:55:52 -070098 state->commandBuffer()->clear(fClip, fColor, fRenderTarget.get());
bsalomon53469832015-08-18 09:20:09 -070099 }
100
csmartdalton29df7602016-08-31 11:55:52 -0700101 GrFixedClip fClip;
bsalomon53469832015-08-18 09:20:09 -0700102 GrColor fColor;
103 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
reed1b55a962015-09-17 20:16:13 -0700104
105 typedef GrBatch INHERITED;
bsalomon53469832015-08-18 09:20:09 -0700106};
107
bsalomon53469832015-08-18 09:20:09 -0700108#endif