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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrTexture.h" |
| 11 | #include "src/core/SkConvertPixels.h" |
| 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrGpu.h" |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 14 | #include "src/gpu/SkGr.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "tests/Test.h" |
| 16 | #include "tests/TestUtils.h" |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 17 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 18 | void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct, |
| 19 | GrRenderable renderable, bool doDataUpload, GrMipMapped mipMapped) { |
| 20 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 21 | const int kWidth = 16; |
| 22 | const int kHeight = 16; |
| 23 | SkAutoTMalloc<GrColor> srcBuffer; |
| 24 | if (doDataUpload) { |
| 25 | srcBuffer.reset(kWidth * kHeight); |
| 26 | fill_pixel_data(kWidth, kHeight, srcBuffer.get()); |
| 27 | } |
| 28 | SkAutoTMalloc<GrColor> dstBuffer(kWidth * kHeight); |
| 29 | |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 30 | const GrCaps* caps = context->priv().caps(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 31 | GrGpu* gpu = context->priv().getGpu(); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 32 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 33 | GrPixelConfig config = SkColorType2GrPixelConfig(ct); |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 34 | if (!caps->isConfigTexturable(config)) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 35 | return; |
| 36 | } |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 37 | |
| 38 | GrColorType grCT = SkColorTypeToGrColorType(ct); |
| 39 | if (GrColorType::kUnknown == grCT) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 43 | if (caps->supportedReadPixelsColorType(config, grCT) != grCT) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | |
| 47 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture( |
| 48 | kWidth, kHeight, ct, |
| 49 | mipMapped, renderable, srcBuffer); |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 50 | if (!backendTex.isValid()) { |
| 51 | return; |
| 52 | } |
| 53 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 54 | sk_sp<GrTexture> wrappedTex; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 55 | if (GrRenderable::kYes == renderable) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 56 | wrappedTex = gpu->wrapRenderableBackendTexture( |
| 57 | backendTex, 1, GrWrapOwnership::kAdopt_GrWrapOwnership, GrWrapCacheable::kNo); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 58 | } else { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 59 | wrappedTex = gpu->wrapBackendTexture(backendTex, GrWrapOwnership::kAdopt_GrWrapOwnership, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 60 | GrWrapCacheable::kNo, kRead_GrIOType); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 61 | } |
| 62 | REPORTER_ASSERT(reporter, wrappedTex); |
| 63 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 64 | int rowBytes = GrColorTypeBytesPerPixel(grCT) * kWidth; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 65 | bool result = gpu->readPixels(wrappedTex.get(), 0, 0, kWidth, |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 66 | kHeight, grCT, dstBuffer, rowBytes); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 67 | |
| 68 | if (!doDataUpload) { |
| 69 | // createTestingOnlyBackendTexture will fill the texture with 0's if no data is provided, so |
| 70 | // we set the expected result likewise. |
| 71 | srcBuffer.reset(kWidth * kHeight); |
| 72 | memset(srcBuffer, 0, kWidth * kHeight * sizeof(GrColor)); |
| 73 | } |
| 74 | REPORTER_ASSERT(reporter, result); |
| 75 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, dstBuffer, |
| 76 | kWidth, kHeight)); |
| 77 | } |
| 78 | |
| 79 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 80 | for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) { |
| 81 | for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 82 | for (bool doDataUpload: {true, false}) { |
| 83 | testing_only_texture_test(reporter, ctxInfo.grContext(), colorType, |
| 84 | renderable, doDataUpload, GrMipMapped::kNo); |
| 85 | |
| 86 | if (!doDataUpload) { |
| 87 | testing_only_texture_test(reporter, ctxInfo.grContext(), colorType, |
| 88 | renderable, doDataUpload, GrMipMapped::kYes); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |