Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | */ |
| 7 | |
| 8 | // This is a GPU-backend specific test. |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "tests/Test.h" |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 11 | |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 12 | using namespace sk_gpu_test; |
| 13 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tools/gpu/GrContextFactory.h" |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 15 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 16 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkCanvas.h" |
| 18 | #include "include/core/SkSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 19 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/core/SkImagePriv.h" |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 21 | |
| 22 | static bool surface_is_expected_color(SkSurface* surf, const SkImageInfo& ii, SkColor color) { |
| 23 | SkBitmap bm; |
| 24 | bm.allocPixels(ii); |
| 25 | |
| 26 | surf->readPixels(bm, 0, 0); |
| 27 | |
| 28 | for (int y = 0; y < bm.height(); ++y) { |
| 29 | for (int x = 0; x < bm.width(); ++x) { |
| 30 | if (bm.getColor(x, y) != color) { |
| 31 | return false; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return true; |
| 37 | } |
| 38 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 39 | static void basic_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) { |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 40 | const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType); |
| 41 | |
| 42 | SkBitmap bm; |
| 43 | bm.allocPixels(ii); |
| 44 | |
| 45 | SkCanvas bmCanvas(bm); |
| 46 | bmCanvas.clear(SK_ColorRED); |
| 47 | |
| 48 | // We start off with the raster image being all red. |
| 49 | sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode); |
| 50 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 51 | sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 52 | SkCanvas* canvas = gpuSurface->getCanvas(); |
| 53 | |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 54 | // w/o pinning - the gpu draw always reflects the current state of the underlying bitmap |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 55 | { |
| 56 | canvas->drawImage(img, 0, 0); |
| 57 | REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED)); |
| 58 | |
| 59 | bmCanvas.clear(SK_ColorGREEN); |
| 60 | |
| 61 | canvas->drawImage(img, 0, 0); |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN)); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // w/ pinning - the gpu draw is stuck at the pinned state |
| 66 | { |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 67 | SkImage_pinAsTexture(img.get(), rContext); // pin at blue |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 68 | |
| 69 | canvas->drawImage(img, 0, 0); |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN)); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 71 | |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 72 | bmCanvas.clear(SK_ColorBLUE); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 73 | |
| 74 | canvas->drawImage(img, 0, 0); |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN)); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 76 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 77 | SkImage_unpinAsTexture(img.get(), rContext); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 80 | // once unpinned local changes will be picked up |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 81 | { |
| 82 | canvas->drawImage(img, 0, 0); |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE)); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | // Deleting the context while there are still pinned images shouldn't result in a crash. |
| 88 | static void cleanup_test(skiatest::Reporter* reporter) { |
| 89 | |
| 90 | const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType); |
| 91 | |
| 92 | SkBitmap bm; |
| 93 | bm.allocPixels(ii); |
| 94 | |
| 95 | SkCanvas bmCanvas(bm); |
| 96 | bmCanvas.clear(SK_ColorRED); |
| 97 | |
Brian Salomon | 490f192 | 2021-02-01 13:14:37 -0500 | [diff] [blame] | 98 | GrMockOptions options; |
| 99 | sk_sp<GrDirectContext> mockContext = GrDirectContext::MakeMock(&options); |
| 100 | |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 101 | for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) { |
| 102 | GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i; |
| 103 | |
| 104 | { |
| 105 | sk_sp<SkImage> img; |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 106 | GrDirectContext* dContext = nullptr; |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 107 | |
| 108 | { |
| 109 | GrContextFactory testFactory; |
| 110 | ContextInfo info = testFactory.getContextInfo(ctxType); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 111 | dContext = info.directContext(); |
| 112 | if (!dContext) { |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 113 | continue; |
| 114 | } |
| 115 | |
| 116 | img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 117 | if (!SkImage_pinAsTexture(img.get(), dContext)) { |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 118 | continue; |
| 119 | } |
Brian Salomon | 490f192 | 2021-02-01 13:14:37 -0500 | [diff] [blame] | 120 | // Pinning on a second context should be blocked. |
| 121 | REPORTER_ASSERT(reporter, !SkImage_pinAsTexture(img.get(), mockContext.get())); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 124 | // The context used to pin the image is gone at this point! |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 125 | // "context" isn't technically used in this call but it can't be null! |
| 126 | // We don't really want to support this use case but it currently happens. |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 127 | SkImage_unpinAsTexture(img.get(), dContext); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 133 | basic_test(reporter, ctxInfo.directContext()); |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 134 | cleanup_test(reporter); |
| 135 | } |