Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "SkTypes.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | |
| 12 | #include "GrBackendSurface.h" |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 13 | #include "GrBackendTextureImageGenerator.h" |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 14 | #include "GrContext.h" |
| 15 | #include "GrContextPriv.h" |
| 16 | #include "GrGpu.h" |
| 17 | #include "GrRenderTargetContext.h" |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 18 | #include "GrSemaphore.h" |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 19 | #include "GrSurfaceProxyPriv.h" |
| 20 | #include "GrTest.h" |
| 21 | #include "GrTexturePriv.h" |
| 22 | #include "GrTextureProxy.h" |
| 23 | #include "SkCanvas.h" |
| 24 | #include "SkImage_Base.h" |
| 25 | #include "SkGpuDevice.h" |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 26 | #include "SkPoint.h" |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 27 | #include "SkSurface.h" |
| 28 | #include "SkSurface_Gpu.h" |
| 29 | #include "Test.h" |
| 30 | |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 31 | static constexpr int kSize = 8; |
| 32 | |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 33 | // Test that the correct mip map states are on the GrTextures when wrapping GrBackendTextures in |
| 34 | // SkImages and SkSurfaces |
| 35 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrWrappedMipMappedTest, reporter, ctxInfo) { |
| 36 | GrContext* context = ctxInfo.grContext(); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 37 | if (!context->caps()->mipMapSupport()) { |
| 38 | return; |
| 39 | } |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 40 | for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) { |
| 41 | for (auto isRT : {false, true}) { |
| 42 | // CreateTestingOnlyBackendTexture currently doesn't support uploading data to mip maps |
| 43 | // so we don't send any. However, we pretend there is data for the checks below which is |
| 44 | // fine since we are never actually using these textures for any work on the gpu. |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 45 | GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture( |
Robert Phillips | 57e0828 | 2017-11-16 14:59:48 -0500 | [diff] [blame] | 46 | nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, isRT, mipMapped); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 47 | |
Robert Phillips | e0070c0 | 2017-11-13 12:47:24 -0500 | [diff] [blame] | 48 | sk_sp<GrTextureProxy> proxy; |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 49 | sk_sp<SkImage> image; |
| 50 | if (isRT) { |
| 51 | sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture( |
| 52 | context, |
| 53 | backendTex, |
| 54 | kTopLeft_GrSurfaceOrigin, |
| 55 | 0, |
| 56 | nullptr, |
| 57 | nullptr); |
| 58 | |
| 59 | SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice(); |
Robert Phillips | e0070c0 | 2017-11-13 12:47:24 -0500 | [diff] [blame] | 60 | proxy = device->accessRenderTargetContext()->asTextureProxyRef(); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 61 | } else { |
| 62 | image = SkImage::MakeFromTexture(context, backendTex, |
| 63 | kTopLeft_GrSurfaceOrigin, |
| 64 | kPremul_SkAlphaType, nullptr); |
Robert Phillips | e0070c0 | 2017-11-13 12:47:24 -0500 | [diff] [blame] | 65 | proxy = as_IB(image)->asTextureProxyRef(); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 66 | } |
| 67 | REPORTER_ASSERT(reporter, proxy); |
| 68 | if (!proxy) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 69 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
| 73 | REPORTER_ASSERT(reporter, proxy->priv().isInstantiated()); |
| 74 | |
| 75 | GrTexture* texture = proxy->priv().peekTexture(); |
| 76 | REPORTER_ASSERT(reporter, texture); |
| 77 | if (!texture) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 78 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (GrMipMapped::kYes == mipMapped) { |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, GrMipMapped::kYes == texture->texturePriv().mipMapped()); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 84 | if (isRT) { |
| 85 | REPORTER_ASSERT(reporter, texture->texturePriv().mipMapsAreDirty()); |
| 86 | } else { |
| 87 | REPORTER_ASSERT(reporter, !texture->texturePriv().mipMapsAreDirty()); |
| 88 | } |
| 89 | } else { |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 90 | REPORTER_ASSERT(reporter, GrMipMapped::kNo == texture->texturePriv().mipMapped()); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 91 | } |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 92 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 97 | // Test that we correctly copy or don't copy GrBackendTextures in the GrBackendTextureImageGenerator |
| 98 | // based on if we will use mips in the draw and the mip status of the GrBackendTexture. |
| 99 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrBackendTextureImageMipMappedTest, reporter, ctxInfo) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 100 | GrContext* context = ctxInfo.grContext(); |
| 101 | if (!context->caps()->mipMapSupport()) { |
| 102 | return; |
| 103 | } |
| 104 | for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) { |
| 105 | for (auto willUseMips : {false, true}) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 106 | GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture( |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 107 | nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, false, mipMapped); |
| 108 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 109 | sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backendTex, |
| 110 | kTopLeft_GrSurfaceOrigin, |
| 111 | kPremul_SkAlphaType, nullptr); |
| 112 | |
| 113 | GrTextureProxy* proxy = as_IB(image)->peekProxy(); |
| 114 | REPORTER_ASSERT(reporter, proxy); |
| 115 | if (!proxy) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 116 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
| 120 | REPORTER_ASSERT(reporter, proxy->priv().isInstantiated()); |
| 121 | |
| 122 | sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture()); |
| 123 | REPORTER_ASSERT(reporter, texture); |
| 124 | if (!texture) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 125 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
| 129 | std::unique_ptr<SkImageGenerator> imageGen = GrBackendTextureImageGenerator::Make( |
| 130 | texture, kTopLeft_GrSurfaceOrigin, nullptr, kPremul_SkAlphaType, nullptr); |
| 131 | REPORTER_ASSERT(reporter, imageGen); |
| 132 | if (!imageGen) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 133 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
| 137 | SkIPoint origin = SkIPoint::Make(0,0); |
| 138 | // The transfer function behavior isn't used in the generator so set we set it |
| 139 | // arbitrarily here. |
| 140 | SkTransferFunctionBehavior behavior = SkTransferFunctionBehavior::kIgnore; |
| 141 | SkImageInfo imageInfo = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, |
| 142 | kPremul_SkAlphaType); |
| 143 | sk_sp<GrTextureProxy> genProxy = imageGen->generateTexture(context, imageInfo, |
| 144 | origin, behavior, |
| 145 | willUseMips); |
| 146 | |
| 147 | REPORTER_ASSERT(reporter, genProxy); |
| 148 | if (!genProxy) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 149 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
| 153 | REPORTER_ASSERT(reporter, genProxy->priv().isInstantiated()); |
| 154 | |
| 155 | GrTexture* genTexture = genProxy->priv().peekTexture(); |
| 156 | REPORTER_ASSERT(reporter, genTexture); |
| 157 | if (!genTexture) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 158 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
| 162 | GrBackendObject genBackendObject = genTexture->getTextureHandle(); |
| 163 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 164 | if (kOpenGL_GrBackend == context->contextPriv().getBackend()) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 165 | const GrGLTextureInfo* origTexInfo = backendTex.getGLTextureInfo(); |
| 166 | GrGLTextureInfo* genTexInfo = (GrGLTextureInfo*)genBackendObject; |
| 167 | if (willUseMips && GrMipMapped::kNo == mipMapped) { |
| 168 | // We did a copy so the texture IDs should be different |
| 169 | REPORTER_ASSERT(reporter, origTexInfo->fID != genTexInfo->fID); |
| 170 | } else { |
| 171 | REPORTER_ASSERT(reporter, origTexInfo->fID == genTexInfo->fID); |
| 172 | } |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 173 | } else if (kVulkan_GrBackend == context->contextPriv().getBackend()) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 174 | #ifdef SK_VULKAN |
| 175 | const GrVkImageInfo* origImageInfo = backendTex.getVkImageInfo(); |
| 176 | GrVkImageInfo* genImageInfo = (GrVkImageInfo*)genBackendObject; |
| 177 | if (willUseMips && GrMipMapped::kNo == mipMapped) { |
| 178 | // We did a copy so the texture IDs should be different |
| 179 | REPORTER_ASSERT(reporter, origImageInfo->fImage != genImageInfo->fImage); |
| 180 | } else { |
| 181 | REPORTER_ASSERT(reporter, origImageInfo->fImage == genImageInfo->fImage); |
| 182 | } |
| 183 | #endif |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 184 | } else if (kMetal_GrBackend == context->contextPriv().getBackend()) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, false); |
| 186 | } else { |
| 187 | REPORTER_ASSERT(reporter, false); |
| 188 | } |
| 189 | |
| 190 | // Must make sure the uses of the backend texture have finished (we possibly have a |
| 191 | // queued up copy) before we delete the backend texture. Thus we use readPixels here |
| 192 | // just to force the synchronization. |
| 193 | sk_sp<GrSurfaceContext> surfContext = |
| 194 | context->contextPriv().makeWrappedSurfaceContext(genProxy, nullptr); |
| 195 | |
| 196 | SkBitmap bitmap; |
| 197 | bitmap.allocPixels(imageInfo); |
| 198 | surfContext->readPixels(imageInfo, bitmap.getPixels(), 0, 0, 0, 0); |
| 199 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 200 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 205 | // Test that when we call makeImageSnapshot on an SkSurface we retains the same mip status as the |
| 206 | // resource we took the snapshot of. |
| 207 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxInfo) { |
| 208 | GrContext* context = ctxInfo.grContext(); |
| 209 | if (!context->caps()->mipMapSupport()) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | for (auto willUseMips : {false, true}) { |
| 214 | for (auto isWrapped : {false, true}) { |
| 215 | GrMipMapped mipMapped = willUseMips ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 216 | sk_sp<SkSurface> surface; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 217 | GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture( |
| 218 | nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, true, mipMapped); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 219 | if (isWrapped) { |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 220 | surface = SkSurface::MakeFromBackendTexture(context, |
| 221 | backendTex, |
| 222 | kTopLeft_GrSurfaceOrigin, |
| 223 | 0, |
| 224 | nullptr, |
| 225 | nullptr); |
| 226 | } else { |
| 227 | SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, |
| 228 | kPremul_SkAlphaType); |
| 229 | surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0, |
| 230 | kTopLeft_GrSurfaceOrigin, nullptr, |
| 231 | willUseMips); |
| 232 | } |
| 233 | REPORTER_ASSERT(reporter, surface); |
| 234 | if (!surface) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 235 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 236 | } |
| 237 | SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice(); |
| 238 | GrTextureProxy* texProxy = device->accessRenderTargetContext()->asTextureProxy(); |
| 239 | REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped()); |
| 240 | |
| 241 | texProxy->instantiate(context->resourceProvider()); |
| 242 | GrTexture* texture = texProxy->priv().peekTexture(); |
| 243 | REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped()); |
| 244 | |
| 245 | sk_sp<SkImage> image = surface->makeImageSnapshot(); |
| 246 | REPORTER_ASSERT(reporter, image); |
| 247 | if (!image) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 248 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 249 | } |
| 250 | texProxy = as_IB(image)->peekProxy(); |
| 251 | REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped()); |
| 252 | |
| 253 | texProxy->instantiate(context->resourceProvider()); |
| 254 | texture = texProxy->priv().peekTexture(); |
| 255 | REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped()); |
| 256 | |
| 257 | // Must flush the context to make sure all the cmds (copies, etc.) from above are sent |
| 258 | // to the gpu before we delete the backendHandle. |
| 259 | context->flush(); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame^] | 260 | context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 264 | |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 265 | #endif |