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