xref: /third_party/skia/src/gpu/GrRenderTarget.cpp (revision cb93a386)
1/*
2 * Copyright 2011 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
9#include "src/gpu/GrRenderTarget.h"
10
11#include "src/core/SkRectPriv.h"
12#include "src/gpu/GrAttachment.h"
13#include "src/gpu/GrBackendUtils.h"
14#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrStencilSettings.h"
16
17GrRenderTarget::GrRenderTarget(GrGpu* gpu,
18                               const SkISize& dimensions,
19                               int sampleCount,
20                               GrProtected isProtected,
21                               sk_sp<GrAttachment> stencil)
22        : INHERITED(gpu, dimensions, isProtected)
23        , fSampleCnt(sampleCount) {
24    if (this->numSamples() > 1) {
25        fMSAAStencilAttachment = std::move(stencil);
26    } else {
27        fStencilAttachment = std::move(stencil);
28    }
29}
30
31GrRenderTarget::~GrRenderTarget() = default;
32
33void GrRenderTarget::onRelease() {
34    fStencilAttachment = nullptr;
35    fMSAAStencilAttachment = nullptr;
36
37    INHERITED::onRelease();
38}
39
40void GrRenderTarget::onAbandon() {
41    fStencilAttachment = nullptr;
42    fMSAAStencilAttachment = nullptr;
43
44    INHERITED::onAbandon();
45}
46
47void GrRenderTarget::attachStencilAttachment(sk_sp<GrAttachment> stencil, bool useMSAASurface) {
48    auto stencilAttachment = (useMSAASurface) ? &GrRenderTarget::fMSAAStencilAttachment
49                                              : &GrRenderTarget::fStencilAttachment;
50    if (!stencil && !(this->*stencilAttachment)) {
51        // No need to do any work since we currently don't have a stencil attachment and
52        // we're not actually adding one.
53        return;
54    }
55
56    if (!this->completeStencilAttachment(stencil.get(), useMSAASurface)) {
57        return;
58    }
59
60    this->*stencilAttachment = std::move(stencil);
61}
62
63int GrRenderTarget::numStencilBits(bool useMSAASurface) const {
64    return GrBackendFormatStencilBits(this->getStencilAttachment(useMSAASurface)->backendFormat());
65}
66