blob: 7e26a447e87b156fd41377a38de48fe184efe2f5 [file] [log] [blame]
Greg Daniel177e6952017-10-12 12:27:11 -04001/*
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
Greg Daniel177e6952017-10-12 12:27:11 -040010#include "GrBackendSurface.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040011#include "GrBackendTextureImageGenerator.h"
Greg Daniel177e6952017-10-12 12:27:11 -040012#include "GrContext.h"
13#include "GrContextPriv.h"
14#include "GrGpu.h"
15#include "GrRenderTargetContext.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040016#include "GrSemaphore.h"
Greg Daniel177e6952017-10-12 12:27:11 -040017#include "GrSurfaceProxyPriv.h"
Greg Daniel177e6952017-10-12 12:27:11 -040018#include "GrTexturePriv.h"
19#include "GrTextureProxy.h"
20#include "SkCanvas.h"
21#include "SkImage_Base.h"
22#include "SkGpuDevice.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040023#include "SkPoint.h"
Greg Daniel177e6952017-10-12 12:27:11 -040024#include "SkSurface.h"
25#include "SkSurface_Gpu.h"
26#include "Test.h"
27
Greg Daniel45d63032017-10-30 13:41:26 -040028static constexpr int kSize = 8;
29
Greg Daniel177e6952017-10-12 12:27:11 -040030// Test that the correct mip map states are on the GrTextures when wrapping GrBackendTextures in
31// SkImages and SkSurfaces
32DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrWrappedMipMappedTest, reporter, ctxInfo) {
33 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050034 if (!context->priv().caps()->mipMapSupport()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -040035 return;
36 }
Robert Phillips9da87e02019-02-04 13:26:26 -050037 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050038
Greg Daniel177e6952017-10-12 12:27:11 -040039 for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) {
40 for (auto isRT : {false, true}) {
41 // CreateTestingOnlyBackendTexture currently doesn't support uploading data to mip maps
42 // so we don't send any. However, we pretend there is data for the checks below which is
43 // fine since we are never actually using these textures for any work on the gpu.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050044 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -040045 nullptr, kSize, kSize, GrColorType::kRGBA_8888, isRT, mipMapped);
Greg Daniel177e6952017-10-12 12:27:11 -040046
Robert Phillipse0070c02017-11-13 12:47:24 -050047 sk_sp<GrTextureProxy> proxy;
Greg Daniel177e6952017-10-12 12:27:11 -040048 sk_sp<SkImage> image;
49 if (isRT) {
50 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(
51 context,
52 backendTex,
53 kTopLeft_GrSurfaceOrigin,
54 0,
Greg Danielfaa095e2017-12-19 13:15:02 -050055 kRGBA_8888_SkColorType,
Greg Daniel177e6952017-10-12 12:27:11 -040056 nullptr,
57 nullptr);
58
59 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
Robert Phillipse0070c02017-11-13 12:47:24 -050060 proxy = device->accessRenderTargetContext()->asTextureProxyRef();
Greg Daniel177e6952017-10-12 12:27:11 -040061 } else {
62 image = SkImage::MakeFromTexture(context, backendTex,
63 kTopLeft_GrSurfaceOrigin,
Greg Danielf5d87582017-12-18 14:48:15 -050064 kRGBA_8888_SkColorType,
65 kPremul_SkAlphaType, nullptr,
66 nullptr, nullptr);
Robert Phillips6603a172019-03-05 12:35:44 -050067 proxy = as_IB(image)->asTextureProxyRef(context);
Greg Daniel177e6952017-10-12 12:27:11 -040068 }
69 REPORTER_ASSERT(reporter, proxy);
70 if (!proxy) {
Brian Salomon26102cb2018-03-09 09:33:19 -050071 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040072 return;
73 }
74
Brian Salomonfd98c2c2018-07-31 17:25:29 -040075 REPORTER_ASSERT(reporter, proxy->isInstantiated());
Greg Daniel177e6952017-10-12 12:27:11 -040076
Brian Salomonfd98c2c2018-07-31 17:25:29 -040077 GrTexture* texture = proxy->peekTexture();
Greg Daniel177e6952017-10-12 12:27:11 -040078 REPORTER_ASSERT(reporter, texture);
79 if (!texture) {
Brian Salomon26102cb2018-03-09 09:33:19 -050080 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040081 return;
82 }
83
84 if (GrMipMapped::kYes == mipMapped) {
Greg Daniele252f082017-10-23 16:05:23 -040085 REPORTER_ASSERT(reporter, GrMipMapped::kYes == texture->texturePriv().mipMapped());
Greg Daniel177e6952017-10-12 12:27:11 -040086 if (isRT) {
87 REPORTER_ASSERT(reporter, texture->texturePriv().mipMapsAreDirty());
88 } else {
89 REPORTER_ASSERT(reporter, !texture->texturePriv().mipMapsAreDirty());
90 }
91 } else {
Greg Daniele252f082017-10-23 16:05:23 -040092 REPORTER_ASSERT(reporter, GrMipMapped::kNo == texture->texturePriv().mipMapped());
Greg Daniel177e6952017-10-12 12:27:11 -040093 }
Brian Salomon26102cb2018-03-09 09:33:19 -050094 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040095 }
96 }
97}
98
Greg Daniel261b8aa2017-10-23 09:37:36 -040099// Test that we correctly copy or don't copy GrBackendTextures in the GrBackendTextureImageGenerator
100// based on if we will use mips in the draw and the mip status of the GrBackendTexture.
101DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrBackendTextureImageMipMappedTest, reporter, ctxInfo) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400102 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500103 if (!context->priv().caps()->mipMapSupport()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400104 return;
105 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500106 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500107
Greg Daniel261b8aa2017-10-23 09:37:36 -0400108 for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) {
109 for (auto willUseMips : {false, true}) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500110 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -0400111 nullptr, kSize, kSize, GrColorType::kRGBA_8888, false, mipMapped);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400112
Greg Daniel261b8aa2017-10-23 09:37:36 -0400113 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backendTex,
114 kTopLeft_GrSurfaceOrigin,
Greg Danielf5d87582017-12-18 14:48:15 -0500115 kRGBA_8888_SkColorType,
116 kPremul_SkAlphaType, nullptr,
117 nullptr, nullptr);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400118
119 GrTextureProxy* proxy = as_IB(image)->peekProxy();
120 REPORTER_ASSERT(reporter, proxy);
121 if (!proxy) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500122 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400123 return;
124 }
125
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400126 REPORTER_ASSERT(reporter, proxy->isInstantiated());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400127
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400128 sk_sp<GrTexture> texture = sk_ref_sp(proxy->peekTexture());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400129 REPORTER_ASSERT(reporter, texture);
130 if (!texture) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500131 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400132 return;
133 }
134
135 std::unique_ptr<SkImageGenerator> imageGen = GrBackendTextureImageGenerator::Make(
Brian Osman052ef692018-03-27 09:56:31 -0400136 texture, kTopLeft_GrSurfaceOrigin, nullptr, kRGBA_8888_SkColorType,
137 kPremul_SkAlphaType, nullptr);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400138 REPORTER_ASSERT(reporter, imageGen);
139 if (!imageGen) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500140 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400141 return;
142 }
143
144 SkIPoint origin = SkIPoint::Make(0,0);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400145 SkImageInfo imageInfo = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
146 kPremul_SkAlphaType);
147 sk_sp<GrTextureProxy> genProxy = imageGen->generateTexture(context, imageInfo,
Brian Osmanc87cfb62018-07-11 09:08:46 -0400148 origin, willUseMips);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400149
150 REPORTER_ASSERT(reporter, genProxy);
151 if (!genProxy) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500152 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400153 return;
154 }
155
Greg Daniele728f672018-01-17 10:52:04 -0500156 if (GrSurfaceProxy::LazyState::kNot != genProxy->lazyInstantiationState()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500157 genProxy->priv().doLazyInstantiation(context->priv().resourceProvider());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400158 } else if (!genProxy->isInstantiated()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500159 genProxy->instantiate(context->priv().resourceProvider());
Greg Daniele728f672018-01-17 10:52:04 -0500160 }
161
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400162 REPORTER_ASSERT(reporter, genProxy->isInstantiated());
163 if (!genProxy->isInstantiated()) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500164 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Danielbddcc952018-01-24 13:22:24 -0500165 return;
166 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400167
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400168 GrTexture* genTexture = genProxy->peekTexture();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400169 REPORTER_ASSERT(reporter, genTexture);
170 if (!genTexture) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500171 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400172 return;
173 }
174
Robert Phillipsb67821d2017-12-13 15:00:45 -0500175 GrBackendTexture genBackendTex = genTexture->getBackendTexture();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400176
Greg Danielbdf12ad2018-10-12 09:31:11 -0400177 if (GrBackendApi::kOpenGL == genBackendTex.backend()) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400178 GrGLTextureInfo genTexInfo;
179 GrGLTextureInfo origTexInfo;
180 if (genBackendTex.getGLTextureInfo(&genTexInfo) &&
181 backendTex.getGLTextureInfo(&origTexInfo)) {
182 if (willUseMips && GrMipMapped::kNo == mipMapped) {
183 // We did a copy so the texture IDs should be different
184 REPORTER_ASSERT(reporter, origTexInfo.fID != genTexInfo.fID);
185 } else {
186 REPORTER_ASSERT(reporter, origTexInfo.fID == genTexInfo.fID);
187 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400188 } else {
Greg Daniel52e16d92018-04-10 09:34:07 -0400189 ERRORF(reporter, "Failed to get GrGLTextureInfo");
Greg Daniel261b8aa2017-10-23 09:37:36 -0400190 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400191#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400192 } else if (GrBackendApi::kVulkan == genBackendTex.backend()) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400193 GrVkImageInfo genImageInfo;
194 GrVkImageInfo origImageInfo;
195 if (genBackendTex.getVkImageInfo(&genImageInfo) &&
196 backendTex.getVkImageInfo(&origImageInfo)) {
197 if (willUseMips && GrMipMapped::kNo == mipMapped) {
198 // We did a copy so the texture IDs should be different
199 REPORTER_ASSERT(reporter, origImageInfo.fImage != genImageInfo.fImage);
200 } else {
201 REPORTER_ASSERT(reporter, origImageInfo.fImage == genImageInfo.fImage);
202 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400203 } else {
Greg Daniel52e16d92018-04-10 09:34:07 -0400204 ERRORF(reporter, "Failed to get GrVkImageInfo");
Greg Daniel261b8aa2017-10-23 09:37:36 -0400205 }
206#endif
Jim Van Verth9896a0d2019-04-10 15:11:12 -0400207#ifdef SK_METAL
208 } else if (GrBackendApi::kMetal == genBackendTex.backend()) {
209 GrMtlTextureInfo genImageInfo;
210 GrMtlTextureInfo origImageInfo;
211 if (genBackendTex.getMtlTextureInfo(&genImageInfo) &&
212 backendTex.getMtlTextureInfo(&origImageInfo)) {
213 if (willUseMips && GrMipMapped::kNo == mipMapped) {
214 // We did a copy so the texture IDs should be different
215 REPORTER_ASSERT(reporter, origImageInfo.fTexture != genImageInfo.fTexture);
216 } else {
217 REPORTER_ASSERT(reporter, origImageInfo.fTexture == genImageInfo.fTexture);
218 }
219 } else {
220 ERRORF(reporter, "Failed to get GrMtlTextureInfo");
221 }
222#endif
Greg Daniel261b8aa2017-10-23 09:37:36 -0400223 } else {
224 REPORTER_ASSERT(reporter, false);
225 }
226
227 // Must make sure the uses of the backend texture have finished (we possibly have a
Greg Daniel26b50a42018-03-08 09:49:58 -0500228 // queued up copy) before we delete the backend texture.
229 context->flush();
230 gpu->testingOnly_flushGpuAndSync();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400231
Brian Salomon26102cb2018-03-09 09:33:19 -0500232 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400233 }
234 }
235}
236
Greg Daniel45d63032017-10-30 13:41:26 -0400237// Test that when we call makeImageSnapshot on an SkSurface we retains the same mip status as the
238// resource we took the snapshot of.
239DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxInfo) {
240 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500241 if (!context->priv().caps()->mipMapSupport()) {
Greg Daniel45d63032017-10-30 13:41:26 -0400242 return;
243 }
244
Robert Phillips9da87e02019-02-04 13:26:26 -0500245 auto resourceProvider = context->priv().resourceProvider();
246 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6be756b2018-01-16 15:07:54 -0500247
Greg Daniel45d63032017-10-30 13:41:26 -0400248 for (auto willUseMips : {false, true}) {
249 for (auto isWrapped : {false, true}) {
250 GrMipMapped mipMapped = willUseMips ? GrMipMapped::kYes : GrMipMapped::kNo;
251 sk_sp<SkSurface> surface;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500252 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -0400253 nullptr, kSize, kSize, GrColorType::kRGBA_8888, true, mipMapped);
Greg Daniel45d63032017-10-30 13:41:26 -0400254 if (isWrapped) {
Greg Daniel45d63032017-10-30 13:41:26 -0400255 surface = SkSurface::MakeFromBackendTexture(context,
256 backendTex,
257 kTopLeft_GrSurfaceOrigin,
258 0,
Greg Danielfaa095e2017-12-19 13:15:02 -0500259 kRGBA_8888_SkColorType,
Greg Daniel45d63032017-10-30 13:41:26 -0400260 nullptr,
261 nullptr);
262 } else {
263 SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
264 kPremul_SkAlphaType);
265 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0,
266 kTopLeft_GrSurfaceOrigin, nullptr,
267 willUseMips);
268 }
269 REPORTER_ASSERT(reporter, surface);
270 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500271 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400272 }
273 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
274 GrTextureProxy* texProxy = device->accessRenderTargetContext()->asTextureProxy();
275 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped());
276
Robert Phillips6be756b2018-01-16 15:07:54 -0500277 texProxy->instantiate(resourceProvider);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400278 GrTexture* texture = texProxy->peekTexture();
Greg Daniel45d63032017-10-30 13:41:26 -0400279 REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped());
280
281 sk_sp<SkImage> image = surface->makeImageSnapshot();
282 REPORTER_ASSERT(reporter, image);
283 if (!image) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500284 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400285 }
286 texProxy = as_IB(image)->peekProxy();
287 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped());
288
Robert Phillips6be756b2018-01-16 15:07:54 -0500289 texProxy->instantiate(resourceProvider);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400290 texture = texProxy->peekTexture();
Greg Daniel45d63032017-10-30 13:41:26 -0400291 REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped());
292
293 // Must flush the context to make sure all the cmds (copies, etc.) from above are sent
294 // to the gpu before we delete the backendHandle.
295 context->flush();
Greg Danield2d367d2018-04-16 10:24:45 -0400296 gpu->testingOnly_flushGpuAndSync();
Brian Salomon26102cb2018-03-09 09:33:19 -0500297 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400298 }
299 }
300}
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400301
302// Test that we don't create a mip mapped texture if the size is 1x1 even if the filter mode is set
303// to use mips. This test passes by not crashing or hitting asserts in code.
304DEF_GPUTEST_FOR_RENDERING_CONTEXTS(Gr1x1TextureMipMappedTest, reporter, ctxInfo) {
305 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500306 if (!context->priv().caps()->mipMapSupport()) {
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400307 return;
308 }
309
310 // Make surface to draw into
311 SkImageInfo info = SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType);
312 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
313
314 // Make 1x1 raster bitmap
315 SkBitmap bmp;
316 bmp.allocN32Pixels(1, 1);
317 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(bmp.getPixels());
318 *pixel = 0;
319
320 sk_sp<SkImage> bmpImage = SkImage::MakeFromBitmap(bmp);
321
322 // Make sure we scale so we don't optimize out the use of mips.
323 surface->getCanvas()->scale(0.5f, 0.5f);
324
325 SkPaint paint;
326 // This should upload the image to a non mipped GrTextureProxy.
327 surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint);
328 surface->flush();
329
330 // Now set the filter quality to high so we use mip maps. We should find the non mipped texture
331 // in the cache for the SkImage. Since the texture is 1x1 we should just use that texture
332 // instead of trying to do a copy to a mipped texture.
333 paint.setFilterQuality(kHigh_SkFilterQuality);
334 surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint);
335 surface->flush();
336}
337