GrTextureContext.cpp revision 4a395049a977d7e04515bad490365fe9ec9ffaab
1/*
2 * Copyright 2016 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#include "GrTextureContext.h"
9
10#include "GrContextPriv.h"
11#include "GrDrawingManager.h"
12#include "GrResourceProvider.h"
13#include "GrTextureOpList.h"
14
15#include "../private/GrAuditTrail.h"
16
17#define ASSERT_SINGLE_OWNER \
18    SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
19#define RETURN_FALSE_IF_ABANDONED  if (this->drawingManager()->wasAbandoned()) { return false; }
20
21GrTextureContext::GrTextureContext(GrContext* context,
22                                   GrDrawingManager* drawingMgr,
23                                   sk_sp<GrTextureProxy> textureProxy,
24                                   sk_sp<SkColorSpace> colorSpace,
25                                   GrAuditTrail* auditTrail,
26                                   GrSingleOwner* singleOwner)
27    : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
28    , fTextureProxy(std::move(textureProxy))
29    , fOpList(sk_ref_sp(fTextureProxy->getLastTextureOpList())) {
30    SkDEBUGCODE(this->validate();)
31}
32
33#ifdef SK_DEBUG
34void GrTextureContext::validate() const {
35    SkASSERT(fTextureProxy);
36    fTextureProxy->validate(fContext);
37
38    if (fOpList && !fOpList->isClosed()) {
39        SkASSERT(fTextureProxy->getLastOpList() == fOpList.get());
40    }
41}
42#endif
43
44GrTextureContext::~GrTextureContext() {
45    ASSERT_SINGLE_OWNER
46}
47
48GrRenderTargetProxy* GrTextureContext::asRenderTargetProxy() {
49    // If the proxy can return an RTProxy it should've been wrapped in a RTContext
50    SkASSERT(!fTextureProxy->asRenderTargetProxy());
51    return nullptr;
52}
53
54sk_sp<GrRenderTargetProxy> GrTextureContext::asRenderTargetProxyRef() {
55    // If the proxy can return an RTProxy it should've been wrapped in a RTContext
56    SkASSERT(!fTextureProxy->asRenderTargetProxy());
57    return nullptr;
58}
59
60GrTextureOpList* GrTextureContext::getOpList() {
61    ASSERT_SINGLE_OWNER
62    SkDEBUGCODE(this->validate();)
63
64    if (!fOpList || fOpList->isClosed()) {
65        fOpList = this->drawingManager()->newTextureOpList(fTextureProxy);
66    }
67
68    return fOpList.get();
69}
70
71// TODO: move this (and GrRenderTargetContext::copy) to GrSurfaceContext?
72bool GrTextureContext::onCopy(GrSurfaceProxy* srcProxy,
73                              const SkIRect& srcRect,
74                              const SkIPoint& dstPoint) {
75    ASSERT_SINGLE_OWNER
76    RETURN_FALSE_IF_ABANDONED
77    SkDEBUGCODE(this->validate();)
78    GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrTextureContext::copy");
79
80#ifndef ENABLE_MDB
81    // We can't yet fully defer copies to textures, so GrTextureContext::copySurface will
82    // execute the copy immediately. Ensure the data is ready.
83    fContext->contextPriv().flushSurfaceWrites(srcProxy);
84#endif
85
86    GrTextureOpList* opList = this->getOpList();
87    bool result = opList->copySurface(fContext->resourceProvider(),
88                                      fTextureProxy.get(), srcProxy, srcRect, dstPoint);
89
90#ifndef ENABLE_MDB
91    GrOpFlushState flushState(fContext->getGpu(), nullptr);
92    opList->prepareOps(&flushState);
93    opList->executeOps(&flushState);
94    opList->reset();
95#endif
96
97    return result;
98}
99
100