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