bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
| 9 | #include "tests/TestUtils.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 10 | |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrProxyProvider.h" |
| 14 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrSurfaceContextPriv.h" |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrTexture.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 17 | #include "src/gpu/SkGr.h" |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 18 | #ifdef SK_GL |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/gl/GrGLGpu.h" |
| 20 | #include "src/gpu/gl/GrGLUtil.h" |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 21 | #endif |
Greg Daniel | 84ea049 | 2019-06-05 16:52:02 -0400 | [diff] [blame] | 22 | #include "tools/gpu/ProxyUtils.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 23 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 24 | // skbug.com/5932 |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 25 | static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrRecordingContext* context, |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 26 | GrSurfaceProxyView rectView, GrColorType colorType, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 27 | SkAlphaType alphaType, uint32_t expectedPixelValues[]) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 28 | auto rtContext = GrRenderTargetContext::Make( |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 29 | context, colorType, nullptr, SkBackingFit::kExact, rectView.proxy()->dimensions()); |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 30 | for (auto filter : {GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kLinear}) { |
| 31 | for (auto mm : {GrSamplerState::MipmapMode::kNone, GrSamplerState::MipmapMode::kLinear}) { |
| 32 | rtContext->clear(SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA)); |
| 33 | auto fp = GrTextureEffect::Make(rectView, alphaType, SkMatrix::I(), filter, mm); |
| 34 | GrPaint paint; |
| 35 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 36 | paint.setColorFragmentProcessor(std::move(fp)); |
| 37 | rtContext->drawPaint(nullptr, std::move(paint), SkMatrix::I()); |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 38 | TestReadPixels(reporter, rtContext.get(), expectedPixelValues, |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 39 | "RectangleTexture-basic-draw"); |
| 40 | } |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 44 | static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) { |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 45 | if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 46 | // Clear the whole thing. |
| 47 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 48 | rtc->clear(SkPMColor4f::FromBytes_RGBA(color0)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 49 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 50 | int w = rtc->width(); |
| 51 | int h = rtc->height(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 52 | int pixelCnt = w * h; |
| 53 | SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); |
| 54 | |
| 55 | // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different. |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 56 | uint32_t expectedColor0 = 0; |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 57 | uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 58 | expectedBytes0[0] = GrColorUnpackR(color0); |
| 59 | expectedBytes0[1] = GrColorUnpackG(color0); |
| 60 | expectedBytes0[2] = GrColorUnpackB(color0); |
| 61 | expectedBytes0[3] = GrColorUnpackA(color0); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 62 | for (int i = 0; i < rtc->width() * rtc->height(); ++i) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 63 | expectedPixels.get()[i] = expectedColor0; |
| 64 | } |
| 65 | |
| 66 | // Clear the the top to a different color. |
| 67 | GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); |
| 68 | SkIRect rect = SkIRect::MakeWH(w, h/2); |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 69 | rtc->clear(rect, SkPMColor4f::FromBytes_RGBA(color1)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 70 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 71 | uint32_t expectedColor1 = 0; |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 72 | uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 73 | expectedBytes1[0] = GrColorUnpackR(color1); |
| 74 | expectedBytes1[1] = GrColorUnpackG(color1); |
| 75 | expectedBytes1[2] = GrColorUnpackB(color1); |
| 76 | expectedBytes1[3] = GrColorUnpackA(color1); |
| 77 | |
| 78 | for (int y = 0; y < h/2; ++y) { |
| 79 | for (int x = 0; x < w; ++x) { |
| 80 | expectedPixels.get()[y * h + x] = expectedColor1; |
| 81 | } |
| 82 | } |
| 83 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 84 | TestReadPixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 88 | static void test_copy_to_surface(skiatest::Reporter* reporter, |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 89 | GrDirectContext* context, |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 90 | GrSurfaceContext* dstContext, |
| 91 | const char* testName) { |
| 92 | |
| 93 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 94 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 95 | for (int y = 0; y < dstContext->width(); ++y) { |
| 96 | for (int x = 0; x < dstContext->height(); ++x) { |
| 97 | pixels.get()[y * dstContext->width() + x] = |
| 98 | SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y)); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 103 | auto origin = dstContext->origin(); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 104 | auto src = sk_gpu_test::MakeTextureProxyFromData( |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 105 | context, renderable, origin, |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 106 | {GrColorType::kRGBA_8888, kPremul_SkAlphaType, nullptr, dstContext->width(), |
| 107 | dstContext->height()}, |
| 108 | pixels.get(), 0); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 109 | // If this assert ever fails we can add a fallback to do copy as draw, but until then we can |
| 110 | // be more restrictive. |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 111 | SkAssertResult(dstContext->testCopy(src.get())); |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 112 | TestReadPixels(reporter, dstContext, pixels.get(), testName); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 116 | #ifdef SK_GL |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 117 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 118 | auto direct = ctxInfo.directContext(); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 119 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 120 | GrProxyProvider* proxyProvider = direct->priv().proxyProvider(); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 121 | static const int kWidth = 16; |
| 122 | static const int kHeight = 16; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 123 | |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 124 | uint32_t pixels[kWidth * kHeight]; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 125 | for (int y = 0; y < kHeight; ++y) { |
| 126 | for (int x = 0; x < kWidth; ++x) { |
| 127 | pixels[y * kWidth + x] = y * kWidth + x; |
| 128 | } |
| 129 | } |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 130 | auto ii = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 131 | SkPixmap pm(ii, pixels, sizeof(uint32_t)*kWidth); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 132 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 133 | for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) { |
| 134 | bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin; |
| 135 | |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 136 | auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE); |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 137 | GrBackendTexture rectangleTex = direct->createBackendTexture(kWidth, |
| 138 | kHeight, |
| 139 | format, |
| 140 | GrMipmapped::kNo, |
| 141 | GrRenderable::kYes); |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 142 | if (!rectangleTex.isValid()) { |
| 143 | continue; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 146 | if (!direct->updateBackendTexture(rectangleTex, &pm, 1, nullptr, nullptr)) { |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 147 | continue; |
| 148 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 149 | |
| 150 | GrColor refPixels[kWidth * kHeight]; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 151 | for (int y = 0; y < kHeight; ++y) { |
| 152 | for (int x = 0; x < kWidth; ++x) { |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 153 | int y0 = useBLOrigin ? kHeight - y - 1 : y; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 154 | refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; |
| 155 | } |
| 156 | } |
| 157 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 158 | sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture( |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 159 | rectangleTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 160 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 161 | if (!rectProxy) { |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 162 | direct->deleteBackendTexture(rectangleTex); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 163 | continue; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 166 | SkASSERT(rectProxy->mipmapped() == GrMipmapped::kNo); |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 167 | SkASSERT(rectProxy->peekTexture()->mipmapped() == GrMipmapped::kNo); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 168 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 169 | SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle); |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 170 | SkASSERT(rectProxy->peekTexture()->textureType() == GrTextureType::kRectangle); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 171 | SkASSERT(rectProxy->hasRestrictedSampling()); |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 172 | SkASSERT(rectProxy->peekTexture()->hasRestrictedSampling()); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 173 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 174 | GrSwizzle swizzle = direct->priv().caps()->getReadSwizzle(rectangleTex.getBackendFormat(), |
| 175 | GrColorType::kRGBA_8888); |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 176 | GrSurfaceProxyView view(rectProxy, origin, swizzle); |
| 177 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 178 | test_basic_draw_as_src(reporter, direct, view, GrColorType::kRGBA_8888, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 179 | kPremul_SkAlphaType, refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 180 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 181 | // Test copy to both a texture and RT |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 182 | TestCopyFromSurface(reporter, direct, rectProxy.get(), origin, GrColorType::kRGBA_8888, |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 183 | refPixels, "RectangleTexture-copy-from"); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 184 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 185 | auto rectContext = GrSurfaceContext::Make(direct, std::move(view), |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 186 | GrColorType::kRGBA_8888, kPremul_SkAlphaType, |
| 187 | nullptr); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 188 | SkASSERT(rectContext); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 189 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 190 | TestReadPixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 191 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 192 | test_copy_to_surface(reporter, direct, rectContext.get(), "RectangleTexture-copy-to"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 193 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 194 | TestWritePixels(reporter, rectContext.get(), true, "RectangleTexture-write"); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 195 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 196 | test_clear(reporter, rectContext.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 197 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame^] | 198 | direct->deleteBackendTexture(rectangleTex); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 201 | #endif |