Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [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 | // This is a GPU-backend specific test. It relies on static intializers to work |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrImageInfo.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrSurfaceContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrSurfaceProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/SkGr.h" |
| 18 | #include "tests/Test.h" |
| 19 | #include "tests/TestUtils.h" |
| 20 | #include "tools/gpu/GrContextFactory.h" |
| 21 | #include "tools/gpu/ProxyUtils.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | |
kkinnunen | 2ae4b2e | 2016-03-31 08:08:20 -0700 | [diff] [blame] | 23 | using sk_gpu_test::GrContextFactory; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 24 | |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 25 | void basic_texture_test(skiatest::Reporter* reporter, GrDirectContext* context, SkColorType ct, |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 26 | GrRenderable renderable) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | const int kWidth = 16; |
| 28 | const int kHeight = 16; |
| 29 | SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight); |
| 30 | SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight); |
| 31 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 32 | FillPixelData(kWidth, kHeight, srcBuffer.get()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 33 | |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 34 | auto grCT = SkColorTypeToGrColorType(ct); |
| 35 | auto proxy = sk_gpu_test::MakeTextureProxyFromData( |
| 36 | context, renderable, kTopLeft_GrSurfaceOrigin, |
| 37 | {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0); |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, proxy); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 39 | if (proxy) { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 40 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), grCT); |
| 41 | GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle); |
| 42 | auto sContext = GrSurfaceContext::Make(context, std::move(view), grCT, kPremul_SkAlphaType, |
| 43 | nullptr); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 44 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 45 | SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 46 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 47 | bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0}); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, result); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 49 | REPORTER_ASSERT(reporter, |
| 50 | DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 51 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 52 | dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 53 | result = sContext->writePixels(dstInfo, srcBuffer, 0, {2, 10}); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, result); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 55 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 56 | memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor)); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 57 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 58 | result = sContext->readPixels(dstInfo, dstBuffer, 0, {2, 10}); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, result); |
| 60 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 10, 2)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 64 | proxy = sk_gpu_test::MakeTextureProxyFromData( |
| 65 | context, renderable, kBottomLeft_GrSurfaceOrigin, |
| 66 | {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0); |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, proxy); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 68 | if (proxy) { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 69 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), grCT); |
| 70 | GrSurfaceProxyView view(proxy, kBottomLeft_GrSurfaceOrigin, swizzle); |
| 71 | auto sContext = GrSurfaceContext::Make(context, std::move(view), grCT, kPremul_SkAlphaType, |
| 72 | nullptr); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 73 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 74 | SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 75 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 76 | bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0}); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, result); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, |
| 79 | DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 80 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 81 | dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 82 | result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4}); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, result); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 84 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 85 | memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor)); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 86 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 87 | result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4}); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 88 | REPORTER_ASSERT(reporter, result); |
| 89 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 90 | REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 4, 5)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 91 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Timothy Liang | a8046af | 2018-07-19 09:58:00 -0400 | [diff] [blame] | 95 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) { |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 96 | auto direct = ctxInfo.directContext(); |
| 97 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 98 | // RGBA |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 99 | basic_texture_test(reporter, direct, kRGBA_8888_SkColorType, GrRenderable::kNo); |
| 100 | basic_texture_test(reporter, direct, kRGBA_8888_SkColorType, GrRenderable::kYes); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 101 | |
| 102 | // BGRA |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 103 | basic_texture_test(reporter, direct, kBGRA_8888_SkColorType, GrRenderable::kNo); |
| 104 | basic_texture_test(reporter, direct, kBGRA_8888_SkColorType, GrRenderable::kYes); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | } |