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 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 8 | #include "SkAutoPixmapStorage.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkImage.h" |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 12 | #include "SkPixmap.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 13 | #include "SkSpecialImage.h" |
| 14 | #include "SkSpecialSurface.h" |
| 15 | #include "Test.h" |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 16 | #include "TestingSpecialImageAccess.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 17 | |
| 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrContext.h" |
| 20 | #endif |
| 21 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 22 | |
| 23 | // This test creates backing resources exactly sized to [kFullSize x kFullSize]. |
| 24 | // It then wraps them in an SkSpecialImage with only the center (red) region being active. |
| 25 | // It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none |
| 26 | // of the inactive (green) region leaked out. |
| 27 | |
| 28 | static const int kSmallerSize = 10; |
| 29 | static const int kPad = 3; |
| 30 | static const int kFullSize = kSmallerSize + 2 * kPad; |
| 31 | |
| 32 | // Create a bitmap with red in the center and green around it |
| 33 | static SkBitmap create_bm() { |
| 34 | SkBitmap bm; |
| 35 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 36 | |
| 37 | SkCanvas temp(bm); |
| 38 | |
| 39 | temp.clear(SK_ColorGREEN); |
| 40 | SkPaint p; |
| 41 | p.setColor(SK_ColorRED); |
| 42 | p.setAntiAlias(false); |
| 43 | |
| 44 | temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), |
| 45 | SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)), |
| 46 | p); |
| 47 | |
| 48 | return bm; |
| 49 | } |
| 50 | |
| 51 | // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw) |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 52 | static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter, |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 53 | bool peekPixelsSucceeds, bool peekTextureSucceeds, |
| 54 | int offset, int size) { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 55 | const SkIRect subset = TestingSpecialImageAccess::Subset(img.get()); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 56 | REPORTER_ASSERT(reporter, offset == subset.left()); |
| 57 | REPORTER_ASSERT(reporter, offset == subset.top()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); |
| 59 | REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); |
| 60 | |
| 61 | //-------------- |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, peekTextureSucceeds == |
| 63 | !!TestingSpecialImageAccess::PeekTexture(img.get())); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 64 | |
| 65 | //-------------- |
| 66 | SkPixmap pixmap; |
| 67 | REPORTER_ASSERT(reporter, peekPixelsSucceeds == |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 68 | !!TestingSpecialImageAccess::PeekPixels(img.get(), &pixmap)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 69 | if (peekPixelsSucceeds) { |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, size == pixmap.width()); |
| 71 | REPORTER_ASSERT(reporter, size == pixmap.height()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | //-------------- |
| 75 | SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType); |
| 76 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 77 | sk_sp<SkSpecialSurface> surf(img->makeSurface(info)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 78 | |
| 79 | SkCanvas* canvas = surf->getCanvas(); |
| 80 | |
| 81 | canvas->clear(SK_ColorBLUE); |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 82 | img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 83 | |
| 84 | SkBitmap bm; |
| 85 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 86 | |
| 87 | bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); |
| 88 | SkASSERT_RELEASE(result); |
| 89 | |
| 90 | // Only the center (red) portion should've been drawn into the canvas |
| 91 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); |
| 92 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); |
| 93 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, |
| 94 | kSmallerSize+kPad-1)); |
| 95 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, |
| 96 | kSmallerSize+kPad)); |
| 97 | } |
| 98 | |
| 99 | DEF_TEST(SpecialImage_Raster, reporter) { |
| 100 | SkBitmap bm = create_bm(); |
| 101 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 102 | sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 103 | nullptr, |
| 104 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 105 | bm)); |
| 106 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 107 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 108 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 109 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 110 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(nullptr, subset, bm)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 111 | test_image(subSImg1, reporter, true, false, kPad, kFullSize); |
| 112 | } |
| 113 | |
| 114 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 115 | sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 116 | test_image(subSImg2, reporter, true, false, 0, kSmallerSize); |
| 117 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | DEF_TEST(SpecialImage_Image, reporter) { |
| 121 | SkBitmap bm = create_bm(); |
| 122 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 123 | sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 124 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 125 | sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 126 | nullptr, |
| 127 | SkIRect::MakeWH(kFullSize, kFullSize), |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 128 | fullImage)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 129 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 130 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 131 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 132 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 133 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, subset, |
| 134 | fullImage)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 135 | test_image(subSImg1, reporter, true, false, kPad, kFullSize); |
| 136 | } |
| 137 | |
| 138 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 139 | sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 140 | test_image(subSImg2, reporter, true, false, 0, kSmallerSize); |
| 141 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 142 | } |
| 143 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 144 | DEF_TEST(SpecialImage_Pixmap, reporter) { |
| 145 | SkAutoPixmapStorage pixmap; |
| 146 | |
| 147 | const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType); |
| 148 | pixmap.alloc(info); |
| 149 | pixmap.erase(SK_ColorGREEN); |
| 150 | |
| 151 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 152 | |
| 153 | pixmap.erase(SK_ColorRED, subset); |
| 154 | |
| 155 | { |
| 156 | // The SkAutoPixmapStorage keeps hold of the memory |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 157 | sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(nullptr, subset, pixmap, |
| 158 | nullptr, nullptr)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 159 | test_image(img, reporter, true, false, kPad, kFullSize); |
| 160 | } |
| 161 | |
| 162 | { |
| 163 | // The image takes ownership of the memory |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 164 | sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 165 | nullptr, subset, pixmap, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 166 | [] (void* addr, void*) -> void { |
| 167 | sk_free(addr); |
| 168 | }, |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 169 | nullptr)); |
| 170 | pixmap.release(); |
| 171 | test_image(img, reporter, true, false, kPad, kFullSize); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 176 | #if SK_SUPPORT_GPU |
| 177 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) { |
| 178 | SkBitmap bm = create_bm(); |
| 179 | |
| 180 | GrSurfaceDesc desc; |
| 181 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 182 | desc.fFlags = kNone_GrSurfaceFlags; |
| 183 | desc.fWidth = kFullSize; |
| 184 | desc.fHeight = kFullSize; |
| 185 | |
bungeman | 92701ab | 2016-03-18 05:36:20 -0700 | [diff] [blame] | 186 | SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, SkBudgeted::kNo, |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 187 | bm.getPixels(), 0)); |
| 188 | if (!texture) { |
| 189 | return; |
| 190 | } |
| 191 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 192 | sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 193 | nullptr, |
| 194 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 195 | kNeedNewImageUniqueID_SpecialImage, |
| 196 | texture)); |
| 197 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 198 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 199 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 200 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 201 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 202 | nullptr, subset, |
| 203 | kNeedNewImageUniqueID_SpecialImage, |
| 204 | texture)); |
| 205 | test_image(subSImg1, reporter, false, true, kPad, kFullSize); |
| 206 | } |
| 207 | |
| 208 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 209 | sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 210 | test_image(subSImg2, reporter, false, true, kPad, kFullSize); |
| 211 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | #endif |