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