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 | |
| 8 | #include "Test.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 9 | #include "TestUtils.h" |
| 10 | |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 11 | #include "GrClip.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 12 | #include "GrContext.h" |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 15 | #include "GrRenderTargetContext.h" |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 16 | #include "GrSurfacePriv.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 17 | #include "GrTest.h" |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 18 | #include "GrTexturePriv.h" |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 19 | #include "GrTextureProxyPriv.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 20 | #include "gl/GLTestContext.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 21 | #include "gl/GrGLGpu.h" |
| 22 | #include "gl/GrGLUtil.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 |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 25 | static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 26 | sk_sp<GrTextureProxy> rectProxy, uint32_t expectedPixelValues[]) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 27 | sk_sp<GrRenderTargetContext> rtContext(context->contextPriv().makeDeferredRenderTargetContext( |
| 28 | SkBackingFit::kExact, rectProxy->width(), |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 29 | rectProxy->height(), rectProxy->config(), |
| 30 | 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}) { |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 34 | rtContext->clear(nullptr, 0xDDCCBBAA, GrRenderTargetContext::CanClearFullscreen::kYes); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 35 | auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 36 | GrPaint paint; |
| 37 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 38 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 39 | rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 40 | test_read_pixels(reporter, rtContext.get(), expectedPixelValues, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 41 | "RectangleTexture-basic-draw"); |
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); |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 49 | rtc->clear(nullptr, color0, GrRenderTargetContext::CanClearFullscreen::kNo); |
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); |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 70 | rtc->clear(&rect, color1, GrRenderTargetContext::CanClearFullscreen::kNo); |
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 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 85 | test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 89 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 90 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 91 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 92 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 93 | static const int kWidth = 13; |
| 94 | static const int kHeight = 13; |
| 95 | |
| 96 | GrColor pixels[kWidth * kHeight]; |
| 97 | for (int y = 0; y < kHeight; ++y) { |
| 98 | for (int x = 0; x < kWidth; ++x) { |
| 99 | pixels[y * kWidth + x] = y * kWidth + x; |
| 100 | } |
| 101 | } |
| 102 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 103 | for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) { |
| 104 | bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin; |
| 105 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 106 | GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, |
| 107 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, |
| 108 | pixels); |
| 109 | |
| 110 | if (!rectTexID) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | // Let GrContext know that we messed with the GL context directly. |
| 115 | context->resetContext(); |
| 116 | |
| 117 | // Wrap the rectangle texture ID in a GrTexture |
| 118 | GrGLTextureInfo rectangleInfo; |
| 119 | rectangleInfo.fID = rectTexID; |
| 120 | rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; |
| 121 | |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 122 | GrBackendTexture rectangleTex(kWidth, kHeight, GrMipMapped::kNo, rectangleInfo); |
| 123 | rectangleTex.setPixelConfig(kRGBA_8888_GrPixelConfig); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 124 | |
| 125 | GrColor refPixels[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) { |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 128 | int y0 = useBLOrigin ? kHeight - y - 1 : y; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 129 | refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; |
| 130 | } |
| 131 | } |
| 132 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 133 | sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(rectangleTex, origin); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 134 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 135 | if (!rectProxy) { |
| 136 | ERRORF(reporter, "Error creating proxy for rectangle texture."); |
| 137 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 138 | continue; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 141 | SkASSERT(rectProxy->mipMapped() == GrMipMapped::kNo); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 142 | SkASSERT(rectProxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 143 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 144 | SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle); |
| 145 | SkASSERT(rectProxy->peekTexture()->texturePriv().textureType() == |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 146 | GrTextureType::kRectangle); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 147 | SkASSERT(rectProxy->hasRestrictedSampling()); |
| 148 | SkASSERT(rectProxy->peekTexture()->texturePriv().hasRestrictedSampling()); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 149 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 150 | test_basic_draw_as_src(reporter, context, rectProxy, refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 151 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 152 | // Test copy to both a texture and RT |
| 153 | test_copy_from_surface(reporter, context, rectProxy.get(), refPixels, |
| 154 | false, "RectangleTexture-copy-from"); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 155 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 156 | sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext( |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 157 | std::move(rectProxy)); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 158 | SkASSERT(rectContext); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 159 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 160 | test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 161 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 162 | test_copy_to_surface(reporter, context->contextPriv().proxyProvider(), |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 163 | rectContext.get(), "RectangleTexture-copy-to"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 164 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 165 | test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write"); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 166 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 167 | test_clear(reporter, rectContext.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 168 | |
| 169 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 170 | } |
| 171 | } |