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.
6 */
7
8#include "src/gpu/ops/GrClearOp.h"
9
10#include "include/gpu/GrRecordingContext.h"
11#include "src/gpu/GrMemoryPool.h"
12#include "src/gpu/GrOpFlushState.h"
13#include "src/gpu/GrOpsRenderPass.h"
14#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrRecordingContextPriv.h"
16
17static bool contains_scissor(const GrScissorState& a, const GrScissorState& b) {
18 return !a.enabled() || (b.enabled() && a.rect().contains(b.rect()));
19}
20
21std::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
28std::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
35GrClearOp::GrClearOp(Buffer buffer, const GrScissorState& scissor,
36 const SkPMColor4f& color, bool insideMask)
37 : INHERITED(ClassID())
38 , fScissor(scissor)
39 , fColor(color)
40 , fStencilInsideMask(insideMask)
41 , fBuffer(buffer) {
42 this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo);
43}
44
45GrOp::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
78void GrClearOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
79 SkASSERT(state->opsRenderPass());
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 }
87}
88