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