Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tests/ImageTest.cpp

Issue 1452123002: Move SkImage tests from SurfaceTest to ImageTest (Closed) Base URL: https://skia.googlesource.com/skia.git@commandbuffer-as-api-00-skia_gpu=0-build
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/SurfaceTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkData.h" 10 #include "SkData.h"
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 SkAutoTUnref<SkColorTable> ctable( 354 SkAutoTUnref<SkColorTable> ctable(
355 new SkColorTable(pmColors, SK_ARRAY_COUNT(pmColors))); 355 new SkColorTable(pmColors, SK_ARRAY_COUNT(pmColors)));
356 SkImageInfo info = 356 SkImageInfo info =
357 SkImageInfo::Make(1, 1, kIndex_8_SkColorType, kPremul_SkAlphaType); 357 SkImageInfo::Make(1, 1, kIndex_8_SkColorType, kPremul_SkAlphaType);
358 bm.allocPixels(info, nullptr, ctable); 358 bm.allocPixels(info, nullptr, ctable);
359 SkAutoLockPixels autoLockPixels(bm); 359 SkAutoLockPixels autoLockPixels(bm);
360 *bm.getAddr8(0, 0) = 0; 360 *bm.getAddr8(0, 0) = 0;
361 SkAutoTUnref<SkImage> img(SkImage::NewFromBitmap(bm)); 361 SkAutoTUnref<SkImage> img(SkImage::NewFromBitmap(bm));
362 REPORTER_ASSERT(r, img.get() != nullptr); 362 REPORTER_ASSERT(r, img.get() != nullptr);
363 } 363 }
364
365 // TODO: The tests below were moved from SurfaceTests and should be reformatted.
366
367 enum ImageType {
368 kRasterCopy_ImageType,
369 kRasterData_ImageType,
370 kRasterProc_ImageType,
371 kGpu_ImageType,
372 kCodec_ImageType,
373 };
374
375 #include "SkImageGenerator.h"
376
377 class EmptyGenerator : public SkImageGenerator {
378 public:
379 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
380 };
381
382 static void test_empty_image(skiatest::Reporter* reporter) {
383 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_S kAlphaType);
384
385 REPORTER_ASSERT(reporter, nullptr == SkImage::NewRasterCopy(info, nullptr, 0 ));
386 REPORTER_ASSERT(reporter, nullptr == SkImage::NewRasterData(info, nullptr, 0 ));
387 REPORTER_ASSERT(reporter, nullptr == SkImage::NewFromRaster(info, nullptr, 0 , nullptr, nullptr));
388 REPORTER_ASSERT(reporter, nullptr == SkImage::NewFromGenerator(new EmptyGene rator));
389 }
390
391 static void test_image(skiatest::Reporter* reporter) {
392 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
393 size_t rowBytes = info.minRowBytes();
394 size_t size = info.getSafeSize(rowBytes);
395 SkData* data = SkData::NewUninitialized(size);
396
397 REPORTER_ASSERT(reporter, data->unique());
398 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
399 REPORTER_ASSERT(reporter, !data->unique());
400 image->unref();
401 REPORTER_ASSERT(reporter, data->unique());
402 data->unref();
403 }
404
405 // Want to ensure that our Release is called when the owning image is destroyed
406 struct ReleaseDataContext {
407 skiatest::Reporter* fReporter;
408 SkData* fData;
409
410 static void Release(const void* pixels, void* context) {
411 ReleaseDataContext* state = (ReleaseDataContext*)context;
412 REPORTER_ASSERT(state->fReporter, state->fData);
413 state->fData->unref();
414 state->fData = nullptr;
415 }
416 };
417
418 // May we (soon) eliminate the need to keep testing this, by hiding the bloody d evice!
419 #include "SkDevice.h"
420 static uint32_t get_legacy_gen_id(SkSurface* surf) {
421 SkBaseDevice* device = surf->getCanvas()->getDevice_just_for_deprecated_comp atibility_testing();
422 return device->accessBitmap(false).getGenerationID();
423 }
424
425 /*
426 * Test legacy behavor of bumping the surface's device's bitmap's genID when we access its
427 * texture handle for writing.
428 *
429 * Note: this needs to be tested separately from checking newImageSnapshot, as calling that
430 * can also incidentally bump the genID (when a new backing surface is created) .
431 */
432 template <class F>
433 static void test_texture_handle_genID(skiatest::Reporter* reporter, SkSurface* s urf, F f) {
434 const uint32_t gen0 = get_legacy_gen_id(surf);
435 f(surf, SkSurface::kFlushRead_BackendHandleAccess);
436 const uint32_t gen1 = get_legacy_gen_id(surf);
437 REPORTER_ASSERT(reporter, gen0 == gen1);
438
439 f(surf, SkSurface::kFlushWrite_BackendHandleAccess);
440 const uint32_t gen2 = get_legacy_gen_id(surf);
441 REPORTER_ASSERT(reporter, gen0 != gen2);
442
443 f(surf, SkSurface::kDiscardWrite_BackendHandleAccess);
444 const uint32_t gen3 = get_legacy_gen_id(surf);
445 REPORTER_ASSERT(reporter, gen0 != gen3);
446 REPORTER_ASSERT(reporter, gen2 != gen3);
447 }
448
449 template <class F>
450 static void test_backend_handle(skiatest::Reporter* reporter, SkSurface* surf, F f) {
451 SkAutoTUnref<SkImage> image0(surf->newImageSnapshot());
452 GrBackendObject obj = f(surf, SkSurface::kFlushRead_BackendHandleAccess);
453 REPORTER_ASSERT(reporter, obj != 0);
454 SkAutoTUnref<SkImage> image1(surf->newImageSnapshot());
455 // just read access should not affect the snapshot
456 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
457
458 obj = f(surf, SkSurface::kFlushWrite_BackendHandleAccess);
459 REPORTER_ASSERT(reporter, obj != 0);
460 SkAutoTUnref<SkImage> image2(surf->newImageSnapshot());
461 // expect a new image, since we claimed we would write
462 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
463
464 obj = f(surf, SkSurface::kDiscardWrite_BackendHandleAccess);
465 REPORTER_ASSERT(reporter, obj != 0);
466 SkAutoTUnref<SkImage> image3(surf->newImageSnapshot());
467 // expect a new(er) image, since we claimed we would write
468 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
469 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
470 }
471
472 static SkImage* create_image(skiatest::Reporter* reporter,
473 ImageType imageType, GrContext* context, SkColor co lor,
474 ReleaseDataContext* releaseContext) {
475 const SkPMColor pmcolor = SkPreMultiplyColor(color);
476 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
477 const size_t rowBytes = info.minRowBytes();
478 const size_t size = rowBytes * info.height();
479
480 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
481 void* addr = data->writable_data();
482 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
483
484 switch (imageType) {
485 case kRasterCopy_ImageType:
486 return SkImage::NewRasterCopy(info, addr, rowBytes);
487 case kRasterData_ImageType:
488 return SkImage::NewRasterData(info, data, rowBytes);
489 case kRasterProc_ImageType:
490 SkASSERT(releaseContext);
491 releaseContext->fData = SkRef(data.get());
492 return SkImage::NewFromRaster(info, addr, rowBytes,
493 ReleaseDataContext::Release, releaseCo ntext);
494 case kGpu_ImageType: {
495 SkAutoTUnref<SkSurface> surf(
496 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, inf o, 0));
497 surf->getCanvas()->clear(color);
498 // test our backing texture / rendertarget while were here...
499 auto textureAccessorFunc =
500 [](SkSurface* surf, SkSurface::BackendHandleAccess access) - > GrBackendObject {
501 return surf->getTextureHandle(access); };
502 auto renderTargetAccessorFunc =
503 [](SkSurface* surf, SkSurface::BackendHandleAccess access) - > GrBackendObject {
504 GrBackendObject obj;
505 SkAssertResult(surf->getRenderTargetHandle(&obj, access) );
506 return obj; };
507 test_backend_handle(reporter, surf, textureAccessorFunc);
508 test_backend_handle(reporter, surf, renderTargetAccessorFunc);
509 test_texture_handle_genID(reporter, surf, textureAccessorFunc);
510 test_texture_handle_genID(reporter, surf, renderTargetAccessorFunc);
511
512 // redraw so our returned image looks as expected.
513 surf->getCanvas()->clear(color);
514 return surf->newImageSnapshot();
515 }
516 case kCodec_ImageType: {
517 SkBitmap bitmap;
518 bitmap.installPixels(info, addr, rowBytes);
519 SkAutoTUnref<SkData> src(
520 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 1 00));
521 return SkImage::NewFromEncoded(src);
522 }
523 }
524 SkASSERT(false);
525 return nullptr;
526 }
527
528 static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) {
529 sk_memset32(pixels, color, count);
530 }
531 static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
532 for (int i = 0; i < count; ++i) {
533 if (pixels[i] != expected) {
534 return false;
535 }
536 }
537 return true;
538 }
539
540 static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image,
541 SkPMColor expected) {
542 const SkPMColor notExpected = ~expected;
543
544 const int w = 2, h = 2;
545 const size_t rowBytes = w * sizeof(SkPMColor);
546 SkPMColor pixels[w*h];
547
548 SkImageInfo info;
549
550 info = SkImageInfo::MakeUnknown(w, h);
551 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
552
553 // out-of-bounds should fail
554 info = SkImageInfo::MakeN32Premul(w, h);
555 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0)) ;
556 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h)) ;
557 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image-> width(), 0));
558 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, imag e->height()));
559
560 // top-left should succeed
561 set_pixels(pixels, w*h, notExpected);
562 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
563 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
564
565 // bottom-right should succeed
566 set_pixels(pixels, w*h, notExpected);
567 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
568 image->width() - w, image->heigh t() - h));
569 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
570
571 // partial top-left should succeed
572 set_pixels(pixels, w*h, notExpected);
573 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1)) ;
574 REPORTER_ASSERT(reporter, pixels[3] == expected);
575 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
576
577 // partial bottom-right should succeed
578 set_pixels(pixels, w*h, notExpected);
579 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
580 image->width() - 1, image->heigh t() - 1));
581 REPORTER_ASSERT(reporter, pixels[0] == expected);
582 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
583 }
584
585 static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* ima ge,
586 const SkBitmap& bitmap, SkImage::LegacyBitmapMod e mode) {
587 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
588 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
589 REPORTER_ASSERT(reporter, image->isOpaque() == bitmap.isOpaque());
590
591 if (SkImage::kRO_LegacyBitmapMode == mode) {
592 REPORTER_ASSERT(reporter, bitmap.isImmutable());
593 }
594
595 SkAutoLockPixels alp(bitmap);
596 REPORTER_ASSERT(reporter, bitmap.getPixels());
597
598 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
599 SkPMColor imageColor;
600 REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMCo lor), 0, 0));
601 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
602 }
603
604 static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* imag e) {
605 const SkImage::LegacyBitmapMode modes[] = {
606 SkImage::kRO_LegacyBitmapMode,
607 SkImage::kRW_LegacyBitmapMode,
608 };
609 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
610 SkBitmap bitmap;
611 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap, modes[i]));
612 check_legacy_bitmap(reporter, image, bitmap, modes[i]);
613
614 // Test subsetting to exercise the rowBytes logic.
615 SkBitmap tmp;
616 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(ima ge->width() / 2,
617 ima ge->height() / 2)));
618 SkAutoTUnref<SkImage> subsetImage(SkImage::NewFromBitmap(tmp));
619 REPORTER_ASSERT(reporter, subsetImage);
620
621 SkBitmap subsetBitmap;
622 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap, mod es[i]));
623 check_legacy_bitmap(reporter, subsetImage, subsetBitmap, modes[i]);
624 }
625 }
626
627 static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* facto ry) {
628 static const struct {
629 ImageType fType;
630 bool fPeekShouldSucceed;
631 const char* fName;
632 } gRec[] = {
633 { kRasterCopy_ImageType, true, "RasterCopy" },
634 { kRasterData_ImageType, true, "RasterData" },
635 { kRasterProc_ImageType, true, "RasterProc" },
636 { kGpu_ImageType, false, "Gpu" },
637 { kCodec_ImageType, false, "Codec" },
638 };
639
640 const SkColor color = SK_ColorRED;
641 const SkPMColor pmcolor = SkPreMultiplyColor(color);
642
643 GrContext* ctx = nullptr;
644 #if SK_SUPPORT_GPU
645 ctx = factory->get(GrContextFactory::kNative_GLContextType);
646 if (nullptr == ctx) {
647 return;
648 }
649 #endif
650
651 ReleaseDataContext releaseCtx;
652 releaseCtx.fReporter = reporter;
653
654 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
655 SkImageInfo info;
656 size_t rowBytes;
657
658 releaseCtx.fData = nullptr;
659 SkAutoTUnref<SkImage> image(create_image(reporter, gRec[i].fType, ctx, c olor, &releaseCtx));
660 if (!image.get()) {
661 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName);
662 continue; // gpu may not be enabled
663 }
664 if (kRasterProc_ImageType == gRec[i].fType) {
665 REPORTER_ASSERT(reporter, nullptr != releaseCtx.fData); // we are t racking the data
666 } else {
667 REPORTER_ASSERT(reporter, nullptr == releaseCtx.fData); // we ignor ed the context
668 }
669
670 test_legacy_bitmap(reporter, image);
671
672 const void* addr = image->peekPixels(&info, &rowBytes);
673 bool success = SkToBool(addr);
674 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
675 if (success) {
676 REPORTER_ASSERT(reporter, 10 == info.width());
677 REPORTER_ASSERT(reporter, 10 == info.height());
678 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
679 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
680 kOpaque_SkAlphaType == info.alphaType());
681 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
682 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
683 }
684
685 test_image_readpixels(reporter, image, pmcolor);
686 }
687 REPORTER_ASSERT(reporter, nullptr == releaseCtx.fData); // we released the data
688 }
689 #if SK_SUPPORT_GPU
690
691 struct ReleaseTextureContext {
692 ReleaseTextureContext(skiatest::Reporter* reporter) {
693 fReporter = reporter;
694 fIsReleased = false;
695 }
696
697 skiatest::Reporter* fReporter;
698 bool fIsReleased;
699
700 void doRelease() {
701 REPORTER_ASSERT(fReporter, false == fIsReleased);
702 fIsReleased = true;
703 }
704
705 static void ReleaseProc(void* context) {
706 ((ReleaseTextureContext*)context)->doRelease();
707 }
708 };
709
710 static SkImage* make_desc_image(GrContext* ctx, int w, int h, GrBackendObject te xID,
711 ReleaseTextureContext* releaseContext) {
712 GrBackendTextureDesc desc;
713 desc.fConfig = kSkia8888_GrPixelConfig;
714 // need to be a rendertarget for now...
715 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
716 desc.fWidth = w;
717 desc.fHeight = h;
718 desc.fSampleCnt = 0;
719 desc.fTextureHandle = texID;
720 return releaseContext
721 ? SkImage::NewFromTexture(ctx, desc, kPremul_SkAlphaType,
722 ReleaseTextureContext::ReleaseProc, re leaseContext)
723 : SkImage::NewFromTextureCopy(ctx, desc, kPremul_SkAlphaType);
724 }
725
726 static void test_image_color(skiatest::Reporter* reporter, SkImage* image, SkPMC olor expected) {
727 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
728 SkPMColor pixel;
729 REPORTER_ASSERT(reporter, image->readPixels(info, &pixel, sizeof(pixel), 0, 0));
730 REPORTER_ASSERT(reporter, pixel == expected);
731 }
732
733 DEF_GPUTEST(SkImage_NewFromTexture, reporter, factory) {
734 GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
735 if (!ctx) {
736 REPORTER_ASSERT(reporter, false);
737 return;
738 }
739 GrTextureProvider* provider = ctx->textureProvider();
740
741 const int w = 10;
742 const int h = 10;
743 SkPMColor storage[w * h];
744 const SkPMColor expected0 = SkPreMultiplyColor(SK_ColorRED);
745 sk_memset32(storage, expected0, w * h);
746
747 GrSurfaceDesc desc;
748 desc.fFlags = kRenderTarget_GrSurfaceFlag; // needs to be a rendertarget fo r readpixels();
749 desc.fOrigin = kDefault_GrSurfaceOrigin;
750 desc.fWidth = w;
751 desc.fHeight = h;
752 desc.fConfig = kSkia8888_GrPixelConfig;
753 desc.fSampleCnt = 0;
754
755 SkAutoTUnref<GrTexture> tex(provider->createTexture(desc, false, storage, w * 4));
756 if (!tex) {
757 REPORTER_ASSERT(reporter, false);
758 return;
759 }
760
761 GrBackendObject srcTex = tex->getTextureHandle();
762 ReleaseTextureContext releaseCtx(reporter);
763
764 SkAutoTUnref<SkImage> refImg(make_desc_image(ctx, w, h, srcTex, &releaseCtx) );
765 SkAutoTUnref<SkImage> cpyImg(make_desc_image(ctx, w, h, srcTex, nullptr));
766
767 test_image_color(reporter, refImg, expected0);
768 test_image_color(reporter, cpyImg, expected0);
769
770 // Now lets jam new colors into our "external" texture, and see if the image s notice
771 const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE);
772 sk_memset32(storage, expected1, w * h);
773 tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kF lushWrites_PixelOp);
774
775 // The cpy'd one should still see the old color
776 #if 0
777 // There is no guarantee that refImg sees the new color. We are free to have made a copy. Our
778 // write pixels call violated the contract with refImg and refImg is now und efined.
779 test_image_color(reporter, refImg, expected1);
780 #endif
781 test_image_color(reporter, cpyImg, expected0);
782
783 // Now exercise the release proc
784 REPORTER_ASSERT(reporter, !releaseCtx.fIsReleased);
785 refImg.reset(nullptr); // force a release of the image
786 REPORTER_ASSERT(reporter, releaseCtx.fIsReleased);
787 }
788 #endif
789 DEF_GPUTEST(ImageTestsFromSurfaceTestsTODO, reporter, factory) {
790 test_image(reporter);
791 test_empty_image(reporter);
792 test_imagepeek(reporter, factory);
793 }
OLDNEW
« no previous file with comments | « no previous file | tests/SurfaceTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698