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