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