blob: c3313818731a9b6d53672dcc008df63b0f8b47fe [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"
10#include "src/gpu/GrGpu.h"
Brian Salomon6c1205a2019-06-14 11:49:03 -040011#include "src/gpu/GrSurfaceContext.h"
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040012#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "tests/Test.h"
14#include "tests/TestUtils.h"
Timothy Liang760dbc42018-07-17 13:28:20 -040015
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040016void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
17 GrRenderable renderable, bool doDataUpload, GrMipMapped mipMapped) {
18
Timothy Liang760dbc42018-07-17 13:28:20 -040019 const int kWidth = 16;
20 const int kHeight = 16;
Robert Phillipscb1adb42019-06-10 15:09:34 -040021
22 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
23
24 SkAutoPixmapStorage expectedPixels, actualPixels;
25 expectedPixels.alloc(ii);
26 actualPixels.alloc(ii);
Timothy Liang760dbc42018-07-17 13:28:20 -040027
Robert Phillips9b16f812019-05-17 10:01:21 -040028 const GrCaps* caps = context->priv().caps();
Timothy Liang760dbc42018-07-17 13:28:20 -040029
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040030 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Daniel00fb7242019-07-18 14:28:01 -040031
Robert Phillips0a15cc62019-07-30 12:49:10 -040032 GrBackendFormat backendFormat = context->defaultBackendFormat(ct, renderable);
33 if (!backendFormat.isValid()) {
Timothy Liang760dbc42018-07-17 13:28:20 -040034 return;
35 }
36
Robert Phillipscb1adb42019-06-10 15:09:34 -040037 GrBackendTexture backendTex;
38
39 if (doDataUpload) {
40 SkASSERT(GrMipMapped::kNo == mipMapped);
41
42 fill_pixel_data(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
43
Robert Phillipsda2e67a2019-07-01 15:04:06 -040044 backendTex = context->priv().createBackendTexture(&expectedPixels, 1,
45 renderable, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -040046 } else {
47 backendTex = context->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -040048 mipMapped, renderable, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -040049
50 size_t allocSize = SkAutoPixmapStorage::AllocSize(ii, nullptr);
51 // createBackendTexture will fill the texture with 0's if no data is provided, so
52 // we set the expected result likewise.
53 memset(expectedPixels.writable_addr32(0, 0), 0, allocSize);
54 }
Robert Phillips646f6372018-09-25 09:31:10 -040055 if (!backendTex.isValid()) {
56 return;
57 }
Brian Salomonf30b1c12019-06-20 12:25:02 -040058 // skbug.com/9165
59 auto supportedRead =
Greg Daniel00fb7242019-07-18 14:28:01 -040060 caps->supportedReadPixelsColorType(grCT, backendTex.getBackendFormat(), grCT);
Brian Salomon8f8354a2019-07-31 20:12:02 -040061 if (supportedRead.fColorType != grCT) {
Brian Salomonf30b1c12019-06-20 12:25:02 -040062 return;
63 }
Robert Phillips646f6372018-09-25 09:31:10 -040064
Brian Salomon6c1205a2019-06-14 11:49:03 -040065 sk_sp<GrTextureProxy> wrappedProxy;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040066 if (GrRenderable::kYes == renderable) {
Brian Salomon6c1205a2019-06-14 11:49:03 -040067 wrappedProxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillips0902c982019-07-16 07:47:56 -040068 backendTex, kTopLeft_GrSurfaceOrigin, 1, grCT, kAdopt_GrWrapOwnership,
Brian Salomon6c1205a2019-06-14 11:49:03 -040069 GrWrapCacheable::kNo);
Timothy Liang760dbc42018-07-17 13:28:20 -040070 } else {
Brian Salomon6c1205a2019-06-14 11:49:03 -040071 wrappedProxy = context->priv().proxyProvider()->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -040072 backendTex, grCT, kTopLeft_GrSurfaceOrigin, kAdopt_GrWrapOwnership,
73 GrWrapCacheable::kNo, GrIOType::kRW_GrIOType);
Timothy Liang760dbc42018-07-17 13:28:20 -040074 }
Brian Salomon6c1205a2019-06-14 11:49:03 -040075 REPORTER_ASSERT(reporter, wrappedProxy);
Timothy Liang760dbc42018-07-17 13:28:20 -040076
Brian Salomond6287472019-06-24 15:50:07 -040077 auto surfaceContext = context->priv().makeWrappedSurfaceContext(std::move(wrappedProxy), grCT,
Brian Salomone7499c72019-06-24 12:12:36 -040078 kPremul_SkAlphaType);
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);
Robert Phillipscb1adb42019-06-10 15:09:34 -040086 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(expectedPixels.addr32(),
87 actualPixels.addr32(),
Timothy Liang760dbc42018-07-17 13:28:20 -040088 kWidth, kHeight));
89}
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