blob: ce7a39fbef2b178fbf70519bfccd7d29fe32fac7 [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 Phillipsb5204762019-06-19 14:12:13 -040012#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040013#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrTextureProxy.h"
Robert Phillipseaa86252016-11-08 13:49:39 +000016
17static const int kSize = 64;
18
Brian Salomonbf6b9792019-08-21 09:38:10 -040019static std::unique_ptr<GrRenderTargetContext> get_rtc(GrContext* ctx) {
Brian Salomon27ae52c2019-07-03 11:27:44 -040020 return ctx->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, kSize, kSize,
Brian Salomond6287472019-06-24 15:50:07 -040021 GrColorType::kRGBA_8888, nullptr);
Robert Phillipseaa86252016-11-08 13:49:39 +000022}
23
Robert Phillipsb5204762019-06-19 14:12:13 -040024static void check_instantiation_status(skiatest::Reporter* reporter,
25 GrRenderTargetContext* rtCtx,
26 bool wrappedExpectation) {
27 REPORTER_ASSERT(reporter, rtCtx->testingOnly_IsInstantiated() == wrappedExpectation);
Robert Phillipseaa86252016-11-08 13:49:39 +000028
Robert Phillipsf200a902017-01-30 13:27:37 -050029 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000030 REPORTER_ASSERT(reporter, tProxy);
31
Robert Phillipsb5204762019-06-19 14:12:13 -040032 REPORTER_ASSERT(reporter, tProxy->isInstantiated() == wrappedExpectation);
Robert Phillipseaa86252016-11-08 13:49:39 +000033}
34
35DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RenderTargetContextTest, reporter, ctxInfo) {
36 GrContext* ctx = ctxInfo.grContext();
37
Robert Phillipseaa86252016-11-08 13:49:39 +000038 // Calling instantiate on a GrRenderTargetContext's textureProxy also instantiates the
39 // GrRenderTargetContext
40 {
Brian Salomonbf6b9792019-08-21 09:38:10 -040041 auto rtCtx = get_rtc(ctx);
Robert Phillipseaa86252016-11-08 13:49:39 +000042
Robert Phillipsb5204762019-06-19 14:12:13 -040043 check_instantiation_status(reporter, rtCtx.get(), false);
Robert Phillipseaa86252016-11-08 13:49:39 +000044
Robert Phillipsf200a902017-01-30 13:27:37 -050045 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000046 REPORTER_ASSERT(reporter, tProxy);
47
Robert Phillipsb5204762019-06-19 14:12:13 -040048 REPORTER_ASSERT(reporter, tProxy->instantiate(ctx->priv().resourceProvider()));
Robert Phillipseaa86252016-11-08 13:49:39 +000049
Robert Phillipsb5204762019-06-19 14:12:13 -040050 check_instantiation_status(reporter, rtCtx.get(), true);
Robert Phillipseaa86252016-11-08 13:49:39 +000051 }
52
53 // readPixels switches a deferred rtCtx to wrapped
54 {
Brian Salomonbf6b9792019-08-21 09:38:10 -040055 auto rtCtx = get_rtc(ctx);
Robert Phillipseaa86252016-11-08 13:49:39 +000056
Robert Phillipsb5204762019-06-19 14:12:13 -040057 check_instantiation_status(reporter, rtCtx.get(), false);
Robert Phillipseaa86252016-11-08 13:49:39 +000058
59 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
60 SkAutoTMalloc<uint32_t> dstBuffer(kSize * kSize);
61 static const size_t kRowBytes = sizeof(uint32_t) * kSize;
62
Brian Salomon1d435302019-07-01 13:05:28 -040063 bool result = rtCtx->readPixels(dstInfo, dstBuffer.get(), kRowBytes, {0, 0});
Robert Phillipseaa86252016-11-08 13:49:39 +000064 REPORTER_ASSERT(reporter, result);
65
Robert Phillipsb5204762019-06-19 14:12:13 -040066 check_instantiation_status(reporter, rtCtx.get(), true);
Robert Phillipseaa86252016-11-08 13:49:39 +000067 }
68
69 // TODO: in a future world we should be able to add a test that the majority of
Ben Wagner63fd7602017-10-09 15:45:33 -040070 // GrRenderTargetContext calls do not force the instantiation of a deferred
Robert Phillipseaa86252016-11-08 13:49:39 +000071 // GrRenderTargetContext
72}