blob: b40b615b2e12eace1091addfce02f2da02624dbb [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
Brian Salomon7dae46a2016-12-14 16:21:37 -05008#ifndef GrClearOp_DEFINED
9#define GrClearOp_DEFINED
bsalomon53469832015-08-18 09:20:09 -070010
csmartdalton29df7602016-08-31 11:55:52 -070011#include "GrFixedClip.h"
bsalomon53469832015-08-18 09:20:09 -070012#include "GrGpu.h"
egdaniel9cb63402016-06-23 08:37:05 -070013#include "GrGpuCommandBuffer.h"
Brian Salomon25a88092016-12-01 09:36:50 -050014#include "GrOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050015#include "GrOpFlushState.h"
bsalomon53469832015-08-18 09:20:09 -070016#include "GrRenderTarget.h"
17
Brian Salomon7dae46a2016-12-14 16:21:37 -050018class GrClearOp final : public GrOp {
bsalomon53469832015-08-18 09:20:09 -070019public:
Brian Salomon25a88092016-12-01 09:36:50 -050020 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070021
Brian Salomonf8334782017-01-03 09:42:58 -050022 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 Salomon7dae46a2016-12-14 16:21:37 -050025 if (!op->fRenderTarget) {
csmartdalton29df7602016-08-31 11:55:52 -070026 return nullptr; // The clip did not contain any pixels within the render target.
27 }
Brian Salomon7dae46a2016-12-14 16:21:37 -050028 return op;
bsalomon53469832015-08-18 09:20:09 -070029 }
30
Brian Salomonf8334782017-01-03 09:42:58 -050031 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 Phillips784b7bf2016-12-09 13:35:02 -050034 }
35
bsalomon53469832015-08-18 09:20:09 -070036 const char* name() const override { return "Clear"; }
37
Brian Salomon9dc2a9a2015-08-18 12:46:51 -040038 SkString dumpInfo() const override {
csmartdalton29df7602016-08-31 11:55:52 -070039 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 Phillips294870f2016-11-11 12:38:40 -050044 string.appendf("], Color: 0x%08x, RT: %d", fColor,
45 fRenderTarget.get()->uniqueID().asUInt());
robertphillips44fbc792016-06-29 06:56:12 -070046 string.append(INHERITED::dumpInfo());
bsalomon53469832015-08-18 09:20:09 -070047 return string;
48 }
49
bsalomonfd8d0132016-08-11 11:25:33 -070050 void setColor(GrColor color) { fColor = color; }
51
bsalomon53469832015-08-18 09:20:09 -070052private:
Brian Salomon7dae46a2016-12-14 16:21:37 -050053 GrClearOp(const GrFixedClip& clip, GrColor color, GrRenderTarget* rt)
robertphillips9199a9f2016-07-13 07:48:43 -070054 : INHERITED(ClassID())
csmartdalton29df7602016-08-31 11:55:52 -070055 , fClip(clip)
56 , fColor(color) {
57 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
58 if (fClip.scissorEnabled()) {
Brian Salomon53e4c3c2016-12-21 11:38:53 -050059 // Don't let scissors extend outside the RT. This may improve op combining.
csmartdalton29df7602016-08-31 11:55:52 -070060 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);
robertphillips9199a9f2016-07-13 07:48:43 -070070 }
71
Brian Salomon7dae46a2016-12-14 16:21:37 -050072 GrClearOp(const SkIRect& rect, GrColor color, GrRenderTarget* rt, bool fullScreen)
Robert Phillips784b7bf2016-12-09 13:35:02 -050073 : 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 Salomon25a88092016-12-01 09:36:50 -050083 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
bsalomone63ffef2016-02-05 07:17:34 -080084 // 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 Salomon7dae46a2016-12-14 16:21:37 -050087 GrClearOp* cb = t->cast<GrClearOp>();
bsalomone63ffef2016-02-05 07:17:34 -080088 SkASSERT(cb->fRenderTarget == fRenderTarget);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070089 if (!fClip.windowRectsState().cheapEqualTo(cb->fClip.windowRectsState())) {
90 return false;
91 }
csmartdalton29df7602016-08-31 11:55:52 -070092 if (cb->contains(this)) {
93 fClip = cb->fClip;
bsalomon88cf17d2016-07-08 06:40:56 -070094 this->replaceBounds(*t);
bsalomone63ffef2016-02-05 07:17:34 -080095 fColor = cb->fColor;
96 return true;
csmartdalton29df7602016-08-31 11:55:52 -070097 } else if (cb->fColor == fColor && this->contains(cb)) {
bsalomone63ffef2016-02-05 07:17:34 -080098 return true;
99 }
bsalomon53469832015-08-18 09:20:09 -0700100 return false;
101 }
102
Brian Salomon7dae46a2016-12-14 16:21:37 -0500103 bool contains(const GrClearOp* that) const {
csmartdalton29df7602016-08-31 11:55:52 -0700104 // 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 Salomon742e31d2016-12-07 17:06:19 -0500110 void onPrepare(GrOpFlushState*) override {}
bsalomon53469832015-08-18 09:20:09 -0700111
Brian Salomonbde42852016-12-21 11:37:49 -0500112 void onExecute(GrOpFlushState* state, const SkRect& /*bounds*/) override {
Brian Salomonc293a292016-11-30 13:38:32 -0500113 state->commandBuffer()->clear(fRenderTarget.get(), fClip, fColor);
bsalomon53469832015-08-18 09:20:09 -0700114 }
115
csmartdalton29df7602016-08-31 11:55:52 -0700116 GrFixedClip fClip;
bsalomon53469832015-08-18 09:20:09 -0700117 GrColor fColor;
118 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
reed1b55a962015-09-17 20:16:13 -0700119
Brian Salomon25a88092016-12-01 09:36:50 -0500120 typedef GrOp INHERITED;
bsalomon53469832015-08-18 09:20:09 -0700121};
122
bsalomon53469832015-08-18 09:20:09 -0700123#endif