blob: 5917a2d9d446cbbb8c752a9b6a434961a88308f5 [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
18static 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 }
29}
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
45 // 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
51 // A deferred rtCtx's textureProxy is also deferred and GrRenderTargetContext::instantiate()
52 // swaps both from deferred to wrapped
53 {
54 sk_sp<GrRenderTargetContext> rtCtx(get_rtc(ctx, false));
55
56 check_is_wrapped_status(reporter, rtCtx.get(), false);
57
58 GrRenderTarget* rt = rtCtx->instantiate();
59 REPORTER_ASSERT(reporter, rt);
60
61 check_is_wrapped_status(reporter, rtCtx.get(), true);
62 }
63
64 // Calling instantiate on a GrRenderTargetContext's textureProxy also instantiates the
65 // GrRenderTargetContext
66 {
67 sk_sp<GrRenderTargetContext> rtCtx(get_rtc(ctx, false));
68
69 check_is_wrapped_status(reporter, rtCtx.get(), false);
70
Robert Phillipsf200a902017-01-30 13:27:37 -050071 GrTextureProxy* tProxy = rtCtx->asTextureProxy();
Robert Phillipseaa86252016-11-08 13:49:39 +000072 REPORTER_ASSERT(reporter, tProxy);
73
74 GrTexture* tex = tProxy->instantiate(ctx->textureProvider());
75 REPORTER_ASSERT(reporter, tex);
76
77 check_is_wrapped_status(reporter, rtCtx.get(), true);
78 }
79
80 // readPixels switches a deferred rtCtx to wrapped
81 {
82 sk_sp<GrRenderTargetContext> rtCtx(get_rtc(ctx, false));
83
84 check_is_wrapped_status(reporter, rtCtx.get(), false);
85
86 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
87 SkAutoTMalloc<uint32_t> dstBuffer(kSize * kSize);
88 static const size_t kRowBytes = sizeof(uint32_t) * kSize;
89
90 bool result = rtCtx->readPixels(dstInfo, dstBuffer.get(), kRowBytes, 0, 0);
91 REPORTER_ASSERT(reporter, result);
92
93 check_is_wrapped_status(reporter, rtCtx.get(), true);
94 }
95
96 // TODO: in a future world we should be able to add a test that the majority of
97 // GrRenderTargetContext calls do not force the instantiation of a deferred
98 // GrRenderTargetContext
99}
100#endif