blob: 3db0473b62debe03d664a4300f7903b04a9a0616 [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
10#include "Test.h"
11
12#if SK_SUPPORT_GPU
13#include "GrTextureProxy.h"
14#include "GrRenderTargetContext.h"
15
16static const int kSize = 64;
17
Robert Phillips93429212017-04-11 12:23:17 +000018static sk_sp<GrRenderTargetContext> get_rtc(GrContext* ctx, bool wrapped) {
19
20 if (wrapped) {
21 return ctx->makeRenderTargetContext(SkBackingFit::kExact,
22 kSize, kSize,
23 kRGBA_8888_GrPixelConfig, nullptr);
24 } else {
25 return ctx->makeDeferredRenderTargetContext(SkBackingFit::kExact,
26 kSize, kSize,
27 kRGBA_8888_GrPixelConfig, nullptr);
28 }
Robert Phillipseaa86252016-11-08 13:49:39 +000029}
30
31static void check_is_wrapped_status(skiatest::Reporter* reporter,
32 GrRenderTargetContext* rtCtx,
33 bool wrappedExpectation) {
34 REPORTER_ASSERT(reporter, rtCtx->isWrapped_ForTesting() == wrappedExpectation);
35
Robert Phillipsf200a902017-01-30 13:27:37 -050036 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000037 REPORTER_ASSERT(reporter, tProxy);
38
39 REPORTER_ASSERT(reporter, tProxy->isWrapped_ForTesting() == wrappedExpectation);
40}
41
42DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RenderTargetContextTest, reporter, ctxInfo) {
43 GrContext* ctx = ctxInfo.grContext();
44
Robert Phillips93429212017-04-11 12:23:17 +000045 // A wrapped rtCtx's textureProxy is also wrapped
46 {
47 sk_sp<GrRenderTargetContext> rtCtx(get_rtc(ctx, true));
48 check_is_wrapped_status(reporter, rtCtx.get(), true);
49 }
50
Robert Phillipseaa86252016-11-08 13:49:39 +000051 // Calling instantiate on a GrRenderTargetContext's textureProxy also instantiates the
52 // GrRenderTargetContext
53 {
Robert Phillips93429212017-04-11 12:23:17 +000054 sk_sp<GrRenderTargetContext> rtCtx(get_rtc(ctx, false));
Robert Phillipseaa86252016-11-08 13:49:39 +000055
56 check_is_wrapped_status(reporter, rtCtx.get(), false);
57
Robert Phillipsf200a902017-01-30 13:27:37 -050058 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000059 REPORTER_ASSERT(reporter, tProxy);
60
Brian Osman32342f02017-03-04 08:12:46 -050061 GrTexture* tex = tProxy->instantiate(ctx->resourceProvider());
Robert Phillipseaa86252016-11-08 13:49:39 +000062 REPORTER_ASSERT(reporter, tex);
63
64 check_is_wrapped_status(reporter, rtCtx.get(), true);
65 }
66
67 // readPixels switches a deferred rtCtx to wrapped
68 {
Robert Phillips93429212017-04-11 12:23:17 +000069 sk_sp<GrRenderTargetContext> rtCtx(get_rtc(ctx, false));
Robert Phillipseaa86252016-11-08 13:49:39 +000070
71 check_is_wrapped_status(reporter, rtCtx.get(), false);
72
73 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
74 SkAutoTMalloc<uint32_t> dstBuffer(kSize * kSize);
75 static const size_t kRowBytes = sizeof(uint32_t) * kSize;
76
77 bool result = rtCtx->readPixels(dstInfo, dstBuffer.get(), kRowBytes, 0, 0);
78 REPORTER_ASSERT(reporter, result);
79
80 check_is_wrapped_status(reporter, rtCtx.get(), true);
81 }
82
83 // TODO: in a future world we should be able to add a test that the majority of
84 // GrRenderTargetContext calls do not force the instantiation of a deferred
85 // GrRenderTargetContext
86}
87#endif