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