Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 11 | #include "GrClip.h" |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 12 | #include "GrContext.h" |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 14 | #include "GrRenderTargetContext.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 15 | #include "GrResourceProvider.h" |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 16 | #include "GrTexture.h" |
| 17 | #include "effects/GrSimpleTextureEffect.h" |
| 18 | |
| 19 | template <typename I> |
| 20 | static SK_WHEN(std::is_integral<I>::value && 4 == sizeof(I), void) |
Brian Osman | 07792b2 | 2017-01-04 14:11:36 -0500 | [diff] [blame] | 21 | check_pixels(skiatest::Reporter* reporter, int w, int h, const I exepctedData[], |
| 22 | const I actualData[], const char* testName) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 23 | for (int j = 0; j < h; ++j) { |
| 24 | for (int i = 0; i < w; ++i) { |
| 25 | I expected = exepctedData[j * w + i]; |
| 26 | I actual = actualData[j * w + i]; |
| 27 | if (expected != actual) { |
Brian Osman | 07792b2 | 2017-01-04 14:11:36 -0500 | [diff] [blame] | 28 | ERRORF(reporter, "[%s] Expected 0x08%x, got 0x%08x at %d, %d.", testName, expected, |
| 29 | actual, i, j); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 30 | return; |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(IntTexture, reporter, ctxInfo) { |
| 37 | GrContext* context = ctxInfo.grContext(); |
| 38 | if (!context->caps()->isConfigTexturable(kRGBA_8888_sint_GrPixelConfig)) { |
| 39 | return; |
| 40 | } |
| 41 | static const int kS = UINT8_MAX + 1; |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 42 | static const size_t kRowBytes = kS * sizeof(int32_t); |
| 43 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 44 | GrSurfaceDesc desc; |
| 45 | desc.fConfig = kRGBA_8888_sint_GrPixelConfig; |
| 46 | desc.fWidth = kS; |
| 47 | desc.fHeight = kS; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 48 | |
| 49 | std::unique_ptr<int32_t[]> testData(new int32_t[kS * kS]); |
| 50 | for (int j = 0; j < kS; ++j) { |
| 51 | for (int i = 0; i < kS; ++i) { |
| 52 | uint32_t r = i - INT8_MIN; |
| 53 | uint32_t g = j - INT8_MIN; |
| 54 | uint32_t b = INT8_MAX - r; |
| 55 | uint32_t a = INT8_MAX - g; |
| 56 | testData.get()[j * kS + i] = (a << 24) | (b << 16) | (g << 8) | r; |
| 57 | } |
| 58 | } |
| 59 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 60 | // Test that attempting to create a integer texture with multiple MIP levels fails. |
| 61 | { |
| 62 | GrMipLevel levels[2]; |
| 63 | levels[0].fPixels = testData.get(); |
| 64 | levels[0].fRowBytes = kRowBytes; |
| 65 | levels[1].fPixels = testData.get(); |
| 66 | levels[1].fRowBytes = (kS / 2) * sizeof(int32_t); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 67 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 68 | sk_sp<GrTextureProxy> temp(context->resourceProvider()->createMipMappedTexture( |
| 69 | desc, |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 70 | SkBudgeted::kYes, |
| 71 | levels, 2)); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 72 | REPORTER_ASSERT(reporter, !temp); |
| 73 | } |
| 74 | |
| 75 | // Test that we can create an integer texture. |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 76 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 77 | desc, SkBudgeted::kYes, |
| 78 | testData.get(), |
| 79 | kRowBytes); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, proxy); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 81 | if (!proxy) { |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 85 | std::unique_ptr<int32_t[]> readData(new int32_t[kS * kS]); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 86 | // Test that reading to a non-integer config fails. |
| 87 | { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 88 | bool success = context->contextPriv().readSurfacePixels(proxy.get(), nullptr, |
| 89 | 0, 0, kS, kS, |
| 90 | kRGBA_8888_GrPixelConfig, |
| 91 | nullptr, readData.get()); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 92 | REPORTER_ASSERT(reporter, !success); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 93 | } |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 94 | { |
| 95 | std::unique_ptr<uint16_t[]> halfData(new uint16_t[4 * kS * kS]); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 96 | bool success = context->contextPriv().readSurfacePixels(proxy.get(), nullptr, |
| 97 | 0, 0, kS, kS, |
| 98 | kRGBA_half_GrPixelConfig, |
| 99 | nullptr, halfData.get()); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, !success); |
| 101 | } |
| 102 | { |
| 103 | // Can read back as ints. (ES only requires being able to read back into 32bit ints which |
| 104 | // we don't support. Right now this test is counting on GR_RGBA_INTEGER/GL_BYTE being the |
| 105 | // implementation-dependent second format). |
| 106 | sk_bzero(readData.get(), sizeof(int32_t) * kS * kS); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 107 | bool success = context->contextPriv().readSurfacePixels(proxy.get(), nullptr, |
| 108 | 0, 0, kS, kS, |
| 109 | kRGBA_8888_sint_GrPixelConfig, |
| 110 | nullptr, readData.get()); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, success); |
| 112 | if (success) { |
| 113 | check_pixels(reporter, kS, kS, testData.get(), readData.get(), "readPixels"); |
| 114 | } |
| 115 | } |
| 116 | { |
| 117 | // readPixels should fail if we attempt to use the unpremul flag with an integer texture. |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 118 | bool success = context->contextPriv().readSurfacePixels( |
| 119 | proxy.get(), nullptr, |
| 120 | 0, 0, kS, kS, |
| 121 | kRGBA_8888_sint_GrPixelConfig, |
| 122 | nullptr, readData.get(), 0, |
| 123 | GrContextPriv::kUnpremul_PixelOpsFlag); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, !success); |
| 125 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 126 | |
| 127 | // Test that copying from one integer texture to another succeeds. |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 128 | { |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 129 | sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, desc, |
| 130 | proxy.get())); |
| 131 | REPORTER_ASSERT(reporter, dstContext); |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 132 | if (!dstContext || !dstContext->asTextureProxy()) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 136 | sk_bzero(readData.get(), sizeof(int32_t) * kS * kS); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 137 | bool success = context->contextPriv().readSurfacePixels(dstContext->asSurfaceProxy(), |
| 138 | nullptr, 0, 0, kS, kS, |
| 139 | kRGBA_8888_sint_GrPixelConfig, |
| 140 | nullptr, readData.get()); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 141 | REPORTER_ASSERT(reporter, success); |
| 142 | if (success) { |
Brian Osman | 07792b2 | 2017-01-04 14:11:36 -0500 | [diff] [blame] | 143 | check_pixels(reporter, kS, kS, testData.get(), readData.get(), "copyIntegerToInteger"); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 144 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 147 | |
| 148 | // Test that copying to a non-integer (8888) texture fails. |
| 149 | { |
| 150 | GrSurfaceDesc nonIntDesc = desc; |
| 151 | nonIntDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 152 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 153 | sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, nonIntDesc, |
| 154 | proxy.get())); |
| 155 | REPORTER_ASSERT(reporter, !dstContext); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 156 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 157 | |
| 158 | // Test that copying to a non-integer (RGBA_half) texture fails. |
| 159 | if (context->caps()->isConfigTexturable(kRGBA_half_GrPixelConfig)) { |
| 160 | GrSurfaceDesc nonIntDesc = desc; |
| 161 | nonIntDesc.fConfig = kRGBA_half_GrPixelConfig; |
| 162 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 163 | sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, nonIntDesc, |
| 164 | proxy.get())); |
| 165 | REPORTER_ASSERT(reporter, !dstContext); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // We overwrite the top left quarter of the texture with the bottom right quarter of the |
| 169 | // original data. |
| 170 | const void* bottomRightQuarter = testData.get() + kS / 2 * kS + kS / 2; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 171 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 172 | { |
| 173 | // Can't write pixels from a non-int config. |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 174 | bool success = context->contextPriv().writeSurfacePixels(proxy.get(), nullptr, |
| 175 | 0, 0, kS/2, kS/2, |
| 176 | kRGBA_8888_GrPixelConfig, nullptr, |
| 177 | bottomRightQuarter, kRowBytes); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 178 | REPORTER_ASSERT(reporter, !success); |
| 179 | } |
| 180 | { |
| 181 | // Can't use unpremul flag. |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 182 | bool success = context->contextPriv().writeSurfacePixels( |
| 183 | proxy.get(), nullptr, |
| 184 | 0, 0, kS/2, kS/2, |
| 185 | kRGBA_8888_sint_GrPixelConfig, |
| 186 | nullptr, |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 187 | bottomRightQuarter, kRowBytes, |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 188 | GrContextPriv::kUnpremul_PixelOpsFlag); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 189 | REPORTER_ASSERT(reporter, !success); |
| 190 | } |
| 191 | { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 192 | bool success = context->contextPriv().writeSurfacePixels(proxy.get(), nullptr, |
| 193 | 0, 0, kS/2, kS/2, |
| 194 | kRGBA_8888_sint_GrPixelConfig, |
| 195 | nullptr, |
| 196 | bottomRightQuarter, kRowBytes); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, success); |
| 198 | if (!success) { |
| 199 | return; |
| 200 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 201 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 202 | sk_bzero(readData.get(), sizeof(int32_t) * kS * kS); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 203 | success = context->contextPriv().readSurfacePixels(proxy.get(), nullptr, |
| 204 | 0, 0, kS, kS, |
| 205 | kRGBA_8888_sint_GrPixelConfig, |
| 206 | nullptr, readData.get(), 0); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 207 | REPORTER_ASSERT(reporter, success); |
| 208 | if (!success) { |
| 209 | return; |
| 210 | } |
| 211 | std::unique_ptr<int32_t[]> overwrittenTestData(new int32_t[kS * kS]); |
| 212 | memcpy(overwrittenTestData.get(), testData.get(), sizeof(int32_t) * kS * kS); |
| 213 | char* dst = (char*)overwrittenTestData.get(); |
| 214 | char* src = (char*)(testData.get() + kS/2 * kS + kS/2); |
| 215 | for (int i = 0; i < kS/2; ++i) { |
| 216 | memcpy(dst, src, sizeof(int32_t) * kS/2); |
| 217 | dst += kRowBytes; |
| 218 | src += kRowBytes; |
| 219 | } |
| 220 | check_pixels(reporter, kS, kS, overwrittenTestData.get(), readData.get(), "overwrite"); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 221 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 222 | |
| 223 | // Test drawing from the integer texture to a fixed point texture. To avoid any premul issues |
| 224 | // we init the int texture with 0s and 1s and make alpha always be 1. We expect that 1s turn |
| 225 | // into 0xffs and zeros stay zero. |
| 226 | std::unique_ptr<uint32_t[]> expectedData(new uint32_t[kS * kS]); |
| 227 | std::unique_ptr<uint32_t[]> actualData(new uint32_t[kS * kS]); |
| 228 | for (int i = 0; i < kS*kS; ++i) { |
| 229 | int32_t a = 0x1; |
| 230 | int32_t b = ((i & 0x1) ? 1 : 0); |
| 231 | int32_t g = ((i & 0x1) ? 0 : 1); |
| 232 | int32_t r = ((i & 0x2) ? 1 : 0); |
| 233 | testData.get()[i] = (a << 24) | (b << 16) | (g << 8) | r; |
| 234 | expectedData.get()[i] = ((0xFF * a) << 24) | ((0xFF * b) << 16) | |
| 235 | ((0xFF * g) << 8) | (0xFF * r); |
| 236 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 237 | context->contextPriv().writeSurfacePixels(proxy.get(), nullptr, |
| 238 | 0, 0, kS, kS, |
| 239 | kRGBA_8888_sint_GrPixelConfig, nullptr, |
| 240 | testData.get(), 0); |
Robert Phillips | 769e80d | 2017-03-29 13:06:57 -0400 | [diff] [blame] | 241 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 242 | sk_sp<GrRenderTargetContext> rtContext = context->makeRenderTargetContext( |
| 243 | SkBackingFit::kExact, kS, kS, kRGBA_8888_GrPixelConfig, nullptr); |
| 244 | |
Brian Osman | 07792b2 | 2017-01-04 14:11:36 -0500 | [diff] [blame] | 245 | struct { |
| 246 | GrSamplerParams::FilterMode fMode; |
| 247 | const char* fName; |
| 248 | } kNamedFilters[] ={ |
| 249 | { GrSamplerParams::kNone_FilterMode, "filter-none" }, |
| 250 | { GrSamplerParams::kBilerp_FilterMode, "filter-bilerp" }, |
| 251 | { GrSamplerParams::kMipMap_FilterMode, "filter-mipmap" } |
| 252 | }; |
| 253 | |
| 254 | for (auto filter : kNamedFilters) { |
Robert Phillips | 769e80d | 2017-03-29 13:06:57 -0400 | [diff] [blame] | 255 | sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(context->resourceProvider(), |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 256 | proxy, nullptr, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 257 | SkMatrix::I(), |
Brian Osman | 07792b2 | 2017-01-04 14:11:36 -0500 | [diff] [blame] | 258 | filter.fMode)); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 259 | REPORTER_ASSERT(reporter, fp); |
| 260 | if (!fp) { |
| 261 | return; |
| 262 | } |
| 263 | rtContext->clear(nullptr, 0xDDAABBCC, true); |
| 264 | GrPaint paint; |
| 265 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 266 | paint.addColorFragmentProcessor(fp); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 267 | rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 268 | SkImageInfo readInfo = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType, |
| 269 | kPremul_SkAlphaType); |
| 270 | rtContext->readPixels(readInfo, actualData.get(), 0, 0, 0); |
Brian Osman | 07792b2 | 2017-01-04 14:11:36 -0500 | [diff] [blame] | 271 | check_pixels(reporter, kS, kS, expectedData.get(), actualData.get(), filter.fName); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 274 | { |
| 275 | // No rendering to integer textures. |
| 276 | GrSurfaceDesc intRTDesc = desc; |
| 277 | intRTDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 278 | sk_sp<GrTexture> temp(context->resourceProvider()->createTexture(intRTDesc, |
| 279 | SkBudgeted::kYes)); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 280 | REPORTER_ASSERT(reporter, !temp); |
| 281 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | #endif |