Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | |
| 14 | #include "GrContextFactory.h" |
| 15 | #include "GrContextPriv.h" |
| 16 | #include "GrGpu.h" |
| 17 | #include "GrResourceProvider.h" |
| 18 | #include "GrSurfaceProxy.h" |
| 19 | #include "GrTexture.h" |
| 20 | #include "GrTest.h" |
| 21 | #include "SkGr.h" |
| 22 | #include "SkSurface.h" |
| 23 | #include "Test.h" |
| 24 | |
| 25 | using sk_gpu_test::GrContextFactory; |
| 26 | |
| 27 | void fill_transfer_data(int left, int top, int width, int height, int bufferWidth, |
| 28 | GrColor* data) { |
| 29 | |
| 30 | // build red-green gradient |
| 31 | for (int j = top; j < top + height; ++j) { |
| 32 | for (int i = left; i < left + width; ++i) { |
| 33 | unsigned int red = (unsigned int)(256.f*((i - left) / (float)width)); |
| 34 | unsigned int green = (unsigned int)(256.f*((j - top) / (float)height)); |
| 35 | data[i + j*bufferWidth] = GrColorPackRGBA(red - (red>>8), |
| 36 | green - (green>>8), 0xff, 0xff); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | bool does_full_buffer_contain_correct_values(GrColor* srcBuffer, |
| 42 | GrColor* dstBuffer, |
| 43 | int width, |
| 44 | int height, |
| 45 | int bufferWidth, |
| 46 | int bufferHeight, |
| 47 | GrSurfaceOrigin origin) { |
| 48 | GrColor* srcPtr = srcBuffer; |
| 49 | bool bottomUp = SkToBool(kBottomLeft_GrSurfaceOrigin == origin); |
| 50 | GrColor* dstPtr = bottomUp ? dstBuffer + bufferWidth*(bufferHeight-1) : dstBuffer; |
| 51 | int dstIncrement = bottomUp ? -bufferWidth : +bufferWidth; |
| 52 | |
| 53 | for (int j = 0; j < height; ++j) { |
| 54 | for (int i = 0; i < width; ++i) { |
| 55 | if (srcPtr[i] != dstPtr[i]) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | srcPtr += bufferWidth; |
| 60 | dstPtr += dstIncrement; |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 65 | void basic_transfer_test(skiatest::Reporter* reporter, GrContext* context, GrColorType colorType, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 66 | GrSurfaceOrigin origin, bool renderTarget) { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame^] | 67 | if (GrCaps::kNone_MapFlags == context->contextPriv().caps()->mapBufferFlags()) { |
Brian Salomon | 7f56d3d | 2017-10-09 13:02:49 -0400 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 71 | auto resourceProvider = context->contextPriv().resourceProvider(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 72 | GrGpu* gpu = context->contextPriv().getGpu(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 73 | |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 74 | // set up the data |
| 75 | const int kTextureWidth = 16; |
| 76 | const int kTextureHeight = 16; |
| 77 | const int kBufferWidth = 20; |
| 78 | const int kBufferHeight = 16; |
| 79 | size_t rowBytes = kBufferWidth * sizeof(GrColor); |
| 80 | SkAutoTMalloc<GrColor> srcBuffer(kBufferWidth*kBufferHeight); |
| 81 | SkAutoTMalloc<GrColor> dstBuffer(kBufferWidth*kBufferHeight); |
| 82 | |
| 83 | fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, kBufferWidth, srcBuffer.get()); |
| 84 | |
| 85 | // create and fill transfer buffer |
| 86 | size_t size = rowBytes*kBufferHeight; |
| 87 | uint32_t bufferFlags = GrResourceProvider::kNoPendingIO_Flag; |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 88 | sk_sp<GrBuffer> buffer(resourceProvider->createBuffer(size, |
| 89 | kXferCpuToGpu_GrBufferType, |
| 90 | kDynamic_GrAccessPattern, |
| 91 | bufferFlags)); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 92 | if (!buffer) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | void* data = buffer->map(); |
| 97 | memcpy(data, srcBuffer.get(), size); |
| 98 | buffer->unmap(); |
| 99 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 100 | for (auto srgbEncoding : {GrSRGBEncoded::kNo, GrSRGBEncoded::kYes}) { |
| 101 | // create texture |
| 102 | GrSurfaceDesc desc; |
| 103 | desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 104 | desc.fWidth = kTextureWidth; |
| 105 | desc.fHeight = kTextureHeight; |
| 106 | desc.fConfig = GrColorTypeToPixelConfig(colorType, srgbEncoding); |
| 107 | desc.fSampleCnt = 1; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 108 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 109 | if (kUnknown_GrPixelConfig == desc.fConfig) { |
| 110 | SkASSERT(GrSRGBEncoded::kYes == srgbEncoding); |
| 111 | continue; |
| 112 | } |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 113 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame^] | 114 | if (!context->contextPriv().caps()->isConfigTexturable(desc.fConfig) || |
| 115 | (renderTarget && !context->contextPriv().caps()->isConfigRenderable(desc.fConfig))) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 116 | continue; |
| 117 | } |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 118 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 119 | sk_sp<GrTexture> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 120 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 121 | ////////////////////////// |
| 122 | // transfer full data |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 123 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 124 | bool result; |
| 125 | result = gpu->transferPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType, |
| 126 | buffer.get(), 0, rowBytes); |
| 127 | REPORTER_ASSERT(reporter, result); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 128 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 129 | memset(dstBuffer.get(), 0xCDCD, size); |
| 130 | result = gpu->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight, colorType, |
| 131 | dstBuffer.get(), rowBytes); |
| 132 | if (result) { |
| 133 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer, |
| 134 | dstBuffer, |
| 135 | kTextureWidth, |
| 136 | kTextureHeight, |
| 137 | kBufferWidth, |
| 138 | kBufferHeight, |
| 139 | origin)); |
| 140 | } |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 141 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 142 | ////////////////////////// |
| 143 | // transfer partial data |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 144 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 145 | const int kLeft = 2; |
| 146 | const int kTop = 10; |
| 147 | const int kWidth = 10; |
| 148 | const int kHeight = 2; |
| 149 | |
| 150 | // change color of subrectangle |
| 151 | fill_transfer_data(kLeft, kTop, kWidth, kHeight, kBufferWidth, srcBuffer.get()); |
| 152 | data = buffer->map(); |
| 153 | memcpy(data, srcBuffer.get(), size); |
| 154 | buffer->unmap(); |
| 155 | |
| 156 | size_t offset = sizeof(GrColor) * (kTop * kBufferWidth + kLeft); |
| 157 | result = gpu->transferPixels(tex.get(), kLeft, kTop, kWidth, kHeight, colorType, |
| 158 | buffer.get(), offset, rowBytes); |
| 159 | REPORTER_ASSERT(reporter, result); |
| 160 | |
| 161 | memset(dstBuffer.get(), 0xCDCD, size); |
| 162 | result = gpu->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight, colorType, |
| 163 | dstBuffer.get(), rowBytes); |
| 164 | if (result) { |
| 165 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer, |
| 166 | dstBuffer, |
| 167 | kTextureWidth, |
| 168 | kTextureHeight, |
| 169 | kBufferWidth, |
| 170 | kBufferHeight, |
| 171 | origin)); |
| 172 | } |
Jim Van Verth | 52fb02e | 2017-06-27 10:36:56 -0400 | [diff] [blame] | 173 | } |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest, reporter, ctxInfo) { |
| 177 | // RGBA |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 178 | basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 179 | kTopLeft_GrSurfaceOrigin, false); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 180 | basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 181 | kTopLeft_GrSurfaceOrigin, true); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 182 | basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 183 | kBottomLeft_GrSurfaceOrigin, false); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 184 | basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 185 | kBottomLeft_GrSurfaceOrigin, true); |
| 186 | |
| 187 | // BGRA |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 188 | basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 189 | kTopLeft_GrSurfaceOrigin, false); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 190 | basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 191 | kTopLeft_GrSurfaceOrigin, true); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 192 | basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 193 | kBottomLeft_GrSurfaceOrigin, false); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 194 | basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 195 | kBottomLeft_GrSurfaceOrigin, true); |
| 196 | } |
| 197 | |
| 198 | #endif |