1 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" 13 #include "GrDrawContext.h" 14 #include "GrDrawTarget.h" 15 #include "GrGpu.h" 16 #include "GrRenderTargetPriv.h" 17 #include "GrStencilAttachment.h" 18 19 GrRenderTarget::~GrRenderTarget() { 20 if (fLastDrawTarget) { 21 fLastDrawTarget->clearRT(); 22 } 23 SkSafeUnref(fLastDrawTarget); 24 } 25 26 void GrRenderTarget::discard() { 27 // go through context so that all necessary flushing occurs 28 GrContext* context = this->getContext(); 29 if (!context) { 30 return; 31 } 32 33 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(this)); 34 if (!drawContext) { 35 return; 36 } 37 38 drawContext->discard(); 39 } 40 41 void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) { 42 if (kCanResolve_ResolveType == getResolveType()) { 43 if (rect) { 44 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 54 void GrRenderTarget::overrideResolveRect(const SkIRect rect) { 55 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 } 63 } 64 65 void GrRenderTarget::onRelease() { 66 SkSafeSetNull(fStencilAttachment); 67 68 INHERITED::onRelease(); 69 } 70 71 void GrRenderTarget::onAbandon() { 72 SkSafeSetNull(fStencilAttachment); 73 74 // The contents of this renderTarget are gone/invalid. It isn't useful to point back 75 // the creating drawTarget. 76 this->setLastDrawTarget(nullptr); 77 78 INHERITED::onAbandon(); 79 } 80 81 void GrRenderTarget::setLastDrawTarget(GrDrawTarget* dt) { 82 if (fLastDrawTarget) { 83 // The non-MDB world never closes so we can't check this condition 84 #ifdef ENABLE_MDB 85 SkASSERT(fLastDrawTarget->isClosed()); 86 #endif 87 fLastDrawTarget->clearRT(); 88 } 89 90 SkRefCnt_SafeAssign(fLastDrawTarget, dt); 91 } 92 93 /////////////////////////////////////////////////////////////////////////////// 94 95 bool GrRenderTargetPriv::attachStencilAttachment(GrStencilAttachment* stencil) { 96 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 } 101 fRenderTarget->fStencilAttachment = stencil; 102 if (!fRenderTarget->completeStencilAttachment()) { 103 SkSafeSetNull(fRenderTarget->fStencilAttachment); 104 return false; 105 } 106 return true; 107 } 108