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