PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(sk_sp<SkImage> image) { if (!image) return nullptr; if (image->isTextureBacked()) return AcceleratedStaticBitmapImage::createFromSharedContextImage( std::move(image)); return adoptRef(new StaticBitmapImage(std::move(image))); }
static void test_texture_backed(skiatest::Reporter* reporter, const sk_sp<SkSpecialImage>& orig, const sk_sp<SkSpecialImage>& gpuBacked) { REPORTER_ASSERT(reporter, gpuBacked); REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked()); REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID()); REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() && gpuBacked->subset().height() == orig->subset().height()); }
// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw) static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter, GrContext* context, bool peekTextureSucceeds, int offset, int size) { const SkIRect subset = img->subset(); REPORTER_ASSERT(reporter, offset == subset.left()); REPORTER_ASSERT(reporter, offset == subset.top()); REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); //-------------- // Test that peekTexture reports the correct backing type REPORTER_ASSERT(reporter, peekTextureSucceeds == img->isTextureBacked()); #if SK_SUPPORT_GPU //-------------- // Test getTextureAsRef - as long as there is a context this should succeed if (context) { sk_sp<GrTexture> texture(img->asTextureRef(context)); REPORTER_ASSERT(reporter, texture); } #endif //-------------- // Test getROPixels - this should always succeed regardless of backing store SkBitmap bitmap; REPORTER_ASSERT(reporter, img->getROPixels(&bitmap)); if (context) { REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width()); REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height()); } else { REPORTER_ASSERT(reporter, size == bitmap.width()); REPORTER_ASSERT(reporter, size == bitmap.height()); } //-------------- // Test that draw restricts itself to the subset SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType); sk_sp<SkSpecialSurface> surf(img->makeSurface(info)); SkCanvas* canvas = surf->getCanvas(); canvas->clear(SK_ColorBLUE); img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); SkBitmap bm; bm.allocN32Pixels(kFullSize, kFullSize, true); bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); SkASSERT_RELEASE(result); // Only the center (red) portion should've been drawn into the canvas REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, kSmallerSize+kPad-1)); REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, kSmallerSize+kPad)); //-------------- // Test that makeTightSubset & makeTightSurface return appropriately sized objects // of the correct backing type SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height()); { sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset)); REPORTER_ASSERT(reporter, tightImg->width() == subset.width()); REPORTER_ASSERT(reporter, tightImg->height() == subset.height()); REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture()); SkPixmap tmpPixmap; REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightImg->peekPixels(&tmpPixmap)); } { SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), kPremul_SkAlphaType); sk_sp<SkSurface> tightSurf(img->makeTightSurface(info)); REPORTER_ASSERT(reporter, tightSurf->width() == subset.width()); REPORTER_ASSERT(reporter, tightSurf->height() == subset.height()); REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess)); SkPixmap tmpPixmap; REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels(&tmpPixmap)); } }