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