bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 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 "GrRenderTarget.h" |
| 10 | |
| 11 | #include "GrContext.h" |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 13 | #include "GrDrawContext.h" |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 14 | #include "GrDrawTarget.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 15 | #include "GrGpu.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 16 | #include "GrRenderTargetPriv.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 17 | #include "GrStencilAttachment.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 18 | |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame^] | 19 | GrRenderTarget::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 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 31 | GrRenderTarget::~GrRenderTarget() { |
| 32 | if (fLastDrawTarget) { |
| 33 | fLastDrawTarget->clearRT(); |
| 34 | } |
| 35 | SkSafeUnref(fLastDrawTarget); |
| 36 | } |
| 37 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 38 | void GrRenderTarget::discard() { |
| 39 | // go through context so that all necessary flushing occurs |
| 40 | GrContext* context = this->getContext(); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 41 | if (!context) { |
| 42 | return; |
| 43 | } |
| 44 | |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 45 | sk_sp<GrDrawContext> drawContext(context->contextPriv().makeWrappedDrawContext(sk_ref_sp(this), |
| 46 | nullptr)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 47 | if (!drawContext) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 48 | return; |
| 49 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 50 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 51 | drawContext->discard(); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 52 | } |
| 53 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 54 | void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 55 | if (kCanResolve_ResolveType == getResolveType()) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 56 | if (rect) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 57 | 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.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 67 | void GrRenderTarget::overrideResolveRect(const SkIRect rect) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 68 | 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 76 | } |
| 77 | |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 78 | void GrRenderTarget::onRelease() { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 79 | SkSafeSetNull(fStencilAttachment); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 80 | |
| 81 | INHERITED::onRelease(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void GrRenderTarget::onAbandon() { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 85 | SkSafeSetNull(fStencilAttachment); |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 86 | |
| 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.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 90 | |
| 91 | INHERITED::onAbandon(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 92 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 93 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 94 | void GrRenderTarget::setLastDrawTarget(GrDrawTarget* dt) { |
| 95 | if (fLastDrawTarget) { |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 96 | // The non-MDB world never closes so we can't check this condition |
| 97 | #ifdef ENABLE_MDB |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 98 | SkASSERT(fLastDrawTarget->isClosed()); |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 99 | #endif |
| 100 | fLastDrawTarget->clearRT(); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 101 | } |
| 102 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 103 | SkRefCnt_SafeAssign(fLastDrawTarget, dt); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 104 | } |
| 105 | |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 108 | bool GrRenderTargetPriv::attachStencilAttachment(GrStencilAttachment* stencil) { |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 109 | 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 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 114 | fRenderTarget->fStencilAttachment = stencil; |
| 115 | if (!fRenderTarget->completeStencilAttachment()) { |
| 116 | SkSafeSetNull(fRenderTarget->fStencilAttachment); |
| 117 | return false; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 118 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 119 | return true; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 120 | } |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 121 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 122 | int GrRenderTargetPriv::numStencilBits() const { |
| 123 | return fRenderTarget->fStencilAttachment ? fRenderTarget->fStencilAttachment->bits() : 0; |
| 124 | } |
| 125 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 126 | const GrGpu::MultisampleSpecs& |
| 127 | GrRenderTargetPriv::getMultisampleSpecs(const GrStencilSettings& stencil) const { |
| 128 | return fRenderTarget->getGpu()->getMultisampleSpecs(fRenderTarget, stencil); |
| 129 | } |