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 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 12 | #include "GrClip.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 13 | #include "GrContext.h" |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 15 | #include "GrRenderTargetContext.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 16 | #include "GrTest.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 17 | #include "gl/GLTestContext.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 18 | #include "gl/GrGLGpu.h" |
| 19 | #include "gl/GrGLUtil.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 20 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 21 | // skbug.com/5932 |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 22 | static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 23 | sk_sp<GrTextureProxy> rectProxy, uint32_t expectedPixelValues[]) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 24 | sk_sp<GrRenderTargetContext> rtContext( |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 25 | context->makeDeferredRenderTargetContext(SkBackingFit::kExact, rectProxy->width(), |
| 26 | rectProxy->height(), rectProxy->config(), |
| 27 | nullptr)); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 28 | for (auto filter : {GrSamplerState::Filter::kNearest, |
| 29 | GrSamplerState::Filter::kBilerp, |
| 30 | GrSamplerState::Filter::kMipMap}) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 31 | rtContext->clear(nullptr, 0xDDCCBBAA, true); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 32 | auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 33 | GrPaint paint; |
| 34 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 35 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 36 | rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 37 | test_read_pixels(reporter, rtContext.get(), expectedPixelValues, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 38 | "RectangleTexture-basic-draw"); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 42 | static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) { |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 43 | if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 44 | // Clear the whole thing. |
| 45 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 46 | rtc->clear(nullptr, color0, false); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 47 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 48 | int w = rtc->width(); |
| 49 | int h = rtc->height(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 50 | int pixelCnt = w * h; |
| 51 | SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); |
| 52 | |
| 53 | // 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] | 54 | uint32_t expectedColor0 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 55 | uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0); |
| 56 | expectedBytes0[0] = GrColorUnpackR(color0); |
| 57 | expectedBytes0[1] = GrColorUnpackG(color0); |
| 58 | expectedBytes0[2] = GrColorUnpackB(color0); |
| 59 | expectedBytes0[3] = GrColorUnpackA(color0); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 60 | for (int i = 0; i < rtc->width() * rtc->height(); ++i) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 61 | expectedPixels.get()[i] = expectedColor0; |
| 62 | } |
| 63 | |
| 64 | // Clear the the top to a different color. |
| 65 | GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); |
| 66 | SkIRect rect = SkIRect::MakeWH(w, h/2); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 67 | rtc->clear(&rect, color1, false); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 68 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 69 | uint32_t expectedColor1 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 70 | uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1); |
| 71 | expectedBytes1[0] = GrColorUnpackR(color1); |
| 72 | expectedBytes1[1] = GrColorUnpackG(color1); |
| 73 | expectedBytes1[2] = GrColorUnpackB(color1); |
| 74 | expectedBytes1[3] = GrColorUnpackA(color1); |
| 75 | |
| 76 | for (int y = 0; y < h/2; ++y) { |
| 77 | for (int x = 0; x < w; ++x) { |
| 78 | expectedPixels.get()[y * h + x] = expectedColor1; |
| 79 | } |
| 80 | } |
| 81 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 82 | test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 86 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 87 | GrContext* context = ctxInfo.grContext(); |
| 88 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 89 | static const int kWidth = 13; |
| 90 | static const int kHeight = 13; |
| 91 | |
| 92 | GrColor pixels[kWidth * kHeight]; |
| 93 | for (int y = 0; y < kHeight; ++y) { |
| 94 | for (int x = 0; x < kWidth; ++x) { |
| 95 | pixels[y * kWidth + x] = y * kWidth + x; |
| 96 | } |
| 97 | } |
| 98 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 99 | for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) { |
| 100 | bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin; |
| 101 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 102 | GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, |
| 103 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, |
| 104 | pixels); |
| 105 | |
| 106 | if (!rectTexID) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Let GrContext know that we messed with the GL context directly. |
| 111 | context->resetContext(); |
| 112 | |
| 113 | // Wrap the rectangle texture ID in a GrTexture |
| 114 | GrGLTextureInfo rectangleInfo; |
| 115 | rectangleInfo.fID = rectTexID; |
| 116 | rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; |
| 117 | |
Greg Daniel | 207282e | 2017-04-26 13:29:21 -0400 | [diff] [blame] | 118 | GrBackendTexture rectangleTex(kWidth, kHeight, kRGBA_8888_GrPixelConfig, rectangleInfo); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 119 | |
| 120 | GrColor refPixels[kWidth * kHeight]; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 121 | for (int y = 0; y < kHeight; ++y) { |
| 122 | for (int x = 0; x < kWidth; ++x) { |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 123 | int y0 = useBLOrigin ? kHeight - y - 1 : y; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 124 | refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; |
| 125 | } |
| 126 | } |
| 127 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 128 | sk_sp<GrTextureProxy> rectProxy = GrSurfaceProxy::MakeWrappedBackend(context, |
| 129 | rectangleTex, |
| 130 | origin); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 131 | if (!rectProxy) { |
| 132 | ERRORF(reporter, "Error creating proxy for rectangle texture."); |
| 133 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 134 | continue; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 137 | test_basic_draw_as_src(reporter, context, rectProxy, refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 138 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 139 | // Test copy to both a texture and RT |
| 140 | test_copy_from_surface(reporter, context, rectProxy.get(), refPixels, |
| 141 | false, "RectangleTexture-copy-from"); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 142 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 143 | sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext( |
| 144 | std::move(rectProxy), nullptr); |
| 145 | SkASSERT(rectContext); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 146 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 147 | test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 148 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 149 | test_copy_to_surface(reporter, context->resourceProvider(), |
| 150 | rectContext.get(), "RectangleTexture-copy-to"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 151 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 152 | test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write"); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 153 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 154 | test_clear(reporter, rectContext.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 155 | |
| 156 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | #endif |