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