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