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