blob: c44eadcbb5adb1c95732235ecea97050bd712613 [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"
Robert Phillipsb5204762019-06-19 14:12:13 -040013#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040014#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrTextureProxy.h"
Robert Phillipseaa86252016-11-08 13:49:39 +000017
18static const int kSize = 64;
19
Brian Salomonbf6b9792019-08-21 09:38:10 -040020static std::unique_ptr<GrRenderTargetContext> get_rtc(GrContext* ctx) {
Greg Daniele20fcad2020-01-08 11:52:34 -050021 return GrRenderTargetContext::Make(
22 ctx, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {kSize, kSize});
Robert Phillipseaa86252016-11-08 13:49:39 +000023}
24
Robert Phillipsb5204762019-06-19 14:12:13 -040025static void check_instantiation_status(skiatest::Reporter* reporter,
26 GrRenderTargetContext* rtCtx,
27 bool wrappedExpectation) {
28 REPORTER_ASSERT(reporter, rtCtx->testingOnly_IsInstantiated() == wrappedExpectation);
Robert Phillipseaa86252016-11-08 13:49:39 +000029
Robert Phillipsf200a902017-01-30 13:27:37 -050030 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000031 REPORTER_ASSERT(reporter, tProxy);
32
Robert Phillipsb5204762019-06-19 14:12:13 -040033 REPORTER_ASSERT(reporter, tProxy->isInstantiated() == wrappedExpectation);
Robert Phillipseaa86252016-11-08 13:49:39 +000034}
35
36DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RenderTargetContextTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040037 auto ctx = ctxInfo.directContext();
Robert Phillipseaa86252016-11-08 13:49:39 +000038
Robert Phillipseaa86252016-11-08 13:49:39 +000039 // Calling instantiate on a GrRenderTargetContext's textureProxy also instantiates the
40 // GrRenderTargetContext
41 {
Brian Salomonbf6b9792019-08-21 09:38:10 -040042 auto rtCtx = get_rtc(ctx);
Robert Phillipseaa86252016-11-08 13:49:39 +000043
Robert Phillipsb5204762019-06-19 14:12:13 -040044 check_instantiation_status(reporter, rtCtx.get(), false);
Robert Phillipseaa86252016-11-08 13:49:39 +000045
Robert Phillipsf200a902017-01-30 13:27:37 -050046 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000047 REPORTER_ASSERT(reporter, tProxy);
48
Robert Phillipsb5204762019-06-19 14:12:13 -040049 REPORTER_ASSERT(reporter, tProxy->instantiate(ctx->priv().resourceProvider()));
Robert Phillipseaa86252016-11-08 13:49:39 +000050
Robert Phillipsb5204762019-06-19 14:12:13 -040051 check_instantiation_status(reporter, rtCtx.get(), true);
Robert Phillipseaa86252016-11-08 13:49:39 +000052 }
53
54 // readPixels switches a deferred rtCtx to wrapped
55 {
Brian Salomonbf6b9792019-08-21 09:38:10 -040056 auto rtCtx = get_rtc(ctx);
Robert Phillipseaa86252016-11-08 13:49:39 +000057
Robert Phillipsb5204762019-06-19 14:12:13 -040058 check_instantiation_status(reporter, rtCtx.get(), false);
Robert Phillipseaa86252016-11-08 13:49:39 +000059
60 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
61 SkAutoTMalloc<uint32_t> dstBuffer(kSize * kSize);
62 static const size_t kRowBytes = sizeof(uint32_t) * kSize;
63
Brian Salomon1d435302019-07-01 13:05:28 -040064 bool result = rtCtx->readPixels(dstInfo, dstBuffer.get(), kRowBytes, {0, 0});
Robert Phillipseaa86252016-11-08 13:49:39 +000065 REPORTER_ASSERT(reporter, result);
66
Robert Phillipsb5204762019-06-19 14:12:13 -040067 check_instantiation_status(reporter, rtCtx.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
Ben Wagner63fd7602017-10-09 15:45:33 -040071 // GrRenderTargetContext calls do not force the instantiation of a deferred
Robert Phillipseaa86252016-11-08 13:49:39 +000072 // GrRenderTargetContext
73}