blob: f495cd34bb827a771bec82c9c0f8708dd1428ca3 [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"
Brian Osman11052242016-10-27 14:47:55 -040013#include "GrRenderTargetContext.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000014#include "GrGpu.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040015#include "GrRenderTargetOpList.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070017#include "GrStencilAttachment.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070018#include "GrStencilSettings.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000019
csmartdaltonf9635992016-08-10 11:09:07 -070020GrRenderTarget::GrRenderTarget(GrGpu* gpu, const GrSurfaceDesc& desc, Flags flags,
21 GrStencilAttachment* stencil)
22 : INHERITED(gpu, desc)
23 , fStencilAttachment(stencil)
24 , fMultisampleSpecsID(0)
Robert Phillipsf2361d22016-10-25 14:20:06 -040025 , fFlags(flags) {
csmartdaltonf9635992016-08-10 11:09:07 -070026 SkASSERT(!(fFlags & Flags::kMixedSampled) || fDesc.fSampleCnt > 0);
27 SkASSERT(!(fFlags & Flags::kWindowRectsSupport) || gpu->caps()->maxWindowRectangles() > 0);
28 fResolveRect.setLargestInverted();
29}
30
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000031void GrRenderTarget::discard() {
32 // go through context so that all necessary flushing occurs
33 GrContext* context = this->getContext();
robertphillipsc9a37062015-09-01 08:34:28 -070034 if (!context) {
35 return;
36 }
37
Brian Osman11052242016-10-27 14:47:55 -040038 sk_sp<GrRenderTargetContext> renderTargetContext(
39 context->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(this), nullptr));
40 if (!renderTargetContext) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000041 return;
42 }
robertphillipsea461502015-05-26 11:38:03 -070043
Brian Osman11052242016-10-27 14:47:55 -040044 renderTargetContext->discard();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000045}
46
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000047void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000048 if (kCanResolve_ResolveType == getResolveType()) {
bsalomon49f085d2014-09-05 13:34:00 -070049 if (rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000050 fResolveRect.join(*rect);
51 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
52 fResolveRect.setEmpty();
53 }
54 } else {
55 fResolveRect.setLTRB(0, 0, this->width(), this->height());
56 }
57 }
58}
59
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000060void GrRenderTarget::overrideResolveRect(const SkIRect rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000061 fResolveRect = rect;
62 if (fResolveRect.isEmpty()) {
63 fResolveRect.setLargestInverted();
64 } else {
65 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
66 fResolveRect.setLargestInverted();
67 }
68 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000069}
70
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000071void GrRenderTarget::onRelease() {
egdanielec00d942015-09-14 12:56:10 -070072 SkSafeSetNull(fStencilAttachment);
robertphillips@google.comd3645542012-09-05 18:37:39 +000073
74 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000075}
76
77void GrRenderTarget::onAbandon() {
egdanielec00d942015-09-14 12:56:10 -070078 SkSafeSetNull(fStencilAttachment);
robertphillips498d7ac2015-10-30 10:11:30 -070079
80 // The contents of this renderTarget are gone/invalid. It isn't useful to point back
Robert Phillipsf2361d22016-10-25 14:20:06 -040081 // the creating opList.
82 this->setLastOpList(nullptr);
robertphillips@google.comd3645542012-09-05 18:37:39 +000083
84 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000085}
bsalomon6bc1b5f2015-02-23 09:06:38 -080086
87///////////////////////////////////////////////////////////////////////////////
88
egdanielec00d942015-09-14 12:56:10 -070089bool GrRenderTargetPriv::attachStencilAttachment(GrStencilAttachment* stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -070090 if (!stencil && !fRenderTarget->fStencilAttachment) {
91 // No need to do any work since we currently don't have a stencil attachment and
Robert Phillips29e52f12016-11-03 10:19:14 -040092 // we're not actually adding one.
egdaniel79bd2ae2015-09-15 08:46:13 -070093 return true;
94 }
egdanielec00d942015-09-14 12:56:10 -070095 fRenderTarget->fStencilAttachment = stencil;
96 if (!fRenderTarget->completeStencilAttachment()) {
97 SkSafeSetNull(fRenderTarget->fStencilAttachment);
98 return false;
halcanary9d524f22016-03-29 09:03:52 -070099 }
egdanielec00d942015-09-14 12:56:10 -0700100 return true;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800101}
cdalton28f45b92016-03-07 13:58:26 -0800102
cdalton193d9cf2016-05-12 11:52:02 -0700103int GrRenderTargetPriv::numStencilBits() const {
csmartdaltonc633abb2016-11-01 08:55:55 -0700104 SkASSERT(this->getStencilAttachment());
105 return this->getStencilAttachment()->bits();
cdalton193d9cf2016-05-12 11:52:02 -0700106}
107
cdalton28f45b92016-03-07 13:58:26 -0800108const GrGpu::MultisampleSpecs&
csmartdaltonc633abb2016-11-01 08:55:55 -0700109GrRenderTargetPriv::getMultisampleSpecs(const GrPipeline& pipeline) const {
110 SkASSERT(fRenderTarget == pipeline.getRenderTarget()); // TODO: remove RT from pipeline.
csmartdaltonc25c5d72016-11-01 07:03:59 -0700111 GrGpu* gpu = fRenderTarget->getGpu();
112 if (auto id = fRenderTarget->fMultisampleSpecsID) {
csmartdaltonc633abb2016-11-01 08:55:55 -0700113 SkASSERT(gpu->queryMultisampleSpecs(pipeline).fUniqueID == id);
csmartdaltonc25c5d72016-11-01 07:03:59 -0700114 return gpu->getMultisampleSpecs(id);
115 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700116 const GrGpu::MultisampleSpecs& specs = gpu->queryMultisampleSpecs(pipeline);
csmartdaltonc25c5d72016-11-01 07:03:59 -0700117 fRenderTarget->fMultisampleSpecsID = specs.fUniqueID;
118 return specs;
cdalton28f45b92016-03-07 13:58:26 -0800119}
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700120