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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrContext.h" |
| 12 | #include "src/gpu/GrClip.h" |
| 13 | #include "src/gpu/GrContextPriv.h" |
| 14 | #include "src/gpu/GrProxyProvider.h" |
| 15 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrSurfaceContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrSurfacePriv.h" |
| 18 | #include "src/gpu/GrTexturePriv.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 19 | #include "src/gpu/SkGr.h" |
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" |
Greg Daniel | 84ea049 | 2019-06-05 16:52:02 -0400 | [diff] [blame] | 22 | #include "tools/gpu/ProxyUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/gpu/gl/GLTestContext.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 | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 26 | static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 27 | sk_sp<GrTextureProxy> rectProxy, GrColorType colorType, |
| 28 | uint32_t expectedPixelValues[]) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 29 | sk_sp<GrRenderTargetContext> rtContext(context->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 30 | SkBackingFit::kExact, rectProxy->width(), rectProxy->height(), colorType, nullptr)); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 31 | for (auto filter : {GrSamplerState::Filter::kNearest, |
| 32 | GrSamplerState::Filter::kBilerp, |
| 33 | GrSamplerState::Filter::kMipMap}) { |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 34 | rtContext->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA), |
| 35 | GrRenderTargetContext::CanClearFullscreen::kYes); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 36 | auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 37 | GrPaint paint; |
| 38 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 39 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 40 | rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 41 | test_read_pixels(reporter, rtContext.get(), expectedPixelValues, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 42 | "RectangleTexture-basic-draw"); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 46 | static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) { |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 47 | if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 48 | // Clear the whole thing. |
| 49 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 50 | rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(color0), |
| 51 | GrRenderTargetContext::CanClearFullscreen::kNo); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 52 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 53 | int w = rtc->width(); |
| 54 | int h = rtc->height(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 55 | int pixelCnt = w * h; |
| 56 | SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); |
| 57 | |
| 58 | // 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] | 59 | uint32_t expectedColor0 = 0; |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 60 | uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 61 | expectedBytes0[0] = GrColorUnpackR(color0); |
| 62 | expectedBytes0[1] = GrColorUnpackG(color0); |
| 63 | expectedBytes0[2] = GrColorUnpackB(color0); |
| 64 | expectedBytes0[3] = GrColorUnpackA(color0); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 65 | for (int i = 0; i < rtc->width() * rtc->height(); ++i) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 66 | expectedPixels.get()[i] = expectedColor0; |
| 67 | } |
| 68 | |
| 69 | // Clear the the top to a different color. |
| 70 | GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); |
| 71 | SkIRect rect = SkIRect::MakeWH(w, h/2); |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 72 | rtc->clear(&rect, SkPMColor4f::FromBytes_RGBA(color1), |
| 73 | GrRenderTargetContext::CanClearFullscreen::kNo); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 74 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 75 | uint32_t expectedColor1 = 0; |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 76 | uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 77 | expectedBytes1[0] = GrColorUnpackR(color1); |
| 78 | expectedBytes1[1] = GrColorUnpackG(color1); |
| 79 | expectedBytes1[2] = GrColorUnpackB(color1); |
| 80 | expectedBytes1[3] = GrColorUnpackA(color1); |
| 81 | |
| 82 | for (int y = 0; y < h/2; ++y) { |
| 83 | for (int x = 0; x < w; ++x) { |
| 84 | expectedPixels.get()[y * h + x] = expectedColor1; |
| 85 | } |
| 86 | } |
| 87 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 88 | test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 92 | static void test_copy_to_surface(skiatest::Reporter* reporter, |
| 93 | GrContext* context, |
| 94 | GrSurfaceContext* dstContext, |
| 95 | const char* testName) { |
| 96 | |
| 97 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 98 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 99 | for (int y = 0; y < dstContext->width(); ++y) { |
| 100 | for (int x = 0; x < dstContext->height(); ++x) { |
| 101 | pixels.get()[y * dstContext->width() + x] = |
| 102 | SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y)); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { |
| 107 | auto origin = dstContext->asSurfaceProxy()->origin(); |
| 108 | auto src = sk_gpu_test::MakeTextureProxyFromData( |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 109 | context, renderable, dstContext->width(), dstContext->height(), |
| 110 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, origin, pixels.get(), 0); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 111 | // If this assert ever fails we can add a fallback to do copy as draw, but until then we can |
| 112 | // be more restrictive. |
| 113 | SkAssertResult(dstContext->testCopy(src.get())); |
| 114 | test_read_pixels(reporter, dstContext, pixels.get(), testName); |
| 115 | } |
| 116 | } |
| 117 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 118 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 119 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 120 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 121 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
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 | |
| 125 | GrColor pixels[kWidth * kHeight]; |
| 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 | } |
| 131 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 132 | for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) { |
| 133 | bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin; |
| 134 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 135 | GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, |
| 136 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, |
| 137 | pixels); |
| 138 | |
| 139 | if (!rectTexID) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | // Let GrContext know that we messed with the GL context directly. |
| 144 | context->resetContext(); |
| 145 | |
| 146 | // Wrap the rectangle texture ID in a GrTexture |
| 147 | GrGLTextureInfo rectangleInfo; |
| 148 | rectangleInfo.fID = rectTexID; |
| 149 | rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 150 | rectangleInfo.fFormat = GR_GL_RGBA8; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 151 | |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 152 | GrBackendTexture rectangleTex(kWidth, kHeight, GrMipMapped::kNo, rectangleInfo); |
| 153 | rectangleTex.setPixelConfig(kRGBA_8888_GrPixelConfig); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 154 | |
| 155 | GrColor refPixels[kWidth * kHeight]; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 156 | for (int y = 0; y < kHeight; ++y) { |
| 157 | for (int x = 0; x < kWidth; ++x) { |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 158 | int y0 = useBLOrigin ? kHeight - y - 1 : y; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 159 | refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; |
| 160 | } |
| 161 | } |
| 162 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 163 | sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture( |
Robert Phillips | 9725638 | 2019-07-17 15:26:36 -0400 | [diff] [blame] | 164 | rectangleTex, GrColorType::kRGBA_8888, origin, |
| 165 | kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 166 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 167 | if (!rectProxy) { |
| 168 | ERRORF(reporter, "Error creating proxy for rectangle texture."); |
| 169 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 170 | continue; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 173 | SkASSERT(rectProxy->mipMapped() == GrMipMapped::kNo); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 174 | SkASSERT(rectProxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 175 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 176 | SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle); |
| 177 | SkASSERT(rectProxy->peekTexture()->texturePriv().textureType() == |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 178 | GrTextureType::kRectangle); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 179 | SkASSERT(rectProxy->hasRestrictedSampling()); |
| 180 | SkASSERT(rectProxy->peekTexture()->texturePriv().hasRestrictedSampling()); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 181 | |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 182 | test_basic_draw_as_src(reporter, context, rectProxy, GrColorType::kRGBA_8888, refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 183 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 184 | // Test copy to both a texture and RT |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 185 | test_copy_from_surface(reporter, context, rectProxy.get(), GrColorType::kRGBA_8888, |
| 186 | refPixels, "RectangleTexture-copy-from"); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 187 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 188 | sk_sp<GrSurfaceContext> rectContext = context->priv().makeWrappedSurfaceContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 189 | std::move(rectProxy), GrColorType::kRGBA_8888, kPremul_SkAlphaType); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 190 | SkASSERT(rectContext); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 191 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 192 | test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 193 | |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 194 | test_copy_to_surface(reporter, context, rectContext.get(), "RectangleTexture-copy-to"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 195 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 196 | test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write"); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 197 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 198 | test_clear(reporter, rectContext.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 199 | |
| 200 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 201 | } |
| 202 | } |