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