blob: e42c1c62e3071023361cd303367c1107fb5bb64c [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
Robert Phillips9da87e02019-02-04 13:26:26 -050028 GrGpu* gpu = context->priv().getGpu();
Timothy Liang760dbc42018-07-17 13:28:20 -040029
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) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050050 wrappedTex = gpu->wrapRenderableBackendTexture(
51 backendTex, 1, GrWrapOwnership::kAdopt_GrWrapOwnership, GrWrapCacheable::kNo);
Timothy Liang760dbc42018-07-17 13:28:20 -040052 } else {
Brian Salomonc67c31c2018-12-06 10:00:03 -050053 wrappedTex = gpu->wrapBackendTexture(backendTex, GrWrapOwnership::kAdopt_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050054 GrWrapCacheable::kNo, kRead_GrIOType);
Timothy Liang760dbc42018-07-17 13:28:20 -040055 }
56 REPORTER_ASSERT(reporter, wrappedTex);
57
58 int rowBytes = GrColorTypeBytesPerPixel(ct) * kWidth;
59 bool result = gpu->readPixels(wrappedTex.get(), 0, 0, kWidth,
60 kHeight, ct, dstBuffer, rowBytes);
61
62 if (!doDataUpload) {
63 // createTestingOnlyBackendTexture will fill the texture with 0's if no data is provided, so
64 // we set the expected result likewise.
65 srcBuffer.reset(kWidth * kHeight);
66 memset(srcBuffer, 0, kWidth * kHeight * sizeof(GrColor));
67 }
68 REPORTER_ASSERT(reporter, result);
69 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, dstBuffer,
70 kWidth, kHeight));
71}
72
73DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
Robert Phillips646f6372018-09-25 09:31:10 -040074 for (auto colorType: { GrColorType::kRGBA_8888, GrColorType::kBGRA_8888 }) {
Timothy Liang760dbc42018-07-17 13:28:20 -040075 for (bool renderable: {true, false}) {
76 for (bool doDataUpload: {true, false}) {
77 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
78 renderable, doDataUpload, GrMipMapped::kNo);
79
80 if (!doDataUpload) {
81 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
82 renderable, doDataUpload, GrMipMapped::kYes);
83 }
84 }
85 }
86 }
87}
88