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