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