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 | |
| 10 | #include "SkTypes.h" |
| 11 | |
Jim Van Verth | 9158931 | 2017-06-22 12:52:46 -0400 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU && defined(SK_VULKAN) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 13 | |
| 14 | #include "GrContextFactory.h" |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 15 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 16 | #include "GrProxyProvider.h" |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 17 | #include "GrSurfaceProxy.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | #include "GrTest.h" |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 19 | #include "SkGr.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 20 | #include "Test.h" |
| 21 | #include "vk/GrVkGpu.h" |
| 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 | |
| 25 | void fill_pixel_data(int width, int height, GrColor* data) { |
| 26 | |
| 27 | // build red-green gradient |
| 28 | for (int j = 0; j < height; ++j) { |
| 29 | for (int i = 0; i < width; ++i) { |
| 30 | unsigned int red = (unsigned int)(256.f*(i / (float)width)); |
| 31 | unsigned int green = (unsigned int)(256.f*(j / (float)height)); |
| 32 | data[i + j*width] = GrColorPackRGBA(red - (red>>8), green - (green>>8), 0xff, 0xff); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | bool does_full_buffer_contain_correct_color(GrColor* srcBuffer, |
| 38 | GrColor* dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 39 | int width, |
| 40 | int height) { |
| 41 | GrColor* srcPtr = srcBuffer; |
| 42 | GrColor* dstPtr = dstBuffer; |
| 43 | for (int j = 0; j < height; ++j) { |
| 44 | for (int i = 0; i < width; ++i) { |
| 45 | if (srcPtr[i] != dstPtr[i]) { |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | srcPtr += width; |
| 50 | dstPtr += width; |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config, |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 56 | bool renderTarget) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 57 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 58 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 59 | const int kWidth = 16; |
| 60 | const int kHeight = 16; |
| 61 | SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight); |
| 62 | SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight); |
| 63 | |
| 64 | fill_pixel_data(kWidth, kHeight, srcBuffer.get()); |
| 65 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 66 | GrSurfaceDesc surfDesc; |
| 67 | surfDesc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 68 | surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 69 | surfDesc.fWidth = kWidth; |
| 70 | surfDesc.fHeight = kHeight; |
| 71 | surfDesc.fConfig = config; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame^] | 72 | surfDesc.fSampleCnt = 1; |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 73 | |
| 74 | SkColorType ct; |
| 75 | SkAssertResult(GrPixelConfigToColorType(config, &ct)); |
| 76 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 77 | sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(surfDesc, SkBudgeted::kNo, |
| 78 | srcBuffer, 0); |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, proxy); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 80 | if (proxy) { |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 81 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(proxy); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 82 | |
| 83 | SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType); |
| 84 | |
| 85 | bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0); |
| 86 | REPORTER_ASSERT(reporter, result); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 88 | dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 89 | kWidth, |
| 90 | kHeight)); |
| 91 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 92 | dstInfo = SkImageInfo::Make(10, 2, ct, kOpaque_SkAlphaType); |
| 93 | result = sContext->writePixels(dstInfo, srcBuffer, 0, 2, 10); |
| 94 | REPORTER_ASSERT(reporter, result); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 95 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 96 | memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor)); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 97 | |
| 98 | result = sContext->readPixels(dstInfo, dstBuffer, 0, 2, 10); |
| 99 | REPORTER_ASSERT(reporter, result); |
| 100 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 102 | dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 103 | 10, |
| 104 | 2)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 108 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 109 | proxy = proxyProvider->createTextureProxy(surfDesc, SkBudgeted::kNo, srcBuffer, 0); |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 110 | REPORTER_ASSERT(reporter, proxy); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 111 | if (proxy) { |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 112 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(proxy); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 113 | |
| 114 | SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType); |
| 115 | |
| 116 | bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0); |
| 117 | REPORTER_ASSERT(reporter, result); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 119 | dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 120 | kWidth, |
| 121 | kHeight)); |
| 122 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 123 | dstInfo = SkImageInfo::Make(4, 5, ct, kOpaque_SkAlphaType); |
| 124 | result = sContext->writePixels(dstInfo, srcBuffer, 0, 5, 4); |
| 125 | REPORTER_ASSERT(reporter, result); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 126 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 127 | memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor)); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 128 | |
| 129 | result = sContext->readPixels(dstInfo, dstBuffer, 0, 5, 4); |
| 130 | REPORTER_ASSERT(reporter, result); |
| 131 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 133 | dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 134 | 4, |
| 135 | 5)); |
| 136 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 140 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkUploadPixelsTests, reporter, ctxInfo) { |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 141 | // RGBA |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 142 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false); |
| 143 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 144 | |
| 145 | // BGRA |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 146 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false); |
| 147 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | #endif |