blob: 3902fdc36a66e40481f3c91aa95e3ce507ddc2c7 [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"
Robert Phillips16bf7d32020-07-07 10:20:27 -040016#include "src/gpu/GrContextPriv.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"
Greg Daniel177e6952017-10-12 12:27:11 -040031
Greg Daniel45d63032017-10-30 13:41:26 -040032static constexpr int kSize = 8;
33
Greg Daniel177e6952017-10-12 12:27:11 -040034// Test that the correct mip map states are on the GrTextures when wrapping GrBackendTextures in
35// SkImages and SkSurfaces
36DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrWrappedMipMappedTest, reporter, ctxInfo) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -040037 auto dContext = ctxInfo.directContext();
38 if (!dContext->priv().caps()->mipmapSupport()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -040039 return;
40 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050041
Brian Salomon7e67dca2020-07-21 09:27:25 -040042 for (auto mipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040043 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Robert Phillips9b16f812019-05-17 10:01:21 -040044 // createBackendTexture currently doesn't support uploading data to mip maps
Greg Daniel177e6952017-10-12 12:27:11 -040045 // so we don't send any. However, we pretend there is data for the checks below which is
46 // fine since we are never actually using these textures for any work on the gpu.
Greg Danielc1ad77c2020-05-06 11:40:03 -040047 GrBackendTexture backendTex;
Adlai Hollerdaa9e742020-08-03 12:50:40 -040048 CreateBackendTexture(dContext, &backendTex, kSize, kSize, kRGBA_8888_SkColorType,
Greg Danielc1ad77c2020-05-06 11:40:03 -040049 SkColors::kTransparent, mipMapped, renderable);
Greg Daniel177e6952017-10-12 12:27:11 -040050
Robert Phillipse0070c02017-11-13 12:47:24 -050051 sk_sp<GrTextureProxy> proxy;
Greg Daniel177e6952017-10-12 12:27:11 -040052 sk_sp<SkImage> image;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040053 if (GrRenderable::kYes == renderable) {
Greg Daniel177e6952017-10-12 12:27:11 -040054 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(
Adlai Hollerdaa9e742020-08-03 12:50:40 -040055 dContext,
Greg Daniel177e6952017-10-12 12:27:11 -040056 backendTex,
57 kTopLeft_GrSurfaceOrigin,
58 0,
Greg Danielfaa095e2017-12-19 13:15:02 -050059 kRGBA_8888_SkColorType,
Greg Daniel177e6952017-10-12 12:27:11 -040060 nullptr,
61 nullptr);
62
63 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
Robert Phillipse0070c02017-11-13 12:47:24 -050064 proxy = device->accessRenderTargetContext()->asTextureProxyRef();
Greg Daniel177e6952017-10-12 12:27:11 -040065 } else {
Adlai Hollerdaa9e742020-08-03 12:50:40 -040066 image = SkImage::MakeFromTexture(dContext, backendTex,
Greg Daniel177e6952017-10-12 12:27:11 -040067 kTopLeft_GrSurfaceOrigin,
Greg Danielf5d87582017-12-18 14:48:15 -050068 kRGBA_8888_SkColorType,
69 kPremul_SkAlphaType, nullptr,
70 nullptr, nullptr);
Adlai Hollerdaa9e742020-08-03 12:50:40 -040071 const GrSurfaceProxyView* view = as_IB(image)->view(dContext);
Greg Danielfebdedf2020-02-05 17:06:27 -050072 REPORTER_ASSERT(reporter, view);
73 if (!view) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -040074 dContext->deleteBackendTexture(backendTex);
Greg Danielfebdedf2020-02-05 17:06:27 -050075 return;
76 }
77 proxy = view->asTextureProxyRef();
Greg Daniel177e6952017-10-12 12:27:11 -040078 }
79 REPORTER_ASSERT(reporter, proxy);
80 if (!proxy) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -040081 dContext->deleteBackendTexture(backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040082 return;
83 }
84
Brian Salomonfd98c2c2018-07-31 17:25:29 -040085 REPORTER_ASSERT(reporter, proxy->isInstantiated());
Greg Daniel177e6952017-10-12 12:27:11 -040086
Brian Salomonfd98c2c2018-07-31 17:25:29 -040087 GrTexture* texture = proxy->peekTexture();
Greg Daniel177e6952017-10-12 12:27:11 -040088 REPORTER_ASSERT(reporter, texture);
89 if (!texture) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -040090 dContext->deleteBackendTexture(backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040091 return;
92 }
93
Brian Salomon7e67dca2020-07-21 09:27:25 -040094 if (GrMipmapped::kYes == mipMapped) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -040095 REPORTER_ASSERT(reporter, GrMipmapped::kYes == texture->mipmapped());
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040096 if (GrRenderable::kYes == renderable) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -040097 REPORTER_ASSERT(reporter, texture->mipmapsAreDirty());
Greg Daniel177e6952017-10-12 12:27:11 -040098 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -040099 REPORTER_ASSERT(reporter, !texture->mipmapsAreDirty());
Greg Daniel177e6952017-10-12 12:27:11 -0400100 }
101 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400102 REPORTER_ASSERT(reporter, GrMipmapped::kNo == texture->mipmapped());
Greg Daniel177e6952017-10-12 12:27:11 -0400103 }
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400104 dContext->deleteBackendTexture(backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -0400105 }
106 }
107}
108
Greg Daniel261b8aa2017-10-23 09:37:36 -0400109// Test that we correctly copy or don't copy GrBackendTextures in the GrBackendTextureImageGenerator
110// based on if we will use mips in the draw and the mip status of the GrBackendTexture.
111DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrBackendTextureImageMipMappedTest, reporter, ctxInfo) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400112 auto dContext = ctxInfo.directContext();
113 if (!dContext->priv().caps()->mipmapSupport()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400114 return;
115 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500116
Brian Salomon7e67dca2020-07-21 09:27:25 -0400117 for (auto betMipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
118 for (auto requestMipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400119 GrBackendTexture backendTex;
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400120 CreateBackendTexture(dContext, &backendTex, kSize, kSize, kRGBA_8888_SkColorType,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400121 SkColors::kTransparent, betMipMapped, GrRenderable::kNo);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400122
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400123 sk_sp<SkImage> image = SkImage::MakeFromTexture(dContext, backendTex,
Greg Daniel261b8aa2017-10-23 09:37:36 -0400124 kTopLeft_GrSurfaceOrigin,
Greg Danielf5d87582017-12-18 14:48:15 -0500125 kRGBA_8888_SkColorType,
126 kPremul_SkAlphaType, nullptr,
127 nullptr, nullptr);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400128
129 GrTextureProxy* proxy = as_IB(image)->peekProxy();
130 REPORTER_ASSERT(reporter, proxy);
131 if (!proxy) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400132 dContext->deleteBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400133 return;
134 }
135
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400136 REPORTER_ASSERT(reporter, proxy->isInstantiated());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400137
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400138 sk_sp<GrTexture> texture = sk_ref_sp(proxy->peekTexture());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400139 REPORTER_ASSERT(reporter, texture);
140 if (!texture) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400141 dContext->deleteBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400142 return;
143 }
144
145 std::unique_ptr<SkImageGenerator> imageGen = GrBackendTextureImageGenerator::Make(
Brian Osman052ef692018-03-27 09:56:31 -0400146 texture, kTopLeft_GrSurfaceOrigin, nullptr, kRGBA_8888_SkColorType,
147 kPremul_SkAlphaType, nullptr);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400148 REPORTER_ASSERT(reporter, imageGen);
149 if (!imageGen) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400150 dContext->deleteBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400151 return;
152 }
153
154 SkIPoint origin = SkIPoint::Make(0,0);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400155 SkImageInfo imageInfo = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
156 kPremul_SkAlphaType);
Brian Salomonbc074a62020-03-18 10:06:13 -0400157 GrSurfaceProxyView genView = imageGen->generateTexture(
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400158 dContext, imageInfo, origin, requestMipMapped, GrImageTexGenPolicy::kDraw);
Greg Danielcc104db2020-02-03 14:17:08 -0500159 GrSurfaceProxy* genProxy = genView.proxy();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400160
161 REPORTER_ASSERT(reporter, genProxy);
162 if (!genProxy) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400163 dContext->deleteBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400164 return;
165 }
166
Brian Salomonbeb7f522019-08-30 16:19:42 -0400167 if (genProxy->isLazy()) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400168 genProxy->priv().doLazyInstantiation(dContext->priv().resourceProvider());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400169 } else if (!genProxy->isInstantiated()) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400170 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()) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400175 dContext->deleteBackendTexture(backendTex);
Greg Danielbddcc952018-01-24 13:22:24 -0500176 return;
177 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400178
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400179 GrTexture* genTexture = genProxy->peekTexture();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400180 REPORTER_ASSERT(reporter, genTexture);
181 if (!genTexture) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400182 dContext->deleteBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400183 return;
184 }
185
Robert Phillipsb67821d2017-12-13 15:00:45 -0500186 GrBackendTexture genBackendTex = genTexture->getBackendTexture();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400187
Greg Danielbdf12ad2018-10-12 09:31:11 -0400188 if (GrBackendApi::kOpenGL == genBackendTex.backend()) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400189 GrGLTextureInfo genTexInfo;
190 GrGLTextureInfo origTexInfo;
191 if (genBackendTex.getGLTextureInfo(&genTexInfo) &&
192 backendTex.getGLTextureInfo(&origTexInfo)) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400193 if (requestMipMapped == GrMipmapped::kYes && betMipMapped == GrMipmapped::kNo) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400194 // We did a copy so the texture IDs should be different
195 REPORTER_ASSERT(reporter, origTexInfo.fID != genTexInfo.fID);
196 } else {
197 REPORTER_ASSERT(reporter, origTexInfo.fID == genTexInfo.fID);
198 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400199 } else {
Greg Daniel52e16d92018-04-10 09:34:07 -0400200 ERRORF(reporter, "Failed to get GrGLTextureInfo");
Greg Daniel261b8aa2017-10-23 09:37:36 -0400201 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400202#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400203 } else if (GrBackendApi::kVulkan == genBackendTex.backend()) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400204 GrVkImageInfo genImageInfo;
205 GrVkImageInfo origImageInfo;
206 if (genBackendTex.getVkImageInfo(&genImageInfo) &&
207 backendTex.getVkImageInfo(&origImageInfo)) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400208 if (requestMipMapped == GrMipmapped::kYes && betMipMapped == GrMipmapped::kNo) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400209 // We did a copy so the texture IDs should be different
210 REPORTER_ASSERT(reporter, origImageInfo.fImage != genImageInfo.fImage);
211 } else {
212 REPORTER_ASSERT(reporter, origImageInfo.fImage == genImageInfo.fImage);
213 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400214 } else {
Greg Daniel52e16d92018-04-10 09:34:07 -0400215 ERRORF(reporter, "Failed to get GrVkImageInfo");
Greg Daniel261b8aa2017-10-23 09:37:36 -0400216 }
217#endif
Jim Van Verth9896a0d2019-04-10 15:11:12 -0400218#ifdef SK_METAL
219 } else if (GrBackendApi::kMetal == genBackendTex.backend()) {
220 GrMtlTextureInfo genImageInfo;
221 GrMtlTextureInfo origImageInfo;
222 if (genBackendTex.getMtlTextureInfo(&genImageInfo) &&
223 backendTex.getMtlTextureInfo(&origImageInfo)) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400224 if (requestMipMapped == GrMipmapped::kYes && betMipMapped == GrMipmapped::kNo) {
Jim Van Verth9896a0d2019-04-10 15:11:12 -0400225 // We did a copy so the texture IDs should be different
226 REPORTER_ASSERT(reporter, origImageInfo.fTexture != genImageInfo.fTexture);
227 } else {
228 REPORTER_ASSERT(reporter, origImageInfo.fTexture == genImageInfo.fTexture);
229 }
230 } else {
231 ERRORF(reporter, "Failed to get GrMtlTextureInfo");
232 }
233#endif
Greg Daniel261b8aa2017-10-23 09:37:36 -0400234 } else {
235 REPORTER_ASSERT(reporter, false);
236 }
237
238 // Must make sure the uses of the backend texture have finished (we possibly have a
Greg Daniel26b50a42018-03-08 09:49:58 -0500239 // queued up copy) before we delete the backend texture.
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400240 dContext->flushAndSubmit();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400241
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400242 dContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Robert Phillips9b16f812019-05-17 10:01:21 -0400243
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400244 dContext->deleteBackendTexture(backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400245 }
246 }
247}
248
Greg Daniel45d63032017-10-30 13:41:26 -0400249// Test that when we call makeImageSnapshot on an SkSurface we retains the same mip status as the
250// resource we took the snapshot of.
251DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxInfo) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400252 auto dContext = ctxInfo.directContext();
253 if (!dContext->priv().caps()->mipmapSupport()) {
Greg Daniel45d63032017-10-30 13:41:26 -0400254 return;
255 }
256
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400257 auto resourceProvider = dContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500258
Greg Daniel45d63032017-10-30 13:41:26 -0400259 for (auto willUseMips : {false, true}) {
260 for (auto isWrapped : {false, true}) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400261 GrMipmapped mipMapped = willUseMips ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel45d63032017-10-30 13:41:26 -0400262 sk_sp<SkSurface> surface;
Greg Danielc1ad77c2020-05-06 11:40:03 -0400263 GrBackendTexture backendTex;
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400264 CreateBackendTexture(dContext, &backendTex, kSize, kSize, kRGBA_8888_SkColorType,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400265 SkColors::kTransparent, mipMapped, GrRenderable::kYes);
Greg Daniel45d63032017-10-30 13:41:26 -0400266 if (isWrapped) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400267 surface = SkSurface::MakeFromBackendTexture(dContext,
Greg Daniel45d63032017-10-30 13:41:26 -0400268 backendTex,
269 kTopLeft_GrSurfaceOrigin,
270 0,
Greg Danielfaa095e2017-12-19 13:15:02 -0500271 kRGBA_8888_SkColorType,
Greg Daniel45d63032017-10-30 13:41:26 -0400272 nullptr,
273 nullptr);
274 } else {
275 SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
276 kPremul_SkAlphaType);
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400277 surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, info, 0,
Greg Daniel45d63032017-10-30 13:41:26 -0400278 kTopLeft_GrSurfaceOrigin, nullptr,
279 willUseMips);
280 }
281 REPORTER_ASSERT(reporter, surface);
282 if (!surface) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400283 dContext->deleteBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400284 }
285 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
286 GrTextureProxy* texProxy = device->accessRenderTargetContext()->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400287 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipmapped());
Greg Daniel45d63032017-10-30 13:41:26 -0400288
Robert Phillips6be756b2018-01-16 15:07:54 -0500289 texProxy->instantiate(resourceProvider);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400290 GrTexture* texture = texProxy->peekTexture();
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400291 REPORTER_ASSERT(reporter, mipMapped == texture->mipmapped());
Greg Daniel45d63032017-10-30 13:41:26 -0400292
293 sk_sp<SkImage> image = surface->makeImageSnapshot();
294 REPORTER_ASSERT(reporter, image);
295 if (!image) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400296 dContext->deleteBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400297 }
298 texProxy = as_IB(image)->peekProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400299 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipmapped());
Greg Daniel45d63032017-10-30 13:41:26 -0400300
Robert Phillips6be756b2018-01-16 15:07:54 -0500301 texProxy->instantiate(resourceProvider);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400302 texture = texProxy->peekTexture();
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400303 REPORTER_ASSERT(reporter, mipMapped == texture->mipmapped());
Greg Daniel45d63032017-10-30 13:41:26 -0400304
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400305 // Must flush the dContext to make sure all the cmds (copies, etc.) from above are sent
Greg Daniel45d63032017-10-30 13:41:26 -0400306 // to the gpu before we delete the backendHandle.
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400307 dContext->flushAndSubmit();
308 dContext->priv().getGpu()->testingOnly_flushGpuAndSync();
309 dContext->deleteBackendTexture(backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400310 }
311 }
312}
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400313
314// Test that we don't create a mip mapped texture if the size is 1x1 even if the filter mode is set
315// to use mips. This test passes by not crashing or hitting asserts in code.
316DEF_GPUTEST_FOR_RENDERING_CONTEXTS(Gr1x1TextureMipMappedTest, reporter, ctxInfo) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400317 auto dContext = ctxInfo.directContext();
318 if (!dContext->priv().caps()->mipmapSupport()) {
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400319 return;
320 }
321
322 // Make surface to draw into
323 SkImageInfo info = SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType);
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400324 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400325
326 // Make 1x1 raster bitmap
327 SkBitmap bmp;
328 bmp.allocN32Pixels(1, 1);
329 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(bmp.getPixels());
330 *pixel = 0;
331
332 sk_sp<SkImage> bmpImage = SkImage::MakeFromBitmap(bmp);
333
334 // Make sure we scale so we don't optimize out the use of mips.
335 surface->getCanvas()->scale(0.5f, 0.5f);
336
337 SkPaint paint;
338 // This should upload the image to a non mipped GrTextureProxy.
339 surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400340 surface->flushAndSubmit();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400341
342 // Now set the filter quality to high so we use mip maps. We should find the non mipped texture
343 // in the cache for the SkImage. Since the texture is 1x1 we should just use that texture
344 // instead of trying to do a copy to a mipped texture.
345 paint.setFilterQuality(kHigh_SkFilterQuality);
346 surface->getCanvas()->drawImage(bmpImage, 0, 0, &paint);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400347 surface->flushAndSubmit();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400348}
349
Greg Daniel40903af2020-01-30 14:55:05 -0500350// Create a new render target and draw 'mipmapView' into it using the provided 'filter'.
Brian Salomonbf6b9792019-08-21 09:38:10 -0400351static std::unique_ptr<GrRenderTargetContext> draw_mipmap_into_new_render_target(
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400352 GrRecordingContext* rContext,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400353 GrColorType colorType,
354 SkAlphaType alphaType,
355 GrSurfaceProxyView mipmapView,
356 GrSamplerState::MipmapMode mm) {
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400357 auto proxyProvider = rContext->priv().proxyProvider();
Brian Salomone69b9ef2020-07-22 11:18:06 -0400358 sk_sp<GrSurfaceProxy> renderTarget =
359 proxyProvider->createProxy(mipmapView.proxy()->backendFormat(),
360 {1, 1},
361 GrRenderable::kYes,
362 1,
363 GrMipmapped::kNo,
364 SkBackingFit::kApprox,
365 SkBudgeted::kYes,
366 GrProtected::kNo);
Greg Danielba0ff782020-01-07 15:42:57 -0500367
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400368 auto rtc = GrRenderTargetContext::Make(rContext,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400369 colorType,
370 nullptr,
371 std::move(renderTarget),
372 kTopLeft_GrSurfaceOrigin,
373 nullptr);
Greg Danielba0ff782020-01-07 15:42:57 -0500374
Brian Salomone69b9ef2020-07-22 11:18:06 -0400375 rtc->drawTexture(nullptr,
376 std::move(mipmapView),
377 alphaType,
378 GrSamplerState::Filter::kLinear,
379 mm,
380 SkBlendMode::kSrcOver,
381 {1, 1, 1, 1},
382 SkRect::MakeWH(4, 4),
383 SkRect::MakeWH(1, 1),
384 GrAA::kYes,
385 GrQuadAAFlags::kAll,
386 SkCanvas::kFast_SrcRectConstraint,
387 SkMatrix::I(),
Brian Salomonfc118442019-11-22 19:09:27 -0500388 nullptr);
Chris Dalton3d770272019-08-14 09:24:37 -0600389 return rtc;
390}
391
Greg Danielf41b2bd2019-08-22 16:19:24 -0400392// Test that two opsTasks using the same mipmaps both depend on the same GrTextureResolveRenderTask.
Chris Dalton30eea6c2019-08-21 10:22:50 -0600393DEF_GPUTEST(GrManyDependentsMipMappedTest, reporter, /* options */) {
Chris Dalton30eea6c2019-08-21 10:22:50 -0600394 using Enable = GrContextOptions::Enable;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400395 using MipmapMode = GrSamplerState::MipmapMode;
Chris Dalton30eea6c2019-08-21 10:22:50 -0600396
397 for (auto enableSortingAndReduction : {Enable::kYes, Enable::kNo}) {
398 GrMockOptions mockOptions;
Brian Salomon69100f02020-07-21 10:49:25 -0400399 mockOptions.fMipmapSupport = true;
Chris Dalton30eea6c2019-08-21 10:22:50 -0600400 GrContextOptions ctxOptions;
Greg Daniel93138742019-08-22 17:15:39 -0400401 ctxOptions.fReduceOpsTaskSplitting = enableSortingAndReduction;
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400402 sk_sp<GrDirectContext> dContext = GrDirectContext::MakeMock(&mockOptions, ctxOptions);
403 GrDrawingManager* drawingManager = dContext->priv().drawingManager();
404 if (!dContext) {
405 ERRORF(reporter, "could not create mock dContext with fReduceOpsTaskSplitting %s.",
Chris Dalton30eea6c2019-08-21 10:22:50 -0600406 (Enable::kYes == enableSortingAndReduction) ? "enabled" : "disabled");
407 continue;
408 }
409
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400410 SkASSERT(dContext->priv().caps()->mipmapSupport());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600411
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400412 GrBackendFormat format = dContext->defaultBackendFormat(
Chris Dalton30eea6c2019-08-21 10:22:50 -0600413 kRGBA_8888_SkColorType, GrRenderable::kYes);
Chris Dalton30eea6c2019-08-21 10:22:50 -0600414 GrColorType colorType = GrColorType::kRGBA_8888;
Brian Salomonfc118442019-11-22 19:09:27 -0500415 SkAlphaType alphaType = kPremul_SkAlphaType;
Chris Dalton30eea6c2019-08-21 10:22:50 -0600416
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400417 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
Chris Dalton30eea6c2019-08-21 10:22:50 -0600418
419 // Create a mipmapped render target.
Greg Daniel47c20e82020-01-21 14:29:57 -0500420
Brian Salomonbeb7f522019-08-30 16:19:42 -0400421 sk_sp<GrTextureProxy> mipmapProxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400422 format, {4, 4}, GrRenderable::kYes, 1, GrMipmapped::kYes, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400423 SkBudgeted::kYes, GrProtected::kNo);
Chris Dalton30eea6c2019-08-21 10:22:50 -0600424
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600425 // Mark the mipmaps clean to ensure things still work properly when they won't be marked
426 // dirty again until GrRenderTask::makeClosed().
Brian Salomon8c82a872020-07-21 12:09:58 -0400427 mipmapProxy->markMipmapsClean();
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600428
Greg Danielba0ff782020-01-07 15:42:57 -0500429 auto mipmapRTC = GrRenderTargetContext::Make(
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400430 dContext.get(), colorType, nullptr, mipmapProxy, kTopLeft_GrSurfaceOrigin, nullptr);
Greg Danielba0ff782020-01-07 15:42:57 -0500431
Michael Ludwig81d41722020-05-26 16:57:38 -0400432 mipmapRTC->clear({.1f,.2f,.3f,.4f});
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400433 REPORTER_ASSERT(reporter, drawingManager->getLastRenderTask(mipmapProxy.get()));
Greg Danielf41b2bd2019-08-22 16:19:24 -0400434 // mipmapProxy's last render task should now just be the opsTask containing the clear.
Chris Dalton30eea6c2019-08-21 10:22:50 -0600435 REPORTER_ASSERT(reporter,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400436 mipmapRTC->testingOnly_PeekLastOpsTask() ==
437 drawingManager->getLastRenderTask(mipmapProxy.get()));
Chris Dalton30eea6c2019-08-21 10:22:50 -0600438
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600439 // Mipmaps don't get marked dirty until makeClosed().
Brian Salomon8c82a872020-07-21 12:09:58 -0400440 REPORTER_ASSERT(reporter, !mipmapProxy->mipmapsAreDirty());
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600441
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400442 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(format, colorType);
Greg Daniel40903af2020-01-30 14:55:05 -0500443 GrSurfaceProxyView mipmapView(mipmapProxy, kTopLeft_GrSurfaceOrigin, swizzle);
444
Chris Dalton30eea6c2019-08-21 10:22:50 -0600445 // Draw the dirty mipmap texture into a render target.
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400446 auto rtc1 = draw_mipmap_into_new_render_target(dContext.get(), colorType, alphaType,
447 mipmapView, MipmapMode::kLinear);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400448 auto rtc1Task = sk_ref_sp(rtc1->testingOnly_PeekLastOpsTask());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600449
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600450 // Mipmaps should have gotten marked dirty during makeClosed, then marked clean again as
451 // soon as a GrTextureResolveRenderTask was inserted. The way we know they were resolved is
Greg Danielf41b2bd2019-08-22 16:19:24 -0400452 // if mipmapProxy->getLastRenderTask() has switched from the opsTask that drew to it, to the
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600453 // task that resolved its mips.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400454 GrRenderTask* initialMipmapRegenTask = drawingManager->getLastRenderTask(mipmapProxy.get());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600455 REPORTER_ASSERT(reporter, initialMipmapRegenTask);
456 REPORTER_ASSERT(reporter,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400457 initialMipmapRegenTask != mipmapRTC->testingOnly_PeekLastOpsTask());
Brian Salomon8c82a872020-07-21 12:09:58 -0400458 REPORTER_ASSERT(reporter, !mipmapProxy->mipmapsAreDirty());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600459
460 // Draw the now-clean mipmap texture into a second target.
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400461 auto rtc2 = draw_mipmap_into_new_render_target(dContext.get(), colorType, alphaType,
462 mipmapView, MipmapMode::kLinear);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400463 auto rtc2Task = sk_ref_sp(rtc2->testingOnly_PeekLastOpsTask());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600464
465 // Make sure the mipmap texture still has the same regen task.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400466 REPORTER_ASSERT(reporter,
467 drawingManager->getLastRenderTask(mipmapProxy.get()) == initialMipmapRegenTask);
Brian Salomon8c82a872020-07-21 12:09:58 -0400468 SkASSERT(!mipmapProxy->mipmapsAreDirty());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600469
470 // Reset everything so we can go again, this time with the first draw not mipmapped.
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400471 dContext->flushAndSubmit();
Chris Dalton30eea6c2019-08-21 10:22:50 -0600472
Chris Daltone2a903e2019-09-18 13:41:50 -0600473 // Mip regen tasks don't get added as dependencies until makeClosed().
Brian Salomon3b8486a2020-04-21 12:43:26 -0400474 REPORTER_ASSERT(reporter, rtc1Task->dependsOn(initialMipmapRegenTask));
475 REPORTER_ASSERT(reporter, rtc2Task->dependsOn(initialMipmapRegenTask));
Chris Daltone2a903e2019-09-18 13:41:50 -0600476
Chris Dalton30eea6c2019-08-21 10:22:50 -0600477 // Render something to dirty the mips.
Michael Ludwig81d41722020-05-26 16:57:38 -0400478 mipmapRTC->clear({.1f,.2f,.3f,.4f});
Brian Salomon3b8486a2020-04-21 12:43:26 -0400479 auto mipmapRTCTask = sk_ref_sp(mipmapRTC->testingOnly_PeekLastOpsTask());
480 REPORTER_ASSERT(reporter, mipmapRTCTask);
481
Greg Danielf41b2bd2019-08-22 16:19:24 -0400482 // mipmapProxy's last render task should now just be the opsTask containing the clear.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400483 REPORTER_ASSERT(reporter,
484 mipmapRTCTask.get() == drawingManager->getLastRenderTask(mipmapProxy.get()));
Chris Dalton30eea6c2019-08-21 10:22:50 -0600485
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600486 // Mipmaps don't get marked dirty until makeClosed().
Brian Salomon8c82a872020-07-21 12:09:58 -0400487 REPORTER_ASSERT(reporter, !mipmapProxy->mipmapsAreDirty());
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600488
Chris Dalton30eea6c2019-08-21 10:22:50 -0600489 // Draw the dirty mipmap texture into a render target, but don't do mipmap filtering.
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400490 rtc1 = draw_mipmap_into_new_render_target(dContext.get(), colorType, alphaType,
491 mipmapView, MipmapMode::kNone);
Chris Dalton30eea6c2019-08-21 10:22:50 -0600492
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600493 // Mipmaps should have gotten marked dirty during makeClosed() when adding the dependency.
494 // Since the last draw did not use mips, they will not have been regenerated and should
495 // therefore still be dirty.
Brian Salomon8c82a872020-07-21 12:09:58 -0400496 REPORTER_ASSERT(reporter, mipmapProxy->mipmapsAreDirty());
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600497
498 // Since mips weren't regenerated, the last render task shouldn't have changed.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400499 REPORTER_ASSERT(reporter,
500 mipmapRTCTask.get() == drawingManager->getLastRenderTask(mipmapProxy.get()));
Chris Dalton30eea6c2019-08-21 10:22:50 -0600501
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600502 // Draw the stil-dirty mipmap texture into a second target with mipmap filtering.
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400503 rtc2 = draw_mipmap_into_new_render_target(dContext.get(), colorType, alphaType,
504 std::move(mipmapView), MipmapMode::kLinear);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400505 rtc2Task = sk_ref_sp(rtc2->testingOnly_PeekLastOpsTask());
Chris Dalton30eea6c2019-08-21 10:22:50 -0600506
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600507 // Make sure the mipmap texture now has a new last render task that regenerates the mips,
508 // and that the mipmaps are now clean.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400509 auto mipRegenTask2 = drawingManager->getLastRenderTask(mipmapProxy.get());
Chris Daltone2a903e2019-09-18 13:41:50 -0600510 REPORTER_ASSERT(reporter, mipRegenTask2);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400511 REPORTER_ASSERT(reporter, mipmapRTCTask.get() != mipRegenTask2);
Brian Salomon8c82a872020-07-21 12:09:58 -0400512 SkASSERT(!mipmapProxy->mipmapsAreDirty());
Chris Daltone2a903e2019-09-18 13:41:50 -0600513
514 // Mip regen tasks don't get added as dependencies until makeClosed().
Adlai Hollerdaa9e742020-08-03 12:50:40 -0400515 dContext->flushAndSubmit();
Brian Salomon3b8486a2020-04-21 12:43:26 -0400516 REPORTER_ASSERT(reporter, rtc2Task->dependsOn(mipRegenTask2));
Chris Dalton3d770272019-08-14 09:24:37 -0600517 }
Chris Dalton3d770272019-08-14 09:24:37 -0600518}