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