junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkData.h" |
| 10 | #include "include/core/SkOverdrawCanvas.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/core/SkRRect.h" |
| 13 | #include "include/core/SkRegion.h" |
| 14 | #include "include/core/SkSurface.h" |
| 15 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 16 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 17 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/core/SkDevice.h" |
| 19 | #include "src/core/SkUtils.h" |
| 20 | #include "src/gpu/GrContextPriv.h" |
| 21 | #include "src/gpu/GrGpu.h" |
| 22 | #include "src/gpu/GrGpuResourcePriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrImageInfo.h" |
Stephen White | fdba6c8 | 2020-05-26 17:00:32 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrRenderTargetContext.h" |
| 26 | #include "src/gpu/GrResourceProvider.h" |
| 27 | #include "src/gpu/SkGpuDevice.h" |
| 28 | #include "src/image/SkImage_Base.h" |
| 29 | #include "src/image/SkImage_Gpu.h" |
| 30 | #include "src/image/SkSurface_Gpu.h" |
| 31 | #include "tests/Test.h" |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 32 | #include "tests/TestUtils.h" |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 33 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 34 | #include <functional> |
| 35 | #include <initializer_list> |
| 36 | #include <vector> |
| 37 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 38 | #include "tools/ToolUtils.h" |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 39 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 40 | static void release_direct_surface_storage(void* pixels, void* context) { |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 41 | SkASSERT(pixels == context); |
| 42 | sk_free(pixels); |
| 43 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 44 | static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType, |
| 45 | SkImageInfo* requestedInfo = nullptr) { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 46 | const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 47 | if (requestedInfo) { |
| 48 | *requestedInfo = info; |
| 49 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 50 | return SkSurface::MakeRaster(info); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 51 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 52 | static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType, |
| 53 | SkImageInfo* requestedInfo = nullptr) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 54 | const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); |
| 55 | if (requestedInfo) { |
| 56 | *requestedInfo = info; |
| 57 | } |
| 58 | const size_t rowBytes = info.minRowBytes(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 59 | void* storage = sk_malloc_throw(info.computeByteSize(rowBytes)); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 60 | return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes, |
| 61 | release_direct_surface_storage, |
| 62 | storage); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 63 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 64 | static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType, |
| 65 | SkImageInfo* requestedInfo = nullptr) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 66 | const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); |
| 67 | if (requestedInfo) { |
| 68 | *requestedInfo = info; |
| 69 | } |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 70 | return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 71 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 72 | static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context, |
| 73 | SkAlphaType at = kPremul_SkAlphaType, |
| 74 | SkImageInfo* requestedInfo = nullptr) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 75 | const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); |
| 76 | if (requestedInfo) { |
| 77 | *requestedInfo = info; |
| 78 | } |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 79 | return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 80 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 81 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 82 | DEF_TEST(SurfaceEmpty, reporter) { |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 83 | const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info)); |
| 85 | REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0)); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 86 | |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 87 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 88 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 89 | const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
| 90 | REPORTER_ASSERT(reporter, nullptr == |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 91 | SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info)); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 92 | } |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 93 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 94 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 95 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 96 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 97 | for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) { |
| 98 | static constexpr int kSize = 10; |
| 99 | |
| 100 | SkColorType colorType = static_cast<SkColorType>(ct); |
| 101 | auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 102 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 103 | { |
| 104 | bool can = context->colorTypeSupportedAsSurface(colorType); |
| 105 | auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr); |
| 106 | REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", |
| 107 | colorType, can, SkToBool(surf)); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 108 | |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 109 | GrBackendTexture backendTex; |
| 110 | CreateBackendTexture(context, &backendTex, kSize, kSize, colorType, |
| 111 | SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes, |
| 112 | GrProtected::kNo); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 113 | surf = SkSurface::MakeFromBackendTexture(context, backendTex, |
| 114 | kTopLeft_GrSurfaceOrigin, 0, colorType, |
| 115 | nullptr, nullptr); |
| 116 | REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", |
| 117 | colorType, can, SkToBool(surf)); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 118 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 119 | surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, |
| 120 | kTopLeft_GrSurfaceOrigin, 1, |
| 121 | colorType, nullptr, nullptr); |
| 122 | REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", |
| 123 | colorType, can, SkToBool(surf)); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 124 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 125 | surf.reset(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 126 | context->flushAndSubmit(); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 127 | context->deleteBackendTexture(backendTex); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 130 | // The MSAA test only makes sense if the colorType is renderable to begin with. |
| 131 | if (context->colorTypeSupportedAsSurface(colorType)) { |
| 132 | static constexpr int kSampleCnt = 2; |
| 133 | |
| 134 | bool can = context->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt; |
| 135 | auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, kSampleCnt, |
| 136 | nullptr); |
| 137 | REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", |
| 138 | colorType, can, SkToBool(surf)); |
| 139 | |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 140 | GrBackendTexture backendTex; |
| 141 | CreateBackendTexture(context, &backendTex, kSize, kSize, colorType, |
| 142 | SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes, |
| 143 | GrProtected::kNo); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 144 | surf = SkSurface::MakeFromBackendTexture(context, backendTex, |
| 145 | kTopLeft_GrSurfaceOrigin, kSampleCnt, |
| 146 | colorType, nullptr, nullptr); |
| 147 | REPORTER_ASSERT(reporter, can == SkToBool(surf), |
| 148 | "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf), |
| 149 | colorType); |
| 150 | // Ensure that the sample count stored on the resulting SkSurface is a valid value. |
| 151 | if (surf) { |
| 152 | auto rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext(); |
| 153 | int storedCnt = rtc->numSamples(); |
| 154 | int allowedCnt = context->priv().caps()->getRenderTargetSampleCount( |
| 155 | storedCnt, backendTex.getBackendFormat()); |
| 156 | REPORTER_ASSERT(reporter, storedCnt == allowedCnt, |
| 157 | "Should store an allowed sample count (%d vs %d)", allowedCnt, |
| 158 | storedCnt); |
| 159 | } |
| 160 | |
| 161 | surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, |
| 162 | kTopLeft_GrSurfaceOrigin, |
| 163 | kSampleCnt, colorType, |
| 164 | nullptr, nullptr); |
| 165 | REPORTER_ASSERT(reporter, can == SkToBool(surf), |
| 166 | "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf), |
| 167 | colorType); |
| 168 | if (surf) { |
| 169 | auto rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext(); |
| 170 | int storedCnt = rtc->numSamples(); |
| 171 | int allowedCnt = context->priv().caps()->getRenderTargetSampleCount( |
| 172 | storedCnt, backendTex.getBackendFormat()); |
| 173 | REPORTER_ASSERT(reporter, storedCnt == allowedCnt, |
| 174 | "Should store an allowed sample count (%d vs %d)", allowedCnt, |
| 175 | storedCnt); |
| 176 | } |
| 177 | |
| 178 | surf.reset(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 179 | context->flushAndSubmit(); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 180 | context->deleteBackendTexture(backendTex); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 183 | { |
| 184 | auto* gpu = context->priv().getGpu(); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 185 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 186 | GrBackendRenderTarget backendRenderTarget = gpu->createTestingOnlyBackendRenderTarget( |
| 187 | 16, 16, SkColorTypeToGrColorType(colorType)); |
| 188 | bool can = context->colorTypeSupportedAsSurface(colorType); |
| 189 | auto surf = SkSurface::MakeFromBackendRenderTarget(context, backendRenderTarget, |
| 190 | kTopLeft_GrSurfaceOrigin, colorType, |
| 191 | nullptr, nullptr); |
| 192 | REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", colorType, |
| 193 | can, SkToBool(surf)); |
| 194 | surf.reset(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 195 | context->flushAndSubmit(); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 196 | if (backendRenderTarget.isValid()) { |
| 197 | gpu->deleteTestingOnlyBackendRenderTarget(backendRenderTarget); |
| 198 | } |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 199 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 204 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 205 | |
| 206 | static constexpr int kSize = 10; |
| 207 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 208 | for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 209 | |
| 210 | SkColorType colorType = static_cast<SkColorType>(ct); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 211 | int max = context->maxSurfaceSampleCountForColorType(colorType); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 212 | if (!max) { |
| 213 | continue; |
| 214 | } |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 215 | |
| 216 | GrBackendTexture backendTex; |
| 217 | CreateBackendTexture(context, &backendTex, kSize, kSize, colorType, |
| 218 | SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes, |
| 219 | GrProtected::kNo); |
Brian Salomon | 99501b7 | 2018-03-23 11:26:11 -0400 | [diff] [blame] | 220 | if (!backendTex.isValid()) { |
| 221 | continue; |
| 222 | } |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 223 | SkScopeExit freeTex([&backendTex, context] { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 224 | context->deleteBackendTexture(backendTex); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 225 | }); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 226 | |
| 227 | if (!context->colorTypeSupportedAsSurface(colorType)) { |
| 228 | continue; |
| 229 | } |
| 230 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 231 | auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 232 | auto surf = SkSurface::MakeFromBackendTexture(context, backendTex, |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 233 | kTopLeft_GrSurfaceOrigin, max, |
| 234 | colorType, nullptr, nullptr); |
| 235 | REPORTER_ASSERT(reporter, surf); |
| 236 | if (!surf) { |
| 237 | continue; |
| 238 | } |
| 239 | int sampleCnt = ((SkSurface_Gpu*)(surf.get())) |
| 240 | ->getDevice() |
| 241 | ->accessRenderTargetContext() |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 242 | ->numSamples(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 243 | REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt); |
| 244 | } |
| 245 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 246 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 247 | static void test_canvas_peek(skiatest::Reporter* reporter, |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 248 | sk_sp<SkSurface>& surface, |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 249 | const SkImageInfo& requestInfo, |
| 250 | bool expectPeekSuccess) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 251 | const SkColor color = SK_ColorRED; |
| 252 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 253 | surface->getCanvas()->clear(color); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 254 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 255 | SkPixmap pmap; |
| 256 | bool success = surface->getCanvas()->peekPixels(&pmap); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 257 | REPORTER_ASSERT(reporter, expectPeekSuccess == success); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 258 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 259 | SkPixmap pmap2; |
| 260 | const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 261 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 262 | if (success) { |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 263 | REPORTER_ASSERT(reporter, requestInfo == pmap.info()); |
| 264 | REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes()); |
| 265 | REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32()); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 266 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 267 | REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr()); |
| 268 | REPORTER_ASSERT(reporter, pmap.info() == pmap2.info()); |
| 269 | REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 270 | } else { |
| 271 | REPORTER_ASSERT(reporter, nullptr == addr2); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 274 | DEF_TEST(SurfaceCanvasPeek, reporter) { |
| 275 | for (auto& surface_func : { &create_surface, &create_direct_surface }) { |
| 276 | SkImageInfo requestInfo; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 277 | auto surface(surface_func(kPremul_SkAlphaType, &requestInfo)); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 278 | test_canvas_peek(reporter, surface, requestInfo, true); |
| 279 | } |
| 280 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 281 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 282 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
| 283 | SkImageInfo requestInfo; |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 284 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, &requestInfo)); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 285 | test_canvas_peek(reporter, surface, requestInfo, false); |
| 286 | } |
| 287 | } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 288 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 289 | static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 290 | SkAlphaType expectedAlphaType) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 291 | REPORTER_ASSERT(reporter, surface); |
| 292 | if (surface) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 293 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 294 | REPORTER_ASSERT(reporter, image); |
| 295 | if (image) { |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 296 | REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 300 | DEF_TEST(SurfaceSnapshotAlphaType, reporter) { |
| 301 | for (auto& surface_func : { &create_surface, &create_direct_surface }) { |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 302 | for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) { |
| 303 | auto surface(surface_func(at, nullptr)); |
| 304 | test_snapshot_alphatype(reporter, surface, at); |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 308 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 309 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 310 | // GPU doesn't support creating unpremul surfaces, so only test opaque + premul |
| 311 | for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 312 | auto surface(surface_func(ctxInfo.directContext(), at, nullptr)); |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 313 | test_snapshot_alphatype(reporter, surface, at); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 317 | |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 318 | static void test_backend_texture_access_copy_on_write( |
| 319 | skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) { |
| 320 | GrBackendTexture tex1 = surface->getBackendTexture(access); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 321 | sk_sp<SkImage> snap1(surface->makeImageSnapshot()); |
fmalita | e263908 | 2015-08-06 07:04:51 -0700 | [diff] [blame] | 322 | |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 323 | GrBackendTexture tex2 = surface->getBackendTexture(access); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 324 | sk_sp<SkImage> snap2(surface->makeImageSnapshot()); |
fmalita | e263908 | 2015-08-06 07:04:51 -0700 | [diff] [blame] | 325 | |
| 326 | // If the access mode triggers CoW, then the backend objects should reflect it. |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 327 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2)); |
fmalita | e263908 | 2015-08-06 07:04:51 -0700 | [diff] [blame] | 328 | } |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 329 | |
| 330 | static void test_backend_rendertarget_access_copy_on_write( |
| 331 | skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) { |
| 332 | GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access); |
| 333 | sk_sp<SkImage> snap1(surface->makeImageSnapshot()); |
| 334 | |
| 335 | GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access); |
| 336 | sk_sp<SkImage> snap2(surface->makeImageSnapshot()); |
| 337 | |
| 338 | // If the access mode triggers CoW, then the backend objects should reflect it. |
| 339 | REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) == |
| 340 | (snap1 == snap2)); |
| 341 | } |
| 342 | |
| 343 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 344 | const SkSurface::BackendHandleAccess accessModes[] = { |
| 345 | SkSurface::kFlushRead_BackendHandleAccess, |
| 346 | SkSurface::kFlushWrite_BackendHandleAccess, |
| 347 | SkSurface::kDiscardWrite_BackendHandleAccess, |
| 348 | }; |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 349 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 350 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 351 | for (auto& accessMode : accessModes) { |
| 352 | { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 353 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 354 | test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode); |
| 355 | } |
| 356 | { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 357 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 358 | test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 363 | |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 364 | template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)> |
| 365 | static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 366 | sk_sp<SkImage> image0(surface->makeImageSnapshot()); |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 367 | |
| 368 | Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess); |
| 369 | REPORTER_ASSERT(reporter, obj.isValid()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 370 | sk_sp<SkImage> image1(surface->makeImageSnapshot()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 371 | // just read access should not affect the snapshot |
| 372 | REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID()); |
| 373 | |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 374 | obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess); |
| 375 | REPORTER_ASSERT(reporter, obj.isValid()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 376 | sk_sp<SkImage> image2(surface->makeImageSnapshot()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 377 | // expect a new image, since we claimed we would write |
| 378 | REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID()); |
| 379 | |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 380 | obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess); |
| 381 | REPORTER_ASSERT(reporter, obj.isValid()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 382 | sk_sp<SkImage> image3(surface->makeImageSnapshot()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 383 | // expect a new(er) image, since we claimed we would write |
| 384 | REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID()); |
| 385 | REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID()); |
| 386 | } |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 387 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 388 | // No CPU test. |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 389 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 390 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 391 | { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 392 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 393 | test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter, |
| 394 | surface.get()); |
| 395 | } |
| 396 | { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 397 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 398 | test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>( |
| 399 | reporter, surface.get()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 403 | |
| 404 | // Verify that the right canvas commands trigger a copy on write. |
| 405 | static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) { |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 406 | SkCanvas* canvas = surface->getCanvas(); |
| 407 | |
| 408 | const SkRect testRect = |
| 409 | SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 410 | SkIntToScalar(4), SkIntToScalar(5)); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 411 | SkPath testPath; |
| 412 | testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 413 | SkIntToScalar(2), SkIntToScalar(1))); |
| 414 | |
| 415 | const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); |
| 416 | |
| 417 | SkRegion testRegion; |
| 418 | testRegion.setRect(testIRect); |
| 419 | |
| 420 | |
| 421 | const SkColor testColor = 0x01020304; |
| 422 | const SkPaint testPaint; |
| 423 | const SkPoint testPoints[3] = { |
| 424 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 425 | {SkIntToScalar(2), SkIntToScalar(1)}, |
| 426 | {SkIntToScalar(0), SkIntToScalar(2)} |
| 427 | }; |
| 428 | const size_t testPointCount = 3; |
| 429 | |
| 430 | SkBitmap testBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 431 | testBitmap.allocN32Pixels(10, 10); |
robertphillips@google.com | d1ce77d | 2013-10-09 12:51:09 +0000 | [diff] [blame] | 432 | testBitmap.eraseColor(0); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 433 | |
| 434 | SkRRect testRRect; |
| 435 | testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1); |
| 436 | |
| 437 | SkString testText("Hello World"); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 438 | |
| 439 | #define EXPECT_COPY_ON_WRITE(command) \ |
| 440 | { \ |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 441 | sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \ |
| 442 | sk_sp<SkImage> aur_before(imageBefore); \ |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 443 | canvas-> command ; \ |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 444 | sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \ |
| 445 | sk_sp<SkImage> aur_after(imageAfter); \ |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 446 | REPORTER_ASSERT(reporter, imageBefore != imageAfter); \ |
| 447 | } |
| 448 | |
| 449 | EXPECT_COPY_ON_WRITE(clear(testColor)) |
| 450 | EXPECT_COPY_ON_WRITE(drawPaint(testPaint)) |
| 451 | EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \ |
| 452 | testPaint)) |
| 453 | EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint)) |
| 454 | EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint)) |
| 455 | EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint)) |
| 456 | EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint)) |
| 457 | EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 458 | EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr)) |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 459 | EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, SkFont(), testPaint)) |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 460 | } |
| 461 | DEF_TEST(SurfaceCopyOnWrite, reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 462 | test_copy_on_write(reporter, create_surface().get()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 463 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 464 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 465 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 466 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 467 | test_copy_on_write(reporter, surface.get()); |
fmalita | e263908 | 2015-08-06 07:04:51 -0700 | [diff] [blame] | 468 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 469 | } |
| 470 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 471 | static void test_writable_after_snapshot_release(skiatest::Reporter* reporter, |
| 472 | SkSurface* surface) { |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 473 | // This test succeeds by not triggering an assertion. |
| 474 | // The test verifies that the surface remains writable (usable) after |
| 475 | // acquiring and releasing a snapshot without triggering a copy on write. |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 476 | SkCanvas* canvas = surface->getCanvas(); |
| 477 | canvas->clear(1); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 478 | surface->makeImageSnapshot(); // Create and destroy SkImage |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 479 | canvas->clear(2); // Must not assert internally |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 480 | } |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 481 | DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 482 | test_writable_after_snapshot_release(reporter, create_surface().get()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 483 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 484 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 485 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 486 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 487 | test_writable_after_snapshot_release(reporter, surface.get()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 490 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 491 | static void test_crbug263329(skiatest::Reporter* reporter, |
| 492 | SkSurface* surface1, |
| 493 | SkSurface* surface2) { |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 494 | // This is a regression test for crbug.com/263329 |
| 495 | // Bug was caused by onCopyOnWrite releasing the old surface texture |
| 496 | // back to the scratch texture pool even though the texture is used |
| 497 | // by and active SkImage_Gpu. |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 498 | SkCanvas* canvas1 = surface1->getCanvas(); |
| 499 | SkCanvas* canvas2 = surface2->getCanvas(); |
| 500 | canvas1->clear(1); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 501 | sk_sp<SkImage> image1(surface1->makeImageSnapshot()); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 502 | // Trigger copy on write, new backing is a scratch texture |
| 503 | canvas1->clear(2); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 504 | sk_sp<SkImage> image2(surface1->makeImageSnapshot()); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 505 | // Trigger copy on write, old backing should not be returned to scratch |
| 506 | // pool because it is held by image2 |
| 507 | canvas1->clear(3); |
| 508 | |
| 509 | canvas2->clear(4); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 510 | sk_sp<SkImage> image3(surface2->makeImageSnapshot()); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 511 | // Trigger copy on write on surface2. The new backing store should not |
| 512 | // be recycling a texture that is held by an existing image. |
| 513 | canvas2->clear(5); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 514 | sk_sp<SkImage> image4(surface2->makeImageSnapshot()); |
Greg Daniel | 7c90211 | 2020-03-06 13:07:10 -0500 | [diff] [blame] | 515 | |
| 516 | SkImage_GpuBase* gpuImage1 = static_cast<SkImage_GpuBase*>(as_IB(image1)); |
| 517 | SkImage_GpuBase* gpuImage2 = static_cast<SkImage_GpuBase*>(as_IB(image2)); |
| 518 | SkImage_GpuBase* gpuImage3 = static_cast<SkImage_GpuBase*>(as_IB(image3)); |
| 519 | SkImage_GpuBase* gpuImage4 = static_cast<SkImage_GpuBase*>(as_IB(image4)); |
| 520 | |
| 521 | REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage3->getTexture()); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 522 | // The following assertion checks crbug.com/263329 |
Greg Daniel | 7c90211 | 2020-03-06 13:07:10 -0500 | [diff] [blame] | 523 | REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage2->getTexture()); |
| 524 | REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage1->getTexture()); |
| 525 | REPORTER_ASSERT(reporter, gpuImage3->getTexture() != gpuImage2->getTexture()); |
| 526 | REPORTER_ASSERT(reporter, gpuImage3->getTexture() != gpuImage1->getTexture()); |
| 527 | REPORTER_ASSERT(reporter, gpuImage2->getTexture() != gpuImage1->getTexture()); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 528 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 529 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 530 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 531 | auto surface1(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
| 532 | auto surface2(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 533 | test_crbug263329(reporter, surface1.get(), surface2.get()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 536 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 537 | DEF_TEST(SurfaceGetTexture, reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 538 | auto surface(create_surface()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 539 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 540 | REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 541 | surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 542 | REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 543 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 544 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 545 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 546 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 547 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 548 | |
| 549 | REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked()); |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 550 | GrBackendTexture backendTex = image->getBackendTexture(false); |
| 551 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 552 | surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 553 | REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked()); |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 554 | GrBackendTexture backendTex2 = image->getBackendTexture(false); |
| 555 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2)); |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 556 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 557 | } |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 558 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 559 | static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) { |
| 560 | SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get(); |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 561 | |
| 562 | GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext() |
| 563 | ->asRenderTargetProxy(); |
| 564 | return proxy->isBudgeted(); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 565 | } |
| 566 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 567 | static SkBudgeted is_budgeted(SkImage* image) { |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 568 | return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted(); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 569 | } |
| 570 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 571 | static SkBudgeted is_budgeted(const sk_sp<SkImage> image) { |
| 572 | return is_budgeted(image.get()); |
| 573 | } |
| 574 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 575 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) { |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 576 | SkImageInfo info = SkImageInfo::MakeN32Premul(8,8); |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 577 | for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 578 | auto surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(), budgeted, info)); |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 579 | SkASSERT(surface); |
| 580 | REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface)); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 581 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 582 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 583 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 584 | // Initially the image shares a texture with the surface, and the |
| 585 | // the budgets should always match. |
| 586 | REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface)); |
| 587 | REPORTER_ASSERT(reporter, budgeted == is_budgeted(image)); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 588 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 589 | // Now trigger copy-on-write |
| 590 | surface->getCanvas()->clear(SK_ColorBLUE); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 591 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 592 | // They don't share a texture anymore but the budgets should still match. |
| 593 | REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface)); |
| 594 | REPORTER_ASSERT(reporter, budgeted == is_budgeted(image)); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 595 | } |
| 596 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 597 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 598 | static void test_no_canvas1(skiatest::Reporter* reporter, |
| 599 | SkSurface* surface, |
| 600 | SkSurface::ContentChangeMode mode) { |
| 601 | // Test passes by not asserting |
| 602 | surface->notifyContentWillChange(mode); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 603 | } |
| 604 | static void test_no_canvas2(skiatest::Reporter* reporter, |
| 605 | SkSurface* surface, |
| 606 | SkSurface::ContentChangeMode mode) { |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 607 | // Verifies the robustness of SkSurface for handling use cases where calls |
| 608 | // are made before a canvas is created. |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 609 | sk_sp<SkImage> image1 = surface->makeImageSnapshot(); |
| 610 | sk_sp<SkImage> aur_image1(image1); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 611 | surface->notifyContentWillChange(mode); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 612 | sk_sp<SkImage> image2 = surface->makeImageSnapshot(); |
| 613 | sk_sp<SkImage> aur_image2(image2); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 614 | REPORTER_ASSERT(reporter, image1 != image2); |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 615 | } |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 616 | DEF_TEST(SurfaceNoCanvas, reporter) { |
| 617 | SkSurface::ContentChangeMode modes[] = |
| 618 | { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode}; |
| 619 | for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) { |
| 620 | for (auto& mode : modes) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 621 | test_func(reporter, create_surface().get(), mode); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 625 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 626 | SkSurface::ContentChangeMode modes[] = |
| 627 | { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode}; |
| 628 | for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) { |
| 629 | for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) { |
| 630 | for (auto& mode : modes) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 631 | auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr)); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 632 | test_func(reporter, surface.get(), mode); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 633 | } |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 634 | } |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 635 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 636 | } |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 637 | |
| 638 | static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) { |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 639 | SkPixmap surfacePM; |
| 640 | REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM)); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 641 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 642 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 643 | SkPixmap pm; |
| 644 | REPORTER_ASSERT(reporter, image->peekPixels(&pm)); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 645 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 646 | REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes()); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 647 | |
| 648 | // trigger a copy-on-write |
| 649 | surface->getCanvas()->drawPaint(SkPaint()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 650 | sk_sp<SkImage> image2(surface->makeImageSnapshot()); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 651 | REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID()); |
| 652 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 653 | SkPixmap pm2; |
| 654 | REPORTER_ASSERT(reporter, image2->peekPixels(&pm2)); |
| 655 | REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes()); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | DEF_TEST(surface_rowbytes, reporter) { |
| 659 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 660 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 661 | auto surf0(SkSurface::MakeRaster(info)); |
| 662 | check_rowbytes_remain_consistent(surf0.get(), reporter); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 663 | |
| 664 | // specify a larger rowbytes |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 665 | auto surf1(SkSurface::MakeRaster(info, 500, nullptr)); |
| 666 | check_rowbytes_remain_consistent(surf1.get(), reporter); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 667 | |
| 668 | // Try some illegal rowByte values |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 669 | auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400 |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 670 | REPORTER_ASSERT(reporter, nullptr == s); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 671 | s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 672 | REPORTER_ASSERT(reporter, nullptr == s); |
| 673 | } |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 674 | |
fmalita | 03912f1 | 2016-07-06 06:22:06 -0700 | [diff] [blame] | 675 | DEF_TEST(surface_raster_zeroinitialized, reporter) { |
| 676 | sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100)); |
| 677 | SkPixmap pixmap; |
| 678 | REPORTER_ASSERT(reporter, s->peekPixels(&pixmap)); |
| 679 | |
| 680 | for (int i = 0; i < pixmap.info().width(); ++i) { |
| 681 | for (int j = 0; j < pixmap.info().height(); ++j) { |
| 682 | REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 687 | static sk_sp<SkSurface> create_gpu_surface_backend_texture( |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 688 | GrContext* ctx, int sampleCnt, const SkColor4f& color, GrBackendTexture* outTexture) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 689 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 690 | // On Pixel and Pixel2XL's with Adreno 530 and 540s, setting width and height to 10s reliably |
| 691 | // triggers what appears to be a driver race condition where the 10x10 surface from the |
| 692 | // OverdrawSurface_gpu test is reused(?) for this surface created by the SurfacePartialDraw_gpu |
| 693 | // test. |
| 694 | // |
| 695 | // Immediately after creation of this surface, readback shows the correct initial solid color. |
| 696 | // However, sometime before content is rendered into the upper half of the surface, the driver |
| 697 | // presumably cleans up the OverdrawSurface_gpu's memory which corrupts this color buffer. The |
| 698 | // top half of the surface is fine after the partially-covering rectangle is drawn, but the |
| 699 | // untouched bottom half contains random pixel values that trigger asserts in the |
| 700 | // SurfacePartialDraw_gpu test for no longer matching the initial color. Running the |
| 701 | // SurfacePartialDraw_gpu test without the OverdrawSurface_gpu test completes successfully. |
| 702 | // |
| 703 | // Requesting a much larger backend texture size seems to prevent it from reusing the same |
| 704 | // memory and avoids the issue. |
| 705 | #if defined(SK_BUILD_FOR_SKQP) |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 706 | const int kWidth = 10; |
| 707 | const int kHeight = 10; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 708 | #else |
| 709 | const int kWidth = 100; |
| 710 | const int kHeight = 100; |
| 711 | #endif |
| 712 | |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 713 | SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType, |
| 714 | kPremul_SkAlphaType); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 715 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 716 | if (!CreateBackendTexture(ctx, outTexture, ii, color, GrMipMapped::kNo, GrRenderable::kYes)) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 717 | return nullptr; |
| 718 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 719 | |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 720 | sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(ctx, *outTexture, |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 721 | kTopLeft_GrSurfaceOrigin, sampleCnt, |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 722 | kRGBA_8888_SkColorType, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 723 | nullptr, nullptr); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 724 | if (!surface) { |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 725 | DeleteBackendTexture(ctx, *outTexture); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 726 | return nullptr; |
| 727 | } |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 728 | return surface; |
| 729 | } |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 730 | |
Stephen White | fdba6c8 | 2020-05-26 17:00:32 -0400 | [diff] [blame] | 731 | static bool supports_readpixels(const GrCaps* caps, SkSurface* surface) { |
| 732 | auto surfaceGpu = static_cast<SkSurface_Gpu*>(surface); |
| 733 | GrRenderTargetContext* context = surfaceGpu->getDevice()->accessRenderTargetContext(); |
| 734 | GrRenderTarget* rt = context->accessRenderTarget(); |
| 735 | if (!rt) { |
| 736 | return false; |
| 737 | } |
| 738 | return caps->surfaceSupportsReadPixels(rt) == GrCaps::SurfaceReadPixelsSupport::kSupported; |
| 739 | } |
| 740 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 741 | static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target( |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 742 | GrContext* ctx, int sampleCnt, const SkColor4f& color, GrBackendTexture* outTexture) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 743 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 744 | const int kWidth = 10; |
| 745 | const int kHeight = 10; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 746 | |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 747 | SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType, |
| 748 | kPremul_SkAlphaType); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 749 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 750 | if (!CreateBackendTexture(ctx, outTexture, ii, color, GrMipMapped::kNo, GrRenderable::kYes)) { |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 751 | return nullptr; |
| 752 | } |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 753 | |
| 754 | sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget( |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 755 | ctx, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType, |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 756 | nullptr, nullptr); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 757 | |
| 758 | if (!surface) { |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 759 | DeleteBackendTexture(ctx, *outTexture); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 760 | return nullptr; |
| 761 | } |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 762 | return surface; |
| 763 | } |
| 764 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 765 | static void test_surface_context_clear(skiatest::Reporter* reporter, |
| 766 | GrSurfaceContext* surfaceContext, uint32_t expectedValue) { |
| 767 | int w = surfaceContext->width(); |
| 768 | int h = surfaceContext->height(); |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 769 | |
| 770 | SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 771 | |
| 772 | SkAutoPixmapStorage readback; |
| 773 | readback.alloc(ii); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 774 | |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 775 | readback.erase(~expectedValue); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 776 | surfaceContext->readPixels(readback.info(), readback.writable_addr(), readback.rowBytes(), |
| 777 | {0, 0}); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 778 | for (int y = 0; y < h; ++y) { |
| 779 | for (int x = 0; x < w; ++x) { |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 780 | uint32_t pixel = readback.addr32()[y * w + x]; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 781 | if (pixel != expectedValue) { |
| 782 | SkString msg; |
| 783 | if (expectedValue) { |
| 784 | msg = "SkSurface should have left render target unmodified"; |
| 785 | } else { |
| 786 | msg = "SkSurface should have cleared the render target"; |
| 787 | } |
| 788 | ERRORF(reporter, |
| 789 | "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel, |
| 790 | expectedValue, x, y); |
| 791 | return; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 797 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 798 | auto context = ctxInfo.directContext(); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 799 | // Snaps an image from a surface and then makes a GrSurfaceContext from the image's texture. |
| 800 | auto makeImageSurfaceContext = [context](SkSurface* surface) { |
| 801 | sk_sp<SkImage> i(surface->makeImageSnapshot()); |
| 802 | SkImage_Gpu* gpuImage = (SkImage_Gpu*)as_IB(i); |
Greg Daniel | 37c127f | 2020-02-05 10:37:27 -0500 | [diff] [blame] | 803 | return GrSurfaceContext::Make(context, *gpuImage->view(context), |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 804 | SkColorTypeToGrColorType(i->colorType()), kPremul_SkAlphaType, |
| 805 | gpuImage->refColorSpace()); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 806 | }; |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 807 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 808 | // Test that non-wrapped RTs are created clear. |
| 809 | for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) { |
| 810 | auto surface = surface_func(context, kPremul_SkAlphaType, nullptr); |
| 811 | if (!surface) { |
| 812 | ERRORF(reporter, "Could not create GPU SkSurface."); |
| 813 | return; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 814 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 815 | auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext(); |
| 816 | if (!rtc) { |
| 817 | ERRORF(reporter, "Could access surface context of GPU SkSurface."); |
| 818 | return; |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 819 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 820 | test_surface_context_clear(reporter, rtc, 0x0); |
| 821 | auto imageSurfaceCtx = makeImageSurfaceContext(surface.get()); |
| 822 | test_surface_context_clear(reporter, imageSurfaceCtx.get(), 0x0); |
| 823 | } |
| 824 | |
| 825 | // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface). |
| 826 | const SkColor4f kOrigColor{.67f, .67f, .67f, 1}; |
| 827 | for (auto& surfaceFunc : {&create_gpu_surface_backend_texture, |
| 828 | &create_gpu_surface_backend_texture_as_render_target}) { |
| 829 | GrBackendTexture backendTex; |
| 830 | auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex); |
| 831 | if (!surface) { |
| 832 | ERRORF(reporter, "Could not create GPU SkSurface."); |
| 833 | return; |
| 834 | } |
| 835 | auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext(); |
| 836 | if (!rtc) { |
| 837 | ERRORF(reporter, "Could access surface context of GPU SkSurface."); |
| 838 | return; |
| 839 | } |
| 840 | test_surface_context_clear(reporter, rtc, kOrigColor.toSkColor()); |
| 841 | auto imageSurfaceCtx = makeImageSurfaceContext(surface.get()); |
| 842 | test_surface_context_clear(reporter, imageSurfaceCtx.get(), kOrigColor.toSkColor()); |
| 843 | context->deleteBackendTexture(backendTex); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 844 | } |
| 845 | } |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 846 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 847 | static void test_surface_draw_partially( |
Robert Phillips | 6694440 | 2019-09-30 13:21:25 -0400 | [diff] [blame] | 848 | skiatest::Reporter* reporter, sk_sp<SkSurface> surface, SkColor origColor) { |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 849 | const int kW = surface->width(); |
| 850 | const int kH = surface->height(); |
| 851 | SkPaint paint; |
| 852 | const SkColor kRectColor = ~origColor | 0xFF000000; |
| 853 | paint.setColor(kRectColor); |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 854 | surface->getCanvas()->drawRect(SkRect::MakeIWH(kW, kH/2), paint); |
| 855 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 856 | // Read back RGBA to avoid format conversions that may not be supported on all platforms. |
| 857 | SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 858 | |
| 859 | SkAutoPixmapStorage readback; |
| 860 | readback.alloc(readInfo); |
| 861 | |
| 862 | readback.erase(~origColor); |
Stephen White | dbb3e1d | 2020-05-13 17:55:18 -0400 | [diff] [blame] | 863 | REPORTER_ASSERT(reporter, surface->readPixels(readback.info(), readback.writable_addr(), |
| 864 | readback.rowBytes(), 0, 0)); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 865 | bool stop = false; |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 866 | |
Robert Phillips | 6694440 | 2019-09-30 13:21:25 -0400 | [diff] [blame] | 867 | SkPMColor origColorPM = SkPackARGB_as_RGBA(SkColorGetA(origColor), |
| 868 | SkColorGetR(origColor), |
| 869 | SkColorGetG(origColor), |
| 870 | SkColorGetB(origColor)); |
| 871 | SkPMColor rectColorPM = SkPackARGB_as_RGBA(SkColorGetA(kRectColor), |
| 872 | SkColorGetR(kRectColor), |
| 873 | SkColorGetG(kRectColor), |
| 874 | SkColorGetB(kRectColor)); |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 875 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 876 | for (int y = 0; y < kH/2 && !stop; ++y) { |
| 877 | for (int x = 0; x < kW && !stop; ++x) { |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 878 | REPORTER_ASSERT(reporter, rectColorPM == readback.addr32()[x + y * kW]); |
| 879 | if (rectColorPM != readback.addr32()[x + y * kW]) { |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 880 | SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n", |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 881 | readback.addr32()[x + y * kW], rectColorPM, x, y); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 882 | stop = true; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | stop = false; |
| 887 | for (int y = kH/2; y < kH && !stop; ++y) { |
| 888 | for (int x = 0; x < kW && !stop; ++x) { |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 889 | REPORTER_ASSERT(reporter, origColorPM == readback.addr32()[x + y * kW]); |
| 890 | if (origColorPM != readback.addr32()[x + y * kW]) { |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 891 | SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n", |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 892 | readback.addr32()[x + y * kW], origColorPM, x, y); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 893 | stop = true; |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | } |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 898 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 899 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 900 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 901 | |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 902 | static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 }; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 903 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 904 | for (auto& surfaceFunc : {&create_gpu_surface_backend_texture, |
| 905 | &create_gpu_surface_backend_texture_as_render_target}) { |
| 906 | // Validate that we can draw to the canvas and that the original texture color is |
| 907 | // preserved in pixels that aren't rendered to via the surface. |
| 908 | // This works only for non-multisampled case. |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 909 | GrBackendTexture backendTex; |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 910 | auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex); |
Stephen White | fdba6c8 | 2020-05-26 17:00:32 -0400 | [diff] [blame] | 911 | const GrCaps* caps = context->priv().caps(); |
| 912 | if (!supports_readpixels(caps, surface.get())) { |
| 913 | continue; |
| 914 | } |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 915 | if (surface) { |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 916 | test_surface_draw_partially(reporter, surface, kOrigColor.toSkColor()); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 917 | surface.reset(); |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 918 | context->deleteBackendTexture(backendTex); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 923 | struct ReleaseChecker { |
| 924 | ReleaseChecker() : fReleaseCount(0) {} |
| 925 | int fReleaseCount; |
| 926 | static void Release(void* self) { |
| 927 | static_cast<ReleaseChecker*>(self)->fReleaseCount++; |
| 928 | } |
| 929 | }; |
| 930 | |
| 931 | |
| 932 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) { |
| 933 | const int kWidth = 10; |
| 934 | const int kHeight = 10; |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 935 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 936 | auto ctx = ctxInfo.directContext(); |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 937 | GrGpu* gpu = ctx->priv().getGpu(); |
| 938 | |
| 939 | for (bool useTexture : {false, true}) { |
| 940 | GrBackendTexture backendTex; |
| 941 | GrBackendRenderTarget backendRT; |
| 942 | sk_sp<SkSurface> surface; |
| 943 | |
| 944 | ReleaseChecker releaseChecker; |
| 945 | GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin; |
| 946 | |
| 947 | if (useTexture) { |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 948 | SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType, |
| 949 | kPremul_SkAlphaType); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 950 | if (!CreateBackendTexture(ctx, &backendTex, ii, SkColors::kRed, GrMipMapped::kNo, |
| 951 | GrRenderable::kYes)) { |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 952 | continue; |
| 953 | } |
| 954 | |
| 955 | surface = SkSurface::MakeFromBackendTexture(ctx, backendTex, texOrigin, 1, |
| 956 | kRGBA_8888_SkColorType, |
| 957 | nullptr, nullptr, |
| 958 | ReleaseChecker::Release, |
| 959 | &releaseChecker); |
| 960 | } else { |
| 961 | backendRT = gpu->createTestingOnlyBackendRenderTarget(kWidth, kHeight, |
| 962 | GrColorType::kRGBA_8888); |
| 963 | if (!backendRT.isValid()) { |
| 964 | continue; |
| 965 | } |
| 966 | surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin, |
| 967 | kRGBA_8888_SkColorType, |
| 968 | nullptr, nullptr, |
| 969 | ReleaseChecker::Release, |
| 970 | &releaseChecker); |
| 971 | } |
| 972 | if (!surface) { |
| 973 | ERRORF(reporter, "Failed to create surface"); |
| 974 | continue; |
| 975 | } |
| 976 | |
| 977 | surface->getCanvas()->clear(SK_ColorRED); |
Greg Daniel | ce9f016 | 2020-06-30 13:42:46 -0400 | [diff] [blame] | 978 | surface->flush(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 979 | ctx->submit(true); |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 980 | |
| 981 | // Now exercise the release proc |
| 982 | REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount); |
| 983 | surface.reset(nullptr); // force a release of the surface |
| 984 | REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount); |
| 985 | |
| 986 | if (useTexture) { |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 987 | DeleteBackendTexture(ctx, backendTex); |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 988 | } else { |
| 989 | gpu->deleteTestingOnlyBackendRenderTarget(backendRT); |
| 990 | } |
| 991 | } |
| 992 | } |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 993 | |
| 994 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 995 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 996 | const GrCaps* caps = context->priv().caps(); |
| 997 | |
| 998 | if (caps->avoidStencilBuffers()) { |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 999 | return; |
| 1000 | } |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 1001 | |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 1002 | static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 }; |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 1003 | |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 1004 | auto resourceProvider = context->priv().resourceProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 1005 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 1006 | for (auto& surfaceFunc : {&create_gpu_surface_backend_texture, |
| 1007 | &create_gpu_surface_backend_texture_as_render_target}) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1008 | for (int sampleCnt : {1, 4, 8}) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1009 | GrBackendTexture backendTex; |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 1010 | auto surface = surfaceFunc(context, sampleCnt, kOrigColor, &backendTex); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 1011 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1012 | if (!surface && sampleCnt > 1) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1013 | // Certain platforms don't support MSAA, skip these. |
| 1014 | continue; |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | // Validate that we can attach a stencil buffer to an SkSurface created by either of |
| 1018 | // our surface functions. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1019 | GrRenderTarget* rt = surface->getCanvas() |
| 1020 | ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget(); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 1021 | REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt, sampleCnt)); |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 1022 | context->deleteBackendTexture(backendTex); |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 1023 | } |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 1024 | } |
| 1025 | } |
brianosman | 0e22eb8 | 2016-08-30 07:07:59 -0700 | [diff] [blame] | 1026 | |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 1027 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 1028 | auto context = ctxInfo.directContext(); |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 1029 | |
| 1030 | for (int sampleCnt : {1, 2}) { |
| 1031 | GrBackendTexture backendTexture1; |
| 1032 | auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 1033 | if (!CreateBackendTexture(context, &backendTexture1, ii, SkColors::kTransparent, |
| 1034 | GrMipMapped::kNo, GrRenderable::kYes)) { |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 1035 | continue; |
| 1036 | } |
| 1037 | SkScopeExit delete1( |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 1038 | [context, &backendTexture1] { DeleteBackendTexture(context, backendTexture1); }); |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 1039 | GrBackendTexture backendTexture2; |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 1040 | if (!CreateBackendTexture(context, &backendTexture2, ii, SkColors::kTransparent, |
| 1041 | GrMipMapped::kNo, GrRenderable::kYes)) { |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 1042 | ERRORF(reporter, "Expected to be able to make second texture"); |
| 1043 | continue; |
| 1044 | } |
| 1045 | SkScopeExit delete2( |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 1046 | [context, &backendTexture2] { DeleteBackendTexture(context, backendTexture2); }); |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 1047 | auto ii2 = ii.makeWH(8, 8); |
| 1048 | GrBackendTexture backendTexture3; |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 1049 | if (!CreateBackendTexture(context, &backendTexture3, ii2, SkColors::kTransparent, |
| 1050 | GrMipMapped::kNo, GrRenderable::kYes)) { |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 1051 | ERRORF(reporter, "Couldn't create different sized texture."); |
| 1052 | continue; |
| 1053 | } |
| 1054 | SkScopeExit delete3( |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 1055 | [context, &backendTexture3] { DeleteBackendTexture(context, backendTexture3); }); |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 1056 | |
| 1057 | auto surf = SkSurface::MakeFromBackendTexture( |
| 1058 | context, backendTexture1, kTopLeft_GrSurfaceOrigin, sampleCnt, |
| 1059 | kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr); |
| 1060 | if (!surf) { |
| 1061 | continue; |
| 1062 | } |
| 1063 | surf->getCanvas()->clear(SK_ColorBLUE); |
| 1064 | // Change matrix, layer, and clip state before swapping out the backing texture. |
| 1065 | surf->getCanvas()->translate(5, 5); |
| 1066 | surf->getCanvas()->saveLayer(nullptr, nullptr); |
| 1067 | surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1)); |
| 1068 | // switch origin while we're at it. |
| 1069 | bool replaced = surf->replaceBackendTexture(backendTexture2, kBottomLeft_GrSurfaceOrigin); |
| 1070 | REPORTER_ASSERT(reporter, replaced); |
| 1071 | SkPaint paint; |
| 1072 | paint.setColor(SK_ColorRED); |
| 1073 | surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint); |
| 1074 | surf->getCanvas()->restore(); |
| 1075 | |
| 1076 | // Check that the replacement texture got the right color values. |
| 1077 | SkAutoPixmapStorage pm; |
| 1078 | pm.alloc(ii); |
| 1079 | bool bad = !surf->readPixels(pm, 0, 0); |
| 1080 | REPORTER_ASSERT(reporter, !bad, "Could not read surface."); |
| 1081 | for (int y = 0; y < ii.height() && !bad; ++y) { |
| 1082 | for (int x = 0; x < ii.width() && !bad; ++x) { |
| 1083 | auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000; |
| 1084 | auto found = *pm.addr32(x, y); |
| 1085 | if (found != expected) { |
| 1086 | bad = true; |
| 1087 | ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.", |
| 1088 | expected, found, x, y); |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | // The original texture should still be all blue. |
| 1093 | surf = SkSurface::MakeFromBackendTexture( |
| 1094 | context, backendTexture1, kBottomLeft_GrSurfaceOrigin, sampleCnt, |
| 1095 | kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr); |
| 1096 | if (!surf) { |
| 1097 | ERRORF(reporter, "Could not create second surface."); |
| 1098 | continue; |
| 1099 | } |
| 1100 | bad = !surf->readPixels(pm, 0, 0); |
| 1101 | REPORTER_ASSERT(reporter, !bad, "Could not read second surface."); |
| 1102 | for (int y = 0; y < ii.height() && !bad; ++y) { |
| 1103 | for (int x = 0; x < ii.width() && !bad; ++x) { |
| 1104 | auto expected = 0xFFFF0000; |
| 1105 | auto found = *pm.addr32(x, y); |
| 1106 | if (found != expected) { |
| 1107 | bad = true; |
| 1108 | ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.", |
| 1109 | expected, found, x, y); |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | // Can't replace with the same texture |
| 1115 | REPORTER_ASSERT(reporter, |
| 1116 | !surf->replaceBackendTexture(backendTexture1, kTopLeft_GrSurfaceOrigin)); |
| 1117 | // Can't replace with invalid texture |
| 1118 | REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin)); |
| 1119 | // Can't replace with different size texture. |
| 1120 | REPORTER_ASSERT(reporter, |
| 1121 | !surf->replaceBackendTexture(backendTexture3, kTopLeft_GrSurfaceOrigin)); |
| 1122 | // Can't replace texture of non-wrapped SkSurface. |
| 1123 | surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr); |
| 1124 | REPORTER_ASSERT(reporter, surf); |
| 1125 | if (surf) { |
| 1126 | REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(backendTexture1, |
| 1127 | kTopLeft_GrSurfaceOrigin)); |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
Matt Sarett | 22886c4 | 2016-11-22 11:31:41 -0500 | [diff] [blame] | 1132 | static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) { |
Matt Sarett | e11b614 | 2016-11-28 18:28:07 -0500 | [diff] [blame] | 1133 | SkOverdrawCanvas canvas(surface->getCanvas()); |
| 1134 | canvas.drawPaint(SkPaint()); |
Matt Sarett | 22886c4 | 2016-11-22 11:31:41 -0500 | [diff] [blame] | 1135 | sk_sp<SkImage> image = surface->makeImageSnapshot(); |
| 1136 | |
| 1137 | SkBitmap bitmap; |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 1138 | image->asLegacyBitmap(&bitmap); |
Matt Sarett | 22886c4 | 2016-11-22 11:31:41 -0500 | [diff] [blame] | 1139 | for (int y = 0; y < 10; y++) { |
| 1140 | for (int x = 0; x < 10; x++) { |
| 1141 | REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y))); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | DEF_TEST(OverdrawSurface_Raster, r) { |
| 1147 | sk_sp<SkSurface> surface = create_surface(); |
| 1148 | test_overdraw_surface(r, surface.get()); |
| 1149 | } |
| 1150 | |
Matt Sarett | 22886c4 | 2016-11-22 11:31:41 -0500 | [diff] [blame] | 1151 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 1152 | auto context = ctxInfo.directContext(); |
Matt Sarett | 22886c4 | 2016-11-22 11:31:41 -0500 | [diff] [blame] | 1153 | sk_sp<SkSurface> surface = create_gpu_surface(context); |
| 1154 | test_overdraw_surface(r, surface.get()); |
| 1155 | } |
Mike Reed | 44d04bd | 2017-06-28 19:57:21 -0400 | [diff] [blame] | 1156 | |
| 1157 | DEF_TEST(Surface_null, r) { |
| 1158 | REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr); |
| 1159 | |
| 1160 | const int w = 37; |
| 1161 | const int h = 1000; |
| 1162 | auto surf = SkSurface::MakeNull(w, h); |
| 1163 | auto canvas = surf->getCanvas(); |
| 1164 | |
| 1165 | canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw |
| 1166 | REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr); |
| 1167 | } |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 1168 | |
| 1169 | // assert: if a given imageinfo is valid for a surface, then it must be valid for an image |
| 1170 | // (so the snapshot can succeed) |
| 1171 | DEF_TEST(surface_image_unity, reporter) { |
| 1172 | auto do_test = [reporter](const SkImageInfo& info) { |
| 1173 | size_t rowBytes = info.minRowBytes(); |
| 1174 | auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr); |
| 1175 | if (surf) { |
| 1176 | auto img = surf->makeImageSnapshot(); |
| 1177 | if (!img && false) { // change to true to document the differences |
| 1178 | SkDebugf("image failed: [%08X %08X] %14s %s\n", |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 1179 | info.width(), |
| 1180 | info.height(), |
| 1181 | ToolUtils::colortype_name(info.colorType()), |
| 1182 | ToolUtils::alphatype_name(info.alphaType())); |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 1183 | return; |
| 1184 | } |
| 1185 | REPORTER_ASSERT(reporter, img != nullptr); |
| 1186 | |
| 1187 | char dummyPixel = 0; // just need a valid address (not a valid size) |
| 1188 | SkPixmap pmap = { info, &dummyPixel, rowBytes }; |
| 1189 | img = SkImage::MakeFromRaster(pmap, nullptr, nullptr); |
| 1190 | REPORTER_ASSERT(reporter, img != nullptr); |
| 1191 | } |
| 1192 | }; |
| 1193 | |
Mike Klein | e978ca2 | 2018-10-29 11:29:58 -0400 | [diff] [blame] | 1194 | const int32_t sizes[] = { -1, 0, 1, 1 << 18 }; |
Mike Klein | 30dc8f9 | 2018-02-16 10:08:10 -0500 | [diff] [blame] | 1195 | for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) { |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 1196 | SkColorType ct = static_cast<SkColorType>(cti); |
Mike Klein | 30dc8f9 | 2018-02-16 10:08:10 -0500 | [diff] [blame] | 1197 | for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) { |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 1198 | SkAlphaType at = static_cast<SkAlphaType>(ati); |
| 1199 | for (int32_t size : sizes) { |
| 1200 | do_test(SkImageInfo::Make(1, size, ct, at)); |
| 1201 | do_test(SkImageInfo::Make(size, 1, ct, at)); |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | } |