blob: 5340af11729ef4caf914885f0eb0213291f9e1a3 [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
Robert Phillipsf7a72612017-03-31 10:03:45 -040022 // MDB TODO: replace the renderTargetContext with just the renderTargetProxy.
23 // For now, we need the renderTargetContext for its accessRenderTarget powers.
Brian Salomonf8334782017-01-03 09:42:58 -050024 static std::unique_ptr<GrClearOp> Make(const GrFixedClip& clip, GrColor color,
Robert Phillipsf7a72612017-03-31 10:03:45 -040025 GrRenderTargetContext* rtc) {
26 const SkIRect rtRect = SkIRect::MakeWH(rtc->width(), rtc->height());
27 if (clip.scissorEnabled() && !SkIRect::Intersects(clip.scissorRect(), rtRect)) {
28 return nullptr;
csmartdalton29df7602016-08-31 11:55:52 -070029 }
Robert Phillipsf7a72612017-03-31 10:03:45 -040030
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));
bsalomon53469832015-08-18 09:20:09 -070039 }
40
Robert Phillipsf7a72612017-03-31 10:03:45 -040041 // 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 Salomonf8334782017-01-03 09:42:58 -050044 bool fullScreen) {
Robert Phillipsf7a72612017-03-31 10:03:45 -040045 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 Phillips784b7bf2016-12-09 13:35:02 -050053 }
54
bsalomon53469832015-08-18 09:20:09 -070055 const char* name() const override { return "Clear"; }
56
Brian Salomon9dc2a9a2015-08-18 12:46:51 -040057 SkString dumpInfo() const override {
Robert Phillips1119dc32017-04-11 12:54:57 -040058 SkString string;
59 string.appendf("rtID: %d proxyID: %d Scissor [",
60 fRenderTarget.get()->uniqueID().asUInt(),
61 fProxyUniqueID.asUInt());
csmartdalton29df7602016-08-31 11:55:52 -070062 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 Phillipsf7a72612017-03-31 10:03:45 -040065 } else {
66 string.append("disabled");
csmartdalton29df7602016-08-31 11:55:52 -070067 }
Robert Phillips1119dc32017-04-11 12:54:57 -040068 string.appendf("], Color: 0x%08x ", fColor);
robertphillips44fbc792016-06-29 06:56:12 -070069 string.append(INHERITED::dumpInfo());
bsalomon53469832015-08-18 09:20:09 -070070 return string;
71 }
72
bsalomonfd8d0132016-08-11 11:25:33 -070073 void setColor(GrColor color) { fColor = color; }
74
bsalomon53469832015-08-18 09:20:09 -070075private:
Robert Phillipsf7a72612017-03-31 10:03:45 -040076 GrClearOp(const GrFixedClip& clip, GrColor color, GrRenderTargetContext* rtc)
robertphillips9199a9f2016-07-13 07:48:43 -070077 : INHERITED(ClassID())
csmartdalton29df7602016-08-31 11:55:52 -070078 , fClip(clip)
Robert Phillipsf7a72612017-03-31 10:03:45 -040079 , fColor(color)
80 , fProxyUniqueID(rtc->asSurfaceProxy()->uniqueID()) {
81
82 GrSurfaceProxy* proxy = rtc->asSurfaceProxy();
83 const SkIRect rtRect = SkIRect::MakeWH(proxy->width(), proxy->height());
csmartdalton29df7602016-08-31 11:55:52 -070084 if (fClip.scissorEnabled()) {
Brian Salomon53e4c3c2016-12-21 11:38:53 -050085 // Don't let scissors extend outside the RT. This may improve op combining.
csmartdalton29df7602016-08-31 11:55:52 -070086 if (!fClip.intersect(rtRect)) {
Robert Phillipsf7a72612017-03-31 10:03:45 -040087 SkASSERT(0); // should be caught upstream
88 fClip = GrFixedClip(SkIRect::MakeEmpty());
csmartdalton29df7602016-08-31 11:55:52 -070089 }
Robert Phillipsf7a72612017-03-31 10:03:45 -040090
91 if (GrResourceProvider::IsFunctionallyExact(proxy) && fClip.scissorRect() == rtRect) {
csmartdalton29df7602016-08-31 11:55:52 -070092 fClip.disableScissor();
93 }
94 }
95 this->setBounds(SkRect::Make(fClip.scissorEnabled() ? fClip.scissorRect() : rtRect),
96 HasAABloat::kNo, IsZeroArea::kNo);
Robert Phillipsf7a72612017-03-31 10:03:45 -040097 fRenderTarget.reset(rtc->accessRenderTarget());
robertphillips9199a9f2016-07-13 07:48:43 -070098 }
99
Robert Phillipsf7a72612017-03-31 10:03:45 -0400100 GrClearOp(const SkIRect& rect, GrColor color, GrRenderTargetContext* rtc, bool fullScreen)
Robert Phillips784b7bf2016-12-09 13:35:02 -0500101 : INHERITED(ClassID())
102 , fClip(GrFixedClip(rect))
103 , fColor(color)
Robert Phillipsf7a72612017-03-31 10:03:45 -0400104 , fProxyUniqueID(rtc->asSurfaceProxy()->uniqueID()) {
105
Robert Phillips784b7bf2016-12-09 13:35:02 -0500106 if (fullScreen) {
107 fClip.disableScissor();
108 }
109 this->setBounds(SkRect::Make(rect), HasAABloat::kNo, IsZeroArea::kNo);
Robert Phillipsf7a72612017-03-31 10:03:45 -0400110 fRenderTarget.reset(rtc->accessRenderTarget());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500111 }
112
Brian Salomon25a88092016-12-01 09:36:50 -0500113 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
bsalomone63ffef2016-02-05 07:17:34 -0800114 // 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 Salomon7dae46a2016-12-14 16:21:37 -0500117 GrClearOp* cb = t->cast<GrClearOp>();
bsalomone63ffef2016-02-05 07:17:34 -0800118 SkASSERT(cb->fRenderTarget == fRenderTarget);
Robert Phillipsf7a72612017-03-31 10:03:45 -0400119 SkASSERT(cb->fProxyUniqueID == fProxyUniqueID);
Brian Salomon9a767722017-03-13 17:57:28 -0400120 if (fClip.windowRectsState() != cb->fClip.windowRectsState()) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700121 return false;
122 }
csmartdalton29df7602016-08-31 11:55:52 -0700123 if (cb->contains(this)) {
124 fClip = cb->fClip;
bsalomon88cf17d2016-07-08 06:40:56 -0700125 this->replaceBounds(*t);
bsalomone63ffef2016-02-05 07:17:34 -0800126 fColor = cb->fColor;
127 return true;
csmartdalton29df7602016-08-31 11:55:52 -0700128 } else if (cb->fColor == fColor && this->contains(cb)) {
bsalomone63ffef2016-02-05 07:17:34 -0800129 return true;
130 }
bsalomon53469832015-08-18 09:20:09 -0700131 return false;
132 }
133
Brian Salomon7dae46a2016-12-14 16:21:37 -0500134 bool contains(const GrClearOp* that) const {
csmartdalton29df7602016-08-31 11:55:52 -0700135 // 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 Salomon742e31d2016-12-07 17:06:19 -0500141 void onPrepare(GrOpFlushState*) override {}
bsalomon53469832015-08-18 09:20:09 -0700142
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500143 void onExecute(GrOpFlushState* state) override {
Robert Phillipsf7a72612017-03-31 10:03:45 -0400144 // MDB TODO: instantiate the renderTarget from the proxy in here
Brian Salomonc293a292016-11-30 13:38:32 -0500145 state->commandBuffer()->clear(fRenderTarget.get(), fClip, fColor);
bsalomon53469832015-08-18 09:20:09 -0700146 }
147
csmartdalton29df7602016-08-31 11:55:52 -0700148 GrFixedClip fClip;
bsalomon53469832015-08-18 09:20:09 -0700149 GrColor fColor;
Robert Phillipsf7a72612017-03-31 10:03:45 -0400150
151 // MDB TODO: remove this. When the renderTargetProxy carries the refs this will be redundant.
152 GrSurfaceProxy::UniqueID fProxyUniqueID;
bsalomon53469832015-08-18 09:20:09 -0700153 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
reed1b55a962015-09-17 20:16:13 -0700154
Brian Salomon25a88092016-12-01 09:36:50 -0500155 typedef GrOp INHERITED;
bsalomon53469832015-08-18 09:20:09 -0700156};
157
bsalomon53469832015-08-18 09:20:09 -0700158#endif