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