robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [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 "GrSurfaceProxy.h" |
| 9 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 10 | #include "GrCaps.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 11 | #include "GrGpuResourcePriv.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 12 | #include "GrOpList.h" |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 13 | #include "GrTextureProvider.h" |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 14 | #include "GrTextureRenderTargetProxy.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 15 | |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 16 | #include "SkMathPriv.h" |
| 17 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 18 | GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit) |
| 19 | : INHERITED(std::move(surface)) |
| 20 | , fDesc(fTarget->desc()) |
| 21 | , fFit(fit) |
| 22 | , fBudgeted(fTarget->resourcePriv().isBudgeted()) |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 23 | , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID! |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 24 | , fGpuMemorySize(kInvalidGpuMemorySize) |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 25 | , fLastOpList(nullptr) { |
| 26 | } |
| 27 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 28 | GrSurfaceProxy::~GrSurfaceProxy() { |
| 29 | if (fLastOpList) { |
| 30 | fLastOpList->clearTarget(); |
| 31 | } |
| 32 | SkSafeUnref(fLastOpList); |
| 33 | } |
| 34 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 35 | GrSurface* GrSurfaceProxy::instantiate(GrTextureProvider* texProvider) { |
| 36 | if (fTarget) { |
| 37 | return fTarget; |
| 38 | } |
| 39 | |
| 40 | if (SkBackingFit::kApprox == fFit) { |
| 41 | fTarget = texProvider->createApproxTexture(fDesc); |
| 42 | } else { |
| 43 | fTarget = texProvider->createTexture(fDesc, fBudgeted); |
| 44 | } |
| 45 | if (!fTarget) { |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 49 | this->INHERITED::transferRefs(); |
| 50 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 51 | #ifdef SK_DEBUG |
| 52 | if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) { |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 53 | SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly()); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 54 | } |
| 55 | #endif |
| 56 | |
| 57 | return fTarget; |
| 58 | } |
| 59 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 60 | int GrSurfaceProxy::worstCaseWidth(const GrCaps& caps) const { |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 61 | if (fTarget) { |
| 62 | return fTarget->width(); |
| 63 | } |
| 64 | |
| 65 | if (SkBackingFit::kExact == fFit) { |
| 66 | return fDesc.fWidth; |
| 67 | } |
| 68 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 69 | if (caps.reuseScratchTextures() || fDesc.fFlags & kRenderTarget_GrSurfaceFlag) { |
| 70 | return SkTMax(GrTextureProvider::kMinScratchTextureSize, GrNextPow2(fDesc.fWidth)); |
| 71 | } |
| 72 | |
| 73 | return fDesc.fWidth; |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 76 | int GrSurfaceProxy::worstCaseHeight(const GrCaps& caps) const { |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 77 | if (fTarget) { |
| 78 | return fTarget->height(); |
| 79 | } |
| 80 | |
| 81 | if (SkBackingFit::kExact == fFit) { |
| 82 | return fDesc.fHeight; |
| 83 | } |
| 84 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 85 | if (caps.reuseScratchTextures() || fDesc.fFlags & kRenderTarget_GrSurfaceFlag) { |
| 86 | return SkTMax(GrTextureProvider::kMinScratchTextureSize, GrNextPow2(fDesc.fHeight)); |
| 87 | } |
| 88 | |
| 89 | return fDesc.fHeight; |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 92 | void GrSurfaceProxy::setLastOpList(GrOpList* opList) { |
| 93 | if (fLastOpList) { |
| 94 | // The non-MDB world never closes so we can't check this condition |
| 95 | #ifdef ENABLE_MDB |
| 96 | SkASSERT(fLastOpList->isClosed()); |
| 97 | #endif |
| 98 | fLastOpList->clearTarget(); |
| 99 | } |
| 100 | |
| 101 | SkRefCnt_SafeAssign(fLastOpList, opList); |
| 102 | } |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 103 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 104 | GrRenderTargetOpList* GrSurfaceProxy::getLastRenderTargetOpList() { |
| 105 | return fLastOpList ? fLastOpList->asRenderTargetOpList() : nullptr; |
| 106 | } |
| 107 | |
| 108 | GrTextureOpList* GrSurfaceProxy::getLastTextureOpList() { |
| 109 | return fLastOpList ? fLastOpList->asTextureOpList() : nullptr; |
| 110 | } |
| 111 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 112 | sk_sp<GrSurfaceProxy> GrSurfaceProxy::MakeWrapped(sk_sp<GrSurface> surf) { |
| 113 | if (surf->asTexture()) { |
| 114 | if (surf->asRenderTarget()) { |
| 115 | return sk_sp<GrSurfaceProxy>(new GrTextureRenderTargetProxy(std::move(surf))); |
| 116 | } else { |
| 117 | return sk_sp<GrSurfaceProxy>(new GrTextureProxy(std::move(surf))); |
| 118 | } |
| 119 | } else { |
| 120 | SkASSERT(surf->asRenderTarget()); |
| 121 | |
| 122 | // Not texturable |
| 123 | return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(surf))); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | sk_sp<GrSurfaceProxy> GrSurfaceProxy::MakeDeferred(const GrCaps& caps, |
| 128 | const GrSurfaceDesc& desc, |
| 129 | SkBackingFit fit, |
| 130 | SkBudgeted budgeted) { |
| 131 | if (kRenderTarget_GrSurfaceFlag & desc.fFlags) { |
| 132 | // We know anything we instantiate later from this deferred path will be |
| 133 | // both texturable and renderable |
| 134 | return sk_sp<GrSurfaceProxy>(new GrTextureRenderTargetProxy(caps, desc, fit, budgeted)); |
| 135 | } |
| 136 | |
| 137 | return sk_sp<GrSurfaceProxy>(new GrTextureProxy(desc, fit, budgeted, nullptr, 0)); |
| 138 | } |
| 139 | |
| 140 | sk_sp<GrSurfaceProxy> GrSurfaceProxy::MakeDeferred(const GrCaps& caps, |
| 141 | GrTextureProvider* texProvider, |
| 142 | const GrSurfaceDesc& desc, |
| 143 | SkBudgeted budgeted, |
| 144 | const void* srcData, |
| 145 | size_t rowBytes) { |
| 146 | if (srcData) { |
| 147 | // If we have srcData, for now, we create a wrapped GrTextureProxy |
| 148 | sk_sp<GrSurface> surf(texProvider->createTexture(desc, budgeted, srcData, rowBytes)); |
| 149 | return GrSurfaceProxy::MakeWrapped(std::move(surf)); |
| 150 | } |
| 151 | |
| 152 | return GrSurfaceProxy::MakeDeferred(caps, desc, SkBackingFit::kExact, budgeted); |
| 153 | } |
| 154 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 155 | #ifdef SK_DEBUG |
| 156 | void GrSurfaceProxy::validate(GrContext* context) const { |
| 157 | if (fTarget) { |
| 158 | SkASSERT(fTarget->getContext() == context); |
| 159 | } |
| 160 | |
| 161 | INHERITED::validate(); |
| 162 | } |
| 163 | #endif |