blob: afe93ad61cdd4529b6875f785d04bbea7d38ce74 [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
8#include "SkTypes.h"
9
Greg Danielf2336e42018-01-23 16:38:14 -050010#include "GrBackendSurface.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040011#include "GrCaps.h"
12#include "GrContext.h"
13#include "GrContextFactory.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040014#include "GrContextPriv.h"
Greg Danielf2336e42018-01-23 16:38:14 -050015#include "GrGpu.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040016#include "GrRenderTargetContext.h"
17#include "GrRenderTargetProxy.h"
18#include "GrTextureProxy.h"
Robert Phillipsabf7b762018-03-21 12:13:37 -040019#include "GrTextureProxyPriv.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040020#include "GrTypes.h"
21#include "GrTypesPriv.h"
22#include "SkBitmap.h"
Robert Phillips7ffbcf92017-12-04 12:52:46 -050023#include "SkCanvas.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040024#include "SkColorSpace.h"
25#include "SkDeferredDisplayList.h"
Robert Phillips7ffbcf92017-12-04 12:52:46 -050026#include "SkDeferredDisplayListRecorder.h"
27#include "SkGpuDevice.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040028#include "SkImage.h"
29#include "SkImageInfo.h"
Robert Phillipsabf7b762018-03-21 12:13:37 -040030#include "SkImage_Gpu.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040031#include "SkPaint.h"
32#include "SkRect.h"
33#include "SkRefCnt.h"
Robert Phillips7ffbcf92017-12-04 12:52:46 -050034#include "SkSurface.h"
Robert Phillips7ffbcf92017-12-04 12:52:46 -050035#include "SkSurfaceCharacterization.h"
36#include "SkSurfaceProps.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040037#include "SkSurface_Gpu.h"
Robert Phillips7ffbcf92017-12-04 12:52:46 -050038#include "Test.h"
Robert Phillipsbe77a022018-04-03 17:17:05 -040039#include "gl/GrGLCaps.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040040#include "gl/GrGLDefines.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040041#include "gl/GrGLTypes.h"
42
Robert Phillipsfc711a22018-02-13 17:03:00 -050043#ifdef SK_VULKAN
44#include "vk/GrVkDefines.h"
45#endif
46
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040047#include <initializer_list>
48#include <memory>
49#include <utility>
50
Robert Phillipsbe77a022018-04-03 17:17:05 -040051// Try to create a backend format from the provided colorType and config. Return an invalid
52// backend format if the combination is infeasible.
53static GrBackendFormat create_backend_format(GrContext* context,
54 SkColorType ct, SkColorSpace* cs,
55 GrPixelConfig config) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -040056 const GrCaps* caps = context->contextPriv().caps();
Robert Phillipsfc711a22018-02-13 17:03:00 -050057
Robert Phillipsbe77a022018-04-03 17:17:05 -040058 // TODO: what should be done if we have a colorspace that doesn't have a gammaCloseToSRGB?
59
Robert Phillipsfc711a22018-02-13 17:03:00 -050060 switch (context->contextPriv().getBackend()) {
Robert Phillipsbe77a022018-04-03 17:17:05 -040061 case kOpenGL_GrBackend: {
Brian Salomonc7fe0f72018-05-11 10:14:21 -040062 const GrGLCaps* glCaps = static_cast<const GrGLCaps*>(caps);
Robert Phillipsbe77a022018-04-03 17:17:05 -040063 GrGLStandard standard = glCaps->standard();
64
65 switch (ct) {
66 case kUnknown_SkColorType:
67 return GrBackendFormat();
68 case kAlpha_8_SkColorType:
69 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
70 return GrBackendFormat::MakeGL(GR_GL_ALPHA8, GR_GL_TEXTURE_2D);
71 } else if (kAlpha_8_GrPixelConfig == config ||
72 kAlpha_8_as_Red_GrPixelConfig == config) {
73 return GrBackendFormat::MakeGL(GR_GL_R8, GR_GL_TEXTURE_2D);
74 }
75 break;
76 case kRGB_565_SkColorType:
77 if (kRGB_565_GrPixelConfig == config) {
78 return GrBackendFormat::MakeGL(GR_GL_RGB565, GR_GL_TEXTURE_2D);
79 }
80 break;
81 case kARGB_4444_SkColorType:
82 if (kRGBA_4444_GrPixelConfig == config) {
83 return GrBackendFormat::MakeGL(GR_GL_RGBA4, GR_GL_TEXTURE_2D);
84 }
85 break;
86 case kRGBA_8888_SkColorType:
87 if (kRGBA_8888_GrPixelConfig == config) {
88 if (!cs || (cs->gammaCloseToSRGB() && !caps->srgbSupport())) {
89 return GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_2D);
90 }
91 } else if (kSRGBA_8888_GrPixelConfig == config) {
92 if (caps->srgbSupport() && cs && cs->gammaCloseToSRGB()) {
93 return GrBackendFormat::MakeGL(GR_GL_SRGB8_ALPHA8, GR_GL_TEXTURE_2D);
94 }
95 }
96 break;
97 case kRGB_888x_SkColorType:
98 if (kRGB_888_GrPixelConfig == config) {
99 return GrBackendFormat::MakeGL(GR_GL_RGB8, GR_GL_TEXTURE_2D);
100 }
101 break;
102 case kBGRA_8888_SkColorType:
103 if (kBGRA_8888_GrPixelConfig == config) {
104 if (kGL_GrGLStandard == standard) {
105 return GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_2D);
106 } else if (kGLES_GrGLStandard == standard) {
107 return GrBackendFormat::MakeGL(GR_GL_BGRA8, GR_GL_TEXTURE_2D);
108 }
109 } else if (kSBGRA_8888_GrPixelConfig == config) {
110 if (caps->srgbSupport() && cs && cs->gammaCloseToSRGB()) {
111 return GrBackendFormat::MakeGL(GR_GL_SRGB8_ALPHA8, GR_GL_TEXTURE_2D);
112 }
113 }
114 break;
115 case kRGBA_1010102_SkColorType:
116 if (kRGBA_1010102_GrPixelConfig == config) {
117 return GrBackendFormat::MakeGL(GR_GL_RGB10_A2, GR_GL_TEXTURE_2D);
118 }
119 break;
120 case kRGB_101010x_SkColorType:
121 return GrBackendFormat();
122 case kGray_8_SkColorType:
123 if (kGray_8_as_Lum_GrPixelConfig == config) {
124 return GrBackendFormat::MakeGL(GR_GL_LUMINANCE8, GR_GL_TEXTURE_2D);
125 } else if (kGray_8_GrPixelConfig == config ||
126 kGray_8_as_Red_GrPixelConfig == config) {
127 return GrBackendFormat::MakeGL(GR_GL_R8, GR_GL_TEXTURE_2D);
128 }
129 break;
130 case kRGBA_F16_SkColorType:
131 if (kRGBA_half_GrPixelConfig == config) {
132 return GrBackendFormat::MakeGL(GR_GL_RGBA16F, GR_GL_TEXTURE_2D);
133 }
134 break;
Mike Klein37854712018-06-26 11:43:06 -0400135 case kRGBA_F32_SkColorType:
136 return GrBackendFormat();
Robert Phillipsfc711a22018-02-13 17:03:00 -0500137 }
Robert Phillipsbe77a022018-04-03 17:17:05 -0400138 }
139 break;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500140#ifdef SK_VULKAN
141 case kVulkan_GrBackend:
Robert Phillipsbe77a022018-04-03 17:17:05 -0400142 switch (ct) {
143 case kUnknown_SkColorType:
144 return GrBackendFormat();
145 case kAlpha_8_SkColorType:
146 // TODO: what about kAlpha_8_GrPixelConfig and kAlpha_8_as_Alpha_GrPixelConfig
147 if (kAlpha_8_as_Red_GrPixelConfig == config) {
148 return GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM);
149 }
150 break;
151 case kRGB_565_SkColorType:
152 if (kRGB_565_GrPixelConfig == config) {
153 return GrBackendFormat::MakeVk(VK_FORMAT_R5G6B5_UNORM_PACK16);
154 }
155 break;
156 case kARGB_4444_SkColorType:
157 if (kRGBA_4444_GrPixelConfig == config) {
158 return GrBackendFormat::MakeVk(VK_FORMAT_B4G4R4A4_UNORM_PACK16);
159 }
160 break;
161 case kRGBA_8888_SkColorType:
162 if (kRGBA_8888_GrPixelConfig == config) {
163 if (!cs || (cs->gammaCloseToSRGB() && !caps->srgbSupport())) {
164 return GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM);
165 }
166 } else if (kSRGBA_8888_GrPixelConfig == config) {
167 if (caps->srgbSupport() && cs && cs->gammaCloseToSRGB()) {
168 return GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_SRGB);
169 }
170 }
171 break;
172 case kRGB_888x_SkColorType:
173 if (kRGB_888_GrPixelConfig == config) {
174 return GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8_UNORM);
175 }
176 break;
177 case kBGRA_8888_SkColorType:
178 if (kBGRA_8888_GrPixelConfig == config) {
179 return GrBackendFormat::MakeVk(VK_FORMAT_B8G8R8A8_UNORM);
180 } else if (kSBGRA_8888_GrPixelConfig == config) {
181 if (caps->srgbSupport() && cs && cs->gammaCloseToSRGB()) {
182 return GrBackendFormat::MakeVk(VK_FORMAT_B8G8R8A8_SRGB);
183 }
184 }
185 break;
186 case kRGBA_1010102_SkColorType:
187 if (kRGBA_1010102_GrPixelConfig == config) {
188 return GrBackendFormat::MakeVk(VK_FORMAT_A2B10G10R10_UNORM_PACK32);
189 }
190 break;
191 case kRGB_101010x_SkColorType:
192 return GrBackendFormat();
193 case kGray_8_SkColorType:
194 // TODO: what about kAlpha_8_GrPixelConfig and kGray_8_as_Lum_GrPixelConfig?
195 if (kGray_8_as_Red_GrPixelConfig == config) {
196 return GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM);
197 }
198 break;
199 case kRGBA_F16_SkColorType:
200 if (kRGBA_half_GrPixelConfig == config) {
201 return GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT);
202 }
203 break;
Mike Klein37854712018-06-26 11:43:06 -0400204 case kRGBA_F32_SkColorType:
205 return GrBackendFormat();
Robert Phillipsfc711a22018-02-13 17:03:00 -0500206 }
207 break;
208#endif
209 case kMock_GrBackend:
Robert Phillipsbe77a022018-04-03 17:17:05 -0400210 switch (ct) {
211 case kUnknown_SkColorType:
212 return GrBackendFormat();
213 case kAlpha_8_SkColorType:
214 if (kAlpha_8_GrPixelConfig == config ||
215 kAlpha_8_as_Alpha_GrPixelConfig == config ||
216 kAlpha_8_as_Red_GrPixelConfig == config) {
217 return GrBackendFormat::MakeMock(config);
218 }
219 break;
220 case kRGB_565_SkColorType:
221 if (kRGB_565_GrPixelConfig == config) {
222 return GrBackendFormat::MakeMock(config);
223 }
224 break;
225 case kARGB_4444_SkColorType:
226 if (kRGBA_4444_GrPixelConfig == config) {
227 return GrBackendFormat::MakeMock(config);
228 }
229 break;
230 case kRGBA_8888_SkColorType:
231 if (kRGBA_8888_GrPixelConfig == config) {
232 if (!cs || (cs->gammaCloseToSRGB() && !caps->srgbSupport())) {
233 return GrBackendFormat::MakeMock(config);
234 }
235 } else if (kSRGBA_8888_GrPixelConfig == config) {
236 if (caps->srgbSupport() && cs && cs->gammaCloseToSRGB()) {
237 return GrBackendFormat::MakeMock(config);
238 }
239 }
240 break;
241 case kRGB_888x_SkColorType:
242 if (kRGB_888_GrPixelConfig == config) {
243 return GrBackendFormat::MakeMock(config);
244 }
245 break;
246 case kBGRA_8888_SkColorType:
247 if (kBGRA_8888_GrPixelConfig == config) {
248 return GrBackendFormat::MakeMock(config);
249 } else if (kSBGRA_8888_GrPixelConfig == config) {
250 if (caps->srgbSupport() && cs && cs->gammaCloseToSRGB()) {
251 return GrBackendFormat::MakeMock(config);
252 }
253 }
254 break;
255 case kRGBA_1010102_SkColorType:
256 if (kRGBA_1010102_GrPixelConfig == config) {
257 return GrBackendFormat::MakeMock(config);
258 }
259 break;
260 case kRGB_101010x_SkColorType:
261 return GrBackendFormat();
262 case kGray_8_SkColorType:
263 if (kGray_8_GrPixelConfig == config ||
264 kGray_8_as_Lum_GrPixelConfig == config ||
265 kGray_8_as_Red_GrPixelConfig == config) {
266 return GrBackendFormat::MakeMock(config);
267 }
268 break;
269 case kRGBA_F16_SkColorType:
270 if (kRGBA_half_GrPixelConfig == config) {
271 return GrBackendFormat::MakeMock(config);
272 }
273 break;
Mike Klein37854712018-06-26 11:43:06 -0400274 case kRGBA_F32_SkColorType:
275 return GrBackendFormat();
Robert Phillipsfc711a22018-02-13 17:03:00 -0500276 }
277 break;
278 default:
279 return GrBackendFormat(); // return an invalid format
280 }
281
282 return GrBackendFormat(); // return an invalid format
283}
284
285
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500286class SurfaceParameters {
287public:
Robert Phillipse8fabb22018-02-04 14:33:21 -0500288 static const int kNumParams = 9;
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500289 static const int kSampleCount = 5;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500290 static const int kMipMipCount = 8;
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500291
Robert Phillipsbe77a022018-04-03 17:17:05 -0400292 SurfaceParameters(const GrCaps* caps)
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500293 : fWidth(64)
294 , fHeight(64)
295 , fOrigin(kTopLeft_GrSurfaceOrigin)
296 , fColorType(kRGBA_8888_SkColorType)
Robert Phillipsbe77a022018-04-03 17:17:05 -0400297 , fConfig(caps->srgbSupport() ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig)
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500298 , fColorSpace(SkColorSpace::MakeSRGB())
Brian Salomonbdecacf2018-02-02 20:32:49 -0500299 , fSampleCount(1)
Robert Phillipse8fabb22018-02-04 14:33:21 -0500300 , fSurfaceProps(0x0, kUnknown_SkPixelGeometry)
301 , fShouldCreateMipMaps(true) {
302 }
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500303
304 int sampleCount() const { return fSampleCount; }
305
Robert Phillipsbe77a022018-04-03 17:17:05 -0400306 void setColorType(SkColorType ct) { fColorType = ct; }
307 void setColorSpace(sk_sp<SkColorSpace> cs) { fColorSpace = std::move(cs); }
308 void setConfig(GrPixelConfig config) { fConfig = config; }
309
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500310 // Modify the SurfaceParameters in just one way
311 void modify(int i) {
312 switch (i) {
313 case 0:
314 fWidth = 63;
315 break;
316 case 1:
317 fHeight = 63;
318 break;
319 case 2:
320 fOrigin = kBottomLeft_GrSurfaceOrigin;
321 break;
322 case 3:
Robert Phillipsc1267c62018-04-04 11:12:39 -0400323 // The color type and config need to be changed together.
324 // The original SRGB color space no longer makes sense for F16
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500325 fColorType = kRGBA_F16_SkColorType;
Robert Phillipsbe77a022018-04-03 17:17:05 -0400326 fConfig = kRGBA_half_GrPixelConfig;
327 fColorSpace = SkColorSpace::MakeSRGBLinear();
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500328 break;
329 case 4:
Robert Phillipsc1267c62018-04-04 11:12:39 -0400330 // This just needs to be a colorSpace different from that returned by MakeSRGB()
331 // but still be considered SRGB. In this case we just change the gamut.
332 fColorSpace = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
333 SkColorSpace::kAdobeRGB_Gamut);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500334 break;
335 case kSampleCount:
336 fSampleCount = 4;
337 break;
338 case 6:
339 fSurfaceProps = SkSurfaceProps(0x0, kRGB_H_SkPixelGeometry);
340 break;
341 case 7:
342 fSurfaceProps = SkSurfaceProps(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
343 kUnknown_SkPixelGeometry);
344 break;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500345 case 8:
346 fShouldCreateMipMaps = false;
347 break;
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500348 }
349 }
350
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400351 SkSurfaceCharacterization createCharacterization(GrContext* context) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500352 int maxResourceCount;
353 size_t maxResourceBytes;
354 context->getResourceCacheLimits(&maxResourceCount, &maxResourceBytes);
355
356 // Note that Ganesh doesn't make use of the SkImageInfo's alphaType
357 SkImageInfo ii = SkImageInfo::Make(fWidth, fHeight, fColorType,
358 kPremul_SkAlphaType, fColorSpace);
359
Robert Phillipsbe77a022018-04-03 17:17:05 -0400360 GrBackendFormat backendFormat = create_backend_format(context, fColorType,
361 fColorSpace.get(), fConfig);
362 if (!backendFormat.isValid()) {
363 return SkSurfaceCharacterization();
364 }
Robert Phillipsfc711a22018-02-13 17:03:00 -0500365
366 SkSurfaceCharacterization c = context->threadSafeProxy()->createCharacterization(
367 maxResourceBytes, ii, backendFormat, fSampleCount,
368 fOrigin, fSurfaceProps, fShouldCreateMipMaps);
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400369 return c;
370 }
371
372 // Create a DDL whose characterization captures the current settings
373 std::unique_ptr<SkDeferredDisplayList> createDDL(GrContext* context) const {
374 SkSurfaceCharacterization c = this->createCharacterization(context);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500375 SkAssertResult(c.isValid());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500376
377 SkDeferredDisplayListRecorder r(c);
378 SkCanvas* canvas = r.getCanvas();
379 if (!canvas) {
380 return nullptr;
381 }
382
383 canvas->drawRect(SkRect::MakeXYWH(10, 10, 10, 10), SkPaint());
384 return r.detach();
385 }
386
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500387 // Create the surface with the current set of parameters
Robert Phillipsbe77a022018-04-03 17:17:05 -0400388 sk_sp<SkSurface> make(GrContext* context, GrBackendTexture* backend,
389 bool nonTextureable) const {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500390 GrGpu* gpu = context->contextPriv().getGpu();
391
Robert Phillipsbe77a022018-04-03 17:17:05 -0400392 GrMipMapped mipmapped = nonTextureable
393 ? GrMipMapped::kNo
394 : GrMipMapped(fShouldCreateMipMaps);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500395
396 *backend = gpu->createTestingOnlyBackendTexture(nullptr, fWidth, fHeight,
Robert Phillipsbe77a022018-04-03 17:17:05 -0400397 fConfig, true, mipmapped);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500398 if (!backend->isValid() || !gpu->isTestingOnlyBackendTexture(*backend)) {
399 return nullptr;
400 }
401
Robert Phillipsbe77a022018-04-03 17:17:05 -0400402 sk_sp<SkSurface> surface;
403 if (nonTextureable) {
404 // Create a surface w/ the current parameters but make it non-textureable
405 surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
406 context, *backend, fOrigin, fSampleCount, fColorType,
407 fColorSpace, &fSurfaceProps);
408 } else {
409 surface = SkSurface::MakeFromBackendTexture(
410 context, *backend, fOrigin, fSampleCount, fColorType,
411 fColorSpace, &fSurfaceProps);
412 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500413
414 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500415 gpu->deleteTestingOnlyBackendTexture(*backend);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500416 return nullptr;
417 }
418
419 return surface;
420 }
421
Brian Salomon26102cb2018-03-09 09:33:19 -0500422 void cleanUpBackEnd(GrContext* context, const GrBackendTexture& backend) const {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500423 GrGpu* gpu = context->contextPriv().getGpu();
424
425 gpu->deleteTestingOnlyBackendTexture(backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500426 }
427
428private:
429 int fWidth;
430 int fHeight;
431 GrSurfaceOrigin fOrigin;
432 SkColorType fColorType;
Robert Phillipsbe77a022018-04-03 17:17:05 -0400433 GrPixelConfig fConfig;
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500434 sk_sp<SkColorSpace> fColorSpace;
435 int fSampleCount;
436 SkSurfaceProps fSurfaceProps;
Robert Phillipse8fabb22018-02-04 14:33:21 -0500437 bool fShouldCreateMipMaps;
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500438};
439
Robert Phillipsc1267c62018-04-04 11:12:39 -0400440// Test out operator== && operator!=
441DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLOperatorEqTest, reporter, ctxInfo) {
442 GrContext* context = ctxInfo.grContext();
443
444 for (int i = 0; i < SurfaceParameters::kNumParams; ++i) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400445 SurfaceParameters params1(context->contextPriv().caps());
Robert Phillipsc1267c62018-04-04 11:12:39 -0400446 params1.modify(i);
447
448 SkSurfaceCharacterization char1 = params1.createCharacterization(context);
449 if (!char1.isValid()) {
450 continue; // can happen on some platforms (ChromeOS)
451 }
452
453 for (int j = 0; j < SurfaceParameters::kNumParams; ++j) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400454 SurfaceParameters params2(context->contextPriv().caps());
Robert Phillipsc1267c62018-04-04 11:12:39 -0400455 params2.modify(j);
456
457 SkSurfaceCharacterization char2 = params2.createCharacterization(context);
458 if (!char2.isValid()) {
459 continue; // can happen on some platforms (ChromeOS)
460 }
461
462 if (i == j) {
463 REPORTER_ASSERT(reporter, char1 == char2);
464 } else {
465 REPORTER_ASSERT(reporter, char1 != char2);
466 }
467
468 }
469 }
470
471 {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400472 SurfaceParameters params(context->contextPriv().caps());
Robert Phillipsc1267c62018-04-04 11:12:39 -0400473
474 SkSurfaceCharacterization valid = params.createCharacterization(context);
475 SkASSERT(valid.isValid());
476
477 SkSurfaceCharacterization inval1, inval2;
478 SkASSERT(!inval1.isValid() && !inval2.isValid());
479
480 REPORTER_ASSERT(reporter, inval1 != inval2);
481 REPORTER_ASSERT(reporter, valid != inval1);
482 REPORTER_ASSERT(reporter, inval1 != valid);
483 }
484}
485
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400486////////////////////////////////////////////////////////////////////////////////
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500487// This tests SkSurfaceCharacterization/SkSurface compatibility
Robert Phillipsbe77a022018-04-03 17:17:05 -0400488DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLSurfaceCharacterizationTest, reporter, ctxInfo) {
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500489 GrContext* context = ctxInfo.grContext();
Robert Phillipsbe77a022018-04-03 17:17:05 -0400490 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500491
Robert Phillips9e441ee2018-02-01 15:14:55 -0500492 // Create a bitmap that we can readback into
493 SkImageInfo imageInfo = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType,
494 kPremul_SkAlphaType);
495 SkBitmap bitmap;
496 bitmap.allocPixels(imageInfo);
497
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500498 std::unique_ptr<SkDeferredDisplayList> ddl;
499
500 // First, create a DDL using the stock SkSurface parameters
501 {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400502 SurfaceParameters params(context->contextPriv().caps());
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500503
Robert Phillipse8fabb22018-02-04 14:33:21 -0500504 ddl = params.createDDL(context);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500505 SkAssertResult(ddl);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500506
507 // The DDL should draw into an SkSurface created with the same parameters
Robert Phillipsbe77a022018-04-03 17:17:05 -0400508 GrBackendTexture backend;
509 sk_sp<SkSurface> s = params.make(context, &backend, false);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500510 if (!s) {
511 return;
512 }
513
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500514 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
Robert Phillips9e441ee2018-02-01 15:14:55 -0500515 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400516 context->flush();
517 gpu->testingOnly_flushGpuAndSync();
518 s = nullptr;
519 params.cleanUpBackEnd(context, backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500520 }
521
522 // Then, alter each parameter in turn and check that the DDL & surface are incompatible
523 for (int i = 0; i < SurfaceParameters::kNumParams; ++i) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400524 SurfaceParameters params(context->contextPriv().caps());
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500525 params.modify(i);
526
Robert Phillipsbe77a022018-04-03 17:17:05 -0400527 GrBackendTexture backend;
528 sk_sp<SkSurface> s = params.make(context, &backend, false);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500529 if (!s) {
530 continue;
531 }
532
533 if (SurfaceParameters::kSampleCount == i) {
534 SkSurface_Gpu* gpuSurf = static_cast<SkSurface_Gpu*>(s.get());
535
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400536 int supportedSampleCount = context->contextPriv().caps()->getRenderTargetSampleCount(
537 params.sampleCount(),
538 gpuSurf->getDevice()
539 ->accessRenderTargetContext()
540 ->asRenderTargetProxy()
541 ->config());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500542 if (1 == supportedSampleCount) {
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500543 // If changing the sample count won't result in a different
544 // surface characterization, skip this step
Robert Phillipsbe77a022018-04-03 17:17:05 -0400545 s = nullptr;
546 params.cleanUpBackEnd(context, backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500547 continue;
548 }
549 }
550
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400551 if (SurfaceParameters::kMipMipCount == i &&
552 !context->contextPriv().caps()->mipMapSupport()) {
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400553 // If changing the mipmap setting won't result in a different surface characterization,
554 // skip this step
Robert Phillipsbe77a022018-04-03 17:17:05 -0400555 s = nullptr;
556 params.cleanUpBackEnd(context, backend);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500557 continue;
558 }
559
560 REPORTER_ASSERT(reporter, !s->draw(ddl.get()),
561 "DDLSurfaceCharacterizationTest failed on parameter: %d\n", i);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400562
563 context->flush();
564 gpu->testingOnly_flushGpuAndSync();
565 s = nullptr;
566 params.cleanUpBackEnd(context, backend);
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500567 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500568
569 // Next test the compatibility of resource cache parameters
570 {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400571 const SurfaceParameters params(context->contextPriv().caps());
Robert Phillipsbe77a022018-04-03 17:17:05 -0400572 GrBackendTexture backend;
573
574 sk_sp<SkSurface> s = params.make(context, &backend, false);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500575
576 int maxResourceCount;
577 size_t maxResourceBytes;
578 context->getResourceCacheLimits(&maxResourceCount, &maxResourceBytes);
579
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500580 context->setResourceCacheLimits(maxResourceCount, maxResourceBytes/2);
581 REPORTER_ASSERT(reporter, !s->draw(ddl.get()));
582
Robert Phillips9e441ee2018-02-01 15:14:55 -0500583 // DDL TODO: once proxies/ops can be de-instantiated we can re-enable these tests.
584 // For now, DDLs are drawn once.
585#if 0
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500586 // resource limits >= those at characterization time are accepted
587 context->setResourceCacheLimits(2*maxResourceCount, maxResourceBytes);
588 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
Robert Phillips9e441ee2018-02-01 15:14:55 -0500589 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500590
591 context->setResourceCacheLimits(maxResourceCount, 2*maxResourceBytes);
592 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
Robert Phillips9e441ee2018-02-01 15:14:55 -0500593 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500594
595 context->setResourceCacheLimits(maxResourceCount, maxResourceBytes);
596 REPORTER_ASSERT(reporter, s->draw(ddl.get()));
Robert Phillips9e441ee2018-02-01 15:14:55 -0500597 s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
598#endif
Robert Phillipsbe77a022018-04-03 17:17:05 -0400599
600 context->flush();
601 gpu->testingOnly_flushGpuAndSync();
602 s = nullptr;
603 params.cleanUpBackEnd(context, backend);
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500604 }
605
Robert Phillipse8fabb22018-02-04 14:33:21 -0500606 // Test that the textureability of the DDL characterization can block a DDL draw
607 {
608 GrBackendTexture backend;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400609 const SurfaceParameters params(context->contextPriv().caps());
Robert Phillipsbe77a022018-04-03 17:17:05 -0400610 sk_sp<SkSurface> s = params.make(context, &backend, true);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500611 if (s) {
612 REPORTER_ASSERT(reporter, !s->draw(ddl.get()));
613
Robert Phillipsbe77a022018-04-03 17:17:05 -0400614 context->flush();
615 gpu->testingOnly_flushGpuAndSync();
Robert Phillipse8fabb22018-02-04 14:33:21 -0500616 s = nullptr;
Brian Salomon26102cb2018-03-09 09:33:19 -0500617 params.cleanUpBackEnd(context, backend);
Robert Phillipse8fabb22018-02-04 14:33:21 -0500618 }
619 }
620
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500621 // Make sure non-GPU-backed surfaces fail characterization
622 {
623 SkImageInfo ii = SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType);
624
625 sk_sp<SkSurface> rasterSurface = SkSurface::MakeRaster(ii);
626 SkSurfaceCharacterization c;
627 REPORTER_ASSERT(reporter, !rasterSurface->characterize(&c));
628 }
Robert Phillips94458ee2018-03-06 13:41:51 -0500629
630 // Exercise the createResized method
631 {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400632 SurfaceParameters params(context->contextPriv().caps());
Robert Phillipsbe77a022018-04-03 17:17:05 -0400633 GrBackendTexture backend;
Robert Phillips94458ee2018-03-06 13:41:51 -0500634
Robert Phillipsbe77a022018-04-03 17:17:05 -0400635 sk_sp<SkSurface> s = params.make(context, &backend, false);
Robert Phillips94458ee2018-03-06 13:41:51 -0500636 if (!s) {
637 return;
638 }
639
640 SkSurfaceCharacterization char0;
641 SkAssertResult(s->characterize(&char0));
642
643 // Too small
644 SkSurfaceCharacterization char1 = char0.createResized(-1, -1);
645 REPORTER_ASSERT(reporter, !char1.isValid());
646
647 // Too large
648 SkSurfaceCharacterization char2 = char0.createResized(1000000, 32);
649 REPORTER_ASSERT(reporter, !char2.isValid());
650
651 // Just right
652 SkSurfaceCharacterization char3 = char0.createResized(32, 32);
653 REPORTER_ASSERT(reporter, char3.isValid());
654 REPORTER_ASSERT(reporter, 32 == char3.width());
655 REPORTER_ASSERT(reporter, 32 == char3.height());
Robert Phillipsbe77a022018-04-03 17:17:05 -0400656
657 s = nullptr;
658 params.cleanUpBackEnd(context, backend);
Robert Phillips94458ee2018-03-06 13:41:51 -0500659 }
Robert Phillips7ffbcf92017-12-04 12:52:46 -0500660}
661
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400662////////////////////////////////////////////////////////////////////////////////
663// This tests the SkSurface::MakeRenderTarget variant that takes an SkSurfaceCharacterization.
664// In particular, the SkSurface and the SkSurfaceCharacterization should always be compatible.
Robert Phillipsbe77a022018-04-03 17:17:05 -0400665DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLMakeRenderTargetTest, reporter, ctxInfo) {
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400666 GrContext* context = ctxInfo.grContext();
667
668 for (int i = 0; i < SurfaceParameters::kNumParams; ++i) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400669 SurfaceParameters params(context->contextPriv().caps());
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400670 params.modify(i);
671
672 SkSurfaceCharacterization c = params.createCharacterization(context);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400673 GrBackendTexture backend;
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400674
Robert Phillipsbe77a022018-04-03 17:17:05 -0400675 if (!c.isValid()) {
676 sk_sp<SkSurface> tmp = params.make(context, &backend, false);
677
678 // If we couldn't characterize the surface we shouldn't be able to create it either
679 REPORTER_ASSERT(reporter, !tmp);
680 if (tmp) {
681 tmp = nullptr;
682 params.cleanUpBackEnd(context, backend);
683 }
684 continue;
685 }
686
687 sk_sp<SkSurface> s = params.make(context, &backend, false);
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400688 if (!s) {
689 REPORTER_ASSERT(reporter, !c.isValid());
690 continue;
691 }
692
693 REPORTER_ASSERT(reporter, c.isValid());
694
695 s = SkSurface::MakeRenderTarget(context, c, SkBudgeted::kYes);
696 REPORTER_ASSERT(reporter, s);
697
698 SkSurface_Gpu* g = static_cast<SkSurface_Gpu*>(s.get());
699 REPORTER_ASSERT(reporter, g->isCompatible(c));
Robert Phillipsbe77a022018-04-03 17:17:05 -0400700
701 s = nullptr;
702 params.cleanUpBackEnd(context, backend);
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400703 }
704}
705
706////////////////////////////////////////////////////////////////////////////////
Greg Danielf2336e42018-01-23 16:38:14 -0500707static constexpr int kSize = 8;
708
709struct TextureReleaseChecker {
710 TextureReleaseChecker() : fReleaseCount(0) {}
711 int fReleaseCount;
712 static void Release(void* self) {
713 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
714 }
715};
716
717enum class DDLStage { kMakeImage, kDrawImage, kDetach, kDrawDDL };
718
719// This tests the ability to create and use wrapped textures in a DDL world
720DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLWrapBackendTest, reporter, ctxInfo) {
721 GrContext* context = ctxInfo.grContext();
722 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomonf7778972018-03-08 10:13:17 -0500723 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
724 nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo);
725 if (!backendTex.isValid()) {
726 return;
Greg Danielf2336e42018-01-23 16:38:14 -0500727 }
Brian Salomonf7778972018-03-08 10:13:17 -0500728
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400729 SurfaceParameters params(context->contextPriv().caps());
Robert Phillipsbe77a022018-04-03 17:17:05 -0400730 GrBackendTexture backend;
Brian Salomonf7778972018-03-08 10:13:17 -0500731
Robert Phillipsbe77a022018-04-03 17:17:05 -0400732 sk_sp<SkSurface> s = params.make(context, &backend, false);
Brian Salomonf7778972018-03-08 10:13:17 -0500733 if (!s) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500734 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonf7778972018-03-08 10:13:17 -0500735 return;
736 }
737
738 SkSurfaceCharacterization c;
739 SkAssertResult(s->characterize(&c));
740
741 std::unique_ptr<SkDeferredDisplayListRecorder> recorder(new SkDeferredDisplayListRecorder(c));
742
743 SkCanvas* canvas = recorder->getCanvas();
744 if (!canvas) {
Robert Phillipsbe77a022018-04-03 17:17:05 -0400745 s = nullptr;
746 params.cleanUpBackEnd(context, backend);
Brian Salomon26102cb2018-03-09 09:33:19 -0500747 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonf7778972018-03-08 10:13:17 -0500748 return;
749 }
750
751 GrContext* deferredContext = canvas->getGrContext();
752 if (!deferredContext) {
Robert Phillipsbe77a022018-04-03 17:17:05 -0400753 s = nullptr;
754 params.cleanUpBackEnd(context, backend);
Brian Salomon26102cb2018-03-09 09:33:19 -0500755 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonf7778972018-03-08 10:13:17 -0500756 return;
757 }
758
759 // Wrapped Backend Textures are not supported in DDL
760 sk_sp<SkImage> image =
761 SkImage::MakeFromAdoptedTexture(deferredContext, backendTex, kTopLeft_GrSurfaceOrigin,
762 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
763 REPORTER_ASSERT(reporter, !image);
764
765 TextureReleaseChecker releaseChecker;
766 image = SkImage::MakeFromTexture(deferredContext, backendTex, kTopLeft_GrSurfaceOrigin,
767 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr,
768 TextureReleaseChecker::Release, &releaseChecker);
769 REPORTER_ASSERT(reporter, !image);
770
Brian Salomon26102cb2018-03-09 09:33:19 -0500771 gpu->deleteTestingOnlyBackendTexture(backendTex);
Robert Phillipsbe77a022018-04-03 17:17:05 -0400772
773 s = nullptr;
774 params.cleanUpBackEnd(context, backend);
Greg Danielf2336e42018-01-23 16:38:14 -0500775}
776
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400777static void dummy_fulfill_proc(void*, GrBackendTexture*) { SkASSERT(0); }
778static void dummy_release_proc(void*) { SkASSERT(0); }
Robert Phillipsabf7b762018-03-21 12:13:37 -0400779static void dummy_done_proc(void*) { }
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400780
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400781////////////////////////////////////////////////////////////////////////////////
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400782// Test out the behavior of an invalid DDLRecorder
783DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLInvalidRecorder, reporter, ctxInfo) {
784 GrContext* context = ctxInfo.grContext();
785
786 {
787 SkImageInfo ii = SkImageInfo::MakeN32Premul(32, 32);
788 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
789
790 SkSurfaceCharacterization characterization;
791 SkAssertResult(s->characterize(&characterization));
792
793 // never calling getCanvas means the backing surface is never allocated
794 SkDeferredDisplayListRecorder recorder(characterization);
795 }
796
797 {
798 SkSurfaceCharacterization invalid;
799
800 SkDeferredDisplayListRecorder recorder(invalid);
801
802 const SkSurfaceCharacterization c = recorder.characterization();
803 REPORTER_ASSERT(reporter, !c.isValid());
804 REPORTER_ASSERT(reporter, !recorder.getCanvas());
805 REPORTER_ASSERT(reporter, !recorder.detach());
806
Robert Phillipsbe77a022018-04-03 17:17:05 -0400807 GrBackendFormat format = create_backend_format(context, kRGBA_8888_SkColorType,
808 nullptr, kRGBA_8888_GrPixelConfig);
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400809 sk_sp<SkImage> image = recorder.makePromiseTexture(format, 32, 32, GrMipMapped::kNo,
810 kTopLeft_GrSurfaceOrigin,
811 kRGBA_8888_SkColorType,
812 kPremul_SkAlphaType, nullptr,
813 dummy_fulfill_proc,
814 dummy_release_proc,
Greg Daniel7278d682018-03-16 14:57:21 -0400815 dummy_done_proc,
Robert Phillips6ceaafa2018-03-15 16:53:06 -0400816 nullptr);
817 REPORTER_ASSERT(reporter, !image);
818 }
819
820}
821
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400822////////////////////////////////////////////////////////////////////////////////
Robert Phillips874b5352018-03-16 08:48:24 -0400823// Ensure that flushing while DDL recording doesn't cause a crash
824DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLFlushWhileRecording, reporter, ctxInfo) {
825 GrContext* context = ctxInfo.grContext();
826
827 SkImageInfo ii = SkImageInfo::MakeN32Premul(32, 32);
828 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
829
830 SkSurfaceCharacterization characterization;
831 SkAssertResult(s->characterize(&characterization));
832
833 SkDeferredDisplayListRecorder recorder(characterization);
834 SkCanvas* canvas = recorder.getCanvas();
835
836 canvas->flush();
837 canvas->getGrContext()->flush();
838}
Greg Danielf2336e42018-01-23 16:38:14 -0500839
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400840////////////////////////////////////////////////////////////////////////////////
Robert Phillipsabf7b762018-03-21 12:13:37 -0400841// Check that the texture-specific flags (i.e., for external & rectangle textures) work
842// for promise images. As such, this is a GL-only test.
843DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(DDLTextureFlagsTest, reporter, ctxInfo) {
844 GrContext* context = ctxInfo.grContext();
845
846 SkImageInfo ii = SkImageInfo::MakeN32Premul(32, 32);
847 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
848
849 SkSurfaceCharacterization characterization;
850 SkAssertResult(s->characterize(&characterization));
851
852 SkDeferredDisplayListRecorder recorder(characterization);
853
854 for (GrGLenum target : { GR_GL_TEXTURE_EXTERNAL, GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_2D } ) {
Greg Daniel09c94002018-06-08 22:11:51 +0000855 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
856 GrBackendFormat format = GrBackendFormat::MakeGL(GR_GL_RGBA8, target);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400857
Greg Daniel09c94002018-06-08 22:11:51 +0000858 sk_sp<SkImage> image = recorder.makePromiseTexture(format, 32, 32, mipMapped,
859 kTopLeft_GrSurfaceOrigin,
860 kRGBA_8888_SkColorType,
861 kPremul_SkAlphaType, nullptr,
862 dummy_fulfill_proc,
863 dummy_release_proc,
864 dummy_done_proc,
865 nullptr);
866 if (GR_GL_TEXTURE_2D != target && mipMapped == GrMipMapped::kYes) {
867 REPORTER_ASSERT(reporter, !image);
868 continue;
869 }
870 REPORTER_ASSERT(reporter, image);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400871
Greg Daniel09c94002018-06-08 22:11:51 +0000872 GrTextureProxy* backingProxy = ((SkImage_Gpu*) image.get())->peekProxy();
Robert Phillipsabf7b762018-03-21 12:13:37 -0400873
Greg Daniel09c94002018-06-08 22:11:51 +0000874 REPORTER_ASSERT(reporter, backingProxy->mipMapped() == mipMapped);
875 if (GR_GL_TEXTURE_2D == target) {
876 REPORTER_ASSERT(reporter, !backingProxy->texPriv().isClampOnly());
877 } else {
878 REPORTER_ASSERT(reporter, backingProxy->texPriv().isClampOnly());
879 }
Robert Phillipsabf7b762018-03-21 12:13:37 -0400880 }
881 }
Robert Phillipsbe77a022018-04-03 17:17:05 -0400882}
883
884////////////////////////////////////////////////////////////////////////////////
885
886// Exhaustively test colorType and pixelConfig compatibility.
887DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(DDLCompatibilityTest, reporter, ctxInfo) {
888 GrContext* context = ctxInfo.grContext();
889
890 for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) {
891 SkColorType colorType = static_cast<SkColorType>(ct);
892
893 for (int config = 0; config < kGrPixelConfigCnt; ++config) {
894 GrPixelConfig pixelConfig = static_cast<GrPixelConfig>(config);
895
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400896 SurfaceParameters params(context->contextPriv().caps());
Robert Phillipsbe77a022018-04-03 17:17:05 -0400897 params.setColorType(colorType);
898 params.setConfig(pixelConfig);
899
900 params.setColorSpace(nullptr);
901 if (kSRGBA_8888_GrPixelConfig == pixelConfig ||
902 kSBGRA_8888_GrPixelConfig == pixelConfig) {
903 params.setColorSpace(SkColorSpace::MakeSRGB());
904 }
905
906 SkSurfaceCharacterization c = params.createCharacterization(context);
907 GrBackendTexture backend;
908
909 if (!c.isValid()) {
910 // TODO: this would be cool to enable but there is, currently, too much crossover
911 // allowed internally (e.g., kAlpha_8_SkColorType/kGray_8_as_Red_GrPixelConfig
912 // is permitted on GL).
913#if 0
914 sk_sp<SkSurface> tmp = params.make(context, &backend, false);
915
916 // If we couldn't characterize the surface we shouldn't be able to create it either
917 REPORTER_ASSERT(reporter, !tmp);
918 if (tmp) {
919 tmp = nullptr;
920 params.cleanUpBackEnd(context, backend);
921 }
922#endif
923 continue;
924 }
925
926 sk_sp<SkSurface> s = params.make(context, &backend, false);
927 REPORTER_ASSERT(reporter, s);
928 if (!s) {
929 s = nullptr;
930 params.cleanUpBackEnd(context, backend);
931 continue;
932 }
933
934 SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(s.get());
935 REPORTER_ASSERT(reporter, gpuSurface->isCompatible(c));
936
937 s = nullptr;
938 params.cleanUpBackEnd(context, backend);
939
940 s = SkSurface::MakeRenderTarget(context, c, SkBudgeted::kYes);
941 REPORTER_ASSERT(reporter, s);
942 if (!s) {
943 continue;
944 }
945
946 gpuSurface = static_cast<SkSurface_Gpu*>(s.get());
947 REPORTER_ASSERT(reporter, gpuSurface->isCompatible(c));
948 }
949 }
Robert Phillipsabf7b762018-03-21 12:13:37 -0400950
951}