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