blob: 8cd1008ce0ba70d89f0583ea67677e6046c10449 [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
Robert Phillipscb1adb42019-06-10 15:09:34 -04008#include "src/core/SkAutoPixmapStorage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040010#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "tests/Test.h"
12#include "tests/TestUtils.h"
Timothy Liang760dbc42018-07-17 13:28:20 -040013
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040014void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
15 GrRenderable renderable, bool doDataUpload, GrMipMapped mipMapped) {
16
Timothy Liang760dbc42018-07-17 13:28:20 -040017 const int kWidth = 16;
18 const int kHeight = 16;
Robert Phillipscb1adb42019-06-10 15:09:34 -040019
20 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
21
22 SkAutoPixmapStorage expectedPixels, actualPixels;
23 expectedPixels.alloc(ii);
24 actualPixels.alloc(ii);
Timothy Liang760dbc42018-07-17 13:28:20 -040025
Robert Phillips9b16f812019-05-17 10:01:21 -040026 const GrCaps* caps = context->priv().caps();
Timothy Liang760dbc42018-07-17 13:28:20 -040027
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040028 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Daniel00fb7242019-07-18 14:28:01 -040029
Robert Phillips0a15cc62019-07-30 12:49:10 -040030 GrBackendFormat backendFormat = context->defaultBackendFormat(ct, renderable);
31 if (!backendFormat.isValid()) {
Timothy Liang760dbc42018-07-17 13:28:20 -040032 return;
33 }
34
Robert Phillipscb1adb42019-06-10 15:09:34 -040035 GrBackendTexture backendTex;
36
37 if (doDataUpload) {
38 SkASSERT(GrMipMapped::kNo == mipMapped);
39
40 fill_pixel_data(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
41
Robert Phillips66944402019-09-30 13:21:25 -040042 backendTex = context->createBackendTexture(&expectedPixels, 1,
43 renderable, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -040044 } else {
45 backendTex = context->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -040046 mipMapped, renderable, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -040047
48 size_t allocSize = SkAutoPixmapStorage::AllocSize(ii, nullptr);
49 // createBackendTexture will fill the texture with 0's if no data is provided, so
50 // we set the expected result likewise.
51 memset(expectedPixels.writable_addr32(0, 0), 0, allocSize);
52 }
Robert Phillips646f6372018-09-25 09:31:10 -040053 if (!backendTex.isValid()) {
54 return;
55 }
Brian Salomonf30b1c12019-06-20 12:25:02 -040056 // skbug.com/9165
57 auto supportedRead =
Greg Daniel00fb7242019-07-18 14:28:01 -040058 caps->supportedReadPixelsColorType(grCT, backendTex.getBackendFormat(), grCT);
Brian Salomon8f8354a2019-07-31 20:12:02 -040059 if (supportedRead.fColorType != grCT) {
Brian Salomonf30b1c12019-06-20 12:25:02 -040060 return;
61 }
Robert Phillips646f6372018-09-25 09:31:10 -040062
Brian Salomon6c1205a2019-06-14 11:49:03 -040063 sk_sp<GrTextureProxy> wrappedProxy;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040064 if (GrRenderable::kYes == renderable) {
Brian Salomon6c1205a2019-06-14 11:49:03 -040065 wrappedProxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillips0902c982019-07-16 07:47:56 -040066 backendTex, kTopLeft_GrSurfaceOrigin, 1, grCT, kAdopt_GrWrapOwnership,
Brian Salomon6c1205a2019-06-14 11:49:03 -040067 GrWrapCacheable::kNo);
Timothy Liang760dbc42018-07-17 13:28:20 -040068 } else {
Brian Salomon6c1205a2019-06-14 11:49:03 -040069 wrappedProxy = context->priv().proxyProvider()->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -040070 backendTex, grCT, kTopLeft_GrSurfaceOrigin, kAdopt_GrWrapOwnership,
71 GrWrapCacheable::kNo, GrIOType::kRW_GrIOType);
Timothy Liang760dbc42018-07-17 13:28:20 -040072 }
Brian Salomon6c1205a2019-06-14 11:49:03 -040073 REPORTER_ASSERT(reporter, wrappedProxy);
Timothy Liang760dbc42018-07-17 13:28:20 -040074
Brian Salomond6287472019-06-24 15:50:07 -040075 auto surfaceContext = context->priv().makeWrappedSurfaceContext(std::move(wrappedProxy), grCT,
Brian Salomone7499c72019-06-24 12:12:36 -040076 kPremul_SkAlphaType);
Brian Salomon6c1205a2019-06-14 11:49:03 -040077 REPORTER_ASSERT(reporter, surfaceContext);
78
Brian Salomon1d435302019-07-01 13:05:28 -040079 bool result = surfaceContext->readPixels({grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight},
80 actualPixels.writable_addr(), actualPixels.rowBytes(),
81 {0, 0}, context);
Timothy Liang760dbc42018-07-17 13:28:20 -040082
Timothy Liang760dbc42018-07-17 13:28:20 -040083 REPORTER_ASSERT(reporter, result);
Robert Phillipscb1adb42019-06-10 15:09:34 -040084 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(expectedPixels.addr32(),
85 actualPixels.addr32(),
Timothy Liang760dbc42018-07-17 13:28:20 -040086 kWidth, kHeight));
87}
88
89DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040090 for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) {
91 for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) {
Timothy Liang760dbc42018-07-17 13:28:20 -040092 for (bool doDataUpload: {true, false}) {
93 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
94 renderable, doDataUpload, GrMipMapped::kNo);
95
96 if (!doDataUpload) {
97 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
98 renderable, doDataUpload, GrMipMapped::kYes);
99 }
100 }
101 }
102 }
103}
104