blob: 6229b2209e98fb5c41f9c34140017d629b5e71f2 [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"
Adlai Hollera0693042020-10-14 11:23:11 -040010#include "src/gpu/GrDirectContextPriv.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 Phillipsb25e0652020-07-22 09:35:49 -040017static void testing_only_texture_test(skiatest::Reporter* reporter, GrDirectContext* dContext,
18 SkColorType ct, GrRenderable renderable, bool doDataUpload,
19 GrMipmapped mipMapped) {
Timothy Liang760dbc42018-07-17 13:28:20 -040020 const int kWidth = 16;
21 const int kHeight = 16;
Robert Phillipscb1adb42019-06-10 15:09:34 -040022
23 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
24
25 SkAutoPixmapStorage expectedPixels, actualPixels;
26 expectedPixels.alloc(ii);
27 actualPixels.alloc(ii);
Timothy Liang760dbc42018-07-17 13:28:20 -040028
Robert Phillipsb25e0652020-07-22 09:35:49 -040029 const GrCaps* caps = dContext->priv().caps();
Timothy Liang760dbc42018-07-17 13:28:20 -040030
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040031 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Daniel00fb7242019-07-18 14:28:01 -040032
Robert Phillipsb25e0652020-07-22 09:35:49 -040033 GrBackendFormat backendFormat = dContext->defaultBackendFormat(ct, renderable);
Robert Phillips0a15cc62019-07-30 12:49:10 -040034 if (!backendFormat.isValid()) {
Timothy Liang760dbc42018-07-17 13:28:20 -040035 return;
36 }
37
Robert Phillipscb1adb42019-06-10 15:09:34 -040038 GrBackendTexture backendTex;
39
40 if (doDataUpload) {
Brian Salomon7e67dca2020-07-21 09:27:25 -040041 SkASSERT(GrMipmapped::kNo == mipMapped);
Robert Phillipscb1adb42019-06-10 15:09:34 -040042
Brian Salomon28a8f282019-10-24 20:07:39 -040043 FillPixelData(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
Robert Phillipscb1adb42019-06-10 15:09:34 -040044
Robert Phillipsb25e0652020-07-22 09:35:49 -040045 backendTex = dContext->createBackendTexture(&expectedPixels, 1,
46 renderable, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -040047 } else {
Robert Phillipsb25e0652020-07-22 09:35:49 -040048 backendTex = dContext->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent,
49 mipMapped, renderable, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -040050
51 size_t allocSize = SkAutoPixmapStorage::AllocSize(ii, nullptr);
52 // createBackendTexture will fill the texture with 0's if no data is provided, so
53 // we set the expected result likewise.
54 memset(expectedPixels.writable_addr32(0, 0), 0, allocSize);
55 }
Robert Phillips646f6372018-09-25 09:31:10 -040056 if (!backendTex.isValid()) {
57 return;
58 }
Brian Salomonf30b1c12019-06-20 12:25:02 -040059 // skbug.com/9165
60 auto supportedRead =
Greg Daniel00fb7242019-07-18 14:28:01 -040061 caps->supportedReadPixelsColorType(grCT, backendTex.getBackendFormat(), grCT);
Brian Salomon8f8354a2019-07-31 20:12:02 -040062 if (supportedRead.fColorType != grCT) {
Brian Salomonf30b1c12019-06-20 12:25:02 -040063 return;
64 }
Robert Phillips646f6372018-09-25 09:31:10 -040065
Brian Salomon6c1205a2019-06-14 11:49:03 -040066 sk_sp<GrTextureProxy> wrappedProxy;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040067 if (GrRenderable::kYes == renderable) {
Robert Phillipsb25e0652020-07-22 09:35:49 -040068 wrappedProxy = dContext->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillipsa1121332020-06-29 13:05:29 -040069 backendTex, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr);
Timothy Liang760dbc42018-07-17 13:28:20 -040070 } else {
Robert Phillipsb25e0652020-07-22 09:35:49 -040071 wrappedProxy = dContext->priv().proxyProvider()->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -040072 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, GrIOType::kRW_GrIOType);
Timothy Liang760dbc42018-07-17 13:28:20 -040073 }
Brian Salomon6c1205a2019-06-14 11:49:03 -040074 REPORTER_ASSERT(reporter, wrappedProxy);
Timothy Liang760dbc42018-07-17 13:28:20 -040075
Robert Phillipsb25e0652020-07-22 09:35:49 -040076 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(wrappedProxy->backendFormat(),
77 grCT);
Greg Daniel3912a4b2020-01-14 09:56:04 -050078 GrSurfaceProxyView view(std::move(wrappedProxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillipsb25e0652020-07-22 09:35:49 -040079 auto surfaceContext = GrSurfaceContext::Make(dContext, std::move(view), grCT,
Greg Danielbfa19c42019-12-19 16:41:40 -050080 kPremul_SkAlphaType, nullptr);
Brian Salomon6c1205a2019-06-14 11:49:03 -040081 REPORTER_ASSERT(reporter, surfaceContext);
82
Adlai Hollerc95b5892020-08-11 12:02:22 -040083 bool result = surfaceContext->readPixels(dContext,
84 {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight},
Brian Salomon1d435302019-07-01 13:05:28 -040085 actualPixels.writable_addr(), actualPixels.rowBytes(),
Adlai Hollerc95b5892020-08-11 12:02:22 -040086 {0, 0});
Timothy Liang760dbc42018-07-17 13:28:20 -040087
Timothy Liang760dbc42018-07-17 13:28:20 -040088 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040089 REPORTER_ASSERT(reporter,
90 DoesFullBufferContainCorrectColor(expectedPixels.addr32(),
91 actualPixels.addr32(), kWidth, kHeight));
Timothy Liang760dbc42018-07-17 13:28:20 -040092}
93
94DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040095 for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) {
96 for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) {
Timothy Liang760dbc42018-07-17 13:28:20 -040097 for (bool doDataUpload: {true, false}) {
Robert Phillips6d344c32020-07-06 10:56:46 -040098 testing_only_texture_test(reporter, ctxInfo.directContext(), colorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -040099 renderable, doDataUpload, GrMipmapped::kNo);
Timothy Liang760dbc42018-07-17 13:28:20 -0400100
101 if (!doDataUpload) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400102 testing_only_texture_test(reporter, ctxInfo.directContext(), colorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400103 renderable, doDataUpload, GrMipmapped::kYes);
Timothy Liang760dbc42018-07-17 13:28:20 -0400104 }
105 }
106 }
107 }
108}
109