1/*
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
8#ifndef GrClearBatch_DEFINED
9#define GrClearBatch_DEFINED
10
11#include "GrBatch.h"
12#include "GrBatchFlushState.h"
13#include "GrGpu.h"
14#include "GrRenderTarget.h"
15
16class GrClearBatch final : public GrBatch {
17public:
18    DEFINE_BATCH_CLASS_ID
19
20    GrClearBatch(const SkIRect& rect,  GrColor color, GrRenderTarget* rt)
21        : INHERITED(ClassID())
22        , fRect(rect)
23        , fColor(color)
24        , fRenderTarget(rt) {
25        fBounds = SkRect::Make(rect);
26    }
27
28    const char* name() const override { return "Clear"; }
29
30    uint32_t renderTargetUniqueID() const override { return fRenderTarget.get()->getUniqueID(); }
31    GrRenderTarget* renderTarget() const override { return fRenderTarget.get(); }
32
33    SkString dumpInfo() const override {
34        SkString string;
35        string.printf("Color: 0x%08x, Rect [L: %d, T: %d, R: %d, B: %d], RT: %d",
36                      fColor, fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom,
37                      fRenderTarget.get()->getUniqueID());
38        return string;
39    }
40
41private:
42    bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
43        // This could be much more complicated. Currently we look at cases where the new clear
44        // contains the old clear, or when the new clear is a subset of the old clear and is the
45        // same color.
46        GrClearBatch* cb = t->cast<GrClearBatch>();
47        SkASSERT(cb->fRenderTarget == fRenderTarget);
48        if (cb->fRect.contains(fRect)) {
49            fRect = cb->fRect;
50            fBounds = cb->fBounds;
51            fColor = cb->fColor;
52            return true;
53        } else if (cb->fColor == fColor && fRect.contains(cb->fRect)) {
54            return true;
55        }
56        return false;
57    }
58
59    void onPrepare(GrBatchFlushState*) override {}
60
61    void onDraw(GrBatchFlushState* state) override {
62        state->gpu()->clear(fRect, fColor, fRenderTarget.get());
63    }
64
65    SkIRect                                                 fRect;
66    GrColor                                                 fColor;
67    GrPendingIOResource<GrRenderTarget, kWrite_GrIOType>    fRenderTarget;
68
69    typedef GrBatch INHERITED;
70};
71
72class GrClearStencilClipBatch final : public GrBatch {
73public:
74    DEFINE_BATCH_CLASS_ID
75
76    GrClearStencilClipBatch(const SkIRect& rect, bool insideClip, GrRenderTarget* rt)
77        : INHERITED(ClassID())
78        , fRect(rect)
79        , fInsideClip(insideClip)
80        , fRenderTarget(rt) {
81        fBounds = SkRect::Make(rect);
82    }
83
84    const char* name() const override { return "ClearStencilClip"; }
85
86    uint32_t renderTargetUniqueID() const override { return fRenderTarget.get()->getUniqueID(); }
87    GrRenderTarget* renderTarget() const override { return fRenderTarget.get(); }
88
89    SkString dumpInfo() const override {
90        SkString string;
91        string.printf("Rect [L: %d, T: %d, R: %d, B: %d], IC: %d, RT: 0x%p",
92                      fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom, fInsideClip,
93                      fRenderTarget.get());
94        return string;
95    }
96
97private:
98    bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { return false; }
99
100    void onPrepare(GrBatchFlushState*) override {}
101
102    void onDraw(GrBatchFlushState* state) override {
103        state->gpu()->clearStencilClip(fRect, fInsideClip, fRenderTarget.get());
104    }
105
106    SkIRect                                                 fRect;
107    bool                                                    fInsideClip;
108    GrPendingIOResource<GrRenderTarget, kWrite_GrIOType>    fRenderTarget;
109
110    typedef GrBatch INHERITED;
111};
112
113#endif
114