blob: 104449c2c8462ad4c358b99ec6e3d9e702838c49 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Greg Daniel177e6952017-10-12 12:27:11 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkPoint.h"
12#include "include/core/SkSurface.h"
13#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040014#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrBackendTextureImageGenerator.h"
Adlai Hollera0693042020-10-14 11:23:11 -040016#include "src/gpu/GrDirectContextPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060017#include "src/gpu/GrDrawingManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrGpu.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040019#include "src/gpu/GrProxyProvider.h"
Greg Daniel47c20e82020-01-21 14:29:57 -050020#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRenderTargetContext.h"
22#include "src/gpu/GrSemaphore.h"
23#include "src/gpu/GrSurfaceProxyPriv.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040024#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/SkGpuDevice.h"
27#include "src/image/SkImage_Base.h"
28#include "src/image/SkSurface_Gpu.h"
29#include "tests/Test.h"
Greg Danielc1ad77c2020-05-06 11:40:03 -040030#include "tests/TestUtils.h"
Brian Salomon72050802020-10-12 20:45:06 -040031#include "tools/gpu/BackendSurfaceFactory.h"
32#include "tools/gpu/BackendTextureImageFactory.h"
33#include "tools/gpu/ManagedBackendTexture.h"
Greg Daniel177e6952017-10-12 12:27:11 -040034
Greg Daniel45d63032017-10-30 13:41:26 -040035static constexpr int kSize = 8;
36
Greg Daniel177e6952017-10-12 12:27:11 -040037// Test that the correct mip map states are on the GrTextures when wrapping GrBackendTextures in
38// SkImages and SkSurfaces
39DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrWrappedMipMappedTest, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +000040 auto dContext = ctxInfo.directContext();
41 if (!dContext->priv().caps()->mipmapSupport()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -040042 return;
43 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050044
Brian Salomon7e67dca2020-07-21 09:27:25 -040045 for (auto mipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040046 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Robert Phillips9b16f812019-05-17 10:01:21 -040047 // createBackendTexture currently doesn't support uploading data to mip maps
Greg Daniel177e6952017-10-12 12:27:11 -040048 // so we don't send any. However, we pretend there is data for the checks below which is
49 // fine since we are never actually using these textures for any work on the gpu.
Brian Salomon72050802020-10-12 20:45:06 -040050 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithData(dContext,
51 kSize,
52 kSize,
53 kRGBA_8888_SkColorType,
54 SkColors::kTransparent,
55 mipMapped,
56 renderable,
57 GrProtected::kNo);
Greg Daniel177e6952017-10-12 12:27:11 -040058
Robert Phillipse0070c02017-11-13 12:47:24 -050059 sk_sp<GrTextureProxy> proxy;
Greg Daniel177e6952017-10-12 12:27:11 -040060 sk_sp<SkImage> image;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040061 if (GrRenderable::kYes == renderable) {
Greg Daniel177e6952017-10-12 12:27:11 -040062 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(
Brian Salomon72050802020-10-12 20:45:06 -040063 dContext,
64 mbet->texture(),
65 kTopLeft_GrSurfaceOrigin,
66 0,
67 kRGBA_8888_SkColorType,
68 /*color space*/ nullptr,
69 /*surface props*/ nullptr,
70 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
71 mbet->releaseContext());
Greg Daniel177e6952017-10-12 12:27:11 -040072
73 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
Robert Phillipse0070c02017-11-13 12:47:24 -050074 proxy = device->accessRenderTargetContext()->asTextureProxyRef();
Greg Daniel177e6952017-10-12 12:27:11 -040075 } else {
Brian Salomon72050802020-10-12 20:45:06 -040076 image = SkImage::MakeFromTexture(dContext,
77 mbet->texture(),
Greg Daniel177e6952017-10-12 12:27:11 -040078 kTopLeft_GrSurfaceOrigin,
Greg Danielf5d87582017-12-18 14:48:15 -050079 kRGBA_8888_SkColorType,
Brian Salomon72050802020-10-12 20:45:06 -040080 kPremul_SkAlphaType,
81 /* color space */ nullptr,
82 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
83 mbet->releaseContext());
Adlai Holler14dc7912020-08-11 15:48:49 +000084 const GrSurfaceProxyView* view = as_IB(image)->view(dContext);
Greg Danielfebdedf2020-02-05 17:06:27 -050085 REPORTER_ASSERT(reporter, view);
86 if (!view) {
Brian Salomon72050802020-10-12 20:45:06 -040087 continue;
Greg Danielfebdedf2020-02-05 17:06:27 -050088 }
89 proxy = view->asTextureProxyRef();
Greg Daniel177e6952017-10-12 12:27:11 -040090 }
91 REPORTER_ASSERT(reporter, proxy);
92 if (!proxy) {
Brian Salomon72050802020-10-12 20:45:06 -040093 continue;
Greg Daniel177e6952017-10-12 12:27:11 -040094 }
95
Brian Salomonfd98c2c2018-07-31 17:25:29 -040096 REPORTER_ASSERT(reporter, proxy->isInstantiated());
Greg Daniel177e6952017-10-12 12:27:11 -040097
Brian Salomonfd98c2c2018-07-31 17:25:29 -040098 GrTexture* texture = proxy->peekTexture();
Greg Daniel177e6952017-10-12 12:27:11 -040099 REPORTER_ASSERT(reporter, texture);
100 if (!texture) {
Brian Salomon72050802020-10-12 20:45:06 -0400101 continue;
Greg Daniel177e6952017-10-12 12:27:11 -0400102 }
103
Brian Salomon7e67dca2020-07-21 09:27:25 -0400104 if (GrMipmapped::kYes == mipMapped) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400105 REPORTER_ASSERT(reporter, GrMipmapped::kYes == texture->mipmapped());
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400106 if (GrRenderable::kYes == renderable) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400107 REPORTER_ASSERT(reporter, texture->mipmapsAreDirty());
Greg Daniel177e6952017-10-12 12:27:11 -0400108 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400109 REPORTER_ASSERT(reporter, !texture->mipmapsAreDirty());
Greg Daniel177e6952017-10-12 12:27:11 -0400110 }
111 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400112 REPORTER_ASSERT(reporter, GrMipmapped::kNo == texture->mipmapped());
Greg Daniel177e6952017-10-12 12:27:11 -0400113 }
Greg Daniel177e6952017-10-12 12:27:11 -0400114 }
115 }
116}
117
Greg Daniel261b8aa2017-10-23 09:37:36 -0400118// Test that we correctly copy or don't copy GrBackendTextures in the GrBackendTextureImageGenerator
119// based on if we will use mips in the draw and the mip status of the GrBackendTexture.
120DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrBackendTextureImageMipMappedTest, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000121 auto dContext = ctxInfo.directContext();
122 if (!dContext->priv().caps()->mipmapSupport()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400123 return;
124 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500125
Brian Salomon7e67dca2020-07-21 09:27:25 -0400126 for (auto betMipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
127 for (auto requestMipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
Brian Salomon72050802020-10-12 20:45:06 -0400128 auto ii =
129 SkImageInfo::Make({kSize, kSize}, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
130 sk_sp<SkImage> image = sk_gpu_test::MakeBackendTextureImage(
131 dContext, ii, SkColors::kTransparent, betMipMapped);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400132
133 GrTextureProxy* proxy = as_IB(image)->peekProxy();
134 REPORTER_ASSERT(reporter, proxy);
135 if (!proxy) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400136 return;
137 }
138
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400139 REPORTER_ASSERT(reporter, proxy->isInstantiated());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400140
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400141 sk_sp<GrTexture> texture = sk_ref_sp(proxy->peekTexture());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400142 REPORTER_ASSERT(reporter, texture);
143 if (!texture) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400144 return;
145 }
146
147 std::unique_ptr<SkImageGenerator> imageGen = GrBackendTextureImageGenerator::Make(
Brian Osman052ef692018-03-27 09:56:31 -0400148 texture, kTopLeft_GrSurfaceOrigin, nullptr, kRGBA_8888_SkColorType,
149 kPremul_SkAlphaType, nullptr);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400150 REPORTER_ASSERT(reporter, imageGen);
151 if (!imageGen) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400152 return;
153 }
154
155 SkIPoint origin = SkIPoint::Make(0,0);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400156 SkImageInfo imageInfo = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
157 kPremul_SkAlphaType);
Brian Salomonbc074a62020-03-18 10:06:13 -0400158 GrSurfaceProxyView genView = imageGen->generateTexture(
Adlai Holler14dc7912020-08-11 15:48:49 +0000159 dContext, imageInfo, origin, requestMipMapped, GrImageTexGenPolicy::kDraw);
Greg Danielcc104db2020-02-03 14:17:08 -0500160 GrSurfaceProxy* genProxy = genView.proxy();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400161
162 REPORTER_ASSERT(reporter, genProxy);
163 if (!genProxy) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400164 return;
165 }
166
Brian Salomonbeb7f522019-08-30 16:19:42 -0400167 if (genProxy->isLazy()) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000168 genProxy->priv().doLazyInstantiation(dContext->priv().resourceProvider());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400169 } else if (!genProxy->isInstantiated()) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000170 genProxy->instantiate(dContext->priv().resourceProvider());
Greg Daniele728f672018-01-17 10:52:04 -0500171 }
172
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400173 REPORTER_ASSERT(reporter, genProxy->isInstantiated());
174 if (!genProxy->isInstantiated()) {
Greg Danielbddcc952018-01-24 13:22:24 -0500175 return;
176 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400177
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400178 GrTexture* genTexture = genProxy->peekTexture();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400179 REPORTER_ASSERT(reporter, genTexture);
180 if (!genTexture) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400181 return;
182 }
183
Brian Salomon72050802020-10-12 20:45:06 -0400184 GrBackendTexture backendTex = texture->getBackendTexture();
Robert Phillipsb67821d2017-12-13 15:00:45 -0500185 GrBackendTexture genBackendTex = genTexture->getBackendTexture();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400186
Greg Danielbdf12ad2018-10-12 09:31:11 -0400187 if (GrBackendApi::kOpenGL == genBackendTex.backend()) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400188 GrGLTextureInfo genTexInfo;
189 GrGLTextureInfo origTexInfo;
190 if (genBackendTex.getGLTextureInfo(&genTexInfo) &&
191 backendTex.getGLTextureInfo(&origTexInfo)) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400192 if (requestMipMapped == GrMipmapped::kYes && betMipMapped == GrMipmapped::kNo) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400193 // We did a copy so the texture IDs should be different
194 REPORTER_ASSERT(reporter, origTexInfo.fID != genTexInfo.fID);
195 } else {
196 REPORTER_ASSERT(reporter, origTexInfo.fID == genTexInfo.fID);
197 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400198 } else {
Greg Daniel52e16d92018-04-10 09:34:07 -0400199 ERRORF(reporter, "Failed to get GrGLTextureInfo");
Greg Daniel261b8aa2017-10-23 09:37:36 -0400200 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400201#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400202 } else if (GrBackendApi::kVulkan == genBackendTex.backend()) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400203 GrVkImageInfo genImageInfo;
204 GrVkImageInfo origImageInfo;
205 if (genBackendTex.getVkImageInfo(&genImageInfo) &&
206 backendTex.getVkImageInfo(&origImageInfo)) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400207 if (requestMipMapped == GrMipmapped::kYes && betMipMapped == GrMipmapped::kNo) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400208 // We did a copy so the texture IDs should be different
209 REPORTER_ASSERT(reporter, origImageInfo.fImage != genImageInfo.fImage);
210 } else {
211 REPORTER_ASSERT(reporter, origImageInfo.fImage == genImageInfo.fImage);
212 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400213 } else {
Greg Daniel52e16d92018-04-10 09:34:07 -0400214 ERRORF(reporter, "Failed to get GrVkImageInfo");
Greg Daniel261b8aa2017-10-23 09:37:36 -0400215 }
216#endif
Jim Van Verth9896a0d2019-04-10 15:11:12 -0400217#ifdef SK_METAL
218 } else if (GrBackendApi::kMetal == genBackendTex.backend()) {
219 GrMtlTextureInfo genImageInfo;
220 GrMtlTextureInfo origImageInfo;
221 if (genBackendTex.getMtlTextureInfo(&genImageInfo) &&
222 backendTex.getMtlTextureInfo(&origImageInfo)) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400223 if (requestMipMapped == GrMipmapped::kYes && betMipMapped == GrMipmapped::kNo) {
Jim Van Verth9896a0d2019-04-10 15:11:12 -0400224 // We did a copy so the texture IDs should be different
225 REPORTER_ASSERT(reporter, origImageInfo.fTexture != genImageInfo.fTexture);
226 } else {
227 REPORTER_ASSERT(reporter, origImageInfo.fTexture == genImageInfo.fTexture);
228 }
229 } else {
230 ERRORF(reporter, "Failed to get GrMtlTextureInfo");
231 }
232#endif
Stephen Whitee895cdc2020-10-08 13:43:40 -0400233#ifdef SK_DAWN
234 } else if (GrBackendApi::kDawn == genBackendTex.backend()) {
235 GrDawnTextureInfo genImageInfo;
236 GrDawnTextureInfo origImageInfo;
237 if (genBackendTex.getDawnTextureInfo(&genImageInfo) &&
238 backendTex.getDawnTextureInfo(&origImageInfo)) {
239 if (requestMipMapped == GrMipmapped::kYes && betMipMapped == GrMipmapped::kNo) {
240 // We did a copy so the texture IDs should be different
241 REPORTER_ASSERT(reporter,
242 origImageInfo.fTexture.Get() != genImageInfo.fTexture.Get());
243 } else {
244 REPORTER_ASSERT(reporter,
245 origImageInfo.fTexture.Get() == genImageInfo.fTexture.Get());
246 }
247 } else {
248 ERRORF(reporter, "Failed to get GrDawnTextureInfo");
249 }
250#endif
Greg Daniel261b8aa2017-10-23 09:37:36 -0400251 } else {
252 REPORTER_ASSERT(reporter, false);
253 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400254 }
255 }
256}
257
Greg Daniel45d63032017-10-30 13:41:26 -0400258// Test that when we call makeImageSnapshot on an SkSurface we retains the same mip status as the
259// resource we took the snapshot of.
260DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000261 auto dContext = ctxInfo.directContext();
262 if (!dContext->priv().caps()->mipmapSupport()) {
Greg Daniel45d63032017-10-30 13:41:26 -0400263 return;
264 }
265
Adlai Holler14dc7912020-08-11 15:48:49 +0000266 auto resourceProvider = dContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500267
Greg Daniel45d63032017-10-30 13:41:26 -0400268 for (auto willUseMips : {false, true}) {
269 for (auto isWrapped : {false, true}) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400270 GrMipmapped mipMapped = willUseMips ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel45d63032017-10-30 13:41:26 -0400271 sk_sp<SkSurface> surface;
Brian Salomon72050802020-10-12 20:45:06 -0400272 SkImageInfo info =
273 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Greg Daniel45d63032017-10-30 13:41:26 -0400274 if (isWrapped) {
Brian Salomon72050802020-10-12 20:45:06 -0400275 surface = sk_gpu_test::MakeBackendTextureSurface(dContext,
276 info,
277 kTopLeft_GrSurfaceOrigin,
278 /* sample count */ 1,
279 mipMapped);
Greg Daniel45d63032017-10-30 13:41:26 -0400280 } else {
Brian Salomon72050802020-10-12 20:45:06 -0400281 surface = SkSurface::MakeRenderTarget(dContext,
282 SkBudgeted::kYes,
283 info,
284 /* sample count */ 1,
285 kTopLeft_GrSurfaceOrigin,
286 nullptr,
Greg Daniel45d63032017-10-30 13:41:26 -0400287 willUseMips);
288 }
289 REPORTER_ASSERT(reporter, surface);
Greg Daniel45d63032017-10-30 13:41:26 -0400290 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
291 GrTextureProxy* texProxy = device->accessRenderTargetContext()->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400292 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipmapped());
Greg Daniel45d63032017-10-30 13:41:26 -0400293
Robert Phillips6be756b2018-01-16 15:07:54 -0500294 texProxy->instantiate(resourceProvider);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400295 GrTexture* texture = texProxy->peekTexture();
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400296 REPORTER_ASSERT(reporter, mipMapped == texture->mipmapped());
Greg Daniel45d63032017-10-30 13:41:26 -0400297
298 sk_sp<SkImage> image = surface->makeImageSnapshot();
299 REPORTER_ASSERT(reporter, image);
Greg Daniel45d63032017-10-30 13:41:26 -0400300 texProxy = as_IB(image)->peekProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400301 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipmapped());
Greg Daniel45d63032017-10-30 13:41:26 -0400302
Robert Phillips6be756b2018-01-16 15:07:54 -0500303 texProxy->instantiate(resourceProvider);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400304 texture = texProxy->peekTexture();
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400305 REPORTER_ASSERT(reporter, mipMapped == texture->mipmapped());
Greg Daniel45d63032017-10-30 13:41:26 -0400306 }
307 }
308}
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400309
310// Test that we don't create a mip mapped texture if the size is 1x1 even if the filter mode is set
311// to use mips. This test passes by not crashing or hitting asserts in code.
312DEF_GPUTEST_FOR_RENDERING_CONTEXTS(Gr1x1TextureMipMappedTest, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000313 auto dContext = ctxInfo.directContext();
314 if (!dContext->priv().caps()->mipmapSupport()) {
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400315 return;
316 }
317
318 // Make surface to draw into
319 SkImageInfo info = SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType);
Adlai Holler14dc7912020-08-11 15:48:49 +0000320 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400321
322 // Make 1x1 raster bitmap
323 SkBitmap bmp;
324 bmp.allocN32Pixels(1, 1);
325 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(bmp.getPixels());
326 *pixel = 0;
327
328 sk_sp<SkImage> bmpImage = SkImage::MakeFromBitmap(bmp);
329
330 // Make sure we scale so we don't optimize out the use of mips.
331 surface->getCanvas()->scale(0.5f, 0.5f);
332
333 SkPaint paint;
334 // This should upload the image to a non mipped GrTextureProxy.
335 surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400336 surface->flushAndSubmit();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400337
338 // Now set the filter quality to high so we use mip maps. We should find the non mipped texture
339 // in the cache for the SkImage. Since the texture is 1x1 we should just use that texture
340 // instead of trying to do a copy to a mipped texture.
341 paint.setFilterQuality(kHigh_SkFilterQuality);
342 surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400343 surface->flushAndSubmit();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400344}
345
Greg Daniel40903af2020-01-30 14:55:05 -0500346// Create a new render target and draw 'mipmapView' into it using the provided 'filter'.
Brian Salomonbf6b9792019-08-21 09:38:10 -0400347static std::unique_ptr<GrRenderTargetContext> draw_mipmap_into_new_render_target(
Adlai Holler14dc7912020-08-11 15:48:49 +0000348 GrRecordingContext* rContext,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400349 GrColorType colorType,
350 SkAlphaType alphaType,
351 GrSurfaceProxyView mipmapView,
352 GrSamplerState::MipmapMode mm) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000353 auto proxyProvider = rContext->priv().proxyProvider();
Brian Salomone69b9ef2020-07-22 11:18:06 -0400354 sk_sp<GrSurfaceProxy> renderTarget =
355 proxyProvider->createProxy(mipmapView.proxy()->backendFormat(),
356 {1, 1},
357 GrRenderable::kYes,
358 1,
359 GrMipmapped::kNo,
360 SkBackingFit::kApprox,
361 SkBudgeted::kYes,
362 GrProtected::kNo);
Greg Danielba0ff782020-01-07 15:42:57 -0500363
Adlai Holler14dc7912020-08-11 15:48:49 +0000364 auto rtc = GrRenderTargetContext::Make(rContext,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400365 colorType,
366 nullptr,
367 std::move(renderTarget),
368 kTopLeft_GrSurfaceOrigin,
369 nullptr);
Greg Danielba0ff782020-01-07 15:42:57 -0500370
Brian Salomone69b9ef2020-07-22 11:18:06 -0400371 rtc->drawTexture(nullptr,
372 std::move(mipmapView),
373 alphaType,
374 GrSamplerState::Filter::kLinear,
375 mm,
376 SkBlendMode::kSrcOver,
377 {1, 1, 1, 1},
378 SkRect::MakeWH(4, 4),
379 SkRect::MakeWH(1, 1),
380 GrAA::kYes,
381 GrQuadAAFlags::kAll,
382 SkCanvas::kFast_SrcRectConstraint,
383 SkMatrix::I(),
Brian Salomonfc118442019-11-22 19:09:27 -0500384 nullptr);
Chris Dalton3d770272019-08-14 09:24:37 -0600385 return rtc;
386}
387
Greg Danielf41b2bd2019-08-22 16:19:24 -0400388// Test that two opsTasks using the same mipmaps both depend on the same GrTextureResolveRenderTask.
Chris Dalton30eea6c2019-08-21 10:22:50 -0600389DEF_GPUTEST(GrManyDependentsMipMappedTest, reporter, /* options */) {
Chris Dalton30eea6c2019-08-21 10:22:50 -0600390 using Enable = GrContextOptions::Enable;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400391 using MipmapMode = GrSamplerState::MipmapMode;
Chris Dalton30eea6c2019-08-21 10:22:50 -0600392
393 for (auto enableSortingAndReduction : {Enable::kYes, Enable::kNo}) {
394 GrMockOptions mockOptions;
Brian Salomon69100f02020-07-21 10:49:25 -0400395 mockOptions.fMipmapSupport = true;
Chris Dalton30eea6c2019-08-21 10:22:50 -0600396 GrContextOptions ctxOptions;
Greg Daniel93138742019-08-22 17:15:39 -0400397 ctxOptions.fReduceOpsTaskSplitting = enableSortingAndReduction;
Adlai Holler14dc7912020-08-11 15:48:49 +0000398 sk_sp<GrDirectContext> dContext = GrDirectContext::MakeMock(&mockOptions, ctxOptions);
399 GrDrawingManager* drawingManager = dContext->priv().drawingManager();
400 if (!dContext) {
401 ERRORF(reporter, "could not create mock dContext with fReduceOpsTaskSplitting %s.",
Chris Dalton30eea6c2019-08-21 10:22:50 -0600402 (Enable::kYes == enableSortingAndReduction) ? "enabled" : "disabled");
403 continue;
404 }
405
Adlai Holler14dc7912020-08-11 15:48:49 +0000406 SkASSERT(dContext->priv().caps()->mipmapSupport());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600407
Adlai Holler14dc7912020-08-11 15:48:49 +0000408 GrBackendFormat format = dContext->defaultBackendFormat(
Chris Dalton30eea6c2019-08-21 10:22:50 -0600409 kRGBA_8888_SkColorType, GrRenderable::kYes);
Chris Dalton30eea6c2019-08-21 10:22:50 -0600410 GrColorType colorType = GrColorType::kRGBA_8888;
Brian Salomonfc118442019-11-22 19:09:27 -0500411 SkAlphaType alphaType = kPremul_SkAlphaType;
Chris Dalton30eea6c2019-08-21 10:22:50 -0600412
Adlai Holler14dc7912020-08-11 15:48:49 +0000413 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
Chris Dalton30eea6c2019-08-21 10:22:50 -0600414
415 // Create a mipmapped render target.
Greg Daniel47c20e82020-01-21 14:29:57 -0500416
Brian Salomonbeb7f522019-08-30 16:19:42 -0400417 sk_sp<GrTextureProxy> mipmapProxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400418 format, {4, 4}, GrRenderable::kYes, 1, GrMipmapped::kYes, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400419 SkBudgeted::kYes, GrProtected::kNo);
Chris Dalton30eea6c2019-08-21 10:22:50 -0600420
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600421 // Mark the mipmaps clean to ensure things still work properly when they won't be marked
422 // dirty again until GrRenderTask::makeClosed().
Brian Salomon8c82a872020-07-21 12:09:58 -0400423 mipmapProxy->markMipmapsClean();
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600424
Greg Danielba0ff782020-01-07 15:42:57 -0500425 auto mipmapRTC = GrRenderTargetContext::Make(
Adlai Holler14dc7912020-08-11 15:48:49 +0000426 dContext.get(), colorType, nullptr, mipmapProxy, kTopLeft_GrSurfaceOrigin, nullptr);
Greg Danielba0ff782020-01-07 15:42:57 -0500427
Michael Ludwig81d41722020-05-26 16:57:38 -0400428 mipmapRTC->clear({.1f,.2f,.3f,.4f});
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400429 REPORTER_ASSERT(reporter, drawingManager->getLastRenderTask(mipmapProxy.get()));
Greg Danielf41b2bd2019-08-22 16:19:24 -0400430 // mipmapProxy's last render task should now just be the opsTask containing the clear.
Chris Dalton30eea6c2019-08-21 10:22:50 -0600431 REPORTER_ASSERT(reporter,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400432 mipmapRTC->testingOnly_PeekLastOpsTask() ==
433 drawingManager->getLastRenderTask(mipmapProxy.get()));
Chris Dalton30eea6c2019-08-21 10:22:50 -0600434
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600435 // Mipmaps don't get marked dirty until makeClosed().
Brian Salomon8c82a872020-07-21 12:09:58 -0400436 REPORTER_ASSERT(reporter, !mipmapProxy->mipmapsAreDirty());
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600437
Adlai Holler14dc7912020-08-11 15:48:49 +0000438 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(format, colorType);
Greg Daniel40903af2020-01-30 14:55:05 -0500439 GrSurfaceProxyView mipmapView(mipmapProxy, kTopLeft_GrSurfaceOrigin, swizzle);
440
Chris Dalton30eea6c2019-08-21 10:22:50 -0600441 // Draw the dirty mipmap texture into a render target.
Adlai Holler14dc7912020-08-11 15:48:49 +0000442 auto rtc1 = draw_mipmap_into_new_render_target(dContext.get(), colorType, alphaType,
443 mipmapView, MipmapMode::kLinear);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400444 auto rtc1Task = sk_ref_sp(rtc1->testingOnly_PeekLastOpsTask());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600445
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600446 // Mipmaps should have gotten marked dirty during makeClosed, then marked clean again as
447 // soon as a GrTextureResolveRenderTask was inserted. The way we know they were resolved is
Greg Danielf41b2bd2019-08-22 16:19:24 -0400448 // if mipmapProxy->getLastRenderTask() has switched from the opsTask that drew to it, to the
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600449 // task that resolved its mips.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400450 GrRenderTask* initialMipmapRegenTask = drawingManager->getLastRenderTask(mipmapProxy.get());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600451 REPORTER_ASSERT(reporter, initialMipmapRegenTask);
452 REPORTER_ASSERT(reporter,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400453 initialMipmapRegenTask != mipmapRTC->testingOnly_PeekLastOpsTask());
Brian Salomon8c82a872020-07-21 12:09:58 -0400454 REPORTER_ASSERT(reporter, !mipmapProxy->mipmapsAreDirty());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600455
456 // Draw the now-clean mipmap texture into a second target.
Adlai Holler14dc7912020-08-11 15:48:49 +0000457 auto rtc2 = draw_mipmap_into_new_render_target(dContext.get(), colorType, alphaType,
458 mipmapView, MipmapMode::kLinear);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400459 auto rtc2Task = sk_ref_sp(rtc2->testingOnly_PeekLastOpsTask());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600460
461 // Make sure the mipmap texture still has the same regen task.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400462 REPORTER_ASSERT(reporter,
463 drawingManager->getLastRenderTask(mipmapProxy.get()) == initialMipmapRegenTask);
Brian Salomon8c82a872020-07-21 12:09:58 -0400464 SkASSERT(!mipmapProxy->mipmapsAreDirty());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600465
466 // Reset everything so we can go again, this time with the first draw not mipmapped.
Adlai Holler14dc7912020-08-11 15:48:49 +0000467 dContext->flushAndSubmit();
Chris Dalton30eea6c2019-08-21 10:22:50 -0600468
Chris Daltone2a903e2019-09-18 13:41:50 -0600469 // Mip regen tasks don't get added as dependencies until makeClosed().
Brian Salomon3b8486a2020-04-21 12:43:26 -0400470 REPORTER_ASSERT(reporter, rtc1Task->dependsOn(initialMipmapRegenTask));
471 REPORTER_ASSERT(reporter, rtc2Task->dependsOn(initialMipmapRegenTask));
Chris Daltone2a903e2019-09-18 13:41:50 -0600472
Chris Dalton30eea6c2019-08-21 10:22:50 -0600473 // Render something to dirty the mips.
Michael Ludwig81d41722020-05-26 16:57:38 -0400474 mipmapRTC->clear({.1f,.2f,.3f,.4f});
Brian Salomon3b8486a2020-04-21 12:43:26 -0400475 auto mipmapRTCTask = sk_ref_sp(mipmapRTC->testingOnly_PeekLastOpsTask());
476 REPORTER_ASSERT(reporter, mipmapRTCTask);
477
Greg Danielf41b2bd2019-08-22 16:19:24 -0400478 // mipmapProxy's last render task should now just be the opsTask containing the clear.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400479 REPORTER_ASSERT(reporter,
480 mipmapRTCTask.get() == drawingManager->getLastRenderTask(mipmapProxy.get()));
Chris Dalton30eea6c2019-08-21 10:22:50 -0600481
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600482 // Mipmaps don't get marked dirty until makeClosed().
Brian Salomon8c82a872020-07-21 12:09:58 -0400483 REPORTER_ASSERT(reporter, !mipmapProxy->mipmapsAreDirty());
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600484
Chris Dalton30eea6c2019-08-21 10:22:50 -0600485 // Draw the dirty mipmap texture into a render target, but don't do mipmap filtering.
Adlai Holler14dc7912020-08-11 15:48:49 +0000486 rtc1 = draw_mipmap_into_new_render_target(dContext.get(), colorType, alphaType,
487 mipmapView, MipmapMode::kNone);
Chris Dalton30eea6c2019-08-21 10:22:50 -0600488
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600489 // Mipmaps should have gotten marked dirty during makeClosed() when adding the dependency.
490 // Since the last draw did not use mips, they will not have been regenerated and should
491 // therefore still be dirty.
Brian Salomon8c82a872020-07-21 12:09:58 -0400492 REPORTER_ASSERT(reporter, mipmapProxy->mipmapsAreDirty());
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600493
494 // Since mips weren't regenerated, the last render task shouldn't have changed.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400495 REPORTER_ASSERT(reporter,
496 mipmapRTCTask.get() == drawingManager->getLastRenderTask(mipmapProxy.get()));
Chris Dalton30eea6c2019-08-21 10:22:50 -0600497
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600498 // Draw the stil-dirty mipmap texture into a second target with mipmap filtering.
Adlai Holler14dc7912020-08-11 15:48:49 +0000499 rtc2 = draw_mipmap_into_new_render_target(dContext.get(), colorType, alphaType,
500 std::move(mipmapView), MipmapMode::kLinear);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400501 rtc2Task = sk_ref_sp(rtc2->testingOnly_PeekLastOpsTask());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600502
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600503 // Make sure the mipmap texture now has a new last render task that regenerates the mips,
504 // and that the mipmaps are now clean.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400505 auto mipRegenTask2 = drawingManager->getLastRenderTask(mipmapProxy.get());
Chris Daltone2a903e2019-09-18 13:41:50 -0600506 REPORTER_ASSERT(reporter, mipRegenTask2);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400507 REPORTER_ASSERT(reporter, mipmapRTCTask.get() != mipRegenTask2);
Brian Salomon8c82a872020-07-21 12:09:58 -0400508 SkASSERT(!mipmapProxy->mipmapsAreDirty());
Chris Daltone2a903e2019-09-18 13:41:50 -0600509
510 // Mip regen tasks don't get added as dependencies until makeClosed().
Adlai Holler14dc7912020-08-11 15:48:49 +0000511 dContext->flushAndSubmit();
Brian Salomon3b8486a2020-04-21 12:43:26 -0400512 REPORTER_ASSERT(reporter, rtc2Task->dependsOn(mipRegenTask2));
Chris Dalton3d770272019-08-14 09:24:37 -0600513 }
Chris Dalton3d770272019-08-14 09:24:37 -0600514}