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