blob: 884ed88b44b0ab1e732a9c86413a991b91020b8c [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
Brian Salomon28a8f282019-10-24 20:07:39 -040040 FillPixelData(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
Robert Phillipscb1adb42019-06-10 15:09:34 -040041
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
Greg Daniel3912a4b2020-01-14 09:56:04 -050075 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(wrappedProxy->backendFormat(), grCT);
76 GrSurfaceProxyView view(std::move(wrappedProxy), kTopLeft_GrSurfaceOrigin, swizzle);
77 auto surfaceContext = GrSurfaceContext::Make(context, std::move(view), grCT,
Greg Danielbfa19c42019-12-19 16:41:40 -050078 kPremul_SkAlphaType, nullptr);
Brian Salomon6c1205a2019-06-14 11:49:03 -040079 REPORTER_ASSERT(reporter, surfaceContext);
80
Brian Salomon1d435302019-07-01 13:05:28 -040081 bool result = surfaceContext->readPixels({grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight},
82 actualPixels.writable_addr(), actualPixels.rowBytes(),
83 {0, 0}, context);
Timothy Liang760dbc42018-07-17 13:28:20 -040084
Timothy Liang760dbc42018-07-17 13:28:20 -040085 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040086 REPORTER_ASSERT(reporter,
87 DoesFullBufferContainCorrectColor(expectedPixels.addr32(),
88 actualPixels.addr32(), kWidth, kHeight));
Timothy Liang760dbc42018-07-17 13:28:20 -040089}
90
91DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040092 for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) {
93 for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) {
Timothy Liang760dbc42018-07-17 13:28:20 -040094 for (bool doDataUpload: {true, false}) {
95 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
96 renderable, doDataUpload, GrMipMapped::kNo);
97
98 if (!doDataUpload) {
99 testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
100 renderable, doDataUpload, GrMipMapped::kYes);
101 }
102 }
103 }
104 }
105}
106