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