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" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrProxyProvider.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrSurfaceDrawContext.h" |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrTexture.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 16 | #include "src/gpu/SkGr.h" |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 17 | #ifdef SK_GL |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/gl/GrGLGpu.h" |
| 19 | #include "src/gpu/gl/GrGLUtil.h" |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 20 | #endif |
Greg Daniel | 84ea049 | 2019-06-05 16:52:02 -0400 | [diff] [blame] | 21 | #include "tools/gpu/ProxyUtils.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 22 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 23 | // skbug.com/5932 |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 24 | static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrDirectContext* dContext, |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 25 | GrSurfaceProxyView rectView, GrColorType colorType, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 26 | SkAlphaType alphaType, uint32_t expectedPixelValues[]) { |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 27 | auto fillContext = GrSurfaceFillContext::Make( |
| 28 | dContext, {colorType, kPremul_SkAlphaType, nullptr, rectView.dimensions()}); |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 29 | for (auto filter : {GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kLinear}) { |
| 30 | for (auto mm : {GrSamplerState::MipmapMode::kNone, GrSamplerState::MipmapMode::kLinear}) { |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 31 | fillContext->clear(SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA)); |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 32 | auto fp = GrTextureEffect::Make(rectView, alphaType, SkMatrix::I(), filter, mm); |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 33 | fillContext->fillWithFP(std::move(fp)); |
| 34 | TestReadPixels(reporter, dContext, fillContext.get(), expectedPixelValues, |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 35 | "RectangleTexture-basic-draw"); |
| 36 | } |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 40 | static void test_clear(skiatest::Reporter* reporter, GrDirectContext* dContext, |
| 41 | GrSurfaceContext* rectContext) { |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 42 | if (GrSurfaceFillContext* sfc = rectContext->asFillContext()) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 43 | // Clear the whole thing. |
| 44 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 45 | sfc->clear(SkPMColor4f::FromBytes_RGBA(color0)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 46 | |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 47 | int w = sfc->width(); |
| 48 | int h = sfc->height(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 49 | int pixelCnt = w * h; |
| 50 | SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); |
| 51 | |
| 52 | // 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] | 53 | uint32_t expectedColor0 = 0; |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 54 | uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 55 | expectedBytes0[0] = GrColorUnpackR(color0); |
| 56 | expectedBytes0[1] = GrColorUnpackG(color0); |
| 57 | expectedBytes0[2] = GrColorUnpackB(color0); |
| 58 | expectedBytes0[3] = GrColorUnpackA(color0); |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 59 | for (int i = 0; i < sfc->width() * sfc->height(); ++i) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 60 | expectedPixels.get()[i] = expectedColor0; |
| 61 | } |
| 62 | |
| 63 | // Clear the the top to a different color. |
| 64 | GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); |
| 65 | SkIRect rect = SkIRect::MakeWH(w, h/2); |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 66 | sfc->clear(rect, SkPMColor4f::FromBytes_RGBA(color1)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 67 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 68 | uint32_t expectedColor1 = 0; |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 69 | uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 70 | expectedBytes1[0] = GrColorUnpackR(color1); |
| 71 | expectedBytes1[1] = GrColorUnpackG(color1); |
| 72 | expectedBytes1[2] = GrColorUnpackB(color1); |
| 73 | expectedBytes1[3] = GrColorUnpackA(color1); |
| 74 | |
| 75 | for (int y = 0; y < h/2; ++y) { |
| 76 | for (int x = 0; x < w; ++x) { |
| 77 | expectedPixels.get()[y * h + x] = expectedColor1; |
| 78 | } |
| 79 | } |
| 80 | |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 81 | TestReadPixels(reporter, dContext, sfc, expectedPixels.get(), "RectangleTexture-clear"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 85 | static void test_copy_to_surface(skiatest::Reporter* reporter, |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 86 | GrDirectContext* dContext, |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 87 | GrSurfaceContext* dstContext, |
| 88 | const char* testName) { |
| 89 | |
| 90 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 91 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 92 | for (int y = 0; y < dstContext->width(); ++y) { |
| 93 | for (int x = 0; x < dstContext->height(); ++x) { |
| 94 | pixels.get()[y * dstContext->width() + x] = |
| 95 | SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 100 | auto origin = dstContext->origin(); |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 101 | GrImageInfo info(GrColorType::kRGBA_8888, |
| 102 | kPremul_SkAlphaType, |
| 103 | nullptr, |
| 104 | dstContext->dimensions()); |
| 105 | GrPixmap pixmap(info, pixels.get(), dstContext->width()*sizeof(uint32_t)); |
| 106 | auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(dContext, |
| 107 | renderable, |
| 108 | origin, |
| 109 | pixmap); |
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 | 982127b | 2021-01-21 10:43:35 -0500 | [diff] [blame] | 112 | SkAssertResult(dstContext->testCopy(srcView.refProxy())); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 113 | TestReadPixels(reporter, dContext, 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) { |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 119 | auto dContext = ctxInfo.directContext(); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 120 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 121 | GrProxyProvider* proxyProvider = dContext->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 }) { |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 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 | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 137 | GrBackendTexture rectangleTex = dContext->createBackendTexture(kWidth, |
Brian Salomon | 14f99fc | 2020-12-07 12:19:47 -0500 | [diff] [blame] | 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 | |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 146 | if (!dContext->updateBackendTexture(rectangleTex, &pm, 1, origin, 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) { |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 153 | refPixels[y * kWidth + x] = pixels[y * kWidth + x]; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 157 | sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture( |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 158 | rectangleTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 159 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 160 | if (!rectProxy) { |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 161 | dContext->deleteBackendTexture(rectangleTex); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 162 | continue; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 165 | SkASSERT(rectProxy->mipmapped() == GrMipmapped::kNo); |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 166 | SkASSERT(rectProxy->peekTexture()->mipmapped() == GrMipmapped::kNo); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 167 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 168 | SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle); |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 169 | SkASSERT(rectProxy->peekTexture()->textureType() == GrTextureType::kRectangle); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 170 | SkASSERT(rectProxy->hasRestrictedSampling()); |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 171 | SkASSERT(rectProxy->peekTexture()->hasRestrictedSampling()); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 172 | |
Brian Salomon | 14f99fc | 2020-12-07 12:19:47 -0500 | [diff] [blame] | 173 | GrImageInfo grII = ii; |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 174 | GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(rectangleTex.getBackendFormat(), |
Brian Salomon | 14f99fc | 2020-12-07 12:19:47 -0500 | [diff] [blame] | 175 | grII.colorType()); |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 176 | GrSurfaceProxyView view(rectProxy, origin, swizzle); |
| 177 | |
Brian Salomon | 14f99fc | 2020-12-07 12:19:47 -0500 | [diff] [blame] | 178 | test_basic_draw_as_src(reporter, dContext, view, grII.colorType(), kPremul_SkAlphaType, |
| 179 | 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 |
Brian Salomon | 982127b | 2021-01-21 10:43:35 -0500 | [diff] [blame] | 182 | TestCopyFromSurface(reporter, dContext, rectProxy, origin, grII.colorType(), refPixels, |
| 183 | "RectangleTexture-copy-from"); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 184 | |
Brian Salomon | 14f99fc | 2020-12-07 12:19:47 -0500 | [diff] [blame] | 185 | auto rectContext = GrSurfaceContext::Make(dContext, std::move(view), grII.colorInfo()); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 186 | SkASSERT(rectContext); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 187 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 188 | TestReadPixels(reporter, dContext, rectContext.get(), refPixels, "RectangleTexture-read"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 189 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 190 | test_copy_to_surface(reporter, dContext, rectContext.get(), "RectangleTexture-copy-to"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 191 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 192 | TestWritePixels(reporter, dContext, rectContext.get(), true, "RectangleTexture-write"); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 193 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 194 | test_clear(reporter, dContext, rectContext.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 195 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 196 | dContext->deleteBackendTexture(rectangleTex); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 199 | #endif |