blob: d57ffd75d22a5c41b1a174c73eade517a4cc67d0 [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 Phillipsb7bfbc22020-07-01 12:55:01 -04008#include "include/gpu/GrDirectContext.h"
Robert Phillipscb1adb42019-06-10 15:09:34 -04009#include "src/core/SkAutoPixmapStorage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040011#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040012#include "src/gpu/GrProxyProvider.h"
13#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/Test.h"
15#include "tests/TestUtils.h"
Timothy Liang760dbc42018-07-17 13:28:20 -040016
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040017void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
Brian Salomon7e67dca2020-07-21 09:27:25 -040018 GrRenderable renderable, bool doDataUpload, GrMipmapped mipMapped) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -040019 auto direct = context->asDirectContext();
Robert Phillips44333c52020-06-30 13:28:00 -040020 if (!direct) {
21 return;
22 }
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040023
Timothy Liang760dbc42018-07-17 13:28:20 -040024 const int kWidth = 16;
25 const int kHeight = 16;
Robert Phillipscb1adb42019-06-10 15:09:34 -040026
27 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
28
29 SkAutoPixmapStorage expectedPixels, actualPixels;
30 expectedPixels.alloc(ii);
31 actualPixels.alloc(ii);
Timothy Liang760dbc42018-07-17 13:28:20 -040032
Robert Phillips44333c52020-06-30 13:28:00 -040033 const GrCaps* caps = direct->priv().caps();
Timothy Liang760dbc42018-07-17 13:28:20 -040034
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040035 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Daniel00fb7242019-07-18 14:28:01 -040036
Robert Phillips44333c52020-06-30 13:28:00 -040037 GrBackendFormat backendFormat = direct->defaultBackendFormat(ct, renderable);
Robert Phillips0a15cc62019-07-30 12:49:10 -040038 if (!backendFormat.isValid()) {
Timothy Liang760dbc42018-07-17 13:28:20 -040039 return;
40 }
41
Robert Phillipscb1adb42019-06-10 15:09:34 -040042 GrBackendTexture backendTex;
43
44 if (doDataUpload) {
Brian Salomon7e67dca2020-07-21 09:27:25 -040045 SkASSERT(GrMipmapped::kNo == mipMapped);
Robert Phillipscb1adb42019-06-10 15:09:34 -040046
Brian Salomon28a8f282019-10-24 20:07:39 -040047 FillPixelData(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
Robert Phillipscb1adb42019-06-10 15:09:34 -040048
Robert Phillips44333c52020-06-30 13:28:00 -040049 backendTex = direct->createBackendTexture(&expectedPixels, 1,
50 renderable, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -040051 } else {
Robert Phillips44333c52020-06-30 13:28:00 -040052 backendTex = direct->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent,
53 mipMapped, renderable, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -040054
55 size_t allocSize = SkAutoPixmapStorage::AllocSize(ii, nullptr);
56 // createBackendTexture will fill the texture with 0's if no data is provided, so
57 // we set the expected result likewise.
58 memset(expectedPixels.writable_addr32(0, 0), 0, allocSize);
59 }
Robert Phillips646f6372018-09-25 09:31:10 -040060 if (!backendTex.isValid()) {
61 return;
62 }
Brian Salomonf30b1c12019-06-20 12:25:02 -040063 // skbug.com/9165
64 auto supportedRead =
Greg Daniel00fb7242019-07-18 14:28:01 -040065 caps->supportedReadPixelsColorType(grCT, backendTex.getBackendFormat(), grCT);
Brian Salomon8f8354a2019-07-31 20:12:02 -040066 if (supportedRead.fColorType != grCT) {
Brian Salomonf30b1c12019-06-20 12:25:02 -040067 return;
68 }
Robert Phillips646f6372018-09-25 09:31:10 -040069
Brian Salomon6c1205a2019-06-14 11:49:03 -040070 sk_sp<GrTextureProxy> wrappedProxy;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040071 if (GrRenderable::kYes == renderable) {
Robert Phillips44333c52020-06-30 13:28:00 -040072 wrappedProxy = direct->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillipsa1121332020-06-29 13:05:29 -040073 backendTex, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr);
Timothy Liang760dbc42018-07-17 13:28:20 -040074 } else {
Robert Phillips44333c52020-06-30 13:28:00 -040075 wrappedProxy = direct->priv().proxyProvider()->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -040076 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, GrIOType::kRW_GrIOType);
Timothy Liang760dbc42018-07-17 13:28:20 -040077 }
Brian Salomon6c1205a2019-06-14 11:49:03 -040078 REPORTER_ASSERT(reporter, wrappedProxy);
Timothy Liang760dbc42018-07-17 13:28:20 -040079
Robert Phillips44333c52020-06-30 13:28:00 -040080 GrSwizzle swizzle = direct->priv().caps()->getReadSwizzle(wrappedProxy->backendFormat(), grCT);
Greg Daniel3912a4b2020-01-14 09:56:04 -050081 GrSurfaceProxyView view(std::move(wrappedProxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips44333c52020-06-30 13:28:00 -040082 auto surfaceContext = GrSurfaceContext::Make(direct, std::move(view), grCT,
Greg Danielbfa19c42019-12-19 16:41:40 -050083 kPremul_SkAlphaType, nullptr);
Brian Salomon6c1205a2019-06-14 11:49:03 -040084 REPORTER_ASSERT(reporter, surfaceContext);
85
Brian Salomon1d435302019-07-01 13:05:28 -040086 bool result = surfaceContext->readPixels({grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight},
87 actualPixels.writable_addr(), actualPixels.rowBytes(),
Robert Phillips44333c52020-06-30 13:28:00 -040088 {0, 0}, direct);
Timothy Liang760dbc42018-07-17 13:28:20 -040089
Timothy Liang760dbc42018-07-17 13:28:20 -040090 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040091 REPORTER_ASSERT(reporter,
92 DoesFullBufferContainCorrectColor(expectedPixels.addr32(),
93 actualPixels.addr32(), kWidth, kHeight));
Timothy Liang760dbc42018-07-17 13:28:20 -040094}
95
96DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040097 for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) {
98 for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) {
Timothy Liang760dbc42018-07-17 13:28:20 -040099 for (bool doDataUpload: {true, false}) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400100 testing_only_texture_test(reporter, ctxInfo.directContext(), colorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400101 renderable, doDataUpload, GrMipmapped::kNo);
Timothy Liang760dbc42018-07-17 13:28:20 -0400102
103 if (!doDataUpload) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400104 testing_only_texture_test(reporter, ctxInfo.directContext(), colorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400105 renderable, doDataUpload, GrMipmapped::kYes);
Timothy Liang760dbc42018-07-17 13:28:20 -0400106 }
107 }
108 }
109 }
110}
111