jvanverth | fd359ca | 2016-03-18 11:57:24 -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 | // This is a GPU-backend specific test. It relies on static intializers to work |
| 9 | |
| 10 | #include "SkTypes.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_VULKAN) |
| 13 | |
| 14 | #include "GrContextFactory.h" |
| 15 | #include "GrTest.h" |
| 16 | #include "Test.h" |
| 17 | #include "vk/GrVkCaps.h" |
| 18 | #include "vk/GrVkGpu.h" |
| 19 | #include "vk/GrVkMemory.h" |
| 20 | #include "vk/GrVkTypes.h" |
| 21 | |
kkinnunen | 2ae4b2e | 2016-03-31 08:08:20 -0700 | [diff] [blame] | 22 | using sk_gpu_test::GrContextFactory; |
| 23 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 24 | const int kW = 1024; |
| 25 | const int kH = 1024; |
| 26 | const GrPixelConfig kPixelConfig = kRGBA_8888_GrPixelConfig; |
| 27 | |
| 28 | void wrap_tex_test(skiatest::Reporter* reporter, GrContext* context) { |
| 29 | |
| 30 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu()); |
| 31 | |
| 32 | GrBackendObject backendObj = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kPixelConfig); |
| 33 | const GrVkTextureInfo* backendTex = reinterpret_cast<const GrVkTextureInfo*>(backendObj); |
| 34 | |
| 35 | // check basic borrowed creation |
| 36 | GrBackendTextureDesc desc; |
| 37 | desc.fConfig = kPixelConfig; |
| 38 | desc.fWidth = kW; |
| 39 | desc.fHeight = kH; |
| 40 | desc.fTextureHandle = backendObj; |
| 41 | GrTexture* tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership); |
| 42 | REPORTER_ASSERT(reporter, tex); |
| 43 | tex->unref(); |
| 44 | |
| 45 | // image is null |
| 46 | GrVkTextureInfo backendCopy = *backendTex; |
| 47 | backendCopy.fImage = VK_NULL_HANDLE; |
| 48 | desc.fTextureHandle = (GrBackendObject) &backendCopy; |
| 49 | tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership); |
| 50 | REPORTER_ASSERT(reporter, !tex); |
| 51 | tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership); |
| 52 | REPORTER_ASSERT(reporter, !tex); |
| 53 | |
| 54 | // alloc is null |
| 55 | backendCopy.fImage = backendTex->fImage; |
| 56 | backendCopy.fAlloc = VK_NULL_HANDLE; |
| 57 | tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership); |
| 58 | REPORTER_ASSERT(reporter, !tex); |
| 59 | tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership); |
| 60 | REPORTER_ASSERT(reporter, !tex); |
| 61 | |
| 62 | // check adopt creation |
| 63 | backendCopy.fAlloc = backendTex->fAlloc; |
| 64 | tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership); |
| 65 | REPORTER_ASSERT(reporter, tex); |
| 66 | tex->unref(); |
| 67 | |
| 68 | gpu->deleteTestingOnlyBackendTexture(backendObj, true); |
| 69 | } |
| 70 | |
| 71 | void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) { |
| 72 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu()); |
| 73 | |
| 74 | GrBackendObject backendObj = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kPixelConfig); |
| 75 | const GrVkTextureInfo* backendTex = reinterpret_cast<const GrVkTextureInfo*>(backendObj); |
| 76 | |
| 77 | // check basic borrowed creation |
| 78 | GrBackendRenderTargetDesc desc; |
| 79 | desc.fWidth = kW; |
| 80 | desc.fHeight = kH; |
| 81 | desc.fConfig = kPixelConfig; |
| 82 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 83 | desc.fSampleCnt = 0; |
| 84 | desc.fStencilBits = 0; |
| 85 | desc.fRenderTargetHandle = backendObj; |
| 86 | GrRenderTarget* rt = gpu->wrapBackendRenderTarget(desc, kBorrow_GrWrapOwnership); |
| 87 | REPORTER_ASSERT(reporter, rt); |
| 88 | rt->unref(); |
| 89 | |
| 90 | // image is null |
| 91 | GrVkTextureInfo backendCopy = *backendTex; |
| 92 | backendCopy.fImage = VK_NULL_HANDLE; |
| 93 | desc.fRenderTargetHandle = (GrBackendObject)&backendCopy; |
| 94 | rt = gpu->wrapBackendRenderTarget(desc, kBorrow_GrWrapOwnership); |
| 95 | REPORTER_ASSERT(reporter, !rt); |
| 96 | rt = gpu->wrapBackendRenderTarget(desc, kAdopt_GrWrapOwnership); |
| 97 | REPORTER_ASSERT(reporter, !rt); |
| 98 | |
| 99 | // alloc is null |
| 100 | backendCopy.fImage = backendTex->fImage; |
| 101 | backendCopy.fAlloc = VK_NULL_HANDLE; |
| 102 | // can wrap null alloc if borrowing |
| 103 | rt = gpu->wrapBackendRenderTarget(desc, kBorrow_GrWrapOwnership); |
| 104 | REPORTER_ASSERT(reporter, rt); |
| 105 | // but not if adopting |
| 106 | rt = gpu->wrapBackendRenderTarget(desc, kAdopt_GrWrapOwnership); |
| 107 | REPORTER_ASSERT(reporter, !rt); |
| 108 | |
| 109 | // check adopt creation |
| 110 | backendCopy.fAlloc = backendTex->fAlloc; |
| 111 | rt = gpu->wrapBackendRenderTarget(desc, kAdopt_GrWrapOwnership); |
| 112 | REPORTER_ASSERT(reporter, rt); |
| 113 | rt->unref(); |
| 114 | |
| 115 | gpu->deleteTestingOnlyBackendTexture(backendObj, true); |
| 116 | } |
| 117 | |
| 118 | void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) { |
| 119 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu()); |
| 120 | |
| 121 | GrBackendObject backendObj = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kPixelConfig); |
| 122 | const GrVkTextureInfo* backendTex = reinterpret_cast<const GrVkTextureInfo*>(backendObj); |
| 123 | |
| 124 | // check basic borrowed creation |
| 125 | GrBackendTextureDesc desc; |
| 126 | desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 127 | desc.fConfig = kPixelConfig; |
| 128 | desc.fWidth = kW; |
| 129 | desc.fHeight = kH; |
| 130 | desc.fTextureHandle = backendObj; |
| 131 | GrTexture* tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership); |
| 132 | REPORTER_ASSERT(reporter, tex); |
| 133 | tex->unref(); |
| 134 | |
| 135 | // image is null |
| 136 | GrVkTextureInfo backendCopy = *backendTex; |
| 137 | backendCopy.fImage = VK_NULL_HANDLE; |
| 138 | desc.fTextureHandle = (GrBackendObject)&backendCopy; |
| 139 | tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership); |
| 140 | REPORTER_ASSERT(reporter, !tex); |
| 141 | tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership); |
| 142 | REPORTER_ASSERT(reporter, !tex); |
| 143 | |
| 144 | // alloc is null |
| 145 | backendCopy.fImage = backendTex->fImage; |
| 146 | backendCopy.fAlloc = VK_NULL_HANDLE; |
| 147 | tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership); |
| 148 | REPORTER_ASSERT(reporter, !tex); |
| 149 | tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership); |
| 150 | REPORTER_ASSERT(reporter, !tex); |
| 151 | |
| 152 | // check adopt creation |
| 153 | backendCopy.fAlloc = backendTex->fAlloc; |
| 154 | tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership); |
| 155 | REPORTER_ASSERT(reporter, tex); |
| 156 | tex->unref(); |
| 157 | |
| 158 | gpu->deleteTestingOnlyBackendTexture(backendObj, true); |
| 159 | } |
| 160 | |
| 161 | DEF_GPUTEST(VkWrapTests, reporter, factory) { |
| 162 | GrContextOptions opts; |
| 163 | opts.fSuppressPrints = true; |
| 164 | GrContextFactory debugFactory(opts); |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 165 | for (int type = 0; type < GrContextFactory::kLastContextType; ++type) { |
| 166 | if (static_cast<GrContextFactory::ContextType>(type) != |
| 167 | GrContextFactory::kNativeGL_ContextType) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 168 | continue; |
| 169 | } |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 170 | GrContext* context = debugFactory.get(static_cast<GrContextFactory::ContextType>(type)); |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 171 | if (context) { |
| 172 | wrap_tex_test(reporter, context); |
| 173 | wrap_rt_test(reporter, context); |
| 174 | wrap_trt_test(reporter, context); |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | #endif |