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 "GrRenderTargetProxy.h" |
| 9 | |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 11 | #include "GrRenderTargetOpList.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 12 | #include "GrRenderTargetPriv.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 13 | #include "GrTextureProvider.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 14 | |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 15 | // Deferred version |
| 16 | // TODO: we can probably munge the 'desc' in both the wrapped and deferred |
| 17 | // cases to make the sampleConfig/numSamples stuff more rational. |
| 18 | GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps, const GrSurfaceDesc& desc, |
| 19 | SkBackingFit fit, SkBudgeted budgeted) |
| 20 | : INHERITED(desc, fit, budgeted) |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 21 | , fFlags(GrRenderTarget::Flags::kNone) { |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 22 | // Since we know the newly created render target will be internal, we are able to precompute |
| 23 | // what the flags will ultimately end up being. |
| 24 | if (caps.usesMixedSamples() && fDesc.fSampleCnt > 0) { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 25 | fFlags |= GrRenderTarget::Flags::kMixedSampled; |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 26 | } |
| 27 | if (caps.maxWindowRectangles() > 0) { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 28 | fFlags |= GrRenderTarget::Flags::kWindowRectsSupport; |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
| 32 | // Wrapped version |
| 33 | GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps, sk_sp<GrRenderTarget> rt) |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 34 | : INHERITED(std::move(rt), SkBackingFit::kExact) |
| 35 | , fFlags(fTarget->asRenderTarget()->renderTargetPriv().flags()) { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | GrRenderTarget* GrRenderTargetProxy::instantiate(GrTextureProvider* texProvider) { |
| 39 | if (fTarget) { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 40 | return fTarget->asRenderTarget(); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // TODO: it would be nice to not have to copy the desc here |
| 44 | GrSurfaceDesc desc = fDesc; |
| 45 | desc.fFlags |= GrSurfaceFlags::kRenderTarget_GrSurfaceFlag; |
| 46 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 47 | if (SkBackingFit::kApprox == fFit) { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 48 | fTarget = texProvider->createApproxTexture(desc); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 49 | } else { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 50 | fTarget = texProvider->createTexture(desc, fBudgeted); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 51 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 52 | if (!fTarget) { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 53 | return nullptr; |
| 54 | } |
| 55 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame^] | 56 | #ifdef SK_DEBUG |
| 57 | if (kInvalidGpuMemorySize != fGpuMemorySize) { |
| 58 | SkASSERT(fTarget->gpuMemorySize() <= fGpuMemorySize); |
| 59 | } |
| 60 | #endif |
| 61 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 62 | // Check that our a priori computation matched the ultimate reality |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 63 | SkASSERT(fFlags == fTarget->asRenderTarget()->renderTargetPriv().flags()); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 64 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 65 | return fTarget->asRenderTarget(); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 68 | |
| 69 | #ifdef SK_DEBUG |
| 70 | void GrRenderTargetProxy::validate(GrContext* context) const { |
| 71 | if (fTarget) { |
| 72 | SkASSERT(fTarget->getContext() == context); |
| 73 | } |
| 74 | |
| 75 | INHERITED::validate(); |
| 76 | } |
| 77 | #endif |
| 78 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame^] | 79 | size_t GrRenderTargetProxy::onGpuMemorySize() const { |
| 80 | if (fTarget) { |
| 81 | return fTarget->gpuMemorySize(); |
| 82 | } |
| 83 | |
| 84 | SkASSERT(kUnknown_GrPixelConfig != fDesc.fConfig); |
| 85 | SkASSERT(!GrPixelConfigIsCompressed(fDesc.fConfig)); |
| 86 | size_t colorBytes = GrBytesPerPixel(fDesc.fConfig); |
| 87 | SkASSERT(colorBytes > 0); |
| 88 | |
| 89 | // TODO: do we have enough information to improve this worst case estimate? |
| 90 | return (fDesc.fSampleCnt + 1) * fDesc.fWidth * fDesc.fHeight * colorBytes; |
| 91 | } |
| 92 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 93 | sk_sp<GrRenderTargetProxy> GrRenderTargetProxy::Make(const GrCaps& caps, |
| 94 | const GrSurfaceDesc& desc, |
| 95 | SkBackingFit fit, |
| 96 | SkBudgeted budgeted) { |
| 97 | return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(caps, desc, fit, budgeted)); |
| 98 | } |
| 99 | |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 100 | sk_sp<GrRenderTargetProxy> GrRenderTargetProxy::Make(const GrCaps& caps, sk_sp<GrRenderTarget> rt) { |
| 101 | return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(caps, rt)); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 102 | } |
| 103 | |