1 /* 2 * Copyright 2011 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 <functional> 9 #include "gm.h" 10 #include "SkData.h" 11 #include "SkCanvas.h" 12 #include "SkRandom.h" 13 #include "SkStream.h" 14 #include "SkSurface.h" 15 16 #if SK_SUPPORT_GPU 17 #include "GrContext.h" 18 #endif 19 20 static void drawJpeg(SkCanvas* canvas, const SkISize& size) { 21 // TODO: Make this draw a file that is checked in, so it can 22 // be exercised on machines other than mike's. Will require a 23 // rebaseline. 24 SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.google.jpeg")); 25 if (nullptr == data.get()) { 26 return; 27 } 28 SkImage* image = SkImage::NewFromEncoded(data); 29 if (image) { 30 SkAutoCanvasRestore acr(canvas, true); 31 canvas->scale(size.width() * 1.0f / image->width(), 32 size.height() * 1.0f / image->height()); 33 canvas->drawImage(image, 0, 0, nullptr); 34 image->unref(); 35 } 36 } 37 38 static void drawContents(SkSurface* surface, SkColor fillC) { 39 SkSize size = SkSize::Make(SkIntToScalar(surface->width()), 40 SkIntToScalar(surface->height())); 41 SkCanvas* canvas = surface->getCanvas(); 42 43 SkScalar stroke = size.fWidth / 10; 44 SkScalar radius = (size.fWidth - stroke) / 2; 45 46 SkPaint paint; 47 48 paint.setAntiAlias(true); 49 paint.setColor(fillC); 50 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); 51 52 paint.setStyle(SkPaint::kStroke_Style); 53 paint.setStrokeWidth(stroke); 54 paint.setColor(SK_ColorBLACK); 55 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); 56 } 57 58 static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) { 59 drawContents(surf, SK_ColorRED); 60 SkImage* imgR = surf->newImageSnapshot(); 61 62 if (true) { 63 SkImage* imgR2 = surf->newImageSnapshot(); 64 SkASSERT(imgR == imgR2); 65 imgR2->unref(); 66 } 67 68 drawContents(surf, SK_ColorGREEN); 69 SkImage* imgG = surf->newImageSnapshot(); 70 71 // since we've drawn after we snapped imgR, imgG will be a different obj 72 SkASSERT(imgR != imgG); 73 74 drawContents(surf, SK_ColorBLUE); 75 76 SkPaint paint; 77 // paint.setFilterBitmap(true); 78 // paint.setAlpha(0x80); 79 80 canvas->drawImage(imgR, 0, 0, usePaint ? &paint : nullptr); 81 canvas->drawImage(imgG, 0, 80, usePaint ? &paint : nullptr); 82 surf->draw(canvas, 0, 160, usePaint ? &paint : nullptr); 83 84 SkRect src1, src2, src3; 85 src1.iset(0, 0, surf->width(), surf->height()); 86 src2.iset(-surf->width() / 2, -surf->height() / 2, 87 surf->width(), surf->height()); 88 src3.iset(0, 0, surf->width() / 2, surf->height() / 2); 89 90 SkRect dst1, dst2, dst3, dst4; 91 dst1.set(0, 240, 65, 305); 92 dst2.set(0, 320, 65, 385); 93 dst3.set(0, 400, 65, 465); 94 dst4.set(0, 480, 65, 545); 95 96 canvas->drawImageRect(imgR, src1, dst1, usePaint ? &paint : nullptr); 97 canvas->drawImageRect(imgG, src2, dst2, usePaint ? &paint : nullptr); 98 canvas->drawImageRect(imgR, src3, dst3, usePaint ? &paint : nullptr); 99 canvas->drawImageRect(imgG, dst4, usePaint ? &paint : nullptr); 100 101 imgG->unref(); 102 imgR->unref(); 103 } 104 105 class ImageGM : public skiagm::GM { 106 void* fBuffer; 107 size_t fBufferSize; 108 SkSize fSize; 109 enum { 110 W = 64, 111 H = 64, 112 RB = W * 4 + 8, 113 }; 114 public: 115 ImageGM() { 116 fBufferSize = RB * H; 117 fBuffer = sk_malloc_throw(fBufferSize); 118 fSize.set(SkIntToScalar(W), SkIntToScalar(H)); 119 } 120 121 virtual ~ImageGM() { 122 sk_free(fBuffer); 123 } 124 125 protected: 126 SkString onShortName() override { 127 return SkString("image-surface"); 128 } 129 130 SkISize onISize() override { 131 return SkISize::Make(960, 1200); 132 } 133 134 void onDraw(SkCanvas* canvas) override { 135 drawJpeg(canvas, this->getISize()); 136 137 canvas->scale(2, 2); 138 139 static const char* kLabel1 = "Original Img"; 140 static const char* kLabel2 = "Modified Img"; 141 static const char* kLabel3 = "Cur Surface"; 142 static const char* kLabel4 = "Full Crop"; 143 static const char* kLabel5 = "Over-crop"; 144 static const char* kLabel6 = "Upper-left"; 145 static const char* kLabel7 = "No Crop"; 146 147 static const char* kLabel8 = "Pre-Alloc Img"; 148 static const char* kLabel9 = "New Alloc Img"; 149 static const char* kLabel10 = "GPU"; 150 151 SkPaint textPaint; 152 textPaint.setAntiAlias(true); 153 sk_tool_utils::set_portable_typeface(&textPaint); 154 textPaint.setTextSize(8); 155 156 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint); 157 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint); 158 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint); 159 canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint); 160 canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint); 161 canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint); 162 canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint); 163 164 canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint); 165 canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint); 166 canvas->drawText(kLabel10, strlen(kLabel10), 265, 10, textPaint); 167 168 canvas->translate(80, 20); 169 170 // since we draw into this directly, we need to start fresh 171 sk_bzero(fBuffer, fBufferSize); 172 173 SkImageInfo info = SkImageInfo::MakeN32Premul(W, H); 174 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB)); 175 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info)); 176 SkAutoTUnref<SkSurface> surf2; // gpu 177 178 #if SK_SUPPORT_GPU 179 surf2.reset(SkSurface::NewRenderTarget(canvas->getGrContext(), 180 SkBudgeted::kNo, info)); 181 #endif 182 183 test_surface(canvas, surf0, true); 184 canvas->translate(80, 0); 185 test_surface(canvas, surf1, true); 186 if (surf2) { 187 canvas->translate(80, 0); 188 test_surface(canvas, surf2, true); 189 } 190 } 191 192 private: 193 typedef skiagm::GM INHERITED; 194 }; 195 DEF_GM( return new ImageGM; ) 196 197 /////////////////////////////////////////////////////////////////////////////////////////////////// 198 199 #include "SkPictureRecorder.h" 200 201 static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) { 202 SkBitmap bitmap; 203 bitmap.installPixels(pmap); 204 canvas->drawBitmap(bitmap, 0, 0, nullptr); 205 } 206 207 static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) { 208 SkAutoCanvasRestore acr(canvas, true); 209 210 canvas->drawImage(image, 0, 0, nullptr); 211 canvas->translate(110, 10); 212 213 const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40); 214 SkAutoPixmapStorage storage; 215 storage.alloc(info); 216 217 const SkImage::CachingHint chints[] = { 218 SkImage::kAllow_CachingHint, SkImage::kDisallow_CachingHint, 219 }; 220 const SkFilterQuality qualities[] = { 221 kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality, 222 }; 223 224 for (auto ch : chints) { 225 canvas->save(); 226 for (auto q : qualities) { 227 if (image->scalePixels(storage, q, ch)) { 228 draw_pixmap(canvas, storage); 229 } 230 canvas->translate(70, 0); 231 } 232 canvas->restore(); 233 canvas->translate(0, 45); 234 } 235 } 236 237 static void draw_contents(SkCanvas* canvas) { 238 SkPaint paint; 239 paint.setStyle(SkPaint::kStroke_Style); 240 paint.setStrokeWidth(20); 241 canvas->drawCircle(50, 50, 35, paint); 242 } 243 244 static SkImage* make_raster(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) { 245 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); 246 draw(surface->getCanvas()); 247 return surface->newImageSnapshot(); 248 } 249 250 static SkImage* make_picture(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) { 251 SkPictureRecorder recorder; 252 draw(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height()))); 253 SkAutoTUnref<SkPicture> pict(recorder.endRecording()); 254 return SkImage::NewFromPicture(pict, info.dimensions(), nullptr, nullptr); 255 } 256 257 static SkImage* make_codec(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) { 258 SkAutoTUnref<SkImage> image(make_raster(info, nullptr, draw)); 259 SkAutoTUnref<SkData> data(image->encode()); 260 return SkImage::NewFromEncoded(data); 261 } 262 263 static SkImage* make_gpu(const SkImageInfo& info, GrContext* ctx, void (*draw)(SkCanvas*)) { 264 if (!ctx) { return nullptr; } 265 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkBudgeted::kNo, info)); 266 draw(surface->getCanvas()); 267 return surface->newImageSnapshot(); 268 } 269 270 typedef SkImage* (*ImageMakerProc)(const SkImageInfo&, GrContext*, void (*)(SkCanvas*)); 271 272 class ScalePixelsGM : public skiagm::GM { 273 public: 274 ScalePixelsGM() {} 275 276 protected: 277 SkString onShortName() override { 278 return SkString("scale-pixels"); 279 } 280 281 SkISize onISize() override { 282 return SkISize::Make(960, 1200); 283 } 284 285 void onDraw(SkCanvas* canvas) override { 286 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); 287 288 const ImageMakerProc procs[] = { 289 make_codec, make_raster, make_picture, make_codec, make_gpu, 290 }; 291 for (auto& proc : procs) { 292 SkAutoTUnref<SkImage> image(proc(info, canvas->getGrContext(), draw_contents)); 293 if (image) { 294 show_scaled_pixels(canvas, image); 295 } 296 canvas->translate(0, 120); 297 } 298 } 299 300 private: 301 typedef skiagm::GM INHERITED; 302 }; 303 DEF_GM( return new ScalePixelsGM; ) 304 305 /////////////////////////////////////////////////////////////////////////////////////////////////// 306 307 #include "SkImageGenerator.h" 308 309 static SkImageInfo make_info(SkImage* img) { 310 return SkImageInfo::MakeN32(img->width(), img->height(), 311 img->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType); 312 } 313 314 // Its simple, but I wonder if we should expose this formally? 315 // 316 class ImageGeneratorFromImage : public SkImageGenerator { 317 public: 318 ImageGeneratorFromImage(SkImage* img) : INHERITED(make_info(img)), fImg(SkRef(img)) {} 319 320 protected: 321 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[], 322 int* ctableCount) override { 323 return fImg->readPixels(info, pixels, rowBytes, 0, 0); 324 } 325 326 private: 327 SkAutoTUnref<SkImage> fImg; 328 329 typedef SkImageGenerator INHERITED; 330 }; 331 332 static void draw_opaque_contents(SkCanvas* canvas) { 333 canvas->drawColor(0xFFFF8844); 334 335 SkPaint paint; 336 paint.setStyle(SkPaint::kStroke_Style); 337 paint.setStrokeWidth(20); 338 canvas->drawCircle(50, 50, 35, paint); 339 } 340 341 static SkImageGenerator* gen_raster(const SkImageInfo& info) { 342 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); 343 draw_opaque_contents(surface->getCanvas()); 344 SkAutoTUnref<SkImage> img(surface->newImageSnapshot()); 345 return new ImageGeneratorFromImage(img); 346 } 347 348 static SkImageGenerator* gen_picture(const SkImageInfo& info) { 349 SkPictureRecorder recorder; 350 draw_opaque_contents(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height()))); 351 SkAutoTUnref<SkPicture> pict(recorder.endRecording()); 352 return SkImageGenerator::NewFromPicture(info.dimensions(), pict, nullptr, nullptr); 353 } 354 355 static SkImageGenerator* gen_png(const SkImageInfo& info) { 356 SkAutoTUnref<SkImage> image(make_raster(info, nullptr, draw_opaque_contents)); 357 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100)); 358 return SkImageGenerator::NewFromEncoded(data); 359 } 360 361 static SkImageGenerator* gen_jpg(const SkImageInfo& info) { 362 SkAutoTUnref<SkImage> image(make_raster(info, nullptr, draw_opaque_contents)); 363 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kJPEG_Type, 100)); 364 return SkImageGenerator::NewFromEncoded(data); 365 } 366 367 typedef SkImageGenerator* (*GeneratorMakerProc)(const SkImageInfo&); 368 369 static void show_scaled_generator(SkCanvas* canvas, SkImageGenerator* gen) { 370 const SkImageInfo genInfo = gen->getInfo(); 371 372 SkAutoCanvasRestore acr(canvas, true); 373 374 SkBitmap bm; 375 bm.allocPixels(genInfo); 376 if (gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())) { 377 canvas->drawBitmap(bm, 0, 0, nullptr); 378 } 379 canvas->translate(110, 0); 380 381 const float scales[] = { 0.75f, 0.5f, 0.25f }; 382 for (auto scale : scales) { 383 SkImageGenerator::SupportedSizes sizes; 384 if (gen->computeScaledDimensions(scale, &sizes)) { 385 const SkImageInfo info = SkImageInfo::MakeN32Premul(sizes.fSizes[0].width(), 386 sizes.fSizes[0].height()); 387 bm.allocPixels(info); 388 SkPixmap pmap; 389 bm.peekPixels(&pmap); 390 if (gen->generateScaledPixels(pmap)) { 391 canvas->drawBitmap(bm, 0, SkIntToScalar(genInfo.height() - info.height())/2); 392 } 393 } 394 canvas->translate(100, 0); 395 } 396 } 397 398 class ScaleGeneratorGM : public skiagm::GM { 399 public: 400 ScaleGeneratorGM() {} 401 402 protected: 403 SkString onShortName() override { 404 return SkString("scale-generator"); 405 } 406 407 SkISize onISize() override { 408 return SkISize::Make(500, 500); 409 } 410 411 void onDraw(SkCanvas* canvas) override { 412 canvas->translate(10, 10); 413 414 // explicitly make it opaque, so we can test JPEG (which is only ever opaque) 415 const SkImageInfo info = SkImageInfo::MakeN32(100, 100, kOpaque_SkAlphaType); 416 417 const GeneratorMakerProc procs[] = { 418 gen_raster, gen_picture, gen_png, gen_jpg, 419 }; 420 for (auto& proc : procs) { 421 SkAutoTDelete<SkImageGenerator> gen(proc(info)); 422 if (gen) { 423 show_scaled_generator(canvas, gen); 424 } 425 canvas->translate(0, 120); 426 } 427 } 428 429 private: 430 typedef skiagm::GM INHERITED; 431 }; 432 DEF_GM( return new ScaleGeneratorGM; ) 433 434 #if SK_SUPPORT_GPU 435 #include "GrContextFactory.h" 436 #endif 437 438 DEF_SIMPLE_GM(new_texture_image, canvas, 225, 60) { 439 GrContext* context = nullptr; 440 #if SK_SUPPORT_GPU 441 context = canvas->getGrContext(); 442 GrContextFactory factory; 443 #endif 444 if (!context) { 445 skiagm::GM::DrawGpuOnlyMessage(canvas); 446 return; 447 } 448 449 auto render_image = [](SkCanvas* canvas) { 450 canvas->clear(SK_ColorBLUE); 451 SkPaint paint; 452 paint.setColor(SK_ColorRED); 453 canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint); 454 paint.setColor(SK_ColorGREEN); 455 canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint); 456 paint.setColor(SK_ColorYELLOW); 457 canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint); 458 paint.setColor(SK_ColorCYAN); 459 canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint); 460 }; 461 462 static const int kSize = 50; 463 SkBitmap bmp; 464 bmp.allocN32Pixels(kSize, kSize); 465 SkCanvas bmpCanvas(bmp); 466 render_image(&bmpCanvas); 467 468 std::function<SkImage*()> imageFactories[] = { 469 // Create sw raster image. 470 [bmp] { 471 return SkImage::NewFromBitmap(bmp); 472 }, 473 // Create encoded image. 474 [bmp] { 475 SkAutoTUnref<SkData> src( 476 SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100)); 477 return SkImage::NewFromEncoded(src); 478 }, 479 // Create a picture image. 480 [render_image] { 481 SkPictureRecorder recorder; 482 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize)); 483 render_image(canvas); 484 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 485 return SkImage::NewFromPicture(picture, SkISize::Make(kSize, kSize), nullptr, nullptr); 486 }, 487 // Create a texture image 488 [context, render_image]() -> SkImage* { 489 SkAutoTUnref<SkSurface> surface( 490 SkSurface::NewRenderTarget(context, SkBudgeted::kYes, 491 SkImageInfo::MakeN32Premul(kSize, kSize))); 492 if (!surface) { 493 return nullptr; 494 } 495 render_image(surface->getCanvas()); 496 return surface->newImageSnapshot(); 497 } 498 }; 499 500 static const SkScalar kPad = 5.f; 501 canvas->translate(kPad, kPad); 502 for (auto factory : imageFactories) { 503 SkAutoTUnref<SkImage> image(factory()); 504 if (!image) { 505 continue; 506 } 507 if (context) { 508 SkAutoTUnref<SkImage> texImage(image->newTextureImage(context)); 509 if (texImage) { 510 canvas->drawImage(texImage, 0, 0); 511 } 512 } 513 canvas->translate(image->width() + kPad, 0); 514 } 515 } 516