1176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
2a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/*
3a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar * Copyright 2011 Google Inc.
4176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines *
5176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines * Use of this source code is governed by a BSD-style license that can be
6176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines * found in the LICENSE file.
7176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines */
8176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
9176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
10176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#ifndef GrStencilAttachment_DEFINED
11176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#define GrStencilAttachment_DEFINED
12176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
13176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "GrClip.h"
14176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "GrGpuResource.h"
15176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
16176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesclass GrRenderTarget;
17176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesclass GrResourceKey;
18176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
19176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesclass GrStencilAttachment : public GrGpuResource {
20176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinespublic:
21176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
22176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
23176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    virtual ~GrStencilAttachment() {
24176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        // TODO: allow SB to be purged and detach itself from rts
25176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
26176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
27176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    int width() const { return fWidth; }
28176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    int height() const { return fHeight; }
29176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    int bits() const { return fBits; }
30176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    int numSamples() const { return fSampleCnt; }
31176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
32176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // called to note the last clip drawn to this buffer.
33176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    void setLastClip(int32_t clipStackGenID,
34a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                     const SkIRect& clipSpaceRect,
35a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                     const SkIPoint clipSpaceToStencilOffset) {
36176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        fLastClipStackGenID = clipStackGenID;
37176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        fLastClipStackRect = clipSpaceRect;
38176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        fLastClipSpaceOffset = clipSpaceToStencilOffset;
39176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
40176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
41176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // called to determine if we have to render the clip into SB.
42176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    bool mustRenderClip(int32_t clipStackGenID,
43176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                        const SkIRect& clipSpaceRect,
44176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                        const SkIPoint clipSpaceToStencilOffset) const {
45176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return fLastClipStackGenID != clipStackGenID ||
46176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines               fLastClipSpaceOffset != clipSpaceToStencilOffset ||
47176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines               !fLastClipStackRect.contains(clipSpaceRect);
48176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
49176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
50176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // We create a unique stencil buffer at each width, height and sampleCnt and share it for
51176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // all render targets that require a stencil with those params.
52176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    static void ComputeSharedStencilAttachmentKey(int width, int height, int sampleCnt,
53176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                                  GrUniqueKey* key);
54176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
55176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesprotected:
56176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    GrStencilAttachment(GrGpu* gpu, LifeCycle lifeCycle, int width, int height, int bits,
57176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                        int sampleCnt)
58176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        : GrGpuResource(gpu, lifeCycle)
59176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        , fWidth(width)
60176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        , fHeight(height)
61176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        , fBits(bits)
62176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        , fSampleCnt(sampleCnt)
63176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        , fLastClipStackGenID(SkClipStack::kInvalidGenID) {
64176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        fLastClipStackRect.setEmpty();
65176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
66176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
67176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesprivate:
68176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
69176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    int fWidth;
70176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    int fHeight;
71176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    int fBits;
72    int fSampleCnt;
73
74    int32_t     fLastClipStackGenID;
75    SkIRect     fLastClipStackRect;
76    SkIPoint    fLastClipSpaceOffset;
77
78    typedef GrGpuResource INHERITED;
79};
80
81#endif
82