Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 8 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 9 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrImageInfo.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrProxyProvider.h" |
| 13 | #include "src/gpu/GrSurfaceContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tests/Test.h" |
| 15 | #include "tests/TestUtils.h" |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 16 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 17 | void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct, |
| 18 | GrRenderable renderable, bool doDataUpload, GrMipMapped mipMapped) { |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 19 | auto direct = context->asDirectContext(); |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 20 | if (!direct) { |
| 21 | return; |
| 22 | } |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 23 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 24 | const int kWidth = 16; |
| 25 | const int kHeight = 16; |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 26 | |
| 27 | SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType); |
| 28 | |
| 29 | SkAutoPixmapStorage expectedPixels, actualPixels; |
| 30 | expectedPixels.alloc(ii); |
| 31 | actualPixels.alloc(ii); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 32 | |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 33 | const GrCaps* caps = direct->priv().caps(); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 34 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 35 | GrColorType grCT = SkColorTypeToGrColorType(ct); |
Greg Daniel | 00fb724 | 2019-07-18 14:28:01 -0400 | [diff] [blame] | 36 | |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 37 | GrBackendFormat backendFormat = direct->defaultBackendFormat(ct, renderable); |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 38 | if (!backendFormat.isValid()) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 39 | return; |
| 40 | } |
| 41 | |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 42 | GrBackendTexture backendTex; |
| 43 | |
| 44 | if (doDataUpload) { |
| 45 | SkASSERT(GrMipMapped::kNo == mipMapped); |
| 46 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 47 | FillPixelData(kWidth, kHeight, expectedPixels.writable_addr32(0, 0)); |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 48 | |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 49 | backendTex = direct->createBackendTexture(&expectedPixels, 1, |
| 50 | renderable, GrProtected::kNo); |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 51 | } else { |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 52 | backendTex = direct->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent, |
| 53 | mipMapped, renderable, GrProtected::kNo); |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 54 | |
| 55 | size_t allocSize = SkAutoPixmapStorage::AllocSize(ii, nullptr); |
| 56 | // createBackendTexture will fill the texture with 0's if no data is provided, so |
| 57 | // we set the expected result likewise. |
| 58 | memset(expectedPixels.writable_addr32(0, 0), 0, allocSize); |
| 59 | } |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 60 | if (!backendTex.isValid()) { |
| 61 | return; |
| 62 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 63 | // skbug.com/9165 |
| 64 | auto supportedRead = |
Greg Daniel | 00fb724 | 2019-07-18 14:28:01 -0400 | [diff] [blame] | 65 | caps->supportedReadPixelsColorType(grCT, backendTex.getBackendFormat(), grCT); |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 66 | if (supportedRead.fColorType != grCT) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 67 | return; |
| 68 | } |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 69 | |
Brian Salomon | 6c1205a | 2019-06-14 11:49:03 -0400 | [diff] [blame] | 70 | sk_sp<GrTextureProxy> wrappedProxy; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 71 | if (GrRenderable::kYes == renderable) { |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 72 | wrappedProxy = direct->priv().proxyProvider()->wrapRenderableBackendTexture( |
Robert Phillips | a112133 | 2020-06-29 13:05:29 -0400 | [diff] [blame] | 73 | backendTex, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 74 | } else { |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 75 | wrappedProxy = direct->priv().proxyProvider()->wrapBackendTexture( |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 76 | backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, GrIOType::kRW_GrIOType); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 77 | } |
Brian Salomon | 6c1205a | 2019-06-14 11:49:03 -0400 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, wrappedProxy); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 79 | |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 80 | GrSwizzle swizzle = direct->priv().caps()->getReadSwizzle(wrappedProxy->backendFormat(), grCT); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 81 | GrSurfaceProxyView view(std::move(wrappedProxy), kTopLeft_GrSurfaceOrigin, swizzle); |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 82 | auto surfaceContext = GrSurfaceContext::Make(direct, std::move(view), grCT, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 83 | kPremul_SkAlphaType, nullptr); |
Brian Salomon | 6c1205a | 2019-06-14 11:49:03 -0400 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, surfaceContext); |
| 85 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 86 | bool result = surfaceContext->readPixels({grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, |
| 87 | actualPixels.writable_addr(), actualPixels.rowBytes(), |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 88 | {0, 0}, direct); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 89 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 90 | REPORTER_ASSERT(reporter, result); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, |
| 92 | DoesFullBufferContainCorrectColor(expectedPixels.addr32(), |
| 93 | actualPixels.addr32(), kWidth, kHeight)); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 97 | for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) { |
| 98 | for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 99 | for (bool doDataUpload: {true, false}) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 100 | testing_only_texture_test(reporter, ctxInfo.directContext(), colorType, |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 101 | renderable, doDataUpload, GrMipMapped::kNo); |
| 102 | |
| 103 | if (!doDataUpload) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 104 | testing_only_texture_test(reporter, ctxInfo.directContext(), colorType, |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 105 | renderable, doDataUpload, GrMipMapped::kYes); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |