Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 1 | /* |
| 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 Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 10 | #include "GrBackendSurface.h" |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 11 | #include "GrCaps.h" |
| 12 | #include "GrContext.h" |
| 13 | #include "GrContextFactory.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 15 | #include "GrGpu.h" |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 16 | #include "GrRenderTargetContext.h" |
| 17 | #include "GrRenderTargetProxy.h" |
| 18 | #include "GrTextureProxy.h" |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 19 | #include "GrTextureProxyPriv.h" |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 20 | #include "GrTypes.h" |
| 21 | #include "GrTypesPriv.h" |
| 22 | #include "SkBitmap.h" |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 23 | #include "SkCanvas.h" |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 24 | #include "SkColorSpace.h" |
| 25 | #include "SkDeferredDisplayList.h" |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 26 | #include "SkDeferredDisplayListRecorder.h" |
| 27 | #include "SkGpuDevice.h" |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 28 | #include "SkImage.h" |
| 29 | #include "SkImageInfo.h" |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 30 | #include "SkImage_Gpu.h" |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 31 | #include "SkPaint.h" |
| 32 | #include "SkRect.h" |
| 33 | #include "SkRefCnt.h" |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 34 | #include "SkSurface.h" |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 35 | #include "SkSurfaceCharacterization.h" |
| 36 | #include "SkSurfaceProps.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 37 | #include "SkSurface_Gpu.h" |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 38 | #include "Test.h" |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 39 | #include "gl/GrGLCaps.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 40 | #include "gl/GrGLDefines.h" |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 41 | #include "gl/GrGLTypes.h" |
| 42 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 43 | #ifdef SK_VULKAN |
| 44 | #include "vk/GrVkDefines.h" |
| 45 | #endif |
| 46 | |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 47 | #include <initializer_list> |
| 48 | #include <memory> |
| 49 | #include <utility> |
| 50 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 51 | // Try to create a backend format from the provided colorType and config. Return an invalid |
| 52 | // backend format if the combination is infeasible. |
| 53 | static GrBackendFormat create_backend_format(GrContext* context, |
| 54 | SkColorType ct, SkColorSpace* cs, |
| 55 | GrPixelConfig config) { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 56 | const GrCaps* caps = context->contextPriv().caps(); |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 57 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 58 | // TODO: what should be done if we have a colorspace that doesn't have a gammaCloseToSRGB? |
| 59 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 60 | switch (context->contextPriv().getBackend()) { |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 61 | case kOpenGL_GrBackend: { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 62 | const GrGLCaps* glCaps = static_cast<const GrGLCaps*>(caps); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 63 | 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 Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 135 | case kRGBA_F32_SkColorType: |
| 136 | return GrBackendFormat(); |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 137 | } |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 138 | } |
| 139 | break; |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 140 | #ifdef SK_VULKAN |
| 141 | case kVulkan_GrBackend: |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 142 | 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 Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 204 | case kRGBA_F32_SkColorType: |
| 205 | return GrBackendFormat(); |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 206 | } |
| 207 | break; |
| 208 | #endif |
| 209 | case kMock_GrBackend: |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 210 | 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 Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 274 | case kRGBA_F32_SkColorType: |
| 275 | return GrBackendFormat(); |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 276 | } |
| 277 | break; |
| 278 | default: |
| 279 | return GrBackendFormat(); // return an invalid format |
| 280 | } |
| 281 | |
| 282 | return GrBackendFormat(); // return an invalid format |
| 283 | } |
| 284 | |
| 285 | |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 286 | class SurfaceParameters { |
| 287 | public: |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 288 | static const int kNumParams = 9; |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 289 | static const int kSampleCount = 5; |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 290 | static const int kMipMipCount = 8; |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 291 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 292 | SurfaceParameters(const GrCaps* caps) |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 293 | : fWidth(64) |
| 294 | , fHeight(64) |
| 295 | , fOrigin(kTopLeft_GrSurfaceOrigin) |
| 296 | , fColorType(kRGBA_8888_SkColorType) |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 297 | , fConfig(caps->srgbSupport() ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig) |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 298 | , fColorSpace(SkColorSpace::MakeSRGB()) |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 299 | , fSampleCount(1) |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 300 | , fSurfaceProps(0x0, kUnknown_SkPixelGeometry) |
| 301 | , fShouldCreateMipMaps(true) { |
| 302 | } |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 303 | |
| 304 | int sampleCount() const { return fSampleCount; } |
| 305 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 306 | 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 Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 310 | // 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 Phillips | c1267c6 | 2018-04-04 11:12:39 -0400 | [diff] [blame] | 323 | // The color type and config need to be changed together. |
| 324 | // The original SRGB color space no longer makes sense for F16 |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 325 | fColorType = kRGBA_F16_SkColorType; |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 326 | fConfig = kRGBA_half_GrPixelConfig; |
| 327 | fColorSpace = SkColorSpace::MakeSRGBLinear(); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 328 | break; |
| 329 | case 4: |
Robert Phillips | c1267c6 | 2018-04-04 11:12:39 -0400 | [diff] [blame] | 330 | // 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 Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 334 | 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 Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 345 | case 8: |
| 346 | fShouldCreateMipMaps = false; |
| 347 | break; |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 351 | SkSurfaceCharacterization createCharacterization(GrContext* context) const { |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 352 | 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 Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 360 | GrBackendFormat backendFormat = create_backend_format(context, fColorType, |
| 361 | fColorSpace.get(), fConfig); |
| 362 | if (!backendFormat.isValid()) { |
| 363 | return SkSurfaceCharacterization(); |
| 364 | } |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 365 | |
| 366 | SkSurfaceCharacterization c = context->threadSafeProxy()->createCharacterization( |
| 367 | maxResourceBytes, ii, backendFormat, fSampleCount, |
| 368 | fOrigin, fSurfaceProps, fShouldCreateMipMaps); |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 369 | 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 Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 375 | SkAssertResult(c.isValid()); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 376 | |
| 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 Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 387 | // Create the surface with the current set of parameters |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 388 | sk_sp<SkSurface> make(GrContext* context, GrBackendTexture* backend, |
| 389 | bool nonTextureable) const { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 390 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 391 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 392 | GrMipMapped mipmapped = nonTextureable |
| 393 | ? GrMipMapped::kNo |
| 394 | : GrMipMapped(fShouldCreateMipMaps); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 395 | |
| 396 | *backend = gpu->createTestingOnlyBackendTexture(nullptr, fWidth, fHeight, |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 397 | fConfig, true, mipmapped); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 398 | if (!backend->isValid() || !gpu->isTestingOnlyBackendTexture(*backend)) { |
| 399 | return nullptr; |
| 400 | } |
| 401 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 402 | 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 Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 413 | |
| 414 | if (!surface) { |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 415 | gpu->deleteTestingOnlyBackendTexture(*backend); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 416 | return nullptr; |
| 417 | } |
| 418 | |
| 419 | return surface; |
| 420 | } |
| 421 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 422 | void cleanUpBackEnd(GrContext* context, const GrBackendTexture& backend) const { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 423 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 424 | |
| 425 | gpu->deleteTestingOnlyBackendTexture(backend); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | private: |
| 429 | int fWidth; |
| 430 | int fHeight; |
| 431 | GrSurfaceOrigin fOrigin; |
| 432 | SkColorType fColorType; |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 433 | GrPixelConfig fConfig; |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 434 | sk_sp<SkColorSpace> fColorSpace; |
| 435 | int fSampleCount; |
| 436 | SkSurfaceProps fSurfaceProps; |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 437 | bool fShouldCreateMipMaps; |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 438 | }; |
| 439 | |
Robert Phillips | c1267c6 | 2018-04-04 11:12:39 -0400 | [diff] [blame] | 440 | // Test out operator== && operator!= |
| 441 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLOperatorEqTest, reporter, ctxInfo) { |
| 442 | GrContext* context = ctxInfo.grContext(); |
| 443 | |
| 444 | for (int i = 0; i < SurfaceParameters::kNumParams; ++i) { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 445 | SurfaceParameters params1(context->contextPriv().caps()); |
Robert Phillips | c1267c6 | 2018-04-04 11:12:39 -0400 | [diff] [blame] | 446 | 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 Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 454 | SurfaceParameters params2(context->contextPriv().caps()); |
Robert Phillips | c1267c6 | 2018-04-04 11:12:39 -0400 | [diff] [blame] | 455 | 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 Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 472 | SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | c1267c6 | 2018-04-04 11:12:39 -0400 | [diff] [blame] | 473 | |
| 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 Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 486 | //////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 487 | // This tests SkSurfaceCharacterization/SkSurface compatibility |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 488 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLSurfaceCharacterizationTest, reporter, ctxInfo) { |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 489 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 490 | GrGpu* gpu = context->contextPriv().getGpu(); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 491 | |
Robert Phillips | 9e441ee | 2018-02-01 15:14:55 -0500 | [diff] [blame] | 492 | // 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 Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 498 | std::unique_ptr<SkDeferredDisplayList> ddl; |
| 499 | |
| 500 | // First, create a DDL using the stock SkSurface parameters |
| 501 | { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 502 | SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 503 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 504 | ddl = params.createDDL(context); |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 505 | SkAssertResult(ddl); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 506 | |
| 507 | // The DDL should draw into an SkSurface created with the same parameters |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 508 | GrBackendTexture backend; |
| 509 | sk_sp<SkSurface> s = params.make(context, &backend, false); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 510 | if (!s) { |
| 511 | return; |
| 512 | } |
| 513 | |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 514 | REPORTER_ASSERT(reporter, s->draw(ddl.get())); |
Robert Phillips | 9e441ee | 2018-02-01 15:14:55 -0500 | [diff] [blame] | 515 | s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 516 | context->flush(); |
| 517 | gpu->testingOnly_flushGpuAndSync(); |
| 518 | s = nullptr; |
| 519 | params.cleanUpBackEnd(context, backend); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 520 | } |
| 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 Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 524 | SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 525 | params.modify(i); |
| 526 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 527 | GrBackendTexture backend; |
| 528 | sk_sp<SkSurface> s = params.make(context, &backend, false); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 529 | if (!s) { |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | if (SurfaceParameters::kSampleCount == i) { |
| 534 | SkSurface_Gpu* gpuSurf = static_cast<SkSurface_Gpu*>(s.get()); |
| 535 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 536 | int supportedSampleCount = context->contextPriv().caps()->getRenderTargetSampleCount( |
| 537 | params.sampleCount(), |
| 538 | gpuSurf->getDevice() |
| 539 | ->accessRenderTargetContext() |
| 540 | ->asRenderTargetProxy() |
| 541 | ->config()); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 542 | if (1 == supportedSampleCount) { |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 543 | // If changing the sample count won't result in a different |
| 544 | // surface characterization, skip this step |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 545 | s = nullptr; |
| 546 | params.cleanUpBackEnd(context, backend); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 547 | continue; |
| 548 | } |
| 549 | } |
| 550 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 551 | if (SurfaceParameters::kMipMipCount == i && |
| 552 | !context->contextPriv().caps()->mipMapSupport()) { |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 553 | // If changing the mipmap setting won't result in a different surface characterization, |
| 554 | // skip this step |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 555 | s = nullptr; |
| 556 | params.cleanUpBackEnd(context, backend); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 557 | continue; |
| 558 | } |
| 559 | |
| 560 | REPORTER_ASSERT(reporter, !s->draw(ddl.get()), |
| 561 | "DDLSurfaceCharacterizationTest failed on parameter: %d\n", i); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 562 | |
| 563 | context->flush(); |
| 564 | gpu->testingOnly_flushGpuAndSync(); |
| 565 | s = nullptr; |
| 566 | params.cleanUpBackEnd(context, backend); |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 567 | } |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 568 | |
| 569 | // Next test the compatibility of resource cache parameters |
| 570 | { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 571 | const SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 572 | GrBackendTexture backend; |
| 573 | |
| 574 | sk_sp<SkSurface> s = params.make(context, &backend, false); |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 575 | |
| 576 | int maxResourceCount; |
| 577 | size_t maxResourceBytes; |
| 578 | context->getResourceCacheLimits(&maxResourceCount, &maxResourceBytes); |
| 579 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 580 | context->setResourceCacheLimits(maxResourceCount, maxResourceBytes/2); |
| 581 | REPORTER_ASSERT(reporter, !s->draw(ddl.get())); |
| 582 | |
Robert Phillips | 9e441ee | 2018-02-01 15:14:55 -0500 | [diff] [blame] | 583 | // 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 Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 586 | // resource limits >= those at characterization time are accepted |
| 587 | context->setResourceCacheLimits(2*maxResourceCount, maxResourceBytes); |
| 588 | REPORTER_ASSERT(reporter, s->draw(ddl.get())); |
Robert Phillips | 9e441ee | 2018-02-01 15:14:55 -0500 | [diff] [blame] | 589 | s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0); |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 590 | |
| 591 | context->setResourceCacheLimits(maxResourceCount, 2*maxResourceBytes); |
| 592 | REPORTER_ASSERT(reporter, s->draw(ddl.get())); |
Robert Phillips | 9e441ee | 2018-02-01 15:14:55 -0500 | [diff] [blame] | 593 | s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0); |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 594 | |
| 595 | context->setResourceCacheLimits(maxResourceCount, maxResourceBytes); |
| 596 | REPORTER_ASSERT(reporter, s->draw(ddl.get())); |
Robert Phillips | 9e441ee | 2018-02-01 15:14:55 -0500 | [diff] [blame] | 597 | s->readPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes(), 0, 0); |
| 598 | #endif |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 599 | |
| 600 | context->flush(); |
| 601 | gpu->testingOnly_flushGpuAndSync(); |
| 602 | s = nullptr; |
| 603 | params.cleanUpBackEnd(context, backend); |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 604 | } |
| 605 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 606 | // Test that the textureability of the DDL characterization can block a DDL draw |
| 607 | { |
| 608 | GrBackendTexture backend; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 609 | const SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 610 | sk_sp<SkSurface> s = params.make(context, &backend, true); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 611 | if (s) { |
| 612 | REPORTER_ASSERT(reporter, !s->draw(ddl.get())); |
| 613 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 614 | context->flush(); |
| 615 | gpu->testingOnly_flushGpuAndSync(); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 616 | s = nullptr; |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 617 | params.cleanUpBackEnd(context, backend); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 621 | // 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 Phillips | 94458ee | 2018-03-06 13:41:51 -0500 | [diff] [blame] | 629 | |
| 630 | // Exercise the createResized method |
| 631 | { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 632 | SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 633 | GrBackendTexture backend; |
Robert Phillips | 94458ee | 2018-03-06 13:41:51 -0500 | [diff] [blame] | 634 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 635 | sk_sp<SkSurface> s = params.make(context, &backend, false); |
Robert Phillips | 94458ee | 2018-03-06 13:41:51 -0500 | [diff] [blame] | 636 | 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 Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 656 | |
| 657 | s = nullptr; |
| 658 | params.cleanUpBackEnd(context, backend); |
Robert Phillips | 94458ee | 2018-03-06 13:41:51 -0500 | [diff] [blame] | 659 | } |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 660 | } |
| 661 | |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 662 | //////////////////////////////////////////////////////////////////////////////// |
| 663 | // This tests the SkSurface::MakeRenderTarget variant that takes an SkSurfaceCharacterization. |
| 664 | // In particular, the SkSurface and the SkSurfaceCharacterization should always be compatible. |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 665 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLMakeRenderTargetTest, reporter, ctxInfo) { |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 666 | GrContext* context = ctxInfo.grContext(); |
| 667 | |
| 668 | for (int i = 0; i < SurfaceParameters::kNumParams; ++i) { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 669 | SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 670 | params.modify(i); |
| 671 | |
| 672 | SkSurfaceCharacterization c = params.createCharacterization(context); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 673 | GrBackendTexture backend; |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 674 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 675 | 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 Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 688 | 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 Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 700 | |
| 701 | s = nullptr; |
| 702 | params.cleanUpBackEnd(context, backend); |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
| 706 | //////////////////////////////////////////////////////////////////////////////// |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 707 | static constexpr int kSize = 8; |
| 708 | |
| 709 | struct TextureReleaseChecker { |
| 710 | TextureReleaseChecker() : fReleaseCount(0) {} |
| 711 | int fReleaseCount; |
| 712 | static void Release(void* self) { |
| 713 | static_cast<TextureReleaseChecker*>(self)->fReleaseCount++; |
| 714 | } |
| 715 | }; |
| 716 | |
| 717 | enum class DDLStage { kMakeImage, kDrawImage, kDetach, kDrawDDL }; |
| 718 | |
| 719 | // This tests the ability to create and use wrapped textures in a DDL world |
| 720 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DDLWrapBackendTest, reporter, ctxInfo) { |
| 721 | GrContext* context = ctxInfo.grContext(); |
| 722 | GrGpu* gpu = context->contextPriv().getGpu(); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 723 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture( |
| 724 | nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo); |
| 725 | if (!backendTex.isValid()) { |
| 726 | return; |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 727 | } |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 728 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 729 | SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 730 | GrBackendTexture backend; |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 731 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 732 | sk_sp<SkSurface> s = params.make(context, &backend, false); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 733 | if (!s) { |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 734 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 735 | 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 Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 745 | s = nullptr; |
| 746 | params.cleanUpBackEnd(context, backend); |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 747 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 748 | return; |
| 749 | } |
| 750 | |
| 751 | GrContext* deferredContext = canvas->getGrContext(); |
| 752 | if (!deferredContext) { |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 753 | s = nullptr; |
| 754 | params.cleanUpBackEnd(context, backend); |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 755 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 756 | 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 Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 771 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 772 | |
| 773 | s = nullptr; |
| 774 | params.cleanUpBackEnd(context, backend); |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 775 | } |
| 776 | |
Robert Phillips | 6ceaafa | 2018-03-15 16:53:06 -0400 | [diff] [blame] | 777 | static void dummy_fulfill_proc(void*, GrBackendTexture*) { SkASSERT(0); } |
| 778 | static void dummy_release_proc(void*) { SkASSERT(0); } |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 779 | static void dummy_done_proc(void*) { } |
Robert Phillips | 6ceaafa | 2018-03-15 16:53:06 -0400 | [diff] [blame] | 780 | |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 781 | //////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 6ceaafa | 2018-03-15 16:53:06 -0400 | [diff] [blame] | 782 | // Test out the behavior of an invalid DDLRecorder |
| 783 | DEF_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 Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 807 | GrBackendFormat format = create_backend_format(context, kRGBA_8888_SkColorType, |
| 808 | nullptr, kRGBA_8888_GrPixelConfig); |
Robert Phillips | 6ceaafa | 2018-03-15 16:53:06 -0400 | [diff] [blame] | 809 | 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 Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 815 | dummy_done_proc, |
Robert Phillips | 6ceaafa | 2018-03-15 16:53:06 -0400 | [diff] [blame] | 816 | nullptr); |
| 817 | REPORTER_ASSERT(reporter, !image); |
| 818 | } |
| 819 | |
| 820 | } |
| 821 | |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 822 | //////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 874b535 | 2018-03-16 08:48:24 -0400 | [diff] [blame] | 823 | // Ensure that flushing while DDL recording doesn't cause a crash |
| 824 | DEF_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 Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 839 | |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 840 | //////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 841 | // 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. |
| 843 | DEF_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 Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 855 | for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) { |
| 856 | GrBackendFormat format = GrBackendFormat::MakeGL(GR_GL_RGBA8, target); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 857 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 858 | 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 Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 871 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 872 | GrTextureProxy* backingProxy = ((SkImage_Gpu*) image.get())->peekProxy(); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 873 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 874 | 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 Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 880 | } |
| 881 | } |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | //////////////////////////////////////////////////////////////////////////////// |
| 885 | |
| 886 | // Exhaustively test colorType and pixelConfig compatibility. |
| 887 | DEF_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 Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 896 | SurfaceParameters params(context->contextPriv().caps()); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 897 | 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 Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 950 | |
| 951 | } |