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 | } |
| 23 | |
| 24 | static const SkIRect& Subset(const SkSpecialImage* img) { |
| 25 | return img->subset(); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | // Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise |
| 30 | // the gpu's loose fit behavior |
| 31 | static const int kSmallerSize = 10; |
| 32 | static const int kPad = 5; |
| 33 | static const int kFullSize = kSmallerSize + 2 * kPad; |
| 34 | |
| 35 | // Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot) |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 36 | static void test_surface(const sk_sp<SkSpecialSurface>& surf, |
| 37 | skiatest::Reporter* reporter, |
| 38 | int offset) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 39 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 40 | const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, offset == surfSubset.fLeft); |
| 42 | REPORTER_ASSERT(reporter, offset == surfSubset.fTop); |
| 43 | REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width()); |
| 44 | REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height()); |
| 45 | |
| 46 | SkCanvas* canvas = surf->getCanvas(); |
| 47 | SkASSERT_RELEASE(canvas); |
| 48 | |
| 49 | canvas->clear(SK_ColorRED); |
| 50 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 51 | sk_sp<SkSpecialImage> img(surf->makeImageSnapshot()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 52 | REPORTER_ASSERT(reporter, img); |
| 53 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 54 | const SkIRect imgSubset = TestingSpecialSurfaceAccess::Subset(img.get()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, surfSubset == imgSubset); |
| 56 | |
| 57 | // the canvas was invalidated by the newImageSnapshot call |
| 58 | REPORTER_ASSERT(reporter, !surf->getCanvas()); |
| 59 | } |
| 60 | |
| 61 | DEF_TEST(SpecialSurface_Raster, reporter) { |
| 62 | |
| 63 | SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 64 | sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(nullptr, info)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 65 | |
| 66 | test_surface(surf, reporter, 0); |
| 67 | } |
| 68 | |
| 69 | DEF_TEST(SpecialSurface_Raster2, reporter) { |
| 70 | |
| 71 | SkBitmap bm; |
| 72 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 73 | |
| 74 | const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 75 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 76 | sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(nullptr, subset, bm)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 77 | |
| 78 | test_surface(surf, reporter, kPad); |
| 79 | |
| 80 | // TODO: check that the clear didn't escape the active region |
| 81 | } |
| 82 | |
| 83 | #if SK_SUPPORT_GPU |
| 84 | |
| 85 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, context) { |
| 86 | GrSurfaceDesc desc; |
| 87 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 88 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 89 | desc.fWidth = kSmallerSize; |
| 90 | desc.fHeight = kSmallerSize; |
| 91 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 92 | sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRenderTarget(nullptr, context, desc)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 93 | |
| 94 | test_surface(surf, reporter, 0); |
| 95 | } |
| 96 | |
| 97 | // test the more flexible factory |
| 98 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu2, reporter, context) { |
| 99 | GrSurfaceDesc desc; |
| 100 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 101 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 102 | desc.fWidth = kFullSize; |
| 103 | desc.fHeight = kFullSize; |
| 104 | |
| 105 | SkAutoTUnref<GrTexture> temp(context->textureProvider()->createApproxTexture(desc)); |
| 106 | SkASSERT_RELEASE(temp); |
| 107 | |
| 108 | const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 109 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 110 | sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromTexture(nullptr, subset, temp)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 111 | |
| 112 | test_surface(surf, reporter, kPad); |
| 113 | |
| 114 | // TODO: check that the clear didn't escape the active region |
| 115 | } |
| 116 | |
| 117 | #endif |