Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 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 | |
| 19 | GrTextureContext::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 Phillips | 4431de6 | 2016-12-13 09:01:40 -0500 | [diff] [blame^] | 27 | , fOpList(SkSafeRef(fTextureProxy->getLastTextureOpList())) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 28 | SkDEBUGCODE(this->validate();) |
| 29 | } |
| 30 | |
| 31 | #ifdef SK_DEBUG |
| 32 | void 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 | |
| 42 | GrTextureContext::~GrTextureContext() { |
| 43 | ASSERT_SINGLE_OWNER |
| 44 | SkSafeUnref(fOpList); |
| 45 | } |
| 46 | |
| 47 | GrTextureOpList* 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 Phillips | 4431de6 | 2016-12-13 09:01:40 -0500 | [diff] [blame^] | 58 | // TODO: move this (and GrRenderTargetContext::copy) to GrSurfaceContext? |
| 59 | bool GrTextureContext::onCopy(GrSurfaceProxy* srcProxy, |
| 60 | const SkIRect& srcRect, |
| 61 | const SkIPoint& dstPoint) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 62 | ASSERT_SINGLE_OWNER |
| 63 | RETURN_FALSE_IF_ABANDONED |
| 64 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 4431de6 | 2016-12-13 09:01:40 -0500 | [diff] [blame^] | 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 |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 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(); |
Robert Phillips | 4431de6 | 2016-12-13 09:01:40 -0500 | [diff] [blame^] | 86 | bool result = opList->copySurface(tex.get(), src.get(), srcRect, dstPoint); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 87 | |
| 88 | #ifndef ENABLE_MDB |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 89 | GrOpFlushState flushState(fContext->getGpu(), nullptr); |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 90 | opList->prepareOps(&flushState); |
| 91 | opList->executeOps(&flushState); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 92 | opList->reset(); |
| 93 | #endif |
| 94 | |
| 95 | return result; |
| 96 | } |