blob: 2053a166e57b228b469d3e5eec1df3729a2a18b8 [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001/*
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 "GrRenderTarget.h"
10
11#include "GrContext.h"
robertphillips4fd74ae2016-08-03 14:26:53 -070012#include "GrContextPriv.h"
robertphillipsea461502015-05-26 11:38:03 -070013#include "GrDrawContext.h"
robertphillipsa106c622015-10-16 09:07:06 -070014#include "GrDrawTarget.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrGpu.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070017#include "GrStencilAttachment.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000018
csmartdaltonf9635992016-08-10 11:09:07 -070019GrRenderTarget::GrRenderTarget(GrGpu* gpu, const GrSurfaceDesc& desc, Flags flags,
20 GrStencilAttachment* stencil)
21 : INHERITED(gpu, desc)
22 , fStencilAttachment(stencil)
23 , fMultisampleSpecsID(0)
24 , fFlags(flags)
25 , fLastDrawTarget(nullptr) {
26 SkASSERT(!(fFlags & Flags::kMixedSampled) || fDesc.fSampleCnt > 0);
27 SkASSERT(!(fFlags & Flags::kWindowRectsSupport) || gpu->caps()->maxWindowRectangles() > 0);
28 fResolveRect.setLargestInverted();
29}
30
robertphillips498d7ac2015-10-30 10:11:30 -070031GrRenderTarget::~GrRenderTarget() {
32 if (fLastDrawTarget) {
33 fLastDrawTarget->clearRT();
34 }
35 SkSafeUnref(fLastDrawTarget);
36}
37
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000038void GrRenderTarget::discard() {
39 // go through context so that all necessary flushing occurs
40 GrContext* context = this->getContext();
robertphillipsc9a37062015-09-01 08:34:28 -070041 if (!context) {
42 return;
43 }
44
robertphillips4fd74ae2016-08-03 14:26:53 -070045 sk_sp<GrDrawContext> drawContext(context->contextPriv().makeWrappedDrawContext(sk_ref_sp(this),
46 nullptr));
robertphillipsea461502015-05-26 11:38:03 -070047 if (!drawContext) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000048 return;
49 }
robertphillipsea461502015-05-26 11:38:03 -070050
robertphillips2e1e51f2015-10-15 08:01:48 -070051 drawContext->discard();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000052}
53
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000054void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000055 if (kCanResolve_ResolveType == getResolveType()) {
bsalomon49f085d2014-09-05 13:34:00 -070056 if (rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000057 fResolveRect.join(*rect);
58 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
59 fResolveRect.setEmpty();
60 }
61 } else {
62 fResolveRect.setLTRB(0, 0, this->width(), this->height());
63 }
64 }
65}
66
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000067void GrRenderTarget::overrideResolveRect(const SkIRect rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000068 fResolveRect = rect;
69 if (fResolveRect.isEmpty()) {
70 fResolveRect.setLargestInverted();
71 } else {
72 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
73 fResolveRect.setLargestInverted();
74 }
75 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000076}
77
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000078void GrRenderTarget::onRelease() {
egdanielec00d942015-09-14 12:56:10 -070079 SkSafeSetNull(fStencilAttachment);
robertphillips@google.comd3645542012-09-05 18:37:39 +000080
81 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000082}
83
84void GrRenderTarget::onAbandon() {
egdanielec00d942015-09-14 12:56:10 -070085 SkSafeSetNull(fStencilAttachment);
robertphillips498d7ac2015-10-30 10:11:30 -070086
87 // The contents of this renderTarget are gone/invalid. It isn't useful to point back
88 // the creating drawTarget.
89 this->setLastDrawTarget(nullptr);
robertphillips@google.comd3645542012-09-05 18:37:39 +000090
91 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000092}
bsalomon6bc1b5f2015-02-23 09:06:38 -080093
robertphillipsa106c622015-10-16 09:07:06 -070094void GrRenderTarget::setLastDrawTarget(GrDrawTarget* dt) {
95 if (fLastDrawTarget) {
robertphillips498d7ac2015-10-30 10:11:30 -070096 // The non-MDB world never closes so we can't check this condition
97#ifdef ENABLE_MDB
robertphillipsa106c622015-10-16 09:07:06 -070098 SkASSERT(fLastDrawTarget->isClosed());
robertphillips498d7ac2015-10-30 10:11:30 -070099#endif
100 fLastDrawTarget->clearRT();
robertphillipsa106c622015-10-16 09:07:06 -0700101 }
102
robertphillips498d7ac2015-10-30 10:11:30 -0700103 SkRefCnt_SafeAssign(fLastDrawTarget, dt);
robertphillipsa106c622015-10-16 09:07:06 -0700104}
105
bsalomon6bc1b5f2015-02-23 09:06:38 -0800106///////////////////////////////////////////////////////////////////////////////
107
egdanielec00d942015-09-14 12:56:10 -0700108bool GrRenderTargetPriv::attachStencilAttachment(GrStencilAttachment* stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -0700109 if (!stencil && !fRenderTarget->fStencilAttachment) {
110 // No need to do any work since we currently don't have a stencil attachment and
111 // we're not acctually adding one.
112 return true;
113 }
egdanielec00d942015-09-14 12:56:10 -0700114 fRenderTarget->fStencilAttachment = stencil;
115 if (!fRenderTarget->completeStencilAttachment()) {
116 SkSafeSetNull(fRenderTarget->fStencilAttachment);
117 return false;
halcanary9d524f22016-03-29 09:03:52 -0700118 }
egdanielec00d942015-09-14 12:56:10 -0700119 return true;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800120}
cdalton28f45b92016-03-07 13:58:26 -0800121
cdalton193d9cf2016-05-12 11:52:02 -0700122int GrRenderTargetPriv::numStencilBits() const {
123 return fRenderTarget->fStencilAttachment ? fRenderTarget->fStencilAttachment->bits() : 0;
124}
125
cdalton28f45b92016-03-07 13:58:26 -0800126const GrGpu::MultisampleSpecs&
127GrRenderTargetPriv::getMultisampleSpecs(const GrStencilSettings& stencil) const {
128 return fRenderTarget->getGpu()->getMultisampleSpecs(fRenderTarget, stencil);
129}
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700130
131int GrRenderTargetPriv::maxWindowRectangles() const {
132 return (this->flags() & Flags::kWindowRectsSupport) ?
133 fRenderTarget->getGpu()->caps()->maxWindowRectangles() : 0;
134}