1/*
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// MDB TODO: the early instantiation of the renderTargetContext's backing GrRenderTargetProxy
13// mixes this test up. Re-enable once backing GPU resources are distributed by MDB at flush time.
14#if 0
15
16#if SK_SUPPORT_GPU
17#include "GrTextureProxy.h"
18#include "GrRenderTargetContext.h"
19
20static const int kSize = 64;
21
22static sk_sp<GrRenderTargetContext> get_rtc(GrContext* ctx) {
23    return ctx->makeDeferredRenderTargetContext(SkBackingFit::kExact,
24                                                kSize, kSize,
25                                                kRGBA_8888_GrPixelConfig, nullptr);
26}
27
28static void check_is_wrapped_status(skiatest::Reporter* reporter,
29                                    GrRenderTargetContext* rtCtx,
30                                    bool wrappedExpectation) {
31    REPORTER_ASSERT(reporter, rtCtx->isWrapped_ForTesting() == wrappedExpectation);
32
33    GrTextureProxy* tProxy = rtCtx->asTextureProxy();
34    REPORTER_ASSERT(reporter, tProxy);
35
36    REPORTER_ASSERT(reporter, tProxy->isWrapped_ForTesting() == wrappedExpectation);
37}
38
39DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RenderTargetContextTest, reporter, ctxInfo) {
40    GrContext* ctx = ctxInfo.grContext();
41
42    // Calling instantiate on a GrRenderTargetContext's textureProxy also instantiates the
43    // GrRenderTargetContext
44    {
45        sk_sp<GrRenderTargetContext> rtCtx(get_rtc(ctx));
46
47        check_is_wrapped_status(reporter, rtCtx.get(), false);
48
49        GrTextureProxy* tProxy = rtCtx->asTextureProxy();
50        REPORTER_ASSERT(reporter, tProxy);
51
52        REPORTER_ASSERT(reporter, tProxy->instantiate(ctx->resourceProvider()));
53
54        check_is_wrapped_status(reporter, rtCtx.get(), true);
55    }
56
57    // readPixels switches a deferred rtCtx to wrapped
58    {
59        sk_sp<GrRenderTargetContext> rtCtx(get_rtc(ctx));
60
61        check_is_wrapped_status(reporter, rtCtx.get(), false);
62
63        SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(kSize, kSize);
64        SkAutoTMalloc<uint32_t> dstBuffer(kSize * kSize);
65        static const size_t kRowBytes = sizeof(uint32_t) * kSize;
66
67        bool result = rtCtx->readPixels(dstInfo, dstBuffer.get(), kRowBytes, 0, 0);
68        REPORTER_ASSERT(reporter, result);
69
70        check_is_wrapped_status(reporter, rtCtx.get(), true);
71    }
72
73    // TODO: in a future world we should be able to add a test that the majority of
74    // GrRenderTargetContext calls do not force the instantiation of a deferred
75    // GrRenderTargetContext
76}
77#endif
78#endif
79