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" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 16 | #include "gl/GLTestContext.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 17 | #include "gl/GrGLGpu.h" |
| 18 | #include "gl/GrGLUtil.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 19 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 20 | // skbug.com/5932 |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 21 | static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context, |
| 22 | sk_sp<GrSurfaceProxy> rectProxy, uint32_t expectedPixelValues[]) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 23 | sk_sp<GrRenderTargetContext> rtContext( |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 24 | context->makeRenderTargetContext(SkBackingFit::kExact, rectProxy->width(), |
| 25 | rectProxy->height(), rectProxy->config(), |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 26 | nullptr)); |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 27 | for (auto filter : {GrSamplerParams::kNone_FilterMode, |
| 28 | GrSamplerParams::kBilerp_FilterMode, |
| 29 | GrSamplerParams::kMipMap_FilterMode}) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 30 | rtContext->clear(nullptr, 0xDDCCBBAA, true); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 31 | sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make( |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame^] | 32 | context->resourceProvider(), |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 33 | sk_ref_sp(rectProxy->asTextureProxy()), |
| 34 | nullptr, |
| 35 | 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); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 49 | rtc->clear(nullptr, color0, false); |
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; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 58 | uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0); |
| 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); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 70 | rtc->clear(&rect, color1, false); |
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; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 73 | uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1); |
| 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(); |
| 91 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 92 | static const int kWidth = 13; |
| 93 | static const int kHeight = 13; |
| 94 | |
| 95 | GrColor pixels[kWidth * kHeight]; |
| 96 | for (int y = 0; y < kHeight; ++y) { |
| 97 | for (int x = 0; x < kWidth; ++x) { |
| 98 | pixels[y * kWidth + x] = y * kWidth + x; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | for (int origin = 0; origin < 2; ++origin) { |
| 103 | GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, |
| 104 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, |
| 105 | pixels); |
| 106 | |
| 107 | if (!rectTexID) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Let GrContext know that we messed with the GL context directly. |
| 112 | context->resetContext(); |
| 113 | |
| 114 | // Wrap the rectangle texture ID in a GrTexture |
| 115 | GrGLTextureInfo rectangleInfo; |
| 116 | rectangleInfo.fID = rectTexID; |
| 117 | rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; |
| 118 | |
| 119 | GrBackendTextureDesc rectangleDesc; |
| 120 | rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 121 | rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 122 | rectangleDesc.fWidth = kWidth; |
| 123 | rectangleDesc.fHeight = kHeight; |
| 124 | rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 125 | rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo); |
| 126 | |
| 127 | GrColor refPixels[kWidth * kHeight]; |
| 128 | bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin; |
| 129 | for (int y = 0; y < kHeight; ++y) { |
| 130 | for (int x = 0; x < kWidth; ++x) { |
| 131 | int y0 = flipRef ? kHeight - y - 1 : y; |
| 132 | refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; |
| 133 | } |
| 134 | } |
| 135 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 136 | sk_sp<GrSurfaceProxy> rectProxy = GrSurfaceProxy::MakeWrappedBackend(context, |
| 137 | rectangleDesc); |
| 138 | if (!rectProxy) { |
| 139 | ERRORF(reporter, "Error creating proxy for rectangle texture."); |
| 140 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 141 | continue; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 144 | test_basic_draw_as_src(reporter, context, rectProxy, refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 145 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 146 | // Test copy to both a texture and RT |
| 147 | test_copy_from_surface(reporter, context, rectProxy.get(), refPixels, |
| 148 | false, "RectangleTexture-copy-from"); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 149 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 150 | sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext( |
| 151 | std::move(rectProxy), nullptr); |
| 152 | SkASSERT(rectContext); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 153 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 154 | test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 155 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 156 | test_copy_to_surface(reporter, context->resourceProvider(), |
| 157 | rectContext.get(), "RectangleTexture-copy-to"); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 158 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 159 | test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write"); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 160 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 161 | test_clear(reporter, rectContext.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 162 | |
| 163 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | #endif |