Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ops/GrClearOp.h" |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 9 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrMemoryPool.h" |
| 12 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrOpsRenderPass.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrProxyProvider.h" |
| 15 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 16 | |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 17 | static bool contains_scissor(const GrScissorState& a, const GrScissorState& b) { |
| 18 | return !a.enabled() || (b.enabled() && a.rect().contains(b.rect())); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 19 | } |
| 20 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 21 | GrOp::Owner GrClearOp::MakeColor(GrRecordingContext* context, |
| 22 | const GrScissorState& scissor, |
Brian Salomon | 07bc9a2 | 2020-12-02 13:37:16 -0500 | [diff] [blame] | 23 | std::array<float, 4> color) { |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 24 | return GrOp::Make<GrClearOp>(context, Buffer::kColor, scissor, color, false); |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 25 | } |
| 26 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 27 | GrOp::Owner GrClearOp::MakeStencilClip(GrRecordingContext* context, |
| 28 | const GrScissorState& scissor, |
| 29 | bool insideMask) { |
Brian Salomon | 07bc9a2 | 2020-12-02 13:37:16 -0500 | [diff] [blame] | 30 | return GrOp::Make<GrClearOp>(context, |
| 31 | Buffer::kStencilClip, |
| 32 | scissor, |
| 33 | std::array<float, 4>(), |
| 34 | insideMask); |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Brian Salomon | 07bc9a2 | 2020-12-02 13:37:16 -0500 | [diff] [blame] | 37 | GrClearOp::GrClearOp(Buffer buffer, |
| 38 | const GrScissorState& scissor, |
| 39 | std::array<float, 4> color, |
| 40 | bool insideMask) |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 41 | : INHERITED(ClassID()) |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 42 | , fScissor(scissor) |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 43 | , fColor(color) |
| 44 | , fStencilInsideMask(insideMask) |
| 45 | , fBuffer(buffer) { |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 46 | this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 49 | GrOp::CombineResult GrClearOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) { |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 50 | GrClearOp* other = t->cast<GrClearOp>(); |
| 51 | |
| 52 | if (other->fBuffer == fBuffer) { |
| 53 | // This could be much more complicated. Currently we look at cases where the new clear |
| 54 | // contains the old clear, or when the new clear is a subset of the old clear and they clear |
| 55 | // to the same value (color or stencil mask depending on target). |
| 56 | if (contains_scissor(other->fScissor, fScissor)) { |
| 57 | fScissor = other->fScissor; |
| 58 | fColor = other->fColor; |
| 59 | fStencilInsideMask = other->fStencilInsideMask; |
| 60 | return CombineResult::kMerged; |
| 61 | } else if (other->fColor == fColor && other->fStencilInsideMask == fStencilInsideMask && |
| 62 | contains_scissor(fScissor, other->fScissor)) { |
| 63 | return CombineResult::kMerged; |
| 64 | } |
| 65 | } else if (other->fScissor == fScissor) { |
| 66 | // When the scissors are the exact same but the buffers are different, we can combine and |
| 67 | // clear both stencil and clear together in onExecute(). |
| 68 | if (other->fBuffer & Buffer::kColor) { |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 69 | fColor = other->fColor; |
| 70 | } |
| 71 | if (other->fBuffer & Buffer::kStencilClip) { |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 72 | fStencilInsideMask = other->fStencilInsideMask; |
| 73 | } |
| 74 | fBuffer = Buffer::kBoth; |
Robert Phillips | 4b10d58 | 2020-12-02 18:27:18 -0500 | [diff] [blame^] | 75 | return CombineResult::kMerged; |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 76 | } |
| 77 | return CombineResult::kCannotCombine; |
| 78 | } |
| 79 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 80 | void GrClearOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) { |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 81 | SkASSERT(state->opsRenderPass()); |
Michael Ludwig | 9cced93 | 2020-06-08 09:30:09 -0400 | [diff] [blame] | 82 | if (fBuffer & Buffer::kColor) { |
| 83 | state->opsRenderPass()->clear(fScissor, fColor); |
| 84 | } |
| 85 | |
| 86 | if (fBuffer & Buffer::kStencilClip) { |
| 87 | state->opsRenderPass()->clearStencilClip(fScissor, fStencilInsideMask); |
| 88 | } |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 89 | } |