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" |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 15 | #include "SkSurface.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 16 | #include "Test.h" |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 17 | #include "TestingSpecialImageAccess.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 18 | |
| 19 | #if SK_SUPPORT_GPU |
| 20 | #include "GrContext.h" |
| 21 | #endif |
| 22 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 23 | |
| 24 | // This test creates backing resources exactly sized to [kFullSize x kFullSize]. |
| 25 | // It then wraps them in an SkSpecialImage with only the center (red) region being active. |
| 26 | // It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none |
| 27 | // of the inactive (green) region leaked out. |
| 28 | |
| 29 | static const int kSmallerSize = 10; |
| 30 | static const int kPad = 3; |
| 31 | static const int kFullSize = kSmallerSize + 2 * kPad; |
| 32 | |
| 33 | // Create a bitmap with red in the center and green around it |
| 34 | static SkBitmap create_bm() { |
| 35 | SkBitmap bm; |
| 36 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 37 | |
| 38 | SkCanvas temp(bm); |
| 39 | |
| 40 | temp.clear(SK_ColorGREEN); |
| 41 | SkPaint p; |
| 42 | p.setColor(SK_ColorRED); |
| 43 | p.setAntiAlias(false); |
| 44 | |
| 45 | temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 46 | SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)), |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 47 | p); |
| 48 | |
| 49 | return bm; |
| 50 | } |
| 51 | |
| 52 | // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw) |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 53 | static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter, |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 54 | bool peekPixelsSucceeds, bool peekTextureSucceeds, |
| 55 | int offset, int size) { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 56 | const SkIRect subset = TestingSpecialImageAccess::Subset(img.get()); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, offset == subset.left()); |
| 58 | REPORTER_ASSERT(reporter, offset == subset.top()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); |
| 60 | REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); |
| 61 | |
| 62 | //-------------- |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 63 | // Test that peekTexture reports the correct backing type |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, peekTextureSucceeds == |
| 65 | !!TestingSpecialImageAccess::PeekTexture(img.get())); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 66 | |
| 67 | //-------------- |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 68 | // Test that peekPixels reports the correct backing type |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 69 | SkPixmap pixmap; |
| 70 | REPORTER_ASSERT(reporter, peekPixelsSucceeds == |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 71 | !!TestingSpecialImageAccess::PeekPixels(img.get(), &pixmap)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 72 | if (peekPixelsSucceeds) { |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, size == pixmap.width()); |
| 74 | REPORTER_ASSERT(reporter, size == pixmap.height()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | //-------------- |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 78 | // Test that draw restricts itself to the subset |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 79 | SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType); |
| 80 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 81 | sk_sp<SkSpecialSurface> surf(img->makeSurface(info)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 82 | |
| 83 | SkCanvas* canvas = surf->getCanvas(); |
| 84 | |
| 85 | canvas->clear(SK_ColorBLUE); |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 86 | img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 87 | |
| 88 | SkBitmap bm; |
| 89 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 90 | |
| 91 | bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); |
| 92 | SkASSERT_RELEASE(result); |
| 93 | |
| 94 | // Only the center (red) portion should've been drawn into the canvas |
| 95 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); |
| 96 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); |
| 97 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, |
| 98 | kSmallerSize+kPad-1)); |
| 99 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, |
| 100 | kSmallerSize+kPad)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 101 | |
| 102 | //-------------- |
| 103 | // Test that makeTightSubset & makeTightSurface return appropriately sized objects |
| 104 | // of the correct backing type |
| 105 | SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height()); |
| 106 | { |
| 107 | sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset)); |
| 108 | |
| 109 | REPORTER_ASSERT(reporter, tightImg->width() == subset.width()); |
| 110 | REPORTER_ASSERT(reporter, tightImg->height() == subset.height()); |
| 111 | REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture()); |
| 112 | SkPixmap tmpPixmap; |
| 113 | REPORTER_ASSERT(reporter, peekPixelsSucceeds == !!tightImg->peekPixels(&tmpPixmap)); |
| 114 | } |
| 115 | { |
| 116 | SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), |
| 117 | kPremul_SkAlphaType); |
| 118 | sk_sp<SkSurface> tightSurf(img->makeTightSurface(info)); |
| 119 | |
| 120 | REPORTER_ASSERT(reporter, tightSurf->width() == subset.width()); |
| 121 | REPORTER_ASSERT(reporter, tightSurf->height() == subset.height()); |
| 122 | REPORTER_ASSERT(reporter, peekTextureSucceeds == |
| 123 | !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess)); |
| 124 | SkPixmap tmpPixmap; |
| 125 | REPORTER_ASSERT(reporter, peekPixelsSucceeds == !!tightSurf->peekPixels(&tmpPixmap)); |
| 126 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | DEF_TEST(SpecialImage_Raster, reporter) { |
| 130 | SkBitmap bm = create_bm(); |
| 131 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 132 | sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 133 | nullptr, |
| 134 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 135 | bm)); |
| 136 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 137 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 138 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 139 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 140 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(nullptr, subset, bm)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 141 | test_image(subSImg1, reporter, true, false, kPad, kFullSize); |
| 142 | } |
| 143 | |
| 144 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 145 | sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 146 | test_image(subSImg2, reporter, true, false, 0, kSmallerSize); |
| 147 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | DEF_TEST(SpecialImage_Image, reporter) { |
| 151 | SkBitmap bm = create_bm(); |
| 152 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 153 | sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 154 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 155 | sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 156 | nullptr, |
| 157 | SkIRect::MakeWH(kFullSize, kFullSize), |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 158 | fullImage)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 159 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 160 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 161 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 162 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 163 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, subset, |
| 164 | fullImage)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 165 | test_image(subSImg1, reporter, true, false, kPad, kFullSize); |
| 166 | } |
| 167 | |
| 168 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 169 | sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 170 | test_image(subSImg2, reporter, true, false, 0, kSmallerSize); |
| 171 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 172 | } |
| 173 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 174 | DEF_TEST(SpecialImage_Pixmap, reporter) { |
| 175 | SkAutoPixmapStorage pixmap; |
| 176 | |
| 177 | const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType); |
| 178 | pixmap.alloc(info); |
| 179 | pixmap.erase(SK_ColorGREEN); |
| 180 | |
| 181 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 182 | |
| 183 | pixmap.erase(SK_ColorRED, subset); |
| 184 | |
| 185 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 186 | sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(nullptr, subset, pixmap, |
| 187 | nullptr, nullptr)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 188 | test_image(img, reporter, true, false, kPad, kFullSize); |
| 189 | } |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 193 | #if SK_SUPPORT_GPU |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 194 | |
| 195 | static void test_texture_backed(skiatest::Reporter* reporter, |
| 196 | const sk_sp<SkSpecialImage>& orig, |
| 197 | const sk_sp<SkSpecialImage>& gpuBacked) { |
| 198 | REPORTER_ASSERT(reporter, gpuBacked); |
| 199 | REPORTER_ASSERT(reporter, gpuBacked->peekTexture()); |
| 200 | REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID()); |
| 201 | REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() && |
| 202 | gpuBacked->subset().height() == orig->subset().height()); |
| 203 | } |
| 204 | |
| 205 | // Test out the SkSpecialImage::makeTextureImage entry point |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 206 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) { |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 207 | GrContext* context = ctxInfo.fGrContext; |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 208 | SkBitmap bm = create_bm(); |
| 209 | |
| 210 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 211 | |
| 212 | { |
| 213 | // raster |
| 214 | sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster( |
| 215 | nullptr, |
| 216 | SkIRect::MakeWH(kFullSize, |
| 217 | kFullSize), |
| 218 | bm)); |
| 219 | |
| 220 | { |
| 221 | sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(nullptr, context)); |
| 222 | test_texture_backed(reporter, rasterImage, fromRaster); |
| 223 | } |
| 224 | |
| 225 | { |
| 226 | sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset)); |
| 227 | |
| 228 | sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(nullptr, context)); |
| 229 | test_texture_backed(reporter, subRasterImage, fromSubRaster); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | { |
| 234 | // gpu |
| 235 | GrSurfaceDesc desc; |
| 236 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 237 | desc.fFlags = kNone_GrSurfaceFlags; |
| 238 | desc.fWidth = kFullSize; |
| 239 | desc.fHeight = kFullSize; |
| 240 | |
| 241 | SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, |
| 242 | SkBudgeted::kNo, |
| 243 | bm.getPixels(), |
| 244 | 0)); |
| 245 | if (!texture) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu( |
| 250 | nullptr, |
| 251 | SkIRect::MakeWH(kFullSize, |
| 252 | kFullSize), |
| 253 | kNeedNewImageUniqueID_SpecialImage, |
| 254 | texture)); |
| 255 | |
| 256 | { |
| 257 | sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(nullptr, context)); |
| 258 | test_texture_backed(reporter, gpuImage, fromGPU); |
| 259 | } |
| 260 | |
| 261 | { |
| 262 | sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset)); |
| 263 | |
| 264 | sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(nullptr, context)); |
| 265 | test_texture_backed(reporter, subGPUImage, fromSubGPU); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 270 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) { |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 271 | GrContext* context = ctxInfo.fGrContext; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 272 | SkBitmap bm = create_bm(); |
| 273 | |
| 274 | GrSurfaceDesc desc; |
| 275 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 276 | desc.fFlags = kNone_GrSurfaceFlags; |
| 277 | desc.fWidth = kFullSize; |
| 278 | desc.fHeight = kFullSize; |
| 279 | |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 280 | SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, |
| 281 | SkBudgeted::kNo, |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 282 | bm.getPixels(), 0)); |
| 283 | if (!texture) { |
| 284 | return; |
| 285 | } |
| 286 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 287 | sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 288 | nullptr, |
| 289 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 290 | kNeedNewImageUniqueID_SpecialImage, |
| 291 | texture)); |
| 292 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 293 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 294 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 295 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 296 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu( |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 297 | nullptr, subset, |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 298 | kNeedNewImageUniqueID_SpecialImage, |
| 299 | texture)); |
| 300 | test_image(subSImg1, reporter, false, true, kPad, kFullSize); |
| 301 | } |
| 302 | |
| 303 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 304 | sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 305 | test_image(subSImg2, reporter, false, true, kPad, kFullSize); |
| 306 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | #endif |