blob: fc29d828021a4a685b647dbf03934e3232a757af [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 Phillipse0070c02017-11-13 12:47:24 -050067 proxy = as_IB(image)->asTextureProxyRef();
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
Greg Daniel261b8aa2017-10-23 09:37:36 -0400207 } else {
208 REPORTER_ASSERT(reporter, false);
209 }
210
211 // Must make sure the uses of the backend texture have finished (we possibly have a
Greg Daniel26b50a42018-03-08 09:49:58 -0500212 // queued up copy) before we delete the backend texture.
213 context->flush();
214 gpu->testingOnly_flushGpuAndSync();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400215
Brian Salomon26102cb2018-03-09 09:33:19 -0500216 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400217 }
218 }
219}
220
Greg Daniel45d63032017-10-30 13:41:26 -0400221// Test that when we call makeImageSnapshot on an SkSurface we retains the same mip status as the
222// resource we took the snapshot of.
223DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxInfo) {
224 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500225 if (!context->priv().caps()->mipMapSupport()) {
Greg Daniel45d63032017-10-30 13:41:26 -0400226 return;
227 }
228
Robert Phillips9da87e02019-02-04 13:26:26 -0500229 auto resourceProvider = context->priv().resourceProvider();
230 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6be756b2018-01-16 15:07:54 -0500231
Greg Daniel45d63032017-10-30 13:41:26 -0400232 for (auto willUseMips : {false, true}) {
233 for (auto isWrapped : {false, true}) {
234 GrMipMapped mipMapped = willUseMips ? GrMipMapped::kYes : GrMipMapped::kNo;
235 sk_sp<SkSurface> surface;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500236 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -0400237 nullptr, kSize, kSize, GrColorType::kRGBA_8888, true, mipMapped);
Greg Daniel45d63032017-10-30 13:41:26 -0400238 if (isWrapped) {
Greg Daniel45d63032017-10-30 13:41:26 -0400239 surface = SkSurface::MakeFromBackendTexture(context,
240 backendTex,
241 kTopLeft_GrSurfaceOrigin,
242 0,
Greg Danielfaa095e2017-12-19 13:15:02 -0500243 kRGBA_8888_SkColorType,
Greg Daniel45d63032017-10-30 13:41:26 -0400244 nullptr,
245 nullptr);
246 } else {
247 SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
248 kPremul_SkAlphaType);
249 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0,
250 kTopLeft_GrSurfaceOrigin, nullptr,
251 willUseMips);
252 }
253 REPORTER_ASSERT(reporter, surface);
254 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500255 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400256 }
257 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
258 GrTextureProxy* texProxy = device->accessRenderTargetContext()->asTextureProxy();
259 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped());
260
Robert Phillips6be756b2018-01-16 15:07:54 -0500261 texProxy->instantiate(resourceProvider);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400262 GrTexture* texture = texProxy->peekTexture();
Greg Daniel45d63032017-10-30 13:41:26 -0400263 REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped());
264
265 sk_sp<SkImage> image = surface->makeImageSnapshot();
266 REPORTER_ASSERT(reporter, image);
267 if (!image) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500268 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400269 }
270 texProxy = as_IB(image)->peekProxy();
271 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped());
272
Robert Phillips6be756b2018-01-16 15:07:54 -0500273 texProxy->instantiate(resourceProvider);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400274 texture = texProxy->peekTexture();
Greg Daniel45d63032017-10-30 13:41:26 -0400275 REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped());
276
277 // Must flush the context to make sure all the cmds (copies, etc.) from above are sent
278 // to the gpu before we delete the backendHandle.
279 context->flush();
Greg Danield2d367d2018-04-16 10:24:45 -0400280 gpu->testingOnly_flushGpuAndSync();
Brian Salomon26102cb2018-03-09 09:33:19 -0500281 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400282 }
283 }
284}
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400285
286// Test that we don't create a mip mapped texture if the size is 1x1 even if the filter mode is set
287// to use mips. This test passes by not crashing or hitting asserts in code.
288DEF_GPUTEST_FOR_RENDERING_CONTEXTS(Gr1x1TextureMipMappedTest, reporter, ctxInfo) {
289 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500290 if (!context->priv().caps()->mipMapSupport()) {
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400291 return;
292 }
293
294 // Make surface to draw into
295 SkImageInfo info = SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType);
296 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
297
298 // Make 1x1 raster bitmap
299 SkBitmap bmp;
300 bmp.allocN32Pixels(1, 1);
301 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(bmp.getPixels());
302 *pixel = 0;
303
304 sk_sp<SkImage> bmpImage = SkImage::MakeFromBitmap(bmp);
305
306 // Make sure we scale so we don't optimize out the use of mips.
307 surface->getCanvas()->scale(0.5f, 0.5f);
308
309 SkPaint paint;
310 // This should upload the image to a non mipped GrTextureProxy.
311 surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint);
312 surface->flush();
313
314 // Now set the filter quality to high so we use mip maps. We should find the non mipped texture
315 // in the cache for the SkImage. Since the texture is 1x1 we should just use that texture
316 // instead of trying to do a copy to a mipped texture.
317 paint.setFilterQuality(kHigh_SkFilterQuality);
318 surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint);
319 surface->flush();
320}
321