SpecialImageTest.cpp revision 7992da32f02f90e0ac9ab6914eb31676b502eb71
1b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips/* 2b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * Copyright 2016 Google Inc. 3b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * 4b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * Use of this source code is governed by a BSD-style license that can be 5b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * found in the LICENSE file 6b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips */ 7b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 8c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips#include "SkAutoPixmapStorage.h" 9b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#include "SkBitmap.h" 10b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#include "SkCanvas.h" 11b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#include "SkImage.h" 12c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips#include "SkPixmap.h" 13b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#include "SkSpecialImage.h" 14b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#include "SkSpecialSurface.h" 15b4bd11e66519f545282f914c67b54bf17cecab84robertphillips#include "SkSurface.h" 16b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#include "Test.h" 17b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 18b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#if SK_SUPPORT_GPU 19b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#include "GrContext.h" 203743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips#include "GrSurfaceProxy.h" 21b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#endif 22b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 23b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 24b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// This test creates backing resources exactly sized to [kFullSize x kFullSize]. 25b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// It then wraps them in an SkSpecialImage with only the center (red) region being active. 26b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none 27b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// of the inactive (green) region leaked out. 28b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 29b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstatic const int kSmallerSize = 10; 30b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstatic const int kPad = 3; 31b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstatic const int kFullSize = kSmallerSize + 2 * kPad; 32b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 33b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// Create a bitmap with red in the center and green around it 34b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstatic SkBitmap create_bm() { 35b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkBitmap bm; 36b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips bm.allocN32Pixels(kFullSize, kFullSize, true); 37b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 38b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkCanvas temp(bm); 39b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 40b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips temp.clear(SK_ColorGREEN); 41b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkPaint p; 42b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips p.setColor(SK_ColorRED); 43b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips p.setAntiAlias(false); 44b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 45b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), 469d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)), 47b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips p); 48b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 49b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips return bm; 50b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips} 51b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 52b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw) 5337bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillipsstatic void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter, 54646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips GrContext* context, bool peekTextureSucceeds, 55c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips int offset, int size) { 563e302275b324172c845627cbd00cee8a06571bafrobertphillips const SkIRect subset = img->subset(); 57c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips REPORTER_ASSERT(reporter, offset == subset.left()); 58c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips REPORTER_ASSERT(reporter, offset == subset.top()); 59b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); 60b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); 61b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 62b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips //-------------- 63b4bd11e66519f545282f914c67b54bf17cecab84robertphillips // Test that peekTexture reports the correct backing type 64646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips REPORTER_ASSERT(reporter, peekTextureSucceeds == img->isTextureBacked()); 65646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips 66646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips#if SK_SUPPORT_GPU 67646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips //-------------- 68646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips // Test getTextureAsRef - as long as there is a context this should succeed 69646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips if (context) { 70646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips sk_sp<GrTexture> texture(img->asTextureRef(context)); 71646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips REPORTER_ASSERT(reporter, texture); 72646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips } 73646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips#endif 74b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 75b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips //-------------- 76646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips // Test getROPixels - this should always succeed regardless of backing store 77646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips SkBitmap bitmap; 78646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips REPORTER_ASSERT(reporter, img->getROPixels(&bitmap)); 79ed086cae885050188cbb8761c0b1382bbadf088brobertphillips if (context) { 80ed086cae885050188cbb8761c0b1382bbadf088brobertphillips REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width()); 81ed086cae885050188cbb8761c0b1382bbadf088brobertphillips REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height()); 82ed086cae885050188cbb8761c0b1382bbadf088brobertphillips } else { 83ed086cae885050188cbb8761c0b1382bbadf088brobertphillips REPORTER_ASSERT(reporter, size == bitmap.width()); 84ed086cae885050188cbb8761c0b1382bbadf088brobertphillips REPORTER_ASSERT(reporter, size == bitmap.height()); 85ed086cae885050188cbb8761c0b1382bbadf088brobertphillips } 86b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 87b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips //-------------- 88b4bd11e66519f545282f914c67b54bf17cecab84robertphillips // Test that draw restricts itself to the subset 89eed6b0e1d865a1f93143c09961debba0aca592cabrianosman SkImageFilter::OutputProperties outProps(img->getColorSpace()); 90eed6b0e1d865a1f93143c09961debba0aca592cabrianosman sk_sp<SkSpecialSurface> surf(img->makeSurface(outProps, SkISize::Make(kFullSize, kFullSize), 91eed6b0e1d865a1f93143c09961debba0aca592cabrianosman kOpaque_SkAlphaType)); 92b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 93b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkCanvas* canvas = surf->getCanvas(); 94b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 95b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips canvas->clear(SK_ColorBLUE); 96e8c34974c42b302d78c5cc8c7dcfb36956256baarobertphillips img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); 97b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 98b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkBitmap bm; 99b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips bm.allocN32Pixels(kFullSize, kFullSize, true); 100b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 101b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); 102b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkASSERT_RELEASE(result); 103b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 104b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips // Only the center (red) portion should've been drawn into the canvas 105b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); 106b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); 107b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, 108b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips kSmallerSize+kPad-1)); 109b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, 110b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips kSmallerSize+kPad)); 111b4bd11e66519f545282f914c67b54bf17cecab84robertphillips 112b4bd11e66519f545282f914c67b54bf17cecab84robertphillips //-------------- 113b4bd11e66519f545282f914c67b54bf17cecab84robertphillips // Test that makeTightSubset & makeTightSurface return appropriately sized objects 114b4bd11e66519f545282f914c67b54bf17cecab84robertphillips // of the correct backing type 115b4bd11e66519f545282f914c67b54bf17cecab84robertphillips SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height()); 116b4bd11e66519f545282f914c67b54bf17cecab84robertphillips { 117b4bd11e66519f545282f914c67b54bf17cecab84robertphillips sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset)); 118b4bd11e66519f545282f914c67b54bf17cecab84robertphillips 119b4bd11e66519f545282f914c67b54bf17cecab84robertphillips REPORTER_ASSERT(reporter, tightImg->width() == subset.width()); 120b4bd11e66519f545282f914c67b54bf17cecab84robertphillips REPORTER_ASSERT(reporter, tightImg->height() == subset.height()); 121b4bd11e66519f545282f914c67b54bf17cecab84robertphillips REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture()); 122b4bd11e66519f545282f914c67b54bf17cecab84robertphillips SkPixmap tmpPixmap; 123646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightImg->peekPixels(&tmpPixmap)); 124b4bd11e66519f545282f914c67b54bf17cecab84robertphillips } 125b4bd11e66519f545282f914c67b54bf17cecab84robertphillips { 126eed6b0e1d865a1f93143c09961debba0aca592cabrianosman SkImageFilter::OutputProperties outProps(img->getColorSpace()); 127eed6b0e1d865a1f93143c09961debba0aca592cabrianosman sk_sp<SkSurface> tightSurf(img->makeTightSurface(outProps, subset.size())); 128b4bd11e66519f545282f914c67b54bf17cecab84robertphillips 129b4bd11e66519f545282f914c67b54bf17cecab84robertphillips REPORTER_ASSERT(reporter, tightSurf->width() == subset.width()); 130b4bd11e66519f545282f914c67b54bf17cecab84robertphillips REPORTER_ASSERT(reporter, tightSurf->height() == subset.height()); 131b4bd11e66519f545282f914c67b54bf17cecab84robertphillips REPORTER_ASSERT(reporter, peekTextureSucceeds == 132b4bd11e66519f545282f914c67b54bf17cecab84robertphillips !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess)); 133b4bd11e66519f545282f914c67b54bf17cecab84robertphillips SkPixmap tmpPixmap; 134646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels(&tmpPixmap)); 135b4bd11e66519f545282f914c67b54bf17cecab84robertphillips } 136b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips} 137b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 138b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsDEF_TEST(SpecialImage_Raster, reporter) { 139b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkBitmap bm = create_bm(); 140b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 14137bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( 142c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips SkIRect::MakeWH(kFullSize, kFullSize), 143c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips bm)); 144c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips 145b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 146b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 147c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips { 1483e302275b324172c845627cbd00cee8a06571bafrobertphillips sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm)); 149646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); 150c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips } 151c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips 152c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips { 15337bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); 154646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); 155c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips } 156b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips} 157b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 1587992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osmanstatic void test_specialimage_image(skiatest::Reporter* reporter, 1597992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman SkDestinationSurfaceColorMode colorMode) { 160b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkBitmap bm = create_bm(); 161b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 1629ce9d6772df650ceb0511f275e1a83dffa78ff72reed sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); 163b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 16437bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( 165c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips SkIRect::MakeWH(kFullSize, kFullSize), 1667992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman fullImage, colorMode)); 167c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips 168b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 169b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 170c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips { 1717992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage, colorMode)); 172ed086cae885050188cbb8761c0b1382bbadf088brobertphillips test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); 173c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips } 174c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips 175c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips { 17637bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); 177ed086cae885050188cbb8761c0b1382bbadf088brobertphillips test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); 178c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips } 179b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips} 180b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 1817992da32f02f90e0ac9ab6914eb31676b502eb71Brian OsmanDEF_TEST(SpecialImage_Image_Legacy, reporter) { 1827992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman test_specialimage_image(reporter, SkDestinationSurfaceColorMode::kLegacy); 1837992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman} 1847992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman 1857992da32f02f90e0ac9ab6914eb31676b502eb71Brian OsmanDEF_TEST(SpecialImage_Image_ColorSpaceAware, reporter) { 1867992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman test_specialimage_image(reporter, SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware); 1877992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman} 1887992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman 189b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#if SK_SUPPORT_GPU 19083c17fa56b23159166394cb3feb431ffafbbab48robertphillips 19183c17fa56b23159166394cb3feb431ffafbbab48robertphillipsstatic void test_texture_backed(skiatest::Reporter* reporter, 19283c17fa56b23159166394cb3feb431ffafbbab48robertphillips const sk_sp<SkSpecialImage>& orig, 19383c17fa56b23159166394cb3feb431ffafbbab48robertphillips const sk_sp<SkSpecialImage>& gpuBacked) { 19483c17fa56b23159166394cb3feb431ffafbbab48robertphillips REPORTER_ASSERT(reporter, gpuBacked); 195646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked()); 19683c17fa56b23159166394cb3feb431ffafbbab48robertphillips REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID()); 19783c17fa56b23159166394cb3feb431ffafbbab48robertphillips REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() && 19883c17fa56b23159166394cb3feb431ffafbbab48robertphillips gpuBacked->subset().height() == orig->subset().height()); 199afbf71dd924c7bb46ccdac49e7408b4b088563ffbrianosman REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace()); 20083c17fa56b23159166394cb3feb431ffafbbab48robertphillips} 20183c17fa56b23159166394cb3feb431ffafbbab48robertphillips 20283c17fa56b23159166394cb3feb431ffafbbab48robertphillips// Test out the SkSpecialImage::makeTextureImage entry point 20368d9134bec16e91c4a6cde071bcaa579bc0801a7bsalomonDEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) { 2048b7451aaf6b1c71e9d343a4df107893db277b6aabsalomon GrContext* context = ctxInfo.grContext(); 20583c17fa56b23159166394cb3feb431ffafbbab48robertphillips SkBitmap bm = create_bm(); 20683c17fa56b23159166394cb3feb431ffafbbab48robertphillips 20783c17fa56b23159166394cb3feb431ffafbbab48robertphillips const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 20883c17fa56b23159166394cb3feb431ffafbbab48robertphillips 20983c17fa56b23159166394cb3feb431ffafbbab48robertphillips { 21083c17fa56b23159166394cb3feb431ffafbbab48robertphillips // raster 21183c17fa56b23159166394cb3feb431ffafbbab48robertphillips sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster( 21283c17fa56b23159166394cb3feb431ffafbbab48robertphillips SkIRect::MakeWH(kFullSize, 21383c17fa56b23159166394cb3feb431ffafbbab48robertphillips kFullSize), 21483c17fa56b23159166394cb3feb431ffafbbab48robertphillips bm)); 21583c17fa56b23159166394cb3feb431ffafbbab48robertphillips 21683c17fa56b23159166394cb3feb431ffafbbab48robertphillips { 2173e302275b324172c845627cbd00cee8a06571bafrobertphillips sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context)); 21883c17fa56b23159166394cb3feb431ffafbbab48robertphillips test_texture_backed(reporter, rasterImage, fromRaster); 21983c17fa56b23159166394cb3feb431ffafbbab48robertphillips } 22083c17fa56b23159166394cb3feb431ffafbbab48robertphillips 22183c17fa56b23159166394cb3feb431ffafbbab48robertphillips { 22283c17fa56b23159166394cb3feb431ffafbbab48robertphillips sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset)); 22383c17fa56b23159166394cb3feb431ffafbbab48robertphillips 2243e302275b324172c845627cbd00cee8a06571bafrobertphillips sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context)); 22583c17fa56b23159166394cb3feb431ffafbbab48robertphillips test_texture_backed(reporter, subRasterImage, fromSubRaster); 22683c17fa56b23159166394cb3feb431ffafbbab48robertphillips } 22783c17fa56b23159166394cb3feb431ffafbbab48robertphillips } 22883c17fa56b23159166394cb3feb431ffafbbab48robertphillips 22983c17fa56b23159166394cb3feb431ffafbbab48robertphillips { 23083c17fa56b23159166394cb3feb431ffafbbab48robertphillips // gpu 23183c17fa56b23159166394cb3feb431ffafbbab48robertphillips GrSurfaceDesc desc; 23283c17fa56b23159166394cb3feb431ffafbbab48robertphillips desc.fConfig = kSkia8888_GrPixelConfig; 23383c17fa56b23159166394cb3feb431ffafbbab48robertphillips desc.fFlags = kNone_GrSurfaceFlags; 23483c17fa56b23159166394cb3feb431ffafbbab48robertphillips desc.fWidth = kFullSize; 23583c17fa56b23159166394cb3feb431ffafbbab48robertphillips desc.fHeight = kFullSize; 23683c17fa56b23159166394cb3feb431ffafbbab48robertphillips 237c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc, 238c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips SkBudgeted::kNo, 239c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips bm.getPixels(), 240c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips 0)); 24183c17fa56b23159166394cb3feb431ffafbbab48robertphillips if (!texture) { 24283c17fa56b23159166394cb3feb431ffafbbab48robertphillips return; 24383c17fa56b23159166394cb3feb431ffafbbab48robertphillips } 24483c17fa56b23159166394cb3feb431ffafbbab48robertphillips 24583c17fa56b23159166394cb3feb431ffafbbab48robertphillips sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu( 24683c17fa56b23159166394cb3feb431ffafbbab48robertphillips SkIRect::MakeWH(kFullSize, 24783c17fa56b23159166394cb3feb431ffafbbab48robertphillips kFullSize), 24883c17fa56b23159166394cb3feb431ffafbbab48robertphillips kNeedNewImageUniqueID_SpecialImage, 249afbf71dd924c7bb46ccdac49e7408b4b088563ffbrianosman std::move(texture), nullptr)); 25083c17fa56b23159166394cb3feb431ffafbbab48robertphillips 25183c17fa56b23159166394cb3feb431ffafbbab48robertphillips { 2523e302275b324172c845627cbd00cee8a06571bafrobertphillips sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context)); 25383c17fa56b23159166394cb3feb431ffafbbab48robertphillips test_texture_backed(reporter, gpuImage, fromGPU); 25483c17fa56b23159166394cb3feb431ffafbbab48robertphillips } 25583c17fa56b23159166394cb3feb431ffafbbab48robertphillips 25683c17fa56b23159166394cb3feb431ffafbbab48robertphillips { 25783c17fa56b23159166394cb3feb431ffafbbab48robertphillips sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset)); 25883c17fa56b23159166394cb3feb431ffafbbab48robertphillips 2593e302275b324172c845627cbd00cee8a06571bafrobertphillips sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context)); 26083c17fa56b23159166394cb3feb431ffafbbab48robertphillips test_texture_backed(reporter, subGPUImage, fromSubGPU); 26183c17fa56b23159166394cb3feb431ffafbbab48robertphillips } 26283c17fa56b23159166394cb3feb431ffafbbab48robertphillips } 26383c17fa56b23159166394cb3feb431ffafbbab48robertphillips} 26483c17fa56b23159166394cb3feb431ffafbbab48robertphillips 26568d9134bec16e91c4a6cde071bcaa579bc0801a7bsalomonDEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) { 2668b7451aaf6b1c71e9d343a4df107893db277b6aabsalomon GrContext* context = ctxInfo.grContext(); 267b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips SkBitmap bm = create_bm(); 268b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 269b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips GrSurfaceDesc desc; 270b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips desc.fConfig = kSkia8888_GrPixelConfig; 271b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips desc.fFlags = kNone_GrSurfaceFlags; 272b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips desc.fWidth = kFullSize; 273b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips desc.fHeight = kFullSize; 274b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 275c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc, 276c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips SkBudgeted::kNo, 277c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips bm.getPixels(), 0)); 278b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips if (!texture) { 279b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips return; 280b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips } 281b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 28237bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu( 283c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips SkIRect::MakeWH(kFullSize, kFullSize), 284c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips kNeedNewImageUniqueID_SpecialImage, 285afbf71dd924c7bb46ccdac49e7408b4b088563ffbrianosman texture, nullptr)); 286c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips 287b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 288b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 289c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips { 29037bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu( 2913e302275b324172c845627cbd00cee8a06571bafrobertphillips subset, 292c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips kNeedNewImageUniqueID_SpecialImage, 293afbf71dd924c7bb46ccdac49e7408b4b088563ffbrianosman texture, nullptr)); 294ed086cae885050188cbb8761c0b1382bbadf088brobertphillips test_image(subSImg1, reporter, context, true, kPad, kFullSize); 295c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips } 296c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips 297c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips { 29837bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); 299ed086cae885050188cbb8761c0b1382bbadf088brobertphillips test_image(subSImg2, reporter, context, true, kPad, kFullSize); 300c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips } 301b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips} 302b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips 3038bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert PhillipsDEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_DeferredGpu, reporter, ctxInfo) { 3048bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips GrContext* context = ctxInfo.grContext(); 3058bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips SkBitmap bm = create_bm(); 3068bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips 3078bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips GrSurfaceDesc desc; 3088bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips desc.fConfig = kSkia8888_GrPixelConfig; 3098bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips desc.fFlags = kNone_GrSurfaceFlags; 3108bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips desc.fWidth = kFullSize; 3118bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips desc.fHeight = kFullSize; 3128bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips 3133743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeDeferred(*context->caps(), 3143743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips context->textureProvider(), 3153743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips desc, SkBudgeted::kNo, 3163743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips bm.getPixels(), 0)); 3178bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips if (!proxy) { 3188bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips return; 3198bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips } 3208bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips 3218bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu( 3228bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips context, 3238bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips SkIRect::MakeWH(kFullSize, kFullSize), 3248bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips kNeedNewImageUniqueID_SpecialImage, 3258bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips proxy, nullptr)); 3268bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips 3278bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); 3288bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips 3298bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips { 3308bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu( 3318bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips context, 3328bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips subset, 3338bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips kNeedNewImageUniqueID_SpecialImage, 3348bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips proxy, nullptr)); 3358bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips test_image(subSImg1, reporter, context, true, kPad, kFullSize); 3368bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips } 3378bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips 3388bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips { 3398bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); 3408bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips test_image(subSImg2, reporter, context, true, kPad, kFullSize); 3418bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips } 3428bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips} 3438bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips 344b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#endif 345