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