blob: 28240f8a79af55596b15aec3b5903933fb66acc0 [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
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))
Robert Phillips4431de62016-12-13 09:01:40 -050027 , fOpList(SkSafeRef(fTextureProxy->getLastTextureOpList())) {
Brian Osman45580d32016-11-23 09:37:01 -050028 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
Robert Phillips4431de62016-12-13 09:01:40 -050058// TODO: move this (and GrRenderTargetContext::copy) to GrSurfaceContext?
59bool GrTextureContext::onCopy(GrSurfaceProxy* srcProxy,
60 const SkIRect& srcRect,
61 const SkIPoint& dstPoint) {
Brian Osman45580d32016-11-23 09:37:01 -050062 ASSERT_SINGLE_OWNER
63 RETURN_FALSE_IF_ABANDONED
64 SkDEBUGCODE(this->validate();)
Robert Phillips4431de62016-12-13 09:01:40 -050065 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
Brian Osman45580d32016-11-23 09:37:01 -050078
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();
Robert Phillips4431de62016-12-13 09:01:40 -050086 bool result = opList->copySurface(tex.get(), src.get(), srcRect, dstPoint);
Brian Osman45580d32016-11-23 09:37:01 -050087
88#ifndef ENABLE_MDB
Brian Salomon742e31d2016-12-07 17:06:19 -050089 GrOpFlushState flushState(fContext->getGpu(), nullptr);
Brian Salomon1e41f4a2016-12-07 15:05:04 -050090 opList->prepareOps(&flushState);
91 opList->executeOps(&flushState);
Brian Osman45580d32016-11-23 09:37:01 -050092 opList->reset();
93#endif
94
95 return result;
96}