GrTextureContext.cpp revision 4431de6af930a8638c194b072558ea3a4b79d908
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#include "GrDrawingManager.h"
10#include "GrResourceProvider.h"
11#include "GrTextureOpList.h"
12
13#include "../private/GrAuditTrail.h"
14
15#define ASSERT_SINGLE_OWNER \
16    SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
17#define RETURN_FALSE_IF_ABANDONED  if (fDrawingManager->wasAbandoned()) { return false; }
18
19GrTextureContext::GrTextureContext(GrContext* context,
20                                   GrDrawingManager* drawingMgr,
21                                   sk_sp<GrTextureProxy> textureProxy,
22                                   GrAuditTrail* auditTrail,
23                                   GrSingleOwner* singleOwner)
24    : GrSurfaceContext(context, auditTrail, singleOwner)
25    , fDrawingManager(drawingMgr)
26    , fTextureProxy(std::move(textureProxy))
27    , fOpList(SkSafeRef(fTextureProxy->getLastTextureOpList())) {
28    SkDEBUGCODE(this->validate();)
29}
30
31#ifdef SK_DEBUG
32void GrTextureContext::validate() const {
33    SkASSERT(fTextureProxy);
34    fTextureProxy->validate(fContext);
35
36    if (fOpList && !fOpList->isClosed()) {
37        SkASSERT(fTextureProxy->getLastOpList() == fOpList);
38    }
39}
40#endif
41
42GrTextureContext::~GrTextureContext() {
43    ASSERT_SINGLE_OWNER
44    SkSafeUnref(fOpList);
45}
46
47GrTextureOpList* GrTextureContext::getOpList() {
48    ASSERT_SINGLE_OWNER
49    SkDEBUGCODE(this->validate();)
50
51    if (!fOpList || fOpList->isClosed()) {
52        fOpList = fDrawingManager->newOpList(fTextureProxy.get());
53    }
54
55    return fOpList;
56}
57
58// TODO: move this (and GrRenderTargetContext::copy) to GrSurfaceContext?
59bool GrTextureContext::onCopy(GrSurfaceProxy* srcProxy,
60                              const SkIRect& srcRect,
61                              const SkIPoint& dstPoint) {
62    ASSERT_SINGLE_OWNER
63    RETURN_FALSE_IF_ABANDONED
64    SkDEBUGCODE(this->validate();)
65    GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrTextureContext::copy");
66
67    // TODO: defer instantiation until flush time
68    sk_sp<GrSurface> src(sk_ref_sp(srcProxy->instantiate(fContext->textureProvider())));
69    if (!src) {
70        return false;
71    }
72
73#ifndef ENABLE_MDB
74    // We can't yet fully defer copies to textures, so GrTextureContext::copySurface will
75    // execute the copy immediately. Ensure the data is ready.
76    src->flushWrites();
77#endif
78
79    // TODO: this needs to be fixed up since it ends the deferrable of the GrTexture
80    sk_sp<GrTexture> tex(sk_ref_sp(fTextureProxy->instantiate(fContext->textureProvider())));
81    if (!tex) {
82        return false;
83    }
84
85    GrTextureOpList* opList = this->getOpList();
86    bool result = opList->copySurface(tex.get(), src.get(), srcRect, dstPoint);
87
88#ifndef ENABLE_MDB
89    GrOpFlushState flushState(fContext->getGpu(), nullptr);
90    opList->prepareOps(&flushState);
91    opList->executeOps(&flushState);
92    opList->reset();
93#endif
94
95    return result;
96}
97