blob: 7328dd02389ae4cc3801cc4c5e4e6cb83775781f [file] [log] [blame]
Robert Phillips7ffbcf92017-12-04 12:52:46 -05001/*
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/SkBitmap.h"
9#include "include/core/SkCanvas.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040010#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkColorSpace.h"
12#include "include/core/SkDeferredDisplayListRecorder.h"
13#include "include/core/SkImage.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkPromiseImageTexture.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkSurface.h"
20#include "include/core/SkSurfaceCharacterization.h"
21#include "include/core/SkSurfaceProps.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040022#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/gpu/GrBackendSurface.h"
24#include "include/gpu/GrContext.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040025#include "include/gpu/GrContextThreadSafeProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/gpu/GrTypes.h"
27#include "include/gpu/gl/GrGLTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/private/GrTypesPriv.h"
29#include "include/private/SkDeferredDisplayList.h"
30#include "src/core/SkDeferredDisplayListPriv.h"
31#include "src/gpu/GrCaps.h"
32#include "src/gpu/GrContextPriv.h"
33#include "src/gpu/GrGpu.h"
34#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040035#include "src/gpu/GrRenderTargetProxy.h"
36#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/SkGpuDevice.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/gl/GrGLDefines.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040039#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/image/SkSurface_Gpu.h"
41#include "tests/Test.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040042#include "tests/TestUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "tools/gpu/GrContextFactory.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040044
45#include <initializer_list>
46#include <memory>
47#include <utility>
Robert Phillipsfc711a22018-02-13 17:03:00 -050048
Robert Phillips3cd54322019-07-10 09:28:59 -040049#ifdef SK_VULKAN
50#include "src/gpu/vk/GrVkCaps.h"
51#endif
52
Robert Phillips7ffbcf92017-12-04 12:52:46 -050053class SurfaceParameters {
54public:
Robert Phillips3cd54322019-07-10 09:28:59 -040055 static const int kNumParams = 12;
56 static const int kSampleCount = 5;
57 static const int kMipMipCount = 8;
58 static const int kFBO0Count = 9;
59 static const int kProtectedCount = 11;
Robert Phillips7ffbcf92017-12-04 12:52:46 -050060
Robert Phillips3cd54322019-07-10 09:28:59 -040061 SurfaceParameters(GrContext* context)
62 : fBackend(context->backend())
Robert Phillipsb45f47d2019-02-03 17:17:54 -050063 , fWidth(64)
Robert Phillips7ffbcf92017-12-04 12:52:46 -050064 , fHeight(64)
65 , fOrigin(kTopLeft_GrSurfaceOrigin)
66 , fColorType(kRGBA_8888_SkColorType)
67 , fColorSpace(SkColorSpace::MakeSRGB())
Brian Salomonbdecacf2018-02-02 20:32:49 -050068 , fSampleCount(1)
Robert Phillipse8fabb22018-02-04 14:33:21 -050069 , fSurfaceProps(0x0, kUnknown_SkPixelGeometry)
Robert Phillipsb45f47d2019-02-03 17:17:54 -050070 , fShouldCreateMipMaps(true)
71 , fUsesGLFBO0(false)
Robert Phillips3cd54322019-07-10 09:28:59 -040072 , fIsTextureable(true)
73 , fIsProtected(GrProtected::kNo) {
74#ifdef SK_VULKAN
75 if (GrBackendApi::kVulkan == context->backend()) {
76 const GrVkCaps* vkCaps = (const GrVkCaps*) context->priv().caps();
77
78 fIsProtected = GrProtected(vkCaps->supportsProtectedMemory());
79 }
80#endif
Robert Phillipse8fabb22018-02-04 14:33:21 -050081 }
Robert Phillips7ffbcf92017-12-04 12:52:46 -050082
83 int sampleCount() const { return fSampleCount; }
84
Robert Phillipsbe77a022018-04-03 17:17:05 -040085 void setColorType(SkColorType ct) { fColorType = ct; }
Robert Phillipsd8f79a22019-06-24 13:25:42 -040086 SkColorType colorType() const { return fColorType; }
Robert Phillipsbe77a022018-04-03 17:17:05 -040087 void setColorSpace(sk_sp<SkColorSpace> cs) { fColorSpace = std::move(cs); }
Robert Phillipsb45f47d2019-02-03 17:17:54 -050088 void setTextureable(bool isTextureable) { fIsTextureable = isTextureable; }
Brian Salomon4687bdd2019-05-09 16:28:04 -040089 void setShouldCreateMipMaps(bool shouldCreateMipMaps) {
90 fShouldCreateMipMaps = shouldCreateMipMaps;
91 }
Robert Phillipsbe77a022018-04-03 17:17:05 -040092
Robert Phillips7ffbcf92017-12-04 12:52:46 -050093 // Modify the SurfaceParameters in just one way
94 void modify(int i) {
95 switch (i) {
96 case 0:
97 fWidth = 63;
98 break;
99 case 1:
100 fHeight = 63;
101 break;
102 case 2:
103 fOrigin = kBottomLeft_GrSurfaceOrigin;
104 break;
105 case 3:
106 fColorType = kRGBA_F16_SkColorType;
107 break;
108 case 4:
Brian Osman37f99882018-08-09 10:26:57 -0400109 // This just needs to be a colorSpace different from that returned by MakeSRGB().
110 // In this case we just change the gamut.
Brian Osman82ebe042019-01-04 17:03:00 -0500111 fColorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kAdobeRGB);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500112 break;
113 case kSampleCount:
114 fSampleCount = 4;
115 break;
116 case 6:
117 fSurfaceProps = SkSurfaceProps(0x0, kRGB_H_SkPixelGeometry);
118 break;
119 case 7:
120 fSurfaceProps = SkSurfaceProps(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
121 kUnknown_SkPixelGeometry);
122 break;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500123 case 8:
124 fShouldCreateMipMaps = false;
125 break;
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500126 case 9:
127 if (GrBackendApi::kOpenGL == fBackend) {
128 fUsesGLFBO0 = true;
Robert Phillipsc046ff02019-07-01 10:34:03 -0400129 fShouldCreateMipMaps = false; // needs to changed in tandem w/ textureability
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500130 fIsTextureable = false;
131 }
132 break;
133 case 10:
Robert Phillipsc046ff02019-07-01 10:34:03 -0400134 fShouldCreateMipMaps = false; // needs to changed in tandem w/ textureability
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500135 fIsTextureable = false;
136 break;
Robert Phillips3cd54322019-07-10 09:28:59 -0400137 case 11:
138 fIsProtected = GrProtected::kYes == fIsProtected ? GrProtected::kNo
139 : GrProtected::kYes;
140 break;
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500141 }
142 }
143
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400144 SkSurfaceCharacterization createCharacterization(GrContext* context) const {
Robert Phillipscf39f372019-09-03 10:29:20 -0400145 size_t maxResourceBytes = context->getResourceCacheLimit();
Robert Phillipsfc711a22018-02-13 17:03:00 -0500146
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400147 if (!context->colorTypeSupportedAsSurface(fColorType)) {
148 return SkSurfaceCharacterization();
149 }
150
Robert Phillipsfc711a22018-02-13 17:03:00 -0500151 // Note that Ganesh doesn't make use of the SkImageInfo's alphaType
152 SkImageInfo ii = SkImageInfo::Make(fWidth, fHeight, fColorType,
153 kPremul_SkAlphaType, fColorSpace);
154
Robert Phillips0a15cc62019-07-30 12:49:10 -0400155 GrBackendFormat backendFormat = context->defaultBackendFormat(fColorType,
156 GrRenderable::kYes);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400157 if (!backendFormat.isValid()) {
158 return SkSurfaceCharacterization();
159 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500160
161 SkSurfaceCharacterization c = context->threadSafeProxy()->createCharacterization(
162 maxResourceBytes, ii, backendFormat, fSampleCount,
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500163 fOrigin, fSurfaceProps, fShouldCreateMipMaps,
Robert Phillips3cd54322019-07-10 09:28:59 -0400164 fUsesGLFBO0, fIsTextureable, fIsProtected);
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400165 return c;
166 }
167
168 // Create a DDL whose characterization captures the current settings
169 std::unique_ptr<SkDeferredDisplayList> createDDL(GrContext* context) const {
170 SkSurfaceCharacterization c = this->createCharacterization(context);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500171 SkAssertResult(c.isValid());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500172
173 SkDeferredDisplayListRecorder r(c);
174 SkCanvas* canvas = r.getCanvas();
175 if (!canvas) {
176 return nullptr;
177 }
178
179 canvas->drawRect(SkRect::MakeXYWH(10, 10, 10, 10), SkPaint());
180 return r.detach();
181 }
182
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500183 // Create the surface with the current set of parameters
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500184 sk_sp<SkSurface> make(GrContext* context, GrBackendTexture* backend) const {
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400185 const SkSurfaceCharacterization c = this->createCharacterization(context);
186
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500187 GrMipMapped mipmapped = !fIsTextureable
Robert Phillipsbe77a022018-04-03 17:17:05 -0400188 ? GrMipMapped::kNo
189 : GrMipMapped(fShouldCreateMipMaps);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500190
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500191 if (fUsesGLFBO0) {
192 if (GrBackendApi::kOpenGL != context->backend()) {
193 return nullptr;
194 }
195
196 GrGLFramebufferInfo fboInfo;
197 fboInfo.fFBOID = 0;
198 fboInfo.fFormat = GR_GL_RGBA8;
199 static constexpr int kStencilBits = 8;
200 GrBackendRenderTarget backendRT(fWidth, fHeight, 1, kStencilBits, fboInfo);
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500201
202 if (!backendRT.isValid()) {
203 return nullptr;
204 }
205
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400206 sk_sp<SkSurface> result = SkSurface::MakeFromBackendRenderTarget(context, backendRT,
207 fOrigin, fColorType,
208 fColorSpace,
209 &fSurfaceProps);
Robert Phillips9907e6e2019-06-25 14:47:04 -0400210 SkASSERT(result->isCompatible(c));
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400211 return result;
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500212 }
213
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400214 *backend = context->createBackendTexture(fWidth, fHeight, fColorType,
215 SkColors::kTransparent,
Robert Phillips3cd54322019-07-10 09:28:59 -0400216 mipmapped, GrRenderable::kYes, fIsProtected);
Robert Phillips7c8af172019-07-08 15:55:24 -0400217 if (!backend->isValid()) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500218 return nullptr;
219 }
220
Robert Phillipsc046ff02019-07-01 10:34:03 -0400221 // Even if a characterization couldn't be constructed we want to soldier on to make
222 // sure that surface creation will/would've also failed
223 SkASSERT(!c.isValid() || c.isCompatible(*backend));
224
Robert Phillipsbe77a022018-04-03 17:17:05 -0400225 sk_sp<SkSurface> surface;
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500226 if (!fIsTextureable) {
Robert Phillipsbe77a022018-04-03 17:17:05 -0400227 // Create a surface w/ the current parameters but make it non-textureable
228 surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
229 context, *backend, fOrigin, fSampleCount, fColorType,
230 fColorSpace, &fSurfaceProps);
231 } else {
232 surface = SkSurface::MakeFromBackendTexture(
233 context, *backend, fOrigin, fSampleCount, fColorType,
234 fColorSpace, &fSurfaceProps);
235 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500236
237 if (!surface) {
Robert Phillipsc046ff02019-07-01 10:34:03 -0400238 SkASSERT(!c.isValid());
Robert Phillips9b16f812019-05-17 10:01:21 -0400239 this->cleanUpBackEnd(context, *backend);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500240 return nullptr;
241 }
242
Robert Phillipsc046ff02019-07-01 10:34:03 -0400243 SkASSERT(c.isValid());
Robert Phillips9907e6e2019-06-25 14:47:04 -0400244 SkASSERT(surface->isCompatible(c));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500245 return surface;
246 }
247
Brian Salomon26102cb2018-03-09 09:33:19 -0500248 void cleanUpBackEnd(GrContext* context, const GrBackendTexture& backend) const {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400249 context->deleteBackendTexture(backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500250 }
251
252private:
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500253 GrBackendApi fBackend;
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500254 int fWidth;
255 int fHeight;
256 GrSurfaceOrigin fOrigin;
257 SkColorType fColorType;
258 sk_sp<SkColorSpace> fColorSpace;
259 int fSampleCount;
260 SkSurfaceProps fSurfaceProps;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500261 bool fShouldCreateMipMaps;
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500262 bool fUsesGLFBO0;
263 bool fIsTextureable;
Robert Phillips3cd54322019-07-10 09:28:59 -0400264 GrProtected fIsProtected;
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500265};
266
Robert Phillipsc1267c62018-04-04 11:12:39 -0400267// Test out operator== && operator!=
268DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLOperatorEqTest, reporter, ctxInfo) {
269 GrContext* context = ctxInfo.grContext();
270
271 for (int i = 0; i < SurfaceParameters::kNumParams; ++i) {
Robert Phillips3cd54322019-07-10 09:28:59 -0400272 SurfaceParameters params1(context);
Robert Phillipsc1267c62018-04-04 11:12:39 -0400273 params1.modify(i);
274
275 SkSurfaceCharacterization char1 = params1.createCharacterization(context);
276 if (!char1.isValid()) {
277 continue; // can happen on some platforms (ChromeOS)
278 }
279
280 for (int j = 0; j < SurfaceParameters::kNumParams; ++j) {
Robert Phillips3cd54322019-07-10 09:28:59 -0400281 SurfaceParameters params2(context);
Robert Phillipsc1267c62018-04-04 11:12:39 -0400282 params2.modify(j);
283
284 SkSurfaceCharacterization char2 = params2.createCharacterization(context);
285 if (!char2.isValid()) {
286 continue; // can happen on some platforms (ChromeOS)
287 }
288
289 if (i == j) {
290 REPORTER_ASSERT(reporter, char1 == char2);
291 } else {
292 REPORTER_ASSERT(reporter, char1 != char2);
293 }
294
295 }
296 }
297
298 {
Robert Phillips3cd54322019-07-10 09:28:59 -0400299 SurfaceParameters params(context);
Robert Phillipsc1267c62018-04-04 11:12:39 -0400300
301 SkSurfaceCharacterization valid = params.createCharacterization(context);
302 SkASSERT(valid.isValid());
303
304 SkSurfaceCharacterization inval1, inval2;
305 SkASSERT(!inval1.isValid() && !inval2.isValid());
306
307 REPORTER_ASSERT(reporter, inval1 != inval2);
308 REPORTER_ASSERT(reporter, valid != inval1);
309 REPORTER_ASSERT(reporter, inval1 != valid);
310 }
311}
312
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400313////////////////////////////////////////////////////////////////////////////////
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500314// This tests SkSurfaceCharacterization/SkSurface compatibility
Robert Phillips3cd54322019-07-10 09:28:59 -0400315void DDLSurfaceCharacterizationTestImpl(GrContext* context, skiatest::Reporter* reporter) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500316 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400317 const GrCaps* caps = context->priv().caps();
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500318
Robert Phillips9e441ee2018-02-01 15:14:55 -0500319 // Create a bitmap that we can readback into
320 SkImageInfo imageInfo = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType,
321 kPremul_SkAlphaType);
322 SkBitmap bitmap;
323 bitmap.allocPixels(imageInfo);
324
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500325 std::unique_ptr<SkDeferredDisplayList> ddl;
326
327 // First, create a DDL using the stock SkSurface parameters
328 {
Robert Phillips3cd54322019-07-10 09:28:59 -0400329 SurfaceParameters params(context);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500330
Robert Phillipse8fabb22018-02-04 14:33:21 -0500331 ddl = params.createDDL(context);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500332 SkAssertResult(ddl);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500333
334 // The DDL should draw into an SkSurface created with the same parameters
Robert Phillipsbe77a022018-04-03 17:17:05 -0400335 GrBackendTexture backend;
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500336 sk_sp<SkSurface> s = params.make(context, &backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500337 if (!s) {
338 return;
339 }
340
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500341 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
Robert Phillips9e441ee2018-02-01 15:14:55 -0500342 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400343 context->flush();
344 gpu->testingOnly_flushGpuAndSync();
345 s = nullptr;
346 params.cleanUpBackEnd(context, backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500347 }
348
349 // Then, alter each parameter in turn and check that the DDL & surface are incompatible
350 for (int i = 0; i < SurfaceParameters::kNumParams; ++i) {
Robert Phillips3cd54322019-07-10 09:28:59 -0400351 SurfaceParameters params(context);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500352 params.modify(i);
353
Robert Phillips3cd54322019-07-10 09:28:59 -0400354 if (SurfaceParameters::kProtectedCount == i) {
355 if (context->backend() != GrBackendApi::kVulkan) {
356 // Only the Vulkan backend respects the protected parameter
357 continue;
358 }
359#ifdef SK_VULKAN
360 const GrVkCaps* vkCaps = (const GrVkCaps*) context->priv().caps();
361
362 // And, even then, only when it is a protected context
363 if (!vkCaps->supportsProtectedMemory()) {
364 continue;
365 }
366#endif
367 }
368
Robert Phillipsbe77a022018-04-03 17:17:05 -0400369 GrBackendTexture backend;
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500370 sk_sp<SkSurface> s = params.make(context, &backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500371 if (!s) {
372 continue;
373 }
374
375 if (SurfaceParameters::kSampleCount == i) {
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400376 int supportedSampleCount = caps->getRenderTargetSampleCount(
Greg Daniel6fa62e22019-08-07 15:52:37 -0400377 params.sampleCount(), backend.getBackendFormat());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500378 if (1 == supportedSampleCount) {
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500379 // If changing the sample count won't result in a different
380 // surface characterization, skip this step
Robert Phillipsbe77a022018-04-03 17:17:05 -0400381 s = nullptr;
382 params.cleanUpBackEnd(context, backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500383 continue;
384 }
385 }
386
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400387 if (SurfaceParameters::kMipMipCount == i && !caps->mipMapSupport()) {
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400388 // If changing the mipmap setting won't result in a different surface characterization,
389 // skip this step
Robert Phillipsbe77a022018-04-03 17:17:05 -0400390 s = nullptr;
391 params.cleanUpBackEnd(context, backend);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500392 continue;
393 }
394
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500395 if (SurfaceParameters::kFBO0Count == i && context->backend() != GrBackendApi::kOpenGL) {
396 // FBO0 only affects the surface characterization when using OpenGL
397 s = nullptr;
398 params.cleanUpBackEnd(context, backend);
399 continue;
400 }
401
Robert Phillipse8fabb22018-02-04 14:33:21 -0500402 REPORTER_ASSERT(reporter, !s->draw(ddl.get()),
403 "DDLSurfaceCharacterizationTest failed on parameter: %d\n", i);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400404
405 context->flush();
406 gpu->testingOnly_flushGpuAndSync();
407 s = nullptr;
408 params.cleanUpBackEnd(context, backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500409 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500410
411 // Next test the compatibility of resource cache parameters
412 {
Robert Phillips3cd54322019-07-10 09:28:59 -0400413 const SurfaceParameters params(context);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400414 GrBackendTexture backend;
415
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500416 sk_sp<SkSurface> s = params.make(context, &backend);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500417
Robert Phillipscf39f372019-09-03 10:29:20 -0400418 size_t maxResourceBytes = context->getResourceCacheLimit();
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500419
Robert Phillipscf39f372019-09-03 10:29:20 -0400420 context->setResourceCacheLimit(maxResourceBytes/2);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500421 REPORTER_ASSERT(reporter, !s->draw(ddl.get()));
422
Robert Phillips9e441ee2018-02-01 15:14:55 -0500423 // DDL TODO: once proxies/ops can be de-instantiated we can re-enable these tests.
424 // For now, DDLs are drawn once.
425#if 0
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500426 // resource limits >= those at characterization time are accepted
427 context->setResourceCacheLimits(2*maxResourceCount, maxResourceBytes);
428 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
Robert Phillips9e441ee2018-02-01 15:14:55 -0500429 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500430
431 context->setResourceCacheLimits(maxResourceCount, 2*maxResourceBytes);
432 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
Robert Phillips9e441ee2018-02-01 15:14:55 -0500433 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500434
435 context->setResourceCacheLimits(maxResourceCount, maxResourceBytes);
436 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
Robert Phillips9e441ee2018-02-01 15:14:55 -0500437 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
438#endif
Robert Phillipsbe77a022018-04-03 17:17:05 -0400439
440 context->flush();
441 gpu->testingOnly_flushGpuAndSync();
442 s = nullptr;
443 params.cleanUpBackEnd(context, backend);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500444 }
445
Robert Phillipse8fabb22018-02-04 14:33:21 -0500446 // Test that the textureability of the DDL characterization can block a DDL draw
447 {
448 GrBackendTexture backend;
Robert Phillips3cd54322019-07-10 09:28:59 -0400449 SurfaceParameters params(context);
Robert Phillipsc046ff02019-07-01 10:34:03 -0400450 params.setShouldCreateMipMaps(false);
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500451 params.setTextureable(false);
452
453 sk_sp<SkSurface> s = params.make(context, &backend);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500454 if (s) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500455 REPORTER_ASSERT(reporter, !s->draw(ddl.get())); // bc the DDL was made w/ textureability
Robert Phillipse8fabb22018-02-04 14:33:21 -0500456
Robert Phillipsbe77a022018-04-03 17:17:05 -0400457 context->flush();
458 gpu->testingOnly_flushGpuAndSync();
Robert Phillipse8fabb22018-02-04 14:33:21 -0500459 s = nullptr;
Brian Salomon26102cb2018-03-09 09:33:19 -0500460 params.cleanUpBackEnd(context, backend);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500461 }
462 }
463
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500464 // Make sure non-GPU-backed surfaces fail characterization
465 {
466 SkImageInfo ii = SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType);
467
468 sk_sp<SkSurface> rasterSurface = SkSurface::MakeRaster(ii);
469 SkSurfaceCharacterization c;
470 REPORTER_ASSERT(reporter, !rasterSurface->characterize(&c));
471 }
Robert Phillips94458ee2018-03-06 13:41:51 -0500472
473 // Exercise the createResized method
474 {
Robert Phillips3cd54322019-07-10 09:28:59 -0400475 SurfaceParameters params(context);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400476 GrBackendTexture backend;
Robert Phillips94458ee2018-03-06 13:41:51 -0500477
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500478 sk_sp<SkSurface> s = params.make(context, &backend);
Robert Phillips94458ee2018-03-06 13:41:51 -0500479 if (!s) {
480 return;
481 }
482
483 SkSurfaceCharacterization char0;
484 SkAssertResult(s->characterize(&char0));
485
486 // Too small
487 SkSurfaceCharacterization char1 = char0.createResized(-1, -1);
488 REPORTER_ASSERT(reporter, !char1.isValid());
489
490 // Too large
491 SkSurfaceCharacterization char2 = char0.createResized(1000000, 32);
492 REPORTER_ASSERT(reporter, !char2.isValid());
493
494 // Just right
495 SkSurfaceCharacterization char3 = char0.createResized(32, 32);
496 REPORTER_ASSERT(reporter, char3.isValid());
497 REPORTER_ASSERT(reporter, 32 == char3.width());
498 REPORTER_ASSERT(reporter, 32 == char3.height());
Robert Phillipsbe77a022018-04-03 17:17:05 -0400499
500 s = nullptr;
501 params.cleanUpBackEnd(context, backend);
Robert Phillips94458ee2018-03-06 13:41:51 -0500502 }
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500503}
504
Robert Phillips3cd54322019-07-10 09:28:59 -0400505DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLSurfaceCharacterizationTest, reporter, ctxInfo) {
506 GrContext* context = ctxInfo.grContext();
507
508 DDLSurfaceCharacterizationTestImpl(context, reporter);
509}
510
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500511// Test that a DDL created w/o textureability can be replayed into both a textureable and
512// non-textureable destination. Note that DDLSurfaceCharacterizationTest tests that a
513// textureable DDL cannot be played into a non-textureable destination but can be replayed
514// into a textureable destination.
515DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLNonTextureabilityTest, reporter, ctxInfo) {
516 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500517 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500518
519 // Create a bitmap that we can readback into
520 SkImageInfo imageInfo = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType,
521 kPremul_SkAlphaType);
522 SkBitmap bitmap;
523 bitmap.allocPixels(imageInfo);
524
525 for (bool textureability : { true, false }) {
526 std::unique_ptr<SkDeferredDisplayList> ddl;
527
Robert Phillipsc046ff02019-07-01 10:34:03 -0400528 // First, create a DDL w/o textureability (and thus no mipmaps). TODO: once we have
529 // reusable DDLs, move this outside of the loop.
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500530 {
Robert Phillips3cd54322019-07-10 09:28:59 -0400531 SurfaceParameters params(context);
Robert Phillipsc046ff02019-07-01 10:34:03 -0400532 params.setShouldCreateMipMaps(false);
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500533 params.setTextureable(false);
534
535 ddl = params.createDDL(context);
536 SkAssertResult(ddl);
537 }
538
539 // Then verify it can draw into either flavor of destination
Robert Phillips3cd54322019-07-10 09:28:59 -0400540 SurfaceParameters params(context);
Robert Phillipsc046ff02019-07-01 10:34:03 -0400541 params.setShouldCreateMipMaps(textureability);
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500542 params.setTextureable(textureability);
543
544 GrBackendTexture backend;
545 sk_sp<SkSurface> s = params.make(context, &backend);
546 if (!s) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500547 continue;
548 }
549
550 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
551 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
552 context->flush();
553 gpu->testingOnly_flushGpuAndSync();
554 s = nullptr;
555 params.cleanUpBackEnd(context, backend);
556 }
557
558}
559
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400560static void test_make_render_target(skiatest::Reporter* reporter,
561 GrContext* context,
562 const SurfaceParameters& params) {
563 {
564 const SkSurfaceCharacterization c = params.createCharacterization(context);
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400565
Robert Phillipsbe77a022018-04-03 17:17:05 -0400566 if (!c.isValid()) {
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400567 GrBackendTexture backend;
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500568 sk_sp<SkSurface> tmp = params.make(context, &backend);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400569
570 // If we couldn't characterize the surface we shouldn't be able to create it either
571 REPORTER_ASSERT(reporter, !tmp);
572 if (tmp) {
573 tmp = nullptr;
574 params.cleanUpBackEnd(context, backend);
575 }
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400576 return;
577 }
578 }
579
580 const SkSurfaceCharacterization c = params.createCharacterization(context);
581 GrBackendTexture backend;
582
583 {
584 sk_sp<SkSurface> s = params.make(context, &backend);
585 REPORTER_ASSERT(reporter, s);
586 if (!s) {
587 REPORTER_ASSERT(reporter, !c.isValid());
588 params.cleanUpBackEnd(context, backend);
589 return;
590 }
591
592 REPORTER_ASSERT(reporter, c.isValid());
Robert Phillipsc046ff02019-07-01 10:34:03 -0400593 REPORTER_ASSERT(reporter, c.isCompatible(backend));
Robert Phillips9907e6e2019-06-25 14:47:04 -0400594 REPORTER_ASSERT(reporter, s->isCompatible(c));
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400595 // Note that we're leaving 'backend' live here
596 }
597
598 // Make an SkSurface from scratch
599 {
600 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, c, SkBudgeted::kYes);
601 REPORTER_ASSERT(reporter, s);
Robert Phillips9907e6e2019-06-25 14:47:04 -0400602 REPORTER_ASSERT(reporter, s->isCompatible(c));
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400603 }
604
Robert Phillips02dc0302019-07-02 17:58:27 -0400605 // Make an SkSurface that wraps the existing backend texture
606 {
607 sk_sp<SkSurface> s = SkSurface::MakeFromBackendTexture(context, c, backend);
608 REPORTER_ASSERT(reporter, s);
609 REPORTER_ASSERT(reporter, s->isCompatible(c));
610 }
611
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400612 params.cleanUpBackEnd(context, backend);
613}
614
615////////////////////////////////////////////////////////////////////////////////
616// This tests the SkSurface::MakeRenderTarget variants that take an SkSurfaceCharacterization.
617// In particular, the SkSurface, backendTexture and SkSurfaceCharacterization
618// should always be compatible.
Robert Phillips3cd54322019-07-10 09:28:59 -0400619void DDLMakeRenderTargetTestImpl(GrContext* context, skiatest::Reporter* reporter) {
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400620 for (int i = 0; i < SurfaceParameters::kNumParams; ++i) {
621
622 if (SurfaceParameters::kFBO0Count == i) {
623 // MakeRenderTarget doesn't support FBO0
Robert Phillipsbe77a022018-04-03 17:17:05 -0400624 continue;
625 }
626
Robert Phillips3cd54322019-07-10 09:28:59 -0400627 if (SurfaceParameters::kProtectedCount == i) {
628 if (context->backend() != GrBackendApi::kVulkan) {
629 // Only the Vulkan backend respects the protected parameter
630 continue;
631 }
632#ifdef SK_VULKAN
633 const GrVkCaps* vkCaps = (const GrVkCaps*) context->priv().caps();
634
635 // And, even then, only when it is a protected context
636 if (!vkCaps->supportsProtectedMemory()) {
637 continue;
638 }
639#endif
640 }
641
642
643 SurfaceParameters params(context);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400644 params.modify(i);
645
Brian Salomon4687bdd2019-05-09 16:28:04 -0400646 if (!context->priv().caps()->mipMapSupport()) {
647 params.setShouldCreateMipMaps(false);
648 }
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400649
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400650 test_make_render_target(reporter, context, params);
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400651 }
652}
653
Robert Phillips3cd54322019-07-10 09:28:59 -0400654DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLMakeRenderTargetTest, reporter, ctxInfo) {
655 GrContext* context = ctxInfo.grContext();
656
657 DDLMakeRenderTargetTestImpl(context, reporter);
658}
659
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400660////////////////////////////////////////////////////////////////////////////////
Greg Danielf2336e42018-01-23 16:38:14 -0500661static constexpr int kSize = 8;
662
663struct TextureReleaseChecker {
664 TextureReleaseChecker() : fReleaseCount(0) {}
665 int fReleaseCount;
666 static void Release(void* self) {
667 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
668 }
669};
670
671enum class DDLStage { kMakeImage, kDrawImage, kDetach, kDrawDDL };
672
673// This tests the ability to create and use wrapped textures in a DDL world
674DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLWrapBackendTest, reporter, ctxInfo) {
675 GrContext* context = ctxInfo.grContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400676
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400677 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400678 kSize, kSize, kRGBA_8888_SkColorType,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400679 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Brian Salomonf7778972018-03-08 10:13:17 -0500680 if (!backendTex.isValid()) {
681 return;
Greg Danielf2336e42018-01-23 16:38:14 -0500682 }
Brian Salomonf7778972018-03-08 10:13:17 -0500683
Robert Phillips3cd54322019-07-10 09:28:59 -0400684 SurfaceParameters params(context);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400685 GrBackendTexture backend;
Brian Salomonf7778972018-03-08 10:13:17 -0500686
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500687 sk_sp<SkSurface> s = params.make(context, &backend);
Brian Salomonf7778972018-03-08 10:13:17 -0500688 if (!s) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400689 context->deleteBackendTexture(backendTex);
Brian Salomonf7778972018-03-08 10:13:17 -0500690 return;
691 }
692
693 SkSurfaceCharacterization c;
694 SkAssertResult(s->characterize(&c));
695
696 std::unique_ptr<SkDeferredDisplayListRecorder> recorder(new SkDeferredDisplayListRecorder(c));
697
698 SkCanvas* canvas = recorder->getCanvas();
699 if (!canvas) {
Robert Phillipsbe77a022018-04-03 17:17:05 -0400700 s = nullptr;
701 params.cleanUpBackEnd(context, backend);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400702 context->deleteBackendTexture(backendTex);
Brian Salomonf7778972018-03-08 10:13:17 -0500703 return;
704 }
705
706 GrContext* deferredContext = canvas->getGrContext();
707 if (!deferredContext) {
Robert Phillipsbe77a022018-04-03 17:17:05 -0400708 s = nullptr;
709 params.cleanUpBackEnd(context, backend);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400710 context->deleteBackendTexture(backendTex);
Brian Salomonf7778972018-03-08 10:13:17 -0500711 return;
712 }
713
714 // Wrapped Backend Textures are not supported in DDL
715 sk_sp<SkImage> image =
716 SkImage::MakeFromAdoptedTexture(deferredContext, backendTex, kTopLeft_GrSurfaceOrigin,
717 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
718 REPORTER_ASSERT(reporter, !image);
719
720 TextureReleaseChecker releaseChecker;
721 image = SkImage::MakeFromTexture(deferredContext, backendTex, kTopLeft_GrSurfaceOrigin,
722 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr,
723 TextureReleaseChecker::Release, &releaseChecker);
724 REPORTER_ASSERT(reporter, !image);
725
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400726 context->deleteBackendTexture(backendTex);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400727
728 s = nullptr;
729 params.cleanUpBackEnd(context, backend);
Greg Danielf2336e42018-01-23 16:38:14 -0500730}
731
Brian Salomon59ef8c32019-01-30 12:20:56 -0500732static sk_sp<SkPromiseImageTexture> dummy_fulfill_proc(void*) {
733 SkASSERT(0);
734 return nullptr;
735}
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400736static void dummy_release_proc(void*) { SkASSERT(0); }
Brian Salomon59ef8c32019-01-30 12:20:56 -0500737static void dummy_done_proc(void*) {}
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400738
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400739////////////////////////////////////////////////////////////////////////////////
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400740// Test out the behavior of an invalid DDLRecorder
741DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLInvalidRecorder, reporter, ctxInfo) {
742 GrContext* context = ctxInfo.grContext();
743
744 {
745 SkImageInfo ii = SkImageInfo::MakeN32Premul(32, 32);
746 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
747
748 SkSurfaceCharacterization characterization;
749 SkAssertResult(s->characterize(&characterization));
750
751 // never calling getCanvas means the backing surface is never allocated
752 SkDeferredDisplayListRecorder recorder(characterization);
753 }
754
755 {
756 SkSurfaceCharacterization invalid;
757
758 SkDeferredDisplayListRecorder recorder(invalid);
759
760 const SkSurfaceCharacterization c = recorder.characterization();
761 REPORTER_ASSERT(reporter, !c.isValid());
762 REPORTER_ASSERT(reporter, !recorder.getCanvas());
763 REPORTER_ASSERT(reporter, !recorder.detach());
764
Robert Phillips0a15cc62019-07-30 12:49:10 -0400765 GrBackendFormat format = context->defaultBackendFormat(kRGBA_8888_SkColorType,
766 GrRenderable::kNo);
767 SkASSERT(format.isValid());
Greg Daniel60ea40c2019-02-12 13:38:44 -0500768
Brian Salomonf55e8d52019-01-30 17:28:20 -0500769 sk_sp<SkImage> image = recorder.makePromiseTexture(
770 format, 32, 32, GrMipMapped::kNo,
771 kTopLeft_GrSurfaceOrigin,
772 kRGBA_8888_SkColorType,
773 kPremul_SkAlphaType, nullptr,
774 dummy_fulfill_proc,
775 dummy_release_proc,
776 dummy_done_proc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500777 nullptr,
778 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400779 REPORTER_ASSERT(reporter, !image);
780 }
781
782}
783
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400784////////////////////////////////////////////////////////////////////////////////
Robert Phillips874b5352018-03-16 08:48:24 -0400785// Ensure that flushing while DDL recording doesn't cause a crash
786DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLFlushWhileRecording, reporter, ctxInfo) {
787 GrContext* context = ctxInfo.grContext();
788
789 SkImageInfo ii = SkImageInfo::MakeN32Premul(32, 32);
790 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
791
792 SkSurfaceCharacterization characterization;
793 SkAssertResult(s->characterize(&characterization));
794
795 SkDeferredDisplayListRecorder recorder(characterization);
796 SkCanvas* canvas = recorder.getCanvas();
797
Robert Phillips874b5352018-03-16 08:48:24 -0400798 canvas->getGrContext()->flush();
799}
Greg Danielf2336e42018-01-23 16:38:14 -0500800
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400801////////////////////////////////////////////////////////////////////////////////
Robert Phillipsee5fd132019-05-07 13:29:22 -0400802// Test that flushing a DDL via SkSurface::flush works
803
804struct FulfillInfo {
805 sk_sp<SkPromiseImageTexture> fTex;
806 bool fFulfilled = false;
807 bool fReleased = false;
808 bool fDone = false;
809};
810
811static sk_sp<SkPromiseImageTexture> tracking_fulfill_proc(void* context) {
812 FulfillInfo* info = (FulfillInfo*) context;
813 info->fFulfilled = true;
814 return info->fTex;
815}
816
817static void tracking_release_proc(void* context) {
818 FulfillInfo* info = (FulfillInfo*) context;
819 info->fReleased = true;
820}
821
822static void tracking_done_proc(void* context) {
823 FulfillInfo* info = (FulfillInfo*) context;
824 info->fDone = true;
825}
826
827DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLSkSurfaceFlush, reporter, ctxInfo) {
828 GrContext* context = ctxInfo.grContext();
829
830 SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
831 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
832
833 SkSurfaceCharacterization characterization;
834 SkAssertResult(s->characterize(&characterization));
835
836 GrBackendTexture backendTexture;
837
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400838 if (!create_backend_texture(context, &backendTexture, ii, SkColors::kCyan,
839 GrMipMapped::kNo, GrRenderable::kNo)) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400840 REPORTER_ASSERT(reporter, false);
841 return;
842 }
843
844 FulfillInfo fulfillInfo;
845 fulfillInfo.fTex = SkPromiseImageTexture::Make(backendTexture);
846
847 std::unique_ptr<SkDeferredDisplayList> ddl;
848
849 {
850 SkDeferredDisplayListRecorder recorder(characterization);
851
Robert Phillips0a15cc62019-07-30 12:49:10 -0400852 GrBackendFormat format = context->defaultBackendFormat(kRGBA_8888_SkColorType,
853 GrRenderable::kNo);
854 SkASSERT(format.isValid());
Robert Phillipsee5fd132019-05-07 13:29:22 -0400855
856 sk_sp<SkImage> promiseImage = recorder.makePromiseTexture(
857 format, 32, 32, GrMipMapped::kNo,
858 kTopLeft_GrSurfaceOrigin,
859 kRGBA_8888_SkColorType,
860 kPremul_SkAlphaType, nullptr,
861 tracking_fulfill_proc,
862 tracking_release_proc,
863 tracking_done_proc,
864 &fulfillInfo,
865 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
866
867 SkCanvas* canvas = recorder.getCanvas();
868
869 canvas->clear(SK_ColorRED);
870 canvas->drawImage(promiseImage, 0, 0);
871 ddl = recorder.detach();
872 }
873
Robert Phillips15c91422019-05-07 16:54:48 -0400874 context->flush();
875
Robert Phillipsee5fd132019-05-07 13:29:22 -0400876 s->draw(ddl.get());
877
878 GrFlushInfo flushInfo;
Robert Phillips8e49a692019-05-07 15:57:25 -0400879 s->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
Robert Phillipsee5fd132019-05-07 13:29:22 -0400880
881 REPORTER_ASSERT(reporter, fulfillInfo.fFulfilled);
882 REPORTER_ASSERT(reporter, fulfillInfo.fReleased);
883
Robert Phillips8e49a692019-05-07 15:57:25 -0400884 if (GrBackendApi::kVulkan == context->backend() ||
885 GrBackendApi::kMetal == context->backend()) {
Robert Phillips15c91422019-05-07 16:54:48 -0400886 // In order to receive the done callback with Vulkan we need to perform the equivalent
Robert Phillipsee5fd132019-05-07 13:29:22 -0400887 // of a glFinish
888 GrFlushInfo flushInfoSyncCpu;
889 flushInfoSyncCpu.fFlags = kSyncCpu_GrFlushFlag;
Robert Phillips8e49a692019-05-07 15:57:25 -0400890 s->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfoSyncCpu);
Robert Phillipsee5fd132019-05-07 13:29:22 -0400891 }
892
893 REPORTER_ASSERT(reporter, fulfillInfo.fDone);
894
895 REPORTER_ASSERT(reporter, fulfillInfo.fTex->unique());
896 fulfillInfo.fTex.reset();
897
898 delete_backend_texture(context, backendTexture);
899}
900
901////////////////////////////////////////////////////////////////////////////////
Robert Phillipsf54883c2018-12-18 08:29:09 -0500902// Ensure that reusing a single DDLRecorder to create multiple DDLs works cleanly
903DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLMultipleDDLs, reporter, ctxInfo) {
904 GrContext* context = ctxInfo.grContext();
905
906 SkImageInfo ii = SkImageInfo::MakeN32Premul(32, 32);
907 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
908
909 SkBitmap bitmap;
910 bitmap.allocPixels(ii);
911
912 SkSurfaceCharacterization characterization;
913 SkAssertResult(s->characterize(&characterization));
914
915 SkDeferredDisplayListRecorder recorder(characterization);
916
917 SkCanvas* canvas1 = recorder.getCanvas();
918
919 canvas1->clear(SK_ColorRED);
920
921 canvas1->save();
922 canvas1->clipRect(SkRect::MakeXYWH(8, 8, 16, 16));
923
924 std::unique_ptr<SkDeferredDisplayList> ddl1 = recorder.detach();
925
926 SkCanvas* canvas2 = recorder.getCanvas();
927
928 SkPaint p;
929 p.setColor(SK_ColorGREEN);
930 canvas2->drawRect(SkRect::MakeWH(32, 32), p);
931
932 std::unique_ptr<SkDeferredDisplayList> ddl2 = recorder.detach();
933
934 REPORTER_ASSERT(reporter, ddl1->priv().lazyProxyData());
935 REPORTER_ASSERT(reporter, ddl2->priv().lazyProxyData());
936
937 // The lazy proxy data being different ensures that the SkSurface, SkCanvas and backing-
938 // lazy proxy are all different between the two DDLs
939 REPORTER_ASSERT(reporter, ddl1->priv().lazyProxyData() != ddl2->priv().lazyProxyData());
940
941 s->draw(ddl1.get());
942 s->draw(ddl2.get());
943
944 // Make sure the clipRect from DDL1 didn't percolate into DDL2
945 s->readPixels(ii, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
946 for (int y = 0; y < 32; ++y) {
947 for (int x = 0; x < 32; ++x) {
948 REPORTER_ASSERT(reporter, bitmap.getColor(x, y) == SK_ColorGREEN);
949 if (bitmap.getColor(x, y) != SK_ColorGREEN) {
950 return; // we only really need to report the error once
951 }
952 }
953 }
954}
955
956////////////////////////////////////////////////////////////////////////////////
Robert Phillipsabf7b762018-03-21 12:13:37 -0400957// Check that the texture-specific flags (i.e., for external & rectangle textures) work
958// for promise images. As such, this is a GL-only test.
959DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(DDLTextureFlagsTest, reporter, ctxInfo) {
960 GrContext* context = ctxInfo.grContext();
961
962 SkImageInfo ii = SkImageInfo::MakeN32Premul(32, 32);
963 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
964
965 SkSurfaceCharacterization characterization;
966 SkAssertResult(s->characterize(&characterization));
967
968 SkDeferredDisplayListRecorder recorder(characterization);
969
970 for (GrGLenum target : { GR_GL_TEXTURE_EXTERNAL, GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_2D } ) {
Greg Daniel09c94002018-06-08 22:11:51 +0000971 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
972 GrBackendFormat format = GrBackendFormat::MakeGL(GR_GL_RGBA8, target);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400973
Brian Salomonf55e8d52019-01-30 17:28:20 -0500974 sk_sp<SkImage> image = recorder.makePromiseTexture(
975 format, 32, 32, mipMapped,
976 kTopLeft_GrSurfaceOrigin,
977 kRGBA_8888_SkColorType,
978 kPremul_SkAlphaType, nullptr,
979 dummy_fulfill_proc,
980 dummy_release_proc,
981 dummy_done_proc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500982 nullptr,
983 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Greg Daniel09c94002018-06-08 22:11:51 +0000984 if (GR_GL_TEXTURE_2D != target && mipMapped == GrMipMapped::kYes) {
985 REPORTER_ASSERT(reporter, !image);
986 continue;
987 }
988 REPORTER_ASSERT(reporter, image);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400989
Jim Van Verth21bd60d2018-10-12 15:00:20 -0400990 GrTextureProxy* backingProxy = ((SkImage_GpuBase*) image.get())->peekProxy();
Robert Phillipsabf7b762018-03-21 12:13:37 -0400991
Greg Daniel09c94002018-06-08 22:11:51 +0000992 REPORTER_ASSERT(reporter, backingProxy->mipMapped() == mipMapped);
993 if (GR_GL_TEXTURE_2D == target) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400994 REPORTER_ASSERT(reporter, !backingProxy->hasRestrictedSampling());
Greg Daniel09c94002018-06-08 22:11:51 +0000995 } else {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400996 REPORTER_ASSERT(reporter, backingProxy->hasRestrictedSampling());
Greg Daniel09c94002018-06-08 22:11:51 +0000997 }
Robert Phillipsabf7b762018-03-21 12:13:37 -0400998 }
999 }
Robert Phillipsbe77a022018-04-03 17:17:05 -04001000}
1001
1002////////////////////////////////////////////////////////////////////////////////
Robert Phillips646f6372018-09-25 09:31:10 -04001003// Test colorType and pixelConfig compatibility.
Robert Phillipsbe77a022018-04-03 17:17:05 -04001004DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(DDLCompatibilityTest, reporter, ctxInfo) {
1005 GrContext* context = ctxInfo.grContext();
1006
1007 for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) {
1008 SkColorType colorType = static_cast<SkColorType>(ct);
1009
Robert Phillips3cd54322019-07-10 09:28:59 -04001010 SurfaceParameters params(context);
Greg Daniel60ea40c2019-02-12 13:38:44 -05001011 params.setColorType(colorType);
1012 params.setColorSpace(nullptr);
Robert Phillipsbe77a022018-04-03 17:17:05 -04001013
Brian Salomon4687bdd2019-05-09 16:28:04 -04001014 if (!context->priv().caps()->mipMapSupport()) {
1015 params.setShouldCreateMipMaps(false);
1016 }
Greg Daniel60ea40c2019-02-12 13:38:44 -05001017
Robert Phillipsd8f79a22019-06-24 13:25:42 -04001018 test_make_render_target(reporter, context, params);
Robert Phillipsbe77a022018-04-03 17:17:05 -04001019 }
Robert Phillipsabf7b762018-03-21 12:13:37 -04001020
1021}