1aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
2aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com/*
3aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Copyright 2011 Google Inc.
4aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com *
5aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Use of this source code is governed by a BSD-style license that can be
6aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * found in the LICENSE file.
7aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */
8aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
9aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
10aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrRenderTarget.h"
11aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
12aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrContext.h"
13ea4615034498aca2f9ca1753fb9a1ef10508d8ccrobertphillips#include "GrDrawContext.h"
14a106c627532ad669cf7d879955ae8ea6a53233c1robertphillips#include "GrDrawTarget.h"
15aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrGpu.h"
166bc1b5fab8554a9cb643277b4867965dd4535cd6bsalomon#include "GrRenderTargetPriv.h"
178dc7c3a839b38b73af34cc2674a06f49eb1ce527egdaniel#include "GrStencilAttachment.h"
18aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
19498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillipsGrRenderTarget::~GrRenderTarget() {
20498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips    if (fLastDrawTarget) {
21498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips        fLastDrawTarget->clearRT();
22498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips    }
23498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips    SkSafeUnref(fLastDrawTarget);
24498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips}
25498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips
2628361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.orgvoid GrRenderTarget::discard() {
2728361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    // go through context so that all necessary flushing occurs
2828361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    GrContext* context = this->getContext();
29c9a3706f1ee38c331610ec1e872d9a658566c397robertphillips    if (!context) {
30c9a3706f1ee38c331610ec1e872d9a658566c397robertphillips        return;
31c9a3706f1ee38c331610ec1e872d9a658566c397robertphillips    }
32c9a3706f1ee38c331610ec1e872d9a658566c397robertphillips
332e1e51f04985f7c258b96f0decc190456f5dd74drobertphillips    SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(this));
34ea4615034498aca2f9ca1753fb9a1ef10508d8ccrobertphillips    if (!drawContext) {
3528361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org        return;
3628361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    }
37ea4615034498aca2f9ca1753fb9a1ef10508d8ccrobertphillips
382e1e51f04985f7c258b96f0decc190456f5dd74drobertphillips    drawContext->discard();
3928361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org}
4028361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org
41fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.orgvoid GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
42aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    if (kCanResolve_ResolveType == getResolveType()) {
4349f085dddff10473b6ebf832a974288300224e60bsalomon        if (rect) {
44aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            fResolveRect.join(*rect);
45aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
46aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com                fResolveRect.setEmpty();
47aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            }
48aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        } else {
49aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            fResolveRect.setLTRB(0, 0, this->width(), this->height());
50aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        }
51aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
52aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}
53aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
54fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.orgvoid GrRenderTarget::overrideResolveRect(const SkIRect rect) {
55aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fResolveRect = rect;
56aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    if (fResolveRect.isEmpty()) {
57aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        fResolveRect.setLargestInverted();
58aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    } else {
59aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
60aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            fResolveRect.setLargestInverted();
61aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        }
62aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
6381c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com}
6481c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com
65d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.comvoid GrRenderTarget::onRelease() {
66ec00d94199fad7723b5987b86c1abef8ddafe2d8egdaniel    SkSafeSetNull(fStencilAttachment);
67d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com
68d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    INHERITED::onRelease();
69d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com}
70d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com
71d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.comvoid GrRenderTarget::onAbandon() {
72ec00d94199fad7723b5987b86c1abef8ddafe2d8egdaniel    SkSafeSetNull(fStencilAttachment);
73498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips
74498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips    // The contents of this renderTarget are gone/invalid. It isn't useful to point back
75498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips    // the creating drawTarget.
76498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips    this->setLastDrawTarget(nullptr);
77d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com
78d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    INHERITED::onAbandon();
79d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com}
806bc1b5fab8554a9cb643277b4867965dd4535cd6bsalomon
81a106c627532ad669cf7d879955ae8ea6a53233c1robertphillipsvoid GrRenderTarget::setLastDrawTarget(GrDrawTarget* dt) {
82a106c627532ad669cf7d879955ae8ea6a53233c1robertphillips    if (fLastDrawTarget) {
83498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips        // The non-MDB world never closes so we can't check this condition
84498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips#ifdef ENABLE_MDB
85a106c627532ad669cf7d879955ae8ea6a53233c1robertphillips        SkASSERT(fLastDrawTarget->isClosed());
86498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips#endif
87498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips        fLastDrawTarget->clearRT();
88a106c627532ad669cf7d879955ae8ea6a53233c1robertphillips    }
89a106c627532ad669cf7d879955ae8ea6a53233c1robertphillips
90498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips    SkRefCnt_SafeAssign(fLastDrawTarget, dt);
91a106c627532ad669cf7d879955ae8ea6a53233c1robertphillips}
92a106c627532ad669cf7d879955ae8ea6a53233c1robertphillips
936bc1b5fab8554a9cb643277b4867965dd4535cd6bsalomon///////////////////////////////////////////////////////////////////////////////
946bc1b5fab8554a9cb643277b4867965dd4535cd6bsalomon
95ec00d94199fad7723b5987b86c1abef8ddafe2d8egdanielbool GrRenderTargetPriv::attachStencilAttachment(GrStencilAttachment* stencil) {
9679bd2aee8ca328b688c8d421973f96bcd13fd12degdaniel    if (!stencil && !fRenderTarget->fStencilAttachment) {
9779bd2aee8ca328b688c8d421973f96bcd13fd12degdaniel        // No need to do any work since we currently don't have a stencil attachment and
9879bd2aee8ca328b688c8d421973f96bcd13fd12degdaniel        // we're not acctually adding one.
9979bd2aee8ca328b688c8d421973f96bcd13fd12degdaniel        return true;
10079bd2aee8ca328b688c8d421973f96bcd13fd12degdaniel    }
101ec00d94199fad7723b5987b86c1abef8ddafe2d8egdaniel    fRenderTarget->fStencilAttachment = stencil;
102ec00d94199fad7723b5987b86c1abef8ddafe2d8egdaniel    if (!fRenderTarget->completeStencilAttachment()) {
103ec00d94199fad7723b5987b86c1abef8ddafe2d8egdaniel        SkSafeSetNull(fRenderTarget->fStencilAttachment);
104ec00d94199fad7723b5987b86c1abef8ddafe2d8egdaniel        return false;
105ec00d94199fad7723b5987b86c1abef8ddafe2d8egdaniel    }
106ec00d94199fad7723b5987b86c1abef8ddafe2d8egdaniel    return true;
1076bc1b5fab8554a9cb643277b4867965dd4535cd6bsalomon}
108