blob: 979d05e14c037670b8c08abcfb7c29adb0657957 [file] [log] [blame]
Timothy Liang760dbc42018-07-17 13:28:20 -04001/*
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
17void 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,
Robert Phillips646f6372018-09-25 09:31:10 -040041 ct,
Timothy Liang760dbc42018-07-17 13:28:20 -040042 renderTarget,
43 mipMapped);
Robert Phillips646f6372018-09-25 09:31:10 -040044 if (!backendTex.isValid()) {
45 return;
46 }
47
Timothy Liang760dbc42018-07-17 13:28:20 -040048 sk_sp<GrTexture> wrappedTex;
49 if (renderTarget) {
50 wrappedTex = gpu->wrapRenderableBackendTexture(backendTex, 1,
51 GrWrapOwnership::kAdopt_GrWrapOwnership);
52 } else {
Brian Salomonff4ccaa2018-12-05 21:35:33 +000053 wrappedTex = gpu->wrapBackendTexture(backendTex,
54 GrWrapOwnership::kAdopt_GrWrapOwnership,
55 false);
Timothy Liang760dbc42018-07-17 13:28:20 -040056 }
57 REPORTER_ASSERT(reporter, wrappedTex);
58
59 int rowBytes = GrColorTypeBytesPerPixel(ct) * kWidth;
60 bool result = gpu->readPixels(wrappedTex.get(), 0, 0, kWidth,
61 kHeight, ct, dstBuffer, rowBytes);
62
63 if (!doDataUpload) {
64 // createTestingOnlyBackendTexture will fill the texture with 0's if no data is provided, so
65 // we set the expected result likewise.
66 srcBuffer.reset(kWidth * kHeight);
67 memset(srcBuffer, 0, kWidth * kHeight * sizeof(GrColor));
68 }
69 REPORTER_ASSERT(reporter, result);
70 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, dstBuffer,
71 kWidth, kHeight));
72}
73
74DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
Robert Phillips646f6372018-09-25 09:31:10 -040075 for (auto colorType: { GrColorType::kRGBA_8888, GrColorType::kBGRA_8888 }) {
Timothy Liang760dbc42018-07-17 13:28:20 -040076 for (bool renderable: {true, false}) {
77 for (bool doDataUpload: {true, false}) {
78 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
79 renderable, doDataUpload, GrMipMapped::kNo);
80
81 if (!doDataUpload) {
82 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
83 renderable, doDataUpload, GrMipMapped::kYes);
84 }
85 }
86 }
87 }
88}
89