blob: 3b95318d32b71a84bcd1fc0675b5a7441b080780 [file] [log] [blame]
Robert Phillipseaa86252016-11-08 13:49:39 +00001/*
2 * Copyright 2016 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
8// This is a GPU-backend specific test.
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
Robert Phillipseaa86252016-11-08 13:49:39 +000011
Robert Phillips6d344c32020-07-06 10:56:46 -040012#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040013#include "src/gpu/GrDirectContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040014#include "src/gpu/GrImageInfo.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrTextureProxy.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040016#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Robert Phillipseaa86252016-11-08 13:49:39 +000017
18static const int kSize = 64;
19
Robert Phillips4dca8312021-07-28 15:13:20 -040020static std::unique_ptr<skgpu::v1::SurfaceDrawContext> get_sdc(GrRecordingContext* rContext) {
21 return skgpu::v1::SurfaceDrawContext::Make(rContext, GrColorType::kRGBA_8888, nullptr,
22 SkBackingFit::kExact, {kSize, kSize},
23 SkSurfaceProps());
Robert Phillipseaa86252016-11-08 13:49:39 +000024}
25
Robert Phillipsb5204762019-06-19 14:12:13 -040026static void check_instantiation_status(skiatest::Reporter* reporter,
Robert Phillips4dca8312021-07-28 15:13:20 -040027 skgpu::v1::SurfaceDrawContext* sdc,
Robert Phillipsb5204762019-06-19 14:12:13 -040028 bool wrappedExpectation) {
Robert Phillips4dca8312021-07-28 15:13:20 -040029 REPORTER_ASSERT(reporter, sdc->asRenderTargetProxy()->isInstantiated() == wrappedExpectation);
Robert Phillipseaa86252016-11-08 13:49:39 +000030
Robert Phillips4dca8312021-07-28 15:13:20 -040031 GrTextureProxy* tProxy = sdc->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000032 REPORTER_ASSERT(reporter, tProxy);
33
Robert Phillipsb5204762019-06-19 14:12:13 -040034 REPORTER_ASSERT(reporter, tProxy->isInstantiated() == wrappedExpectation);
Robert Phillipseaa86252016-11-08 13:49:39 +000035}
36
John Stiles0fbc6a32021-06-04 14:40:57 -040037DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceDrawContextTest, reporter, ctxInfo) {
Robert Phillipse94b4e12020-07-23 13:54:35 -040038 auto dContext = ctxInfo.directContext();
Robert Phillipseaa86252016-11-08 13:49:39 +000039
Robert Phillips4dca8312021-07-28 15:13:20 -040040 // Calling instantiate on a SurfaceDrawContext's textureProxy also instantiates the
41 // SurfaceDrawContext
Robert Phillipseaa86252016-11-08 13:49:39 +000042 {
Robert Phillips4dca8312021-07-28 15:13:20 -040043 auto sdc = get_sdc(dContext);
Robert Phillipseaa86252016-11-08 13:49:39 +000044
Robert Phillips4dca8312021-07-28 15:13:20 -040045 check_instantiation_status(reporter, sdc.get(), false);
Robert Phillipseaa86252016-11-08 13:49:39 +000046
Robert Phillips4dca8312021-07-28 15:13:20 -040047 GrTextureProxy* tProxy = sdc->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000048 REPORTER_ASSERT(reporter, tProxy);
49
Robert Phillipse94b4e12020-07-23 13:54:35 -040050 REPORTER_ASSERT(reporter, tProxy->instantiate(dContext->priv().resourceProvider()));
Robert Phillipseaa86252016-11-08 13:49:39 +000051
Robert Phillips4dca8312021-07-28 15:13:20 -040052 check_instantiation_status(reporter, sdc.get(), true);
Robert Phillipseaa86252016-11-08 13:49:39 +000053 }
54
John Stiles0fbc6a32021-06-04 14:40:57 -040055 // readPixels switches a deferred sdCtx to wrapped
Robert Phillipseaa86252016-11-08 13:49:39 +000056 {
Robert Phillips4dca8312021-07-28 15:13:20 -040057 auto sdc = get_sdc(dContext);
Robert Phillipseaa86252016-11-08 13:49:39 +000058
Robert Phillips4dca8312021-07-28 15:13:20 -040059 check_instantiation_status(reporter, sdc.get(), false);
Robert Phillipseaa86252016-11-08 13:49:39 +000060
61 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
Brian Salomonbe1084b2021-01-26 13:29:30 -050062 GrPixmap dstPM = GrPixmap::Allocate(dstInfo);
Robert Phillipseaa86252016-11-08 13:49:39 +000063
Robert Phillips4dca8312021-07-28 15:13:20 -040064 bool result = sdc->readPixels(dContext, dstPM, {0, 0});
Robert Phillipseaa86252016-11-08 13:49:39 +000065 REPORTER_ASSERT(reporter, result);
66
Robert Phillips4dca8312021-07-28 15:13:20 -040067 check_instantiation_status(reporter, sdc.get(), true);
Robert Phillipseaa86252016-11-08 13:49:39 +000068 }
69
70 // TODO: in a future world we should be able to add a test that the majority of
Robert Phillips4dca8312021-07-28 15:13:20 -040071 // SurfaceDrawContext calls do not force the instantiation of a deferred
72 // SurfaceDrawContext
Robert Phillipseaa86252016-11-08 13:49:39 +000073}