blob: 83809114ff4206714b3c58f597982919e7ac948f [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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
csmartdaltonf9635992016-08-10 11:09:07 -070010#include "GrCaps.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050011#include "GrGpuResourcePriv.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040012#include "GrRenderTargetOpList.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040013#include "GrRenderTargetPriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050014#include "GrResourceProvider.h"
Robert Phillips84a81202016-11-04 11:59:10 -040015#include "GrTextureRenderTargetProxy.h"
Brian Salomonbb5711a2017-05-17 13:49:59 -040016#include "SkMathPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070017
csmartdaltonf9635992016-08-10 11:09:07 -070018// Deferred version
19// TODO: we can probably munge the 'desc' in both the wrapped and deferred
20// cases to make the sampleConfig/numSamples stuff more rational.
21GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps, const GrSurfaceDesc& desc,
Robert Phillipsc787e492017-02-28 11:26:32 -050022 SkBackingFit fit, SkBudgeted budgeted, uint32_t flags)
Brian Salomonbb5711a2017-05-17 13:49:59 -040023 : INHERITED(desc, fit, budgeted, flags)
24 , fSampleCnt(desc.fSampleCnt)
Robert Phillipsc4f0a822017-06-13 08:11:36 -040025 , fRenderTargetFlags(GrRenderTargetFlags::kNone) {
csmartdaltonf9635992016-08-10 11:09:07 -070026 // Since we know the newly created render target will be internal, we are able to precompute
27 // what the flags will ultimately end up being.
Brian Salomonbb5711a2017-05-17 13:49:59 -040028 if (caps.usesMixedSamples() && fSampleCnt > 0) {
Robert Phillipsc4f0a822017-06-13 08:11:36 -040029 fRenderTargetFlags |= GrRenderTargetFlags::kMixedSampled;
csmartdaltonf9635992016-08-10 11:09:07 -070030 }
31 if (caps.maxWindowRectangles() > 0) {
Robert Phillipsc4f0a822017-06-13 08:11:36 -040032 fRenderTargetFlags |= GrRenderTargetFlags::kWindowRectsSupport;
csmartdaltonf9635992016-08-10 11:09:07 -070033 }
34}
35
36// Wrapped version
Robert Phillips467022b2017-07-21 08:44:46 -040037GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin)
38 : INHERITED(std::move(surf), origin, SkBackingFit::kExact)
Robert Phillipsc4f0a822017-06-13 08:11:36 -040039 , fSampleCnt(fTarget->asRenderTarget()->numStencilSamples())
40 , fRenderTargetFlags(fTarget->asRenderTarget()->renderTargetPriv().flags()) {
41}
robertphillips76948d42016-05-04 12:47:41 -070042
Robert Phillipsec2249f2016-11-09 08:54:35 -050043int GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const {
Robert Phillipsc4f0a822017-06-13 08:11:36 -040044 return (fRenderTargetFlags & GrRenderTargetFlags::kWindowRectsSupport)
Brian Salomondac5f6b2017-02-28 16:11:04 -050045 ? caps.maxWindowRectangles()
46 : 0;
Robert Phillipsec2249f2016-11-09 08:54:35 -050047}
48
Robert Phillipseee4d6e2017-06-05 09:26:07 -040049bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbb5711a2017-05-17 13:49:59 -040050 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -070051
Robert Phillipseee4d6e2017-06-05 09:26:07 -040052 if (!this->instantiateImpl(resourceProvider, fSampleCnt, kFlags,
53 /* isMipped = */ false,
54 SkDestinationSurfaceColorMode::kLegacy)) {
55 return false;
robertphillips76948d42016-05-04 12:47:41 -070056 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040057 SkASSERT(fTarget->asRenderTarget());
Leon Scroggins7d7d7d12016-11-07 21:26:31 +000058 // Check that our a priori computation matched the ultimate reality
Robert Phillipseee4d6e2017-06-05 09:26:07 -040059 SkASSERT(fRenderTargetFlags == fTarget->asRenderTarget()->renderTargetPriv().flags());
Leon Scroggins7d7d7d12016-11-07 21:26:31 +000060
Robert Phillipseee4d6e2017-06-05 09:26:07 -040061 return true;
robertphillips76948d42016-05-04 12:47:41 -070062}
63
Robert Phillips5af44de2017-07-18 14:49:38 -040064sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
65 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
66
67 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, fSampleCnt, kFlags,
68 /* isMipped = */ false,
69 SkDestinationSurfaceColorMode::kLegacy);
70 if (!surface) {
71 return nullptr;
72 }
73 SkASSERT(surface->asRenderTarget());
74 // Check that our a priori computation matched the ultimate reality
75 SkASSERT(fRenderTargetFlags == surface->asRenderTarget()->renderTargetPriv().flags());
76
77 return surface;
78}
79
Brian Salomonbb5711a2017-05-17 13:49:59 -040080int GrRenderTargetProxy::worstCaseWidth() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -040081 if (fTarget) {
Brian Salomonbb5711a2017-05-17 13:49:59 -040082 return fTarget->width();
Robert Phillips8bc06d02016-11-01 17:28:40 -040083 }
84
Brian Salomonbb5711a2017-05-17 13:49:59 -040085 if (SkBackingFit::kExact == fFit) {
86 return fWidth;
87 }
88 return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fWidth));
89}
90
91int GrRenderTargetProxy::worstCaseHeight() const {
92 if (fTarget) {
93 return fTarget->height();
94 }
95
96 if (SkBackingFit::kExact == fFit) {
97 return fHeight;
98 }
99 return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fHeight));
100}
101
102size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
103 int colorSamplesPerPixel = this->numColorSamples() + 1;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400104 // TODO: do we have enough information to improve this worst case estimate?
Brian Salomonbb5711a2017-05-17 13:49:59 -0400105 return GrSurface::ComputeSize(fConfig, fWidth, fHeight, colorSamplesPerPixel, false,
106 SkBackingFit::kApprox == fFit);
Robert Phillips8bc06d02016-11-01 17:28:40 -0400107}
108
Robert Phillipse2f7d182016-12-15 09:23:05 -0500109bool GrRenderTargetProxy::refsWrappedObjects() const {
110 if (!fTarget) {
111 return false;
112 }
113
114 return fTarget->resourcePriv().refsWrappedObjects();
115}