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 | |
| 8 | #include "SkTypes.h" |
| 9 | |
| 10 | #include "GrGpu.h" |
| 11 | #include "GrContextPriv.h" |
| 12 | #include "GrTexture.h" |
| 13 | #include "SkConvertPixels.h" |
| 14 | #include "Test.h" |
| 15 | #include "TestUtils.h" |
| 16 | |
| 17 | void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, GrColorType ct, |
| 18 | bool renderTarget, bool doDataUpload, GrMipMapped mipMapped) { |
| 19 | const int kWidth = 16; |
| 20 | const int kHeight = 16; |
| 21 | SkAutoTMalloc<GrColor> srcBuffer; |
| 22 | if (doDataUpload) { |
| 23 | srcBuffer.reset(kWidth * kHeight); |
| 24 | fill_pixel_data(kWidth, kHeight, srcBuffer.get()); |
| 25 | } |
| 26 | SkAutoTMalloc<GrColor> dstBuffer(kWidth * kHeight); |
| 27 | |
| 28 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 29 | |
| 30 | GrPixelConfig config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo); |
| 31 | if (!gpu->caps()->isConfigTexturable(config)) { |
| 32 | return; |
| 33 | } |
| 34 | if (gpu->caps()->supportedReadPixelsColorType(config, ct) != ct) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(srcBuffer, |
| 39 | kWidth, |
| 40 | kHeight, |
| 41 | config, |
| 42 | renderTarget, |
| 43 | mipMapped); |
| 44 | sk_sp<GrTexture> wrappedTex; |
| 45 | if (renderTarget) { |
| 46 | wrappedTex = gpu->wrapRenderableBackendTexture(backendTex, 1, |
| 47 | GrWrapOwnership::kAdopt_GrWrapOwnership); |
| 48 | } else { |
| 49 | wrappedTex = gpu->wrapBackendTexture(backendTex, |
| 50 | GrWrapOwnership::kAdopt_GrWrapOwnership); |
| 51 | } |
| 52 | REPORTER_ASSERT(reporter, wrappedTex); |
| 53 | |
| 54 | int rowBytes = GrColorTypeBytesPerPixel(ct) * kWidth; |
| 55 | bool result = gpu->readPixels(wrappedTex.get(), 0, 0, kWidth, |
| 56 | kHeight, ct, dstBuffer, rowBytes); |
| 57 | |
| 58 | if (!doDataUpload) { |
| 59 | // createTestingOnlyBackendTexture will fill the texture with 0's if no data is provided, so |
| 60 | // we set the expected result likewise. |
| 61 | srcBuffer.reset(kWidth * kHeight); |
| 62 | memset(srcBuffer, 0, kWidth * kHeight * sizeof(GrColor)); |
| 63 | } |
| 64 | REPORTER_ASSERT(reporter, result); |
| 65 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, dstBuffer, |
| 66 | kWidth, kHeight)); |
| 67 | } |
| 68 | |
| 69 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) { |
| 70 | for (auto colorType: {GrColorType::kRGBA_8888, GrColorType::kBGRA_8888}) { |
| 71 | for (bool renderable: {true, false}) { |
| 72 | for (bool doDataUpload: {true, false}) { |
| 73 | testing_only_texture_test(reporter, ctxInfo.grContext(), colorType, |
| 74 | renderable, doDataUpload, GrMipMapped::kNo); |
| 75 | |
| 76 | if (!doDataUpload) { |
| 77 | testing_only_texture_test(reporter, ctxInfo.grContext(), colorType, |
| 78 | renderable, doDataUpload, GrMipMapped::kYes); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |