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