SpecialImageTest.cpp revision a76554d2ae7f676776c1d472aa433b048f418ea9
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 "SkAutoPixmapStorage.h" 9#include "SkBitmap.h" 10#include "SkCanvas.h" 11#include "SkImage.h" 12#include "SkPixmap.h" 13#include "SkSpecialImage.h" 14#include "SkSpecialSurface.h" 15#include "SkSurface.h" 16#include "Test.h" 17 18#if SK_SUPPORT_GPU 19#include "GrContext.h" 20#endif 21 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 28static const int kSmallerSize = 10; 29static const int kPad = 3; 30static const int kFullSize = kSmallerSize + 2 * kPad; 31 32// Create a bitmap with red in the center and green around it 33static 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) 52static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter, 53 GrContext* context, bool peekTextureSucceeds, 54 int offset, int size) { 55 const SkIRect subset = img->subset(); 56 REPORTER_ASSERT(reporter, offset == subset.left()); 57 REPORTER_ASSERT(reporter, offset == subset.top()); 58 REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); 59 REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); 60 61 //-------------- 62 // Test that peekTexture reports the correct backing type 63 REPORTER_ASSERT(reporter, peekTextureSucceeds == img->isTextureBacked()); 64 65#if SK_SUPPORT_GPU 66 //-------------- 67 // Test getTextureAsRef - as long as there is a context this should succeed 68 if (context) { 69 sk_sp<GrTexture> texture(img->asTextureRef(context)); 70 REPORTER_ASSERT(reporter, texture); 71 } 72#endif 73 74 //-------------- 75 // Test getROPixels - this should always succeed regardless of backing store 76 SkBitmap bitmap; 77 REPORTER_ASSERT(reporter, img->getROPixels(&bitmap)); 78 if (context) { 79 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width()); 80 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height()); 81 } else { 82 REPORTER_ASSERT(reporter, size == bitmap.width()); 83 REPORTER_ASSERT(reporter, size == bitmap.height()); 84 } 85 86 //-------------- 87 // Test that draw restricts itself to the subset 88 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType); 89 90 sk_sp<SkSpecialSurface> surf(img->makeSurface(info)); 91 92 SkCanvas* canvas = surf->getCanvas(); 93 94 canvas->clear(SK_ColorBLUE); 95 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); 96 97 SkBitmap bm; 98 bm.allocN32Pixels(kFullSize, kFullSize, true); 99 100 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); 101 SkASSERT_RELEASE(result); 102 103 // Only the center (red) portion should've been drawn into the canvas 104 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); 105 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); 106 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, 107 kSmallerSize+kPad-1)); 108 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, 109 kSmallerSize+kPad)); 110 111 //-------------- 112 // Test that makeTightSubset & makeTightSurface return appropriately sized objects 113 // of the correct backing type 114 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height()); 115 { 116 sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset)); 117 118 REPORTER_ASSERT(reporter, tightImg->width() == subset.width()); 119 REPORTER_ASSERT(reporter, tightImg->height() == subset.height()); 120 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture()); 121 SkPixmap tmpPixmap; 122 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightImg->peekPixels(&tmpPixmap)); 123 } 124 { 125 SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), 126 kPremul_SkAlphaType); 127 sk_sp<SkSurface> tightSurf(img->makeTightSurface(info)); 128 129 REPORTER_ASSERT(reporter, tightSurf->width() == subset.width()); 130 REPORTER_ASSERT(reporter, tightSurf->height() == subset.height()); 131 REPORTER_ASSERT(reporter, peekTextureSucceeds == 132 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess)); 133 SkPixmap tmpPixmap; 134 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels(&tmpPixmap)); 135 } 136} 137 138DEF_TEST(SpecialImage_Raster, reporter) { 139 SkBitmap bm = create_bm(); 140 141 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( 142 SkIRect::MakeWH(kFullSize, kFullSize), 143 bm)); 144 145 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 146 147 { 148 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm)); 149 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); 150 } 151 152 { 153 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); 154 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); 155 } 156} 157 158DEF_TEST(SpecialImage_Image, reporter) { 159 SkBitmap bm = create_bm(); 160 161 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); 162 163 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( 164 SkIRect::MakeWH(kFullSize, kFullSize), 165 fullImage)); 166 167 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 168 169 { 170 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage)); 171 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); 172 } 173 174 { 175 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); 176 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); 177 } 178} 179 180#if SK_SUPPORT_GPU 181 182static void test_texture_backed(skiatest::Reporter* reporter, 183 const sk_sp<SkSpecialImage>& orig, 184 const sk_sp<SkSpecialImage>& gpuBacked) { 185 REPORTER_ASSERT(reporter, gpuBacked); 186 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked()); 187 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID()); 188 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() && 189 gpuBacked->subset().height() == orig->subset().height()); 190 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace()); 191} 192 193// Test out the SkSpecialImage::makeTextureImage entry point 194DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) { 195 GrContext* context = ctxInfo.grContext(); 196 SkBitmap bm = create_bm(); 197 198 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 199 200 { 201 // raster 202 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster( 203 SkIRect::MakeWH(kFullSize, 204 kFullSize), 205 bm)); 206 207 { 208 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context)); 209 test_texture_backed(reporter, rasterImage, fromRaster); 210 } 211 212 { 213 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset)); 214 215 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context)); 216 test_texture_backed(reporter, subRasterImage, fromSubRaster); 217 } 218 } 219 220 { 221 // gpu 222 GrSurfaceDesc desc; 223 desc.fConfig = kSkia8888_GrPixelConfig; 224 desc.fFlags = kNone_GrSurfaceFlags; 225 desc.fWidth = kFullSize; 226 desc.fHeight = kFullSize; 227 228 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc, 229 SkBudgeted::kNo, 230 bm.getPixels(), 231 0)); 232 if (!texture) { 233 return; 234 } 235 236 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu( 237 SkIRect::MakeWH(kFullSize, 238 kFullSize), 239 kNeedNewImageUniqueID_SpecialImage, 240 std::move(texture), nullptr)); 241 242 { 243 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context)); 244 test_texture_backed(reporter, gpuImage, fromGPU); 245 } 246 247 { 248 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset)); 249 250 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context)); 251 test_texture_backed(reporter, subGPUImage, fromSubGPU); 252 } 253 } 254} 255 256DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) { 257 GrContext* context = ctxInfo.grContext(); 258 SkBitmap bm = create_bm(); 259 260 GrSurfaceDesc desc; 261 desc.fConfig = kSkia8888_GrPixelConfig; 262 desc.fFlags = kNone_GrSurfaceFlags; 263 desc.fWidth = kFullSize; 264 desc.fHeight = kFullSize; 265 266 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc, 267 SkBudgeted::kNo, 268 bm.getPixels(), 0)); 269 if (!texture) { 270 return; 271 } 272 273 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu( 274 SkIRect::MakeWH(kFullSize, kFullSize), 275 kNeedNewImageUniqueID_SpecialImage, 276 texture, nullptr)); 277 278 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 279 280 { 281 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu( 282 subset, 283 kNeedNewImageUniqueID_SpecialImage, 284 texture, nullptr)); 285 test_image(subSImg1, reporter, context, true, kPad, kFullSize); 286 } 287 288 { 289 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); 290 test_image(subSImg2, reporter, context, true, kPad, kFullSize); 291 } 292} 293 294#endif 295