blob: 8642c6a366b37e350d46d55b6c3f8fcfe289e871 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Timothy Liang760dbc42018-07-17 13:28:20 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrTexture.h"
11#include "src/core/SkConvertPixels.h"
12#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrGpu.h"
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040014#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
16#include "tests/TestUtils.h"
Timothy Liang760dbc42018-07-17 13:28:20 -040017
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040018void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
19 GrRenderable renderable, bool doDataUpload, GrMipMapped mipMapped) {
20
Timothy Liang760dbc42018-07-17 13:28:20 -040021 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 Phillips9b16f812019-05-17 10:01:21 -040030 const GrCaps* caps = context->priv().caps();
Robert Phillips9da87e02019-02-04 13:26:26 -050031 GrGpu* gpu = context->priv().getGpu();
Timothy Liang760dbc42018-07-17 13:28:20 -040032
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040033 GrPixelConfig config = SkColorType2GrPixelConfig(ct);
Robert Phillips9b16f812019-05-17 10:01:21 -040034 if (!caps->isConfigTexturable(config)) {
Timothy Liang760dbc42018-07-17 13:28:20 -040035 return;
36 }
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040037
38 GrColorType grCT = SkColorTypeToGrColorType(ct);
39 if (GrColorType::kUnknown == grCT) {
Timothy Liang760dbc42018-07-17 13:28:20 -040040 return;
41 }
42
Robert Phillips9b16f812019-05-17 10:01:21 -040043 if (caps->supportedReadPixelsColorType(config, grCT) != grCT) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040044 return;
45 }
46
47 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
48 kWidth, kHeight, ct,
49 mipMapped, renderable, srcBuffer);
Robert Phillips646f6372018-09-25 09:31:10 -040050 if (!backendTex.isValid()) {
51 return;
52 }
53
Timothy Liang760dbc42018-07-17 13:28:20 -040054 sk_sp<GrTexture> wrappedTex;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040055 if (GrRenderable::kYes == renderable) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050056 wrappedTex = gpu->wrapRenderableBackendTexture(
57 backendTex, 1, GrWrapOwnership::kAdopt_GrWrapOwnership, GrWrapCacheable::kNo);
Timothy Liang760dbc42018-07-17 13:28:20 -040058 } else {
Brian Salomonc67c31c2018-12-06 10:00:03 -050059 wrappedTex = gpu->wrapBackendTexture(backendTex, GrWrapOwnership::kAdopt_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050060 GrWrapCacheable::kNo, kRead_GrIOType);
Timothy Liang760dbc42018-07-17 13:28:20 -040061 }
62 REPORTER_ASSERT(reporter, wrappedTex);
63
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040064 int rowBytes = GrColorTypeBytesPerPixel(grCT) * kWidth;
Timothy Liang760dbc42018-07-17 13:28:20 -040065 bool result = gpu->readPixels(wrappedTex.get(), 0, 0, kWidth,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040066 kHeight, grCT, dstBuffer, rowBytes);
Timothy Liang760dbc42018-07-17 13:28:20 -040067
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
79DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040080 for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) {
81 for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) {
Timothy Liang760dbc42018-07-17 13:28:20 -040082 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