blob: 77b50ee49814b67c785c2da52e0f73bda6e5023c [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrTextureProxy.h"
Robert Phillipseaa86252016-11-08 13:49:39 +000015
16static const int kSize = 64;
17
Brian Salomonbf6b9792019-08-21 09:38:10 -040018static std::unique_ptr<GrRenderTargetContext> get_rtc(GrContext* ctx) {
Brian Salomon27ae52c2019-07-03 11:27:44 -040019 return ctx->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, kSize, kSize,
Brian Salomond6287472019-06-24 15:50:07 -040020 GrColorType::kRGBA_8888, nullptr);
Robert Phillipseaa86252016-11-08 13:49:39 +000021}
22
Robert Phillipsb5204762019-06-19 14:12:13 -040023static void check_instantiation_status(skiatest::Reporter* reporter,
24 GrRenderTargetContext* rtCtx,
25 bool wrappedExpectation) {
26 REPORTER_ASSERT(reporter, rtCtx->testingOnly_IsInstantiated() == wrappedExpectation);
Robert Phillipseaa86252016-11-08 13:49:39 +000027
Robert Phillipsf200a902017-01-30 13:27:37 -050028 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000029 REPORTER_ASSERT(reporter, tProxy);
30
Robert Phillipsb5204762019-06-19 14:12:13 -040031 REPORTER_ASSERT(reporter, tProxy->isInstantiated() == wrappedExpectation);
Robert Phillipseaa86252016-11-08 13:49:39 +000032}
33
34DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RenderTargetContextTest, reporter, ctxInfo) {
35 GrContext* ctx = ctxInfo.grContext();
36
Robert Phillipseaa86252016-11-08 13:49:39 +000037 // Calling instantiate on a GrRenderTargetContext's textureProxy also instantiates the
38 // GrRenderTargetContext
39 {
Brian Salomonbf6b9792019-08-21 09:38:10 -040040 auto rtCtx = get_rtc(ctx);
Robert Phillipseaa86252016-11-08 13:49:39 +000041
Robert Phillipsb5204762019-06-19 14:12:13 -040042 check_instantiation_status(reporter, rtCtx.get(), false);
Robert Phillipseaa86252016-11-08 13:49:39 +000043
Robert Phillipsf200a902017-01-30 13:27:37 -050044 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000045 REPORTER_ASSERT(reporter, tProxy);
46
Robert Phillipsb5204762019-06-19 14:12:13 -040047 REPORTER_ASSERT(reporter, tProxy->instantiate(ctx->priv().resourceProvider()));
Robert Phillipseaa86252016-11-08 13:49:39 +000048
Robert Phillipsb5204762019-06-19 14:12:13 -040049 check_instantiation_status(reporter, rtCtx.get(), true);
Robert Phillipseaa86252016-11-08 13:49:39 +000050 }
51
52 // readPixels switches a deferred rtCtx to wrapped
53 {
Brian Salomonbf6b9792019-08-21 09:38:10 -040054 auto rtCtx = get_rtc(ctx);
Robert Phillipseaa86252016-11-08 13:49:39 +000055
Robert Phillipsb5204762019-06-19 14:12:13 -040056 check_instantiation_status(reporter, rtCtx.get(), false);
Robert Phillipseaa86252016-11-08 13:49:39 +000057
58 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
59 SkAutoTMalloc<uint32_t> dstBuffer(kSize * kSize);
60 static const size_t kRowBytes = sizeof(uint32_t) * kSize;
61
Brian Salomon1d435302019-07-01 13:05:28 -040062 bool result = rtCtx->readPixels(dstInfo, dstBuffer.get(), kRowBytes, {0, 0});
Robert Phillipseaa86252016-11-08 13:49:39 +000063 REPORTER_ASSERT(reporter, result);
64
Robert Phillipsb5204762019-06-19 14:12:13 -040065 check_instantiation_status(reporter, rtCtx.get(), true);
Robert Phillipseaa86252016-11-08 13:49:39 +000066 }
67
68 // TODO: in a future world we should be able to add a test that the majority of
Ben Wagner63fd7602017-10-09 15:45:33 -040069 // GrRenderTargetContext calls do not force the instantiation of a deferred
Robert Phillipseaa86252016-11-08 13:49:39 +000070 // GrRenderTargetContext
71}