blob: e547021227724d03e11ab0bb4c9053989eb8288e [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "GrRenderTarget.h"
11
12#include "GrContext.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
robertphillips498d7ac2015-10-30 10:11:30 -070019GrRenderTarget::~GrRenderTarget() {
20 if (fLastDrawTarget) {
21 fLastDrawTarget->clearRT();
22 }
23 SkSafeUnref(fLastDrawTarget);
24}
25
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000026void GrRenderTarget::discard() {
27 // go through context so that all necessary flushing occurs
28 GrContext* context = this->getContext();
robertphillipsc9a37062015-09-01 08:34:28 -070029 if (!context) {
30 return;
31 }
32
robertphillips2e1e51f2015-10-15 08:01:48 -070033 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(this));
robertphillipsea461502015-05-26 11:38:03 -070034 if (!drawContext) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000035 return;
36 }
robertphillipsea461502015-05-26 11:38:03 -070037
robertphillips2e1e51f2015-10-15 08:01:48 -070038 drawContext->discard();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000039}
40
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000041void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000042 if (kCanResolve_ResolveType == getResolveType()) {
bsalomon49f085d2014-09-05 13:34:00 -070043 if (rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000044 fResolveRect.join(*rect);
45 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
46 fResolveRect.setEmpty();
47 }
48 } else {
49 fResolveRect.setLTRB(0, 0, this->width(), this->height());
50 }
51 }
52}
53
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000054void GrRenderTarget::overrideResolveRect(const SkIRect rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000055 fResolveRect = rect;
56 if (fResolveRect.isEmpty()) {
57 fResolveRect.setLargestInverted();
58 } else {
59 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
60 fResolveRect.setLargestInverted();
61 }
62 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000063}
64
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000065void GrRenderTarget::onRelease() {
egdanielec00d942015-09-14 12:56:10 -070066 SkSafeSetNull(fStencilAttachment);
robertphillips@google.comd3645542012-09-05 18:37:39 +000067
68 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000069}
70
71void GrRenderTarget::onAbandon() {
egdanielec00d942015-09-14 12:56:10 -070072 SkSafeSetNull(fStencilAttachment);
robertphillips498d7ac2015-10-30 10:11:30 -070073
74 // The contents of this renderTarget are gone/invalid. It isn't useful to point back
75 // the creating drawTarget.
76 this->setLastDrawTarget(nullptr);
robertphillips@google.comd3645542012-09-05 18:37:39 +000077
78 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000079}
bsalomon6bc1b5f2015-02-23 09:06:38 -080080
robertphillipsa106c622015-10-16 09:07:06 -070081void GrRenderTarget::setLastDrawTarget(GrDrawTarget* dt) {
82 if (fLastDrawTarget) {
robertphillips498d7ac2015-10-30 10:11:30 -070083 // The non-MDB world never closes so we can't check this condition
84#ifdef ENABLE_MDB
robertphillipsa106c622015-10-16 09:07:06 -070085 SkASSERT(fLastDrawTarget->isClosed());
robertphillips498d7ac2015-10-30 10:11:30 -070086#endif
87 fLastDrawTarget->clearRT();
robertphillipsa106c622015-10-16 09:07:06 -070088 }
89
robertphillips498d7ac2015-10-30 10:11:30 -070090 SkRefCnt_SafeAssign(fLastDrawTarget, dt);
robertphillipsa106c622015-10-16 09:07:06 -070091}
92
bsalomon6bc1b5f2015-02-23 09:06:38 -080093///////////////////////////////////////////////////////////////////////////////
94
egdanielec00d942015-09-14 12:56:10 -070095bool GrRenderTargetPriv::attachStencilAttachment(GrStencilAttachment* stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -070096 if (!stencil && !fRenderTarget->fStencilAttachment) {
97 // No need to do any work since we currently don't have a stencil attachment and
98 // we're not acctually adding one.
99 return true;
100 }
egdanielec00d942015-09-14 12:56:10 -0700101 fRenderTarget->fStencilAttachment = stencil;
102 if (!fRenderTarget->completeStencilAttachment()) {
103 SkSafeSetNull(fRenderTarget->fStencilAttachment);
104 return false;
105 }
106 return true;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800107}