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 // This is a GPU-backend specific test. 9 10 #include "Test.h" 11 12 #include "GrBackendSurface.h" 13 #include "GrContextPriv.h" 14 #include "GrGpu.h" 15 #include "GrProxyProvider.h" 16 #include "GrRenderTarget.h" 17 #include "GrRenderTargetProxy.h" 18 #include "GrSurfaceProxy.h" 19 #include "GrTexture.h" 20 #include "GrTextureProxy.h" 21 22 static sk_sp<GrSurfaceProxy> make_wrapped_rt(GrProxyProvider* provider, 23 GrGpu* gpu, 24 skiatest::Reporter* reporter, 25 const GrSurfaceDesc& desc, 26 GrSurfaceOrigin origin) { 27 // We don't currently have a way of making MSAA backend render targets. 28 SkASSERT(1 == desc.fSampleCnt); 29 GrSRGBEncoded srgbEncoded; 30 auto ct = GrPixelConfigToColorTypeAndEncoding(desc.fConfig, &srgbEncoded); 31 auto backendRT = gpu->createTestingOnlyBackendRenderTarget(desc.fWidth, desc.fHeight, ct); 32 return provider->wrapBackendRenderTarget(backendRT, origin); 33 } 34 35 void clean_up_wrapped_rt(GrGpu* gpu, sk_sp<GrSurfaceProxy> proxy) { 36 SkASSERT(proxy->isUnique_debugOnly()); 37 SkASSERT(proxy->peekRenderTarget()); 38 GrBackendRenderTarget rt = proxy->peekRenderTarget()->getBackendRenderTarget(); 39 proxy.reset(); 40 gpu->deleteTestingOnlyBackendRenderTarget(rt); 41 } 42 43 static sk_sp<GrSurfaceProxy> make_offscreen_rt(GrProxyProvider* provider, 44 const GrSurfaceDesc& desc, 45 GrSurfaceOrigin origin) { 46 SkASSERT(kRenderTarget_GrSurfaceFlag == desc.fFlags); 47 48 return provider->testingOnly_createInstantiatedProxy(desc, origin, SkBackingFit::kExact, 49 SkBudgeted::kYes); 50 } 51 52 static sk_sp<GrSurfaceProxy> make_texture(GrProxyProvider* provider, 53 const GrSurfaceDesc& desc, 54 GrSurfaceOrigin origin) { 55 return provider->testingOnly_createInstantiatedProxy(desc, origin, SkBackingFit::kExact, 56 SkBudgeted::kYes); 57 } 58 59 // Test converting between RenderTargetProxies and TextureProxies for preinstantiated Proxies 60 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PreinstantiatedProxyConversionTest, reporter, ctxInfo) { 61 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider(); 62 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu(); 63 64 GrSurfaceDesc desc; 65 desc.fFlags = kRenderTarget_GrSurfaceFlag; 66 desc.fWidth = 64; 67 desc.fHeight = 64; 68 desc.fConfig = kRGBA_8888_GrPixelConfig; 69 70 { 71 // External on-screen render target. 72 sk_sp<GrSurfaceProxy> sProxy( 73 make_wrapped_rt(proxyProvider, gpu, reporter, desc, kBottomLeft_GrSurfaceOrigin)); 74 if (sProxy) { 75 // RenderTarget-only 76 GrRenderTargetProxy* rtProxy = sProxy->asRenderTargetProxy(); 77 REPORTER_ASSERT(reporter, rtProxy); 78 REPORTER_ASSERT(reporter, !rtProxy->asTextureProxy()); 79 REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy); 80 clean_up_wrapped_rt(gpu, std::move(sProxy)); 81 } 82 } 83 84 { 85 // Internal offscreen render target. 86 sk_sp<GrSurfaceProxy> sProxy( 87 make_offscreen_rt(proxyProvider, desc, kBottomLeft_GrSurfaceOrigin)); 88 if (sProxy) { 89 // Both RenderTarget and Texture 90 GrRenderTargetProxy* rtProxy = sProxy->asRenderTargetProxy(); 91 REPORTER_ASSERT(reporter, rtProxy); 92 GrTextureProxy* tProxy = rtProxy->asTextureProxy(); 93 REPORTER_ASSERT(reporter, tProxy); 94 REPORTER_ASSERT(reporter, tProxy->asRenderTargetProxy() == rtProxy); 95 REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy); 96 } 97 } 98 99 { 100 // Internal offscreen render target - but through GrTextureProxy 101 sk_sp<GrSurfaceProxy> sProxy( 102 make_texture(proxyProvider, desc, kBottomLeft_GrSurfaceOrigin)); 103 if (sProxy) { 104 // Both RenderTarget and Texture 105 GrTextureProxy* tProxy = sProxy->asTextureProxy(); 106 REPORTER_ASSERT(reporter, tProxy); 107 GrRenderTargetProxy* rtProxy = tProxy->asRenderTargetProxy(); 108 REPORTER_ASSERT(reporter, rtProxy); 109 REPORTER_ASSERT(reporter, rtProxy->asTextureProxy() == tProxy); 110 REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy); 111 } 112 } 113 114 { 115 desc.fFlags = kNone_GrSurfaceFlags; // force no-RT 116 117 sk_sp<GrSurfaceProxy> sProxy( 118 make_texture(proxyProvider, desc, kBottomLeft_GrSurfaceOrigin)); 119 if (sProxy) { 120 // Texture-only 121 GrTextureProxy* tProxy = sProxy->asTextureProxy(); 122 REPORTER_ASSERT(reporter, tProxy); 123 REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy); 124 REPORTER_ASSERT(reporter, !tProxy->asRenderTargetProxy()); 125 } 126 } 127 } 128 129 // Test converting between RenderTargetProxies and TextureProxies for deferred 130 // Proxies 131 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DefferredProxyConversionTest, reporter, ctxInfo) { 132 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider(); 133 134 GrSurfaceDesc desc; 135 desc.fFlags = kRenderTarget_GrSurfaceFlag; 136 desc.fWidth = 64; 137 desc.fHeight = 64; 138 desc.fConfig = kRGBA_8888_GrPixelConfig; 139 140 const GrBackendFormat format = 141 ctxInfo.grContext()->contextPriv().caps()->getBackendFormatFromColorType( 142 kRGBA_8888_SkColorType); 143 { 144 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( 145 format, desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); 146 147 // Both RenderTarget and Texture 148 GrRenderTargetProxy* rtProxy = proxy->asRenderTargetProxy(); 149 REPORTER_ASSERT(reporter, rtProxy); 150 GrTextureProxy* tProxy = rtProxy->asTextureProxy(); 151 REPORTER_ASSERT(reporter, tProxy); 152 REPORTER_ASSERT(reporter, tProxy->asRenderTargetProxy() == rtProxy); 153 REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy); 154 } 155 156 { 157 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( 158 format, desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); 159 160 // Both RenderTarget and Texture - but via GrTextureProxy 161 GrTextureProxy* tProxy = proxy->asTextureProxy(); 162 REPORTER_ASSERT(reporter, tProxy); 163 GrRenderTargetProxy* rtProxy = tProxy->asRenderTargetProxy(); 164 REPORTER_ASSERT(reporter, rtProxy); 165 REPORTER_ASSERT(reporter, rtProxy->asTextureProxy() == tProxy); 166 REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy); 167 } 168 169 { 170 desc.fFlags = kNone_GrSurfaceFlags; // force no-RT 171 172 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( 173 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); 174 // Texture-only 175 GrTextureProxy* tProxy = proxy->asTextureProxy(); 176 REPORTER_ASSERT(reporter, tProxy); 177 REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy); 178 REPORTER_ASSERT(reporter, !tProxy->asRenderTargetProxy()); 179 } 180 } 181