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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkPoint.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "include/gpu/GrBackendSurface.h" |
| 14 | #include "include/gpu/GrContext.h" |
| 15 | #include "include/private/GrTextureProxy.h" |
| 16 | #include "src/gpu/GrBackendTextureImageGenerator.h" |
| 17 | #include "src/gpu/GrContextPriv.h" |
| 18 | #include "src/gpu/GrGpu.h" |
| 19 | #include "src/gpu/GrRenderTargetContext.h" |
| 20 | #include "src/gpu/GrSemaphore.h" |
| 21 | #include "src/gpu/GrSurfaceProxyPriv.h" |
| 22 | #include "src/gpu/GrTexturePriv.h" |
| 23 | #include "src/gpu/SkGpuDevice.h" |
| 24 | #include "src/image/SkImage_Base.h" |
| 25 | #include "src/image/SkSurface_Gpu.h" |
| 26 | #include "tests/Test.h" |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 27 | |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 28 | static constexpr int kSize = 8; |
| 29 | |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 30 | // Test that the correct mip map states are on the GrTextures when wrapping GrBackendTextures in |
| 31 | // SkImages and SkSurfaces |
| 32 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrWrappedMipMappedTest, reporter, ctxInfo) { |
| 33 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 34 | if (!context->priv().caps()->mipMapSupport()) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 35 | return; |
| 36 | } |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 37 | |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 38 | for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 39 | for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 40 | // createBackendTexture currently doesn't support uploading data to mip maps |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 41 | // so we don't send any. However, we pretend there is data for the checks below which is |
| 42 | // fine since we are never actually using these textures for any work on the gpu. |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 43 | GrBackendTexture backendTex = context->createBackendTexture( |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 44 | kSize, kSize, kRGBA_8888_SkColorType, mipMapped, renderable); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 45 | |
Robert Phillips | e0070c0 | 2017-11-13 12:47:24 -0500 | [diff] [blame] | 46 | sk_sp<GrTextureProxy> proxy; |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 47 | sk_sp<SkImage> image; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 48 | if (GrRenderable::kYes == renderable) { |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 49 | sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture( |
| 50 | context, |
| 51 | backendTex, |
| 52 | kTopLeft_GrSurfaceOrigin, |
| 53 | 0, |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 54 | kRGBA_8888_SkColorType, |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 55 | nullptr, |
| 56 | nullptr); |
| 57 | |
| 58 | SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice(); |
Robert Phillips | e0070c0 | 2017-11-13 12:47:24 -0500 | [diff] [blame] | 59 | proxy = device->accessRenderTargetContext()->asTextureProxyRef(); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 60 | } else { |
| 61 | image = SkImage::MakeFromTexture(context, backendTex, |
| 62 | kTopLeft_GrSurfaceOrigin, |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 63 | kRGBA_8888_SkColorType, |
| 64 | kPremul_SkAlphaType, nullptr, |
| 65 | nullptr, nullptr); |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 66 | proxy = as_IB(image)->asTextureProxyRef(context); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 67 | } |
| 68 | REPORTER_ASSERT(reporter, proxy); |
| 69 | if (!proxy) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 70 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, proxy->isInstantiated()); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 75 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 76 | GrTexture* texture = proxy->peekTexture(); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, texture); |
| 78 | if (!texture) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 79 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (GrMipMapped::kYes == mipMapped) { |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, GrMipMapped::kYes == texture->texturePriv().mipMapped()); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 85 | if (GrRenderable::kYes == renderable) { |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 86 | REPORTER_ASSERT(reporter, texture->texturePriv().mipMapsAreDirty()); |
| 87 | } else { |
| 88 | REPORTER_ASSERT(reporter, !texture->texturePriv().mipMapsAreDirty()); |
| 89 | } |
| 90 | } else { |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, GrMipMapped::kNo == texture->texturePriv().mipMapped()); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 92 | } |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 93 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 98 | // Test that we correctly copy or don't copy GrBackendTextures in the GrBackendTextureImageGenerator |
| 99 | // based on if we will use mips in the draw and the mip status of the GrBackendTexture. |
| 100 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrBackendTextureImageMipMappedTest, reporter, ctxInfo) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 101 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 102 | if (!context->priv().caps()->mipMapSupport()) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 103 | return; |
| 104 | } |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 105 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 106 | for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) { |
| 107 | for (auto willUseMips : {false, true}) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 108 | GrBackendTexture backendTex = context->createBackendTexture( |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 109 | kSize, kSize, kRGBA_8888_SkColorType, mipMapped, GrRenderable::kNo); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 110 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 111 | sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backendTex, |
| 112 | kTopLeft_GrSurfaceOrigin, |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 113 | kRGBA_8888_SkColorType, |
| 114 | kPremul_SkAlphaType, nullptr, |
| 115 | nullptr, nullptr); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 116 | |
| 117 | GrTextureProxy* proxy = as_IB(image)->peekProxy(); |
| 118 | REPORTER_ASSERT(reporter, proxy); |
| 119 | if (!proxy) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 120 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, proxy->isInstantiated()); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 125 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 126 | sk_sp<GrTexture> texture = sk_ref_sp(proxy->peekTexture()); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 127 | REPORTER_ASSERT(reporter, texture); |
| 128 | if (!texture) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 129 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
| 133 | std::unique_ptr<SkImageGenerator> imageGen = GrBackendTextureImageGenerator::Make( |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 134 | texture, kTopLeft_GrSurfaceOrigin, nullptr, kRGBA_8888_SkColorType, |
| 135 | kPremul_SkAlphaType, nullptr); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 136 | REPORTER_ASSERT(reporter, imageGen); |
| 137 | if (!imageGen) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 138 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
| 142 | SkIPoint origin = SkIPoint::Make(0,0); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 143 | SkImageInfo imageInfo = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, |
| 144 | kPremul_SkAlphaType); |
| 145 | sk_sp<GrTextureProxy> genProxy = imageGen->generateTexture(context, imageInfo, |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 146 | origin, willUseMips); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 147 | |
| 148 | REPORTER_ASSERT(reporter, genProxy); |
| 149 | if (!genProxy) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 150 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 154 | if (GrSurfaceProxy::LazyState::kNot != genProxy->lazyInstantiationState()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 155 | genProxy->priv().doLazyInstantiation(context->priv().resourceProvider()); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 156 | } else if (!genProxy->isInstantiated()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 157 | genProxy->instantiate(context->priv().resourceProvider()); |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 160 | REPORTER_ASSERT(reporter, genProxy->isInstantiated()); |
| 161 | if (!genProxy->isInstantiated()) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 162 | context->deleteBackendTexture(backendTex); |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 163 | return; |
| 164 | } |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 165 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 166 | GrTexture* genTexture = genProxy->peekTexture(); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 167 | REPORTER_ASSERT(reporter, genTexture); |
| 168 | if (!genTexture) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 169 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 173 | GrBackendTexture genBackendTex = genTexture->getBackendTexture(); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 174 | |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 175 | if (GrBackendApi::kOpenGL == genBackendTex.backend()) { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 176 | GrGLTextureInfo genTexInfo; |
| 177 | GrGLTextureInfo origTexInfo; |
| 178 | if (genBackendTex.getGLTextureInfo(&genTexInfo) && |
| 179 | backendTex.getGLTextureInfo(&origTexInfo)) { |
| 180 | if (willUseMips && GrMipMapped::kNo == mipMapped) { |
| 181 | // We did a copy so the texture IDs should be different |
| 182 | REPORTER_ASSERT(reporter, origTexInfo.fID != genTexInfo.fID); |
| 183 | } else { |
| 184 | REPORTER_ASSERT(reporter, origTexInfo.fID == genTexInfo.fID); |
| 185 | } |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 186 | } else { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 187 | ERRORF(reporter, "Failed to get GrGLTextureInfo"); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 188 | } |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 189 | #ifdef SK_VULKAN |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 190 | } else if (GrBackendApi::kVulkan == genBackendTex.backend()) { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 191 | GrVkImageInfo genImageInfo; |
| 192 | GrVkImageInfo origImageInfo; |
| 193 | if (genBackendTex.getVkImageInfo(&genImageInfo) && |
| 194 | backendTex.getVkImageInfo(&origImageInfo)) { |
| 195 | if (willUseMips && GrMipMapped::kNo == mipMapped) { |
| 196 | // We did a copy so the texture IDs should be different |
| 197 | REPORTER_ASSERT(reporter, origImageInfo.fImage != genImageInfo.fImage); |
| 198 | } else { |
| 199 | REPORTER_ASSERT(reporter, origImageInfo.fImage == genImageInfo.fImage); |
| 200 | } |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 201 | } else { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 202 | ERRORF(reporter, "Failed to get GrVkImageInfo"); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 203 | } |
| 204 | #endif |
Jim Van Verth | 9896a0d | 2019-04-10 15:11:12 -0400 | [diff] [blame] | 205 | #ifdef SK_METAL |
| 206 | } else if (GrBackendApi::kMetal == genBackendTex.backend()) { |
| 207 | GrMtlTextureInfo genImageInfo; |
| 208 | GrMtlTextureInfo origImageInfo; |
| 209 | if (genBackendTex.getMtlTextureInfo(&genImageInfo) && |
| 210 | backendTex.getMtlTextureInfo(&origImageInfo)) { |
| 211 | if (willUseMips && GrMipMapped::kNo == mipMapped) { |
| 212 | // We did a copy so the texture IDs should be different |
| 213 | REPORTER_ASSERT(reporter, origImageInfo.fTexture != genImageInfo.fTexture); |
| 214 | } else { |
| 215 | REPORTER_ASSERT(reporter, origImageInfo.fTexture == genImageInfo.fTexture); |
| 216 | } |
| 217 | } else { |
| 218 | ERRORF(reporter, "Failed to get GrMtlTextureInfo"); |
| 219 | } |
| 220 | #endif |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 221 | } else { |
| 222 | REPORTER_ASSERT(reporter, false); |
| 223 | } |
| 224 | |
| 225 | // Must make sure the uses of the backend texture have finished (we possibly have a |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 226 | // queued up copy) before we delete the backend texture. |
| 227 | context->flush(); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 228 | |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 229 | context->priv().getGpu()->testingOnly_flushGpuAndSync(); |
| 230 | |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 231 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 236 | // Test that when we call makeImageSnapshot on an SkSurface we retains the same mip status as the |
| 237 | // resource we took the snapshot of. |
| 238 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxInfo) { |
| 239 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 240 | if (!context->priv().caps()->mipMapSupport()) { |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 244 | auto resourceProvider = context->priv().resourceProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 245 | |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 246 | for (auto willUseMips : {false, true}) { |
| 247 | for (auto isWrapped : {false, true}) { |
| 248 | GrMipMapped mipMapped = willUseMips ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 249 | sk_sp<SkSurface> surface; |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 250 | GrBackendTexture backendTex = context->createBackendTexture( |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 251 | kSize, kSize, kRGBA_8888_SkColorType, mipMapped, GrRenderable::kYes); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 252 | if (isWrapped) { |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 253 | surface = SkSurface::MakeFromBackendTexture(context, |
| 254 | backendTex, |
| 255 | kTopLeft_GrSurfaceOrigin, |
| 256 | 0, |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 257 | kRGBA_8888_SkColorType, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 258 | nullptr, |
| 259 | nullptr); |
| 260 | } else { |
| 261 | SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, |
| 262 | kPremul_SkAlphaType); |
| 263 | surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0, |
| 264 | kTopLeft_GrSurfaceOrigin, nullptr, |
| 265 | willUseMips); |
| 266 | } |
| 267 | REPORTER_ASSERT(reporter, surface); |
| 268 | if (!surface) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 269 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 270 | } |
| 271 | SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice(); |
| 272 | GrTextureProxy* texProxy = device->accessRenderTargetContext()->asTextureProxy(); |
| 273 | REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped()); |
| 274 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 275 | texProxy->instantiate(resourceProvider); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 276 | GrTexture* texture = texProxy->peekTexture(); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 277 | REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped()); |
| 278 | |
| 279 | sk_sp<SkImage> image = surface->makeImageSnapshot(); |
| 280 | REPORTER_ASSERT(reporter, image); |
| 281 | if (!image) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 282 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 283 | } |
| 284 | texProxy = as_IB(image)->peekProxy(); |
| 285 | REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped()); |
| 286 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 287 | texProxy->instantiate(resourceProvider); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 288 | texture = texProxy->peekTexture(); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 289 | REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped()); |
| 290 | |
| 291 | // Must flush the context to make sure all the cmds (copies, etc.) from above are sent |
| 292 | // to the gpu before we delete the backendHandle. |
| 293 | context->flush(); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 294 | context->priv().getGpu()->testingOnly_flushGpuAndSync(); |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 295 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
Greg Daniel | 8e9b4c4 | 2018-07-20 10:30:48 -0400 | [diff] [blame] | 299 | |
| 300 | // Test that we don't create a mip mapped texture if the size is 1x1 even if the filter mode is set |
| 301 | // to use mips. This test passes by not crashing or hitting asserts in code. |
| 302 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(Gr1x1TextureMipMappedTest, reporter, ctxInfo) { |
| 303 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 304 | if (!context->priv().caps()->mipMapSupport()) { |
Greg Daniel | 8e9b4c4 | 2018-07-20 10:30:48 -0400 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
| 308 | // Make surface to draw into |
| 309 | SkImageInfo info = SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType); |
| 310 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
| 311 | |
| 312 | // Make 1x1 raster bitmap |
| 313 | SkBitmap bmp; |
| 314 | bmp.allocN32Pixels(1, 1); |
| 315 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(bmp.getPixels()); |
| 316 | *pixel = 0; |
| 317 | |
| 318 | sk_sp<SkImage> bmpImage = SkImage::MakeFromBitmap(bmp); |
| 319 | |
| 320 | // Make sure we scale so we don't optimize out the use of mips. |
| 321 | surface->getCanvas()->scale(0.5f, 0.5f); |
| 322 | |
| 323 | SkPaint paint; |
| 324 | // This should upload the image to a non mipped GrTextureProxy. |
| 325 | surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint); |
| 326 | surface->flush(); |
| 327 | |
| 328 | // Now set the filter quality to high so we use mip maps. We should find the non mipped texture |
| 329 | // in the cache for the SkImage. Since the texture is 1x1 we should just use that texture |
| 330 | // instead of trying to do a copy to a mipped texture. |
| 331 | paint.setFilterQuality(kHigh_SkFilterQuality); |
| 332 | surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint); |
| 333 | surface->flush(); |
| 334 | } |
| 335 | |