robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkSpecialImage.h" |
| 11 | #include "SkSpecialSurface.h" |
| 12 | #include "Test.h" |
| 13 | |
| 14 | #if SK_SUPPORT_GPU |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame^] | 15 | #include "GrCaps.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 16 | #include "GrContext.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame^] | 17 | #include "GrContextPriv.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 18 | #include "SkGr.h" |
| 19 | #endif |
| 20 | |
| 21 | class TestingSpecialSurfaceAccess { |
| 22 | public: |
| 23 | static const SkIRect& Subset(const SkSpecialSurface* surf) { |
| 24 | return surf->subset(); |
| 25 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | // Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise |
| 29 | // the gpu's loose fit behavior |
| 30 | static const int kSmallerSize = 10; |
| 31 | static const int kPad = 5; |
| 32 | static const int kFullSize = kSmallerSize + 2 * kPad; |
| 33 | |
| 34 | // Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot) |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 35 | static void test_surface(const sk_sp<SkSpecialSurface>& surf, |
| 36 | skiatest::Reporter* reporter, |
| 37 | int offset) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 38 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 39 | const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 40 | REPORTER_ASSERT(reporter, offset == surfSubset.fLeft); |
| 41 | REPORTER_ASSERT(reporter, offset == surfSubset.fTop); |
| 42 | REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width()); |
| 43 | REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height()); |
| 44 | |
| 45 | SkCanvas* canvas = surf->getCanvas(); |
| 46 | SkASSERT_RELEASE(canvas); |
| 47 | |
| 48 | canvas->clear(SK_ColorRED); |
| 49 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 50 | sk_sp<SkSpecialImage> img(surf->makeImageSnapshot()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, img); |
| 52 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 53 | const SkIRect imgSubset = img->subset(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, surfSubset == imgSubset); |
| 55 | |
| 56 | // the canvas was invalidated by the newImageSnapshot call |
| 57 | REPORTER_ASSERT(reporter, !surf->getCanvas()); |
| 58 | } |
| 59 | |
| 60 | DEF_TEST(SpecialSurface_Raster, reporter) { |
| 61 | |
| 62 | SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 63 | sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(info)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 64 | |
| 65 | test_surface(surf, reporter, 0); |
| 66 | } |
| 67 | |
| 68 | DEF_TEST(SpecialSurface_Raster2, reporter) { |
| 69 | |
| 70 | SkBitmap bm; |
| 71 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 72 | |
| 73 | const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 74 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 75 | sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(subset, bm)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 76 | |
| 77 | test_surface(surf, reporter, kPad); |
| 78 | |
| 79 | // TODO: check that the clear didn't escape the active region |
| 80 | } |
| 81 | |
| 82 | #if SK_SUPPORT_GPU |
| 83 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 84 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, ctxInfo) { |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 85 | for (auto config : { kRGBA_8888_GrPixelConfig, kRGBA_1010102_GrPixelConfig }) { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame^] | 86 | if (!ctxInfo.grContext()->contextPriv().caps()->isConfigRenderable(config)) { |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 87 | continue; |
| 88 | } |
| 89 | sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRenderTarget(ctxInfo.grContext(), |
| 90 | kSmallerSize, kSmallerSize, |
| 91 | config, nullptr)); |
| 92 | test_surface(surf, reporter, 0); |
| 93 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 94 | } |
| 95 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 96 | #endif |