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 "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkImage.h" |
| 11 | #include "SkSpecialImage.h" |
| 12 | #include "SkSpecialSurface.h" |
| 13 | #include "Test.h" |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame^] | 14 | #include "TestingSpecialImageAccess.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 15 | |
| 16 | #if SK_SUPPORT_GPU |
| 17 | #include "GrContext.h" |
| 18 | #endif |
| 19 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 20 | |
| 21 | // This test creates backing resources exactly sized to [kFullSize x kFullSize]. |
| 22 | // It then wraps them in an SkSpecialImage with only the center (red) region being active. |
| 23 | // It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none |
| 24 | // of the inactive (green) region leaked out. |
| 25 | |
| 26 | static const int kSmallerSize = 10; |
| 27 | static const int kPad = 3; |
| 28 | static const int kFullSize = kSmallerSize + 2 * kPad; |
| 29 | |
| 30 | // Create a bitmap with red in the center and green around it |
| 31 | static SkBitmap create_bm() { |
| 32 | SkBitmap bm; |
| 33 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 34 | |
| 35 | SkCanvas temp(bm); |
| 36 | |
| 37 | temp.clear(SK_ColorGREEN); |
| 38 | SkPaint p; |
| 39 | p.setColor(SK_ColorRED); |
| 40 | p.setAntiAlias(false); |
| 41 | |
| 42 | temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), |
| 43 | SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)), |
| 44 | p); |
| 45 | |
| 46 | return bm; |
| 47 | } |
| 48 | |
| 49 | // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw) |
| 50 | static void test_image(SkSpecialImage* img, skiatest::Reporter* reporter, |
| 51 | bool peekPixelsSucceeds, bool peekTextureSucceeds) { |
| 52 | const SkIRect subset = TestingSpecialImageAccess::Subset(img); |
| 53 | REPORTER_ASSERT(reporter, kPad == subset.left()); |
| 54 | REPORTER_ASSERT(reporter, kPad == subset.top()); |
| 55 | REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); |
| 56 | REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); |
| 57 | |
| 58 | //-------------- |
| 59 | REPORTER_ASSERT(reporter, peekTextureSucceeds == !!TestingSpecialImageAccess::PeekTexture(img)); |
| 60 | |
| 61 | //-------------- |
| 62 | SkPixmap pixmap; |
| 63 | REPORTER_ASSERT(reporter, peekPixelsSucceeds == |
| 64 | !!TestingSpecialImageAccess::PeekPixels(img, &pixmap)); |
| 65 | if (peekPixelsSucceeds) { |
| 66 | REPORTER_ASSERT(reporter, kFullSize == pixmap.width()); |
| 67 | REPORTER_ASSERT(reporter, kFullSize == pixmap.height()); |
| 68 | } |
| 69 | |
| 70 | //-------------- |
| 71 | SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType); |
| 72 | |
| 73 | SkAutoTUnref<SkSpecialSurface> surf(img->newSurface(info)); |
| 74 | |
| 75 | SkCanvas* canvas = surf->getCanvas(); |
| 76 | |
| 77 | canvas->clear(SK_ColorBLUE); |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 78 | img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 79 | |
| 80 | SkBitmap bm; |
| 81 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 82 | |
| 83 | bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); |
| 84 | SkASSERT_RELEASE(result); |
| 85 | |
| 86 | // Only the center (red) portion should've been drawn into the canvas |
| 87 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); |
| 88 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); |
| 89 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, |
| 90 | kSmallerSize+kPad-1)); |
| 91 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, |
| 92 | kSmallerSize+kPad)); |
| 93 | } |
| 94 | |
| 95 | DEF_TEST(SpecialImage_Raster, reporter) { |
| 96 | SkBitmap bm = create_bm(); |
| 97 | |
| 98 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 99 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 100 | SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromRaster(nullptr, subset, bm)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 101 | test_image(img, reporter, true, false); |
| 102 | } |
| 103 | |
| 104 | DEF_TEST(SpecialImage_Image, reporter) { |
| 105 | SkBitmap bm = create_bm(); |
| 106 | |
| 107 | SkAutoTUnref<SkImage> fullImage(SkImage::NewFromBitmap(bm)); |
| 108 | |
| 109 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 110 | |
| 111 | SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromImage(subset, fullImage)); |
| 112 | test_image(img, reporter, true, false); |
| 113 | } |
| 114 | |
| 115 | #if SK_SUPPORT_GPU |
| 116 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) { |
| 117 | SkBitmap bm = create_bm(); |
| 118 | |
| 119 | GrSurfaceDesc desc; |
| 120 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 121 | desc.fFlags = kNone_GrSurfaceFlags; |
| 122 | desc.fWidth = kFullSize; |
| 123 | desc.fHeight = kFullSize; |
| 124 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 125 | SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, SkBudgeted::kNo, |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 126 | bm.getPixels(), 0)); |
| 127 | if (!texture) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 132 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 133 | SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromGpu(nullptr, subset, |
| 134 | kNeedNewImageUniqueID_SpecialImage, |
| 135 | texture)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 136 | test_image(img, reporter, false, true); |
| 137 | } |
| 138 | |
| 139 | #endif |