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"
2184dd85793560769e9628d0a767cb50a4234c572aMike Reed#include "GrTextureProxy.h"
223b65598bceb65736486db27ed49a56d787032747Brian Osman#include "SkGr.h"
23b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#endif
24b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
25b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
26b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// This test creates backing resources exactly sized to [kFullSize x kFullSize].
27b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// It then wraps them in an SkSpecialImage with only the center (red) region being active.
28b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none
29b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// of the inactive (green) region leaked out.
30b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
31b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstatic const int kSmallerSize = 10;
32b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstatic const int kPad = 3;
33b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstatic const int kFullSize = kSmallerSize + 2 * kPad;
34b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
35b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// Create a bitmap with red in the center and green around it
36b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstatic SkBitmap create_bm() {
37b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkBitmap bm;
38b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    bm.allocN32Pixels(kFullSize, kFullSize, true);
39b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
40b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkCanvas temp(bm);
41b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
42b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    temp.clear(SK_ColorGREEN);
43b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkPaint p;
44b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    p.setColor(SK_ColorRED);
45b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    p.setAntiAlias(false);
46b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
47b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
489d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary                                   SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
49b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips                  p);
50b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
51b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    return bm;
52b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips}
53b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
54b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
5537bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillipsstatic void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
566de99041f13e87ed440f7db13a07693c6c4c461aRobert Phillips                       GrContext* context, bool isGPUBacked,
57c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips                       int offset, int size) {
583e302275b324172c845627cbd00cee8a06571bafrobertphillips    const SkIRect subset = img->subset();
59c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    REPORTER_ASSERT(reporter, offset == subset.left());
60c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    REPORTER_ASSERT(reporter, offset == subset.top());
61b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
62b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
63b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
64b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    //--------------
652c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips    // Test that isTextureBacked reports the correct backing type
666de99041f13e87ed440f7db13a07693c6c4c461aRobert Phillips    REPORTER_ASSERT(reporter, isGPUBacked == img->isTextureBacked());
67646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips
68646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips#if SK_SUPPORT_GPU
69646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    //--------------
708e1c4e672553ecae2745168514240705f3516773Robert Phillips    // Test asTextureProxyRef - as long as there is a context this should succeed
71646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    if (context) {
728e1c4e672553ecae2745168514240705f3516773Robert Phillips        sk_sp<GrTextureProxy> proxy(img->asTextureProxyRef(context));
738e1c4e672553ecae2745168514240705f3516773Robert Phillips        REPORTER_ASSERT(reporter, proxy);
74646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    }
75646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips#endif
76b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
77b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    //--------------
78646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    // Test getROPixels - this should always succeed regardless of backing store
79646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    SkBitmap bitmap;
80646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    REPORTER_ASSERT(reporter, img->getROPixels(&bitmap));
81ed086cae885050188cbb8761c0b1382bbadf088brobertphillips    if (context) {
82ed086cae885050188cbb8761c0b1382bbadf088brobertphillips        REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width());
83ed086cae885050188cbb8761c0b1382bbadf088brobertphillips        REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height());
84ed086cae885050188cbb8761c0b1382bbadf088brobertphillips    } else {
85ed086cae885050188cbb8761c0b1382bbadf088brobertphillips        REPORTER_ASSERT(reporter, size == bitmap.width());
86ed086cae885050188cbb8761c0b1382bbadf088brobertphillips        REPORTER_ASSERT(reporter, size == bitmap.height());
87ed086cae885050188cbb8761c0b1382bbadf088brobertphillips    }
88b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
89b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    //--------------
90b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    // Test that draw restricts itself to the subset
91eed6b0e1d865a1f93143c09961debba0aca592cabrianosman    SkImageFilter::OutputProperties outProps(img->getColorSpace());
92eed6b0e1d865a1f93143c09961debba0aca592cabrianosman    sk_sp<SkSpecialSurface> surf(img->makeSurface(outProps, SkISize::Make(kFullSize, kFullSize),
93cb6266b5aa5bbfd880532f08eec83b0c585e873fMatt Sarett                                                  kPremul_SkAlphaType));
94b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
95b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkCanvas* canvas = surf->getCanvas();
96b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
97b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    canvas->clear(SK_ColorBLUE);
98e8c34974c42b302d78c5cc8c7dcfb36956256baarobertphillips    img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
99b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
100b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkBitmap bm;
101cb6266b5aa5bbfd880532f08eec83b0c585e873fMatt Sarett    bm.allocN32Pixels(kFullSize, kFullSize, false);
102b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
103b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
104b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkASSERT_RELEASE(result);
105b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
106b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    // Only the center (red) portion should've been drawn into the canvas
107b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
108b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    REPORTER_ASSERT(reporter, SK_ColorRED  == bm.getColor(kPad, kPad));
109b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    REPORTER_ASSERT(reporter, SK_ColorRED  == bm.getColor(kSmallerSize+kPad-1,
110b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips                                                          kSmallerSize+kPad-1));
111b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
112b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips                                                          kSmallerSize+kPad));
113b4bd11e66519f545282f914c67b54bf17cecab84robertphillips
114b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    //--------------
115a5fdc974a996dca79be8388e61db68043001760bRobert Phillips    // Test that asImage & makeTightSurface return appropriately sized objects
116b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    // of the correct backing type
117b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
118b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    {
119a5fdc974a996dca79be8388e61db68043001760bRobert Phillips        sk_sp<SkImage> tightImg(img->asImage(&newSubset));
120b4bd11e66519f545282f914c67b54bf17cecab84robertphillips
121b4bd11e66519f545282f914c67b54bf17cecab84robertphillips        REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
122b4bd11e66519f545282f914c67b54bf17cecab84robertphillips        REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
1230ae6faa34d73ffc7ebec3d13f0473703bade821bRobert Phillips        REPORTER_ASSERT(reporter, isGPUBacked == tightImg->isTextureBacked());
124b4bd11e66519f545282f914c67b54bf17cecab84robertphillips        SkPixmap tmpPixmap;
1256de99041f13e87ed440f7db13a07693c6c4c461aRobert Phillips        REPORTER_ASSERT(reporter, isGPUBacked != !!tightImg->peekPixels(&tmpPixmap));
126b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    }
127b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    {
128eed6b0e1d865a1f93143c09961debba0aca592cabrianosman        SkImageFilter::OutputProperties outProps(img->getColorSpace());
129eed6b0e1d865a1f93143c09961debba0aca592cabrianosman        sk_sp<SkSurface> tightSurf(img->makeTightSurface(outProps, subset.size()));
130b4bd11e66519f545282f914c67b54bf17cecab84robertphillips
131b4bd11e66519f545282f914c67b54bf17cecab84robertphillips        REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
132b4bd11e66519f545282f914c67b54bf17cecab84robertphillips        REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
1336de99041f13e87ed440f7db13a07693c6c4c461aRobert Phillips        REPORTER_ASSERT(reporter, isGPUBacked ==
134b4bd11e66519f545282f914c67b54bf17cecab84robertphillips                     !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
135b4bd11e66519f545282f914c67b54bf17cecab84robertphillips        SkPixmap tmpPixmap;
1366de99041f13e87ed440f7db13a07693c6c4c461aRobert Phillips        REPORTER_ASSERT(reporter, isGPUBacked != !!tightSurf->peekPixels(&tmpPixmap));
137b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    }
138b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips}
139b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
140b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsDEF_TEST(SpecialImage_Raster, reporter) {
141b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkBitmap bm = create_bm();
142b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
14337bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips    sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
144c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips                                                            SkIRect::MakeWH(kFullSize, kFullSize),
145c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips                                                            bm));
146c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips
147b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
148b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
149c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    {
1503e302275b324172c845627cbd00cee8a06571bafrobertphillips        sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
151646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips        test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
152c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    }
153c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips
154c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    {
15537bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips        sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
156646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips        test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
157c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    }
158b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips}
159b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
16061624f0c716b576706659750d87b6956f4c15722Brian Osmanstatic void test_specialimage_image(skiatest::Reporter* reporter, SkColorSpace* dstColorSpace) {
161b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkBitmap bm = create_bm();
162b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
1639ce9d6772df650ceb0511f275e1a83dffa78ff72reed    sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
164b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
16537bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips    sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
166c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips                                                            SkIRect::MakeWH(kFullSize, kFullSize),
16761624f0c716b576706659750d87b6956f4c15722Brian Osman                                                            fullImage, dstColorSpace));
168c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips
169b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
170b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
171c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    {
17261624f0c716b576706659750d87b6956f4c15722Brian Osman        sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage,
17361624f0c716b576706659750d87b6956f4c15722Brian Osman                                                                     dstColorSpace));
174ed086cae885050188cbb8761c0b1382bbadf088brobertphillips        test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
175c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    }
176c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips
177c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    {
17837bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips        sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
179ed086cae885050188cbb8761c0b1382bbadf088brobertphillips        test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
180c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    }
181b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips}
182b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
1837992da32f02f90e0ac9ab6914eb31676b502eb71Brian OsmanDEF_TEST(SpecialImage_Image_Legacy, reporter) {
18461624f0c716b576706659750d87b6956f4c15722Brian Osman    SkColorSpace* legacyColorSpace = nullptr;
18561624f0c716b576706659750d87b6956f4c15722Brian Osman    test_specialimage_image(reporter, legacyColorSpace);
1867992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman}
1877992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman
1887992da32f02f90e0ac9ab6914eb31676b502eb71Brian OsmanDEF_TEST(SpecialImage_Image_ColorSpaceAware, reporter) {
18977a7a1b57c16c97f056c1e50c03bdc954947778cMatt Sarett    sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
19061624f0c716b576706659750d87b6956f4c15722Brian Osman    test_specialimage_image(reporter, srgbColorSpace.get());
1917992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman}
1927992da32f02f90e0ac9ab6914eb31676b502eb71Brian Osman
193b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#if SK_SUPPORT_GPU
19483c17fa56b23159166394cb3feb431ffafbbab48robertphillips
19583c17fa56b23159166394cb3feb431ffafbbab48robertphillipsstatic void test_texture_backed(skiatest::Reporter* reporter,
19683c17fa56b23159166394cb3feb431ffafbbab48robertphillips                                const sk_sp<SkSpecialImage>& orig,
19783c17fa56b23159166394cb3feb431ffafbbab48robertphillips                                const sk_sp<SkSpecialImage>& gpuBacked) {
19883c17fa56b23159166394cb3feb431ffafbbab48robertphillips    REPORTER_ASSERT(reporter, gpuBacked);
199646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
20083c17fa56b23159166394cb3feb431ffafbbab48robertphillips    REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
20183c17fa56b23159166394cb3feb431ffafbbab48robertphillips    REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
20283c17fa56b23159166394cb3feb431ffafbbab48robertphillips                              gpuBacked->subset().height() == orig->subset().height());
203afbf71dd924c7bb46ccdac49e7408b4b088563ffbrianosman    REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
20483c17fa56b23159166394cb3feb431ffafbbab48robertphillips}
20583c17fa56b23159166394cb3feb431ffafbbab48robertphillips
20683c17fa56b23159166394cb3feb431ffafbbab48robertphillips// Test out the SkSpecialImage::makeTextureImage entry point
20768d9134bec16e91c4a6cde071bcaa579bc0801a7bsalomonDEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
2088b7451aaf6b1c71e9d343a4df107893db277b6aabsalomon    GrContext* context = ctxInfo.grContext();
20983c17fa56b23159166394cb3feb431ffafbbab48robertphillips    SkBitmap bm = create_bm();
21083c17fa56b23159166394cb3feb431ffafbbab48robertphillips
21183c17fa56b23159166394cb3feb431ffafbbab48robertphillips    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
21283c17fa56b23159166394cb3feb431ffafbbab48robertphillips
21383c17fa56b23159166394cb3feb431ffafbbab48robertphillips    {
21483c17fa56b23159166394cb3feb431ffafbbab48robertphillips        // raster
21583c17fa56b23159166394cb3feb431ffafbbab48robertphillips        sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
21683c17fa56b23159166394cb3feb431ffafbbab48robertphillips                                                                        SkIRect::MakeWH(kFullSize,
21783c17fa56b23159166394cb3feb431ffafbbab48robertphillips                                                                                        kFullSize),
21883c17fa56b23159166394cb3feb431ffafbbab48robertphillips                                                                        bm));
21983c17fa56b23159166394cb3feb431ffafbbab48robertphillips
22083c17fa56b23159166394cb3feb431ffafbbab48robertphillips        {
2213e302275b324172c845627cbd00cee8a06571bafrobertphillips            sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
22283c17fa56b23159166394cb3feb431ffafbbab48robertphillips            test_texture_backed(reporter, rasterImage, fromRaster);
22383c17fa56b23159166394cb3feb431ffafbbab48robertphillips        }
22483c17fa56b23159166394cb3feb431ffafbbab48robertphillips
22583c17fa56b23159166394cb3feb431ffafbbab48robertphillips        {
22683c17fa56b23159166394cb3feb431ffafbbab48robertphillips            sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
22783c17fa56b23159166394cb3feb431ffafbbab48robertphillips
2283e302275b324172c845627cbd00cee8a06571bafrobertphillips            sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
22983c17fa56b23159166394cb3feb431ffafbbab48robertphillips            test_texture_backed(reporter, subRasterImage, fromSubRaster);
23083c17fa56b23159166394cb3feb431ffafbbab48robertphillips        }
23183c17fa56b23159166394cb3feb431ffafbbab48robertphillips    }
23283c17fa56b23159166394cb3feb431ffafbbab48robertphillips
23383c17fa56b23159166394cb3feb431ffafbbab48robertphillips    {
23483c17fa56b23159166394cb3feb431ffafbbab48robertphillips        // gpu
2352c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips        const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bm.info(), *context->caps());
2362c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips
23726c90e04797e15c37ec00e0f836292b8a207d294Robert Phillips        sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
2382c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips                                                                 desc, SkBudgeted::kNo,
2392c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips                                                                 bm.getPixels(), bm.rowBytes()));
2402f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips        if (!proxy) {
24183c17fa56b23159166394cb3feb431ffafbbab48robertphillips            return;
24283c17fa56b23159166394cb3feb431ffafbbab48robertphillips        }
24383c17fa56b23159166394cb3feb431ffafbbab48robertphillips
2442c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips        sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeDeferredFromGpu(
2452f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                            context,
2462f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                            SkIRect::MakeWH(kFullSize, kFullSize),
2472f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                            kNeedNewImageUniqueID_SpecialImage,
2482f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                            std::move(proxy), nullptr));
24983c17fa56b23159166394cb3feb431ffafbbab48robertphillips
25083c17fa56b23159166394cb3feb431ffafbbab48robertphillips        {
2513e302275b324172c845627cbd00cee8a06571bafrobertphillips            sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
25283c17fa56b23159166394cb3feb431ffafbbab48robertphillips            test_texture_backed(reporter, gpuImage, fromGPU);
25383c17fa56b23159166394cb3feb431ffafbbab48robertphillips        }
25483c17fa56b23159166394cb3feb431ffafbbab48robertphillips
25583c17fa56b23159166394cb3feb431ffafbbab48robertphillips        {
25683c17fa56b23159166394cb3feb431ffafbbab48robertphillips            sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
25783c17fa56b23159166394cb3feb431ffafbbab48robertphillips
2583e302275b324172c845627cbd00cee8a06571bafrobertphillips            sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
25983c17fa56b23159166394cb3feb431ffafbbab48robertphillips            test_texture_backed(reporter, subGPUImage, fromSubGPU);
26083c17fa56b23159166394cb3feb431ffafbbab48robertphillips        }
26183c17fa56b23159166394cb3feb431ffafbbab48robertphillips    }
26283c17fa56b23159166394cb3feb431ffafbbab48robertphillips}
26383c17fa56b23159166394cb3feb431ffafbbab48robertphillips
26468d9134bec16e91c4a6cde071bcaa579bc0801a7bsalomonDEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
2658b7451aaf6b1c71e9d343a4df107893db277b6aabsalomon    GrContext* context = ctxInfo.grContext();
266b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    SkBitmap bm = create_bm();
267b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
2682c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips    const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bm.info(), *context->caps());
269b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
27026c90e04797e15c37ec00e0f836292b8a207d294Robert Phillips    sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
2712c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips                                                             desc, SkBudgeted::kNo,
2722c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips                                                             bm.getPixels(), bm.rowBytes()));
2732f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips    if (!proxy) {
274b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips        return;
275b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    }
276b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
2772c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips    sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
2782c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips                                                            context,
279c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips                                                            SkIRect::MakeWH(kFullSize, kFullSize),
280c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips                                                            kNeedNewImageUniqueID_SpecialImage,
2812f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                            proxy, nullptr));
282c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips
283b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
284b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
285c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    {
2862c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips        sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
2872f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                               context, subset,
288c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips                                                               kNeedNewImageUniqueID_SpecialImage,
2892f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                               std::move(proxy), nullptr));
290ed086cae885050188cbb8761c0b1382bbadf088brobertphillips        test_image(subSImg1, reporter, context, true, kPad, kFullSize);
291c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    }
292c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips
293c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    {
29437bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips        sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
295ed086cae885050188cbb8761c0b1382bbadf088brobertphillips        test_image(subSImg2, reporter, context, true, kPad, kFullSize);
296c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    }
297b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips}
298b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
2998bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert PhillipsDEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_DeferredGpu, reporter, ctxInfo) {
3008bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    GrContext* context = ctxInfo.grContext();
3018bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    SkBitmap bm = create_bm();
3028bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips
3038bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    GrSurfaceDesc desc;
3048bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    desc.fConfig = kSkia8888_GrPixelConfig;
3058bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    desc.fFlags  = kNone_GrSurfaceFlags;
3068bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    desc.fWidth  = kFullSize;
3078bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    desc.fHeight = kFullSize;
3088bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips
30926c90e04797e15c37ec00e0f836292b8a207d294Robert Phillips    sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
3103743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips                                                             desc, SkBudgeted::kNo,
3113743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips                                                             bm.getPixels(), 0));
3128bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    if (!proxy) {
3138bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips        return;
3148bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    }
3158bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips
3168bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
3178bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                            context,
3188bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                            SkIRect::MakeWH(kFullSize, kFullSize),
3198bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                            kNeedNewImageUniqueID_SpecialImage,
3202f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                            proxy, nullptr));
3218bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips
3228bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
3238bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips
3248bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    {
3258bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips        sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
3262f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                               context, subset,
3278bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                               kNeedNewImageUniqueID_SpecialImage,
3282f49314227053dd5acc011e04681faee05c5e0ffRobert Phillips                                                               std::move(proxy), nullptr));
3298bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips        test_image(subSImg1, reporter, context, true, kPad, kFullSize);
3308bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    }
3318bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips
3328bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    {
3338bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips        sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
3348bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips        test_image(subSImg2, reporter, context, true, kPad, kFullSize);
3358bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    }
3368bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips}
3378bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips
338b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#endif
339