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