1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci// This is a GPU-backend specific test.
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "tests/Test.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#include "include/gpu/GrBackendSurface.h"
13cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
14cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h"
15cb93a386Sopenharmony_ci#include "src/gpu/GrGpu.h"
16cb93a386Sopenharmony_ci#include "src/gpu/GrProxyProvider.h"
17cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTarget.h"
18cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTargetProxy.h"
19cb93a386Sopenharmony_ci#include "src/gpu/GrSurfaceProxy.h"
20cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h"
21cb93a386Sopenharmony_ci#include "src/gpu/GrTextureProxy.h"
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_cistatic sk_sp<GrSurfaceProxy> make_wrapped_rt(GrProxyProvider* provider,
24cb93a386Sopenharmony_ci                                             GrGpu* gpu,
25cb93a386Sopenharmony_ci                                             skiatest::Reporter* reporter,
26cb93a386Sopenharmony_ci                                             const SkISize& size,
27cb93a386Sopenharmony_ci                                             GrColorType colorType) {
28cb93a386Sopenharmony_ci    auto backendRT = gpu->createTestingOnlyBackendRenderTarget(size, colorType);
29cb93a386Sopenharmony_ci    return provider->wrapBackendRenderTarget(backendRT, nullptr);
30cb93a386Sopenharmony_ci}
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_civoid clean_up_wrapped_rt(GrGpu* gpu, sk_sp<GrSurfaceProxy> proxy) {
33cb93a386Sopenharmony_ci    SkASSERT(proxy->unique());
34cb93a386Sopenharmony_ci    SkASSERT(proxy->peekRenderTarget());
35cb93a386Sopenharmony_ci    GrBackendRenderTarget rt = proxy->peekRenderTarget()->getBackendRenderTarget();
36cb93a386Sopenharmony_ci    proxy.reset();
37cb93a386Sopenharmony_ci    gpu->deleteTestingOnlyBackendRenderTarget(rt);
38cb93a386Sopenharmony_ci}
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_cistatic sk_sp<GrSurfaceProxy> make_offscreen_rt(GrProxyProvider* provider,
41cb93a386Sopenharmony_ci                                               SkISize dimensions,
42cb93a386Sopenharmony_ci                                               GrColorType colorType) {
43cb93a386Sopenharmony_ci    return provider->testingOnly_createInstantiatedProxy(dimensions, colorType, GrRenderable::kYes,
44cb93a386Sopenharmony_ci                                                         1, SkBackingFit::kExact, SkBudgeted::kYes,
45cb93a386Sopenharmony_ci                                                         GrProtected::kNo);
46cb93a386Sopenharmony_ci}
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_cistatic sk_sp<GrSurfaceProxy> make_texture(GrProxyProvider* provider,
49cb93a386Sopenharmony_ci                                          SkISize dimensions,
50cb93a386Sopenharmony_ci                                          GrColorType colorType,
51cb93a386Sopenharmony_ci                                          GrRenderable renderable) {
52cb93a386Sopenharmony_ci    return provider->testingOnly_createInstantiatedProxy(dimensions, colorType, renderable, 1,
53cb93a386Sopenharmony_ci                                                         SkBackingFit::kExact, SkBudgeted::kYes,
54cb93a386Sopenharmony_ci                                                         GrProtected::kNo);
55cb93a386Sopenharmony_ci}
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci// Test converting between RenderTargetProxies and TextureProxies for preinstantiated Proxies
58cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_RENDERING_CONTEXTS(PreinstantiatedProxyConversionTest, reporter, ctxInfo) {
59cb93a386Sopenharmony_ci    auto context = ctxInfo.directContext();
60cb93a386Sopenharmony_ci    GrProxyProvider* proxyProvider = context->priv().proxyProvider();
61cb93a386Sopenharmony_ci    GrGpu* gpu = context->priv().getGpu();
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci    static constexpr auto kSize = SkISize::Make(64, 64);
64cb93a386Sopenharmony_ci    static constexpr auto kColorType = GrColorType::kRGBA_8888;
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ci    {
67cb93a386Sopenharmony_ci        // External on-screen render target.
68cb93a386Sopenharmony_ci        sk_sp<GrSurfaceProxy> sProxy(
69cb93a386Sopenharmony_ci                make_wrapped_rt(proxyProvider, gpu, reporter, kSize, kColorType));
70cb93a386Sopenharmony_ci        if (sProxy) {
71cb93a386Sopenharmony_ci            // RenderTarget-only
72cb93a386Sopenharmony_ci            GrRenderTargetProxy* rtProxy = sProxy->asRenderTargetProxy();
73cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, rtProxy);
74cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, !rtProxy->asTextureProxy());
75cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy);
76cb93a386Sopenharmony_ci            clean_up_wrapped_rt(gpu, std::move(sProxy));
77cb93a386Sopenharmony_ci        }
78cb93a386Sopenharmony_ci    }
79cb93a386Sopenharmony_ci
80cb93a386Sopenharmony_ci    {
81cb93a386Sopenharmony_ci        // Internal offscreen render target.
82cb93a386Sopenharmony_ci        sk_sp<GrSurfaceProxy> sProxy(make_offscreen_rt(proxyProvider, kSize, kColorType));
83cb93a386Sopenharmony_ci        if (sProxy) {
84cb93a386Sopenharmony_ci            // Both RenderTarget and Texture
85cb93a386Sopenharmony_ci            GrRenderTargetProxy* rtProxy = sProxy->asRenderTargetProxy();
86cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, rtProxy);
87cb93a386Sopenharmony_ci            GrTextureProxy* tProxy = rtProxy->asTextureProxy();
88cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, tProxy);
89cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, tProxy->asRenderTargetProxy() == rtProxy);
90cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy);
91cb93a386Sopenharmony_ci        }
92cb93a386Sopenharmony_ci    }
93cb93a386Sopenharmony_ci
94cb93a386Sopenharmony_ci    {
95cb93a386Sopenharmony_ci        // Internal offscreen render target - but through GrTextureProxy
96cb93a386Sopenharmony_ci        sk_sp<GrSurfaceProxy> sProxy(
97cb93a386Sopenharmony_ci                make_texture(proxyProvider, kSize, kColorType, GrRenderable::kYes));
98cb93a386Sopenharmony_ci        if (sProxy) {
99cb93a386Sopenharmony_ci            // Both RenderTarget and Texture
100cb93a386Sopenharmony_ci            GrTextureProxy* tProxy = sProxy->asTextureProxy();
101cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, tProxy);
102cb93a386Sopenharmony_ci            GrRenderTargetProxy* rtProxy = tProxy->asRenderTargetProxy();
103cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, rtProxy);
104cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, rtProxy->asTextureProxy() == tProxy);
105cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy);
106cb93a386Sopenharmony_ci        }
107cb93a386Sopenharmony_ci    }
108cb93a386Sopenharmony_ci
109cb93a386Sopenharmony_ci    {
110cb93a386Sopenharmony_ci        // force no-RT
111cb93a386Sopenharmony_ci        sk_sp<GrSurfaceProxy> sProxy(
112cb93a386Sopenharmony_ci                make_texture(proxyProvider, kSize, kColorType, GrRenderable::kNo));
113cb93a386Sopenharmony_ci        if (sProxy) {
114cb93a386Sopenharmony_ci            // Texture-only
115cb93a386Sopenharmony_ci            GrTextureProxy* tProxy = sProxy->asTextureProxy();
116cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, tProxy);
117cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy);
118cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, !tProxy->asRenderTargetProxy());
119cb93a386Sopenharmony_ci        }
120cb93a386Sopenharmony_ci    }
121cb93a386Sopenharmony_ci}
122cb93a386Sopenharmony_ci
123cb93a386Sopenharmony_ci// Test converting between RenderTargetProxies and TextureProxies for deferred
124cb93a386Sopenharmony_ci// Proxies
125cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_RENDERING_CONTEXTS(DefferredProxyConversionTest, reporter, ctxInfo) {
126cb93a386Sopenharmony_ci    auto context = ctxInfo.directContext();
127cb93a386Sopenharmony_ci    GrProxyProvider* proxyProvider = context->priv().proxyProvider();
128cb93a386Sopenharmony_ci    const GrCaps* caps = context->priv().caps();
129cb93a386Sopenharmony_ci
130cb93a386Sopenharmony_ci    static constexpr SkISize kDims = {64, 64};
131cb93a386Sopenharmony_ci
132cb93a386Sopenharmony_ci    const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
133cb93a386Sopenharmony_ci                                                                 GrRenderable::kYes);
134cb93a386Sopenharmony_ci    {
135cb93a386Sopenharmony_ci        sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
136cb93a386Sopenharmony_ci                format, kDims, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBackingFit::kApprox,
137cb93a386Sopenharmony_ci                SkBudgeted::kYes, GrProtected::kNo);
138cb93a386Sopenharmony_ci
139cb93a386Sopenharmony_ci        // Both RenderTarget and Texture
140cb93a386Sopenharmony_ci        GrRenderTargetProxy* rtProxy = proxy->asRenderTargetProxy();
141cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, rtProxy);
142cb93a386Sopenharmony_ci        GrTextureProxy* tProxy = rtProxy->asTextureProxy();
143cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tProxy);
144cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tProxy->asRenderTargetProxy() == rtProxy);
145cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy);
146cb93a386Sopenharmony_ci    }
147cb93a386Sopenharmony_ci
148cb93a386Sopenharmony_ci    {
149cb93a386Sopenharmony_ci        sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
150cb93a386Sopenharmony_ci                format, kDims, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBackingFit::kApprox,
151cb93a386Sopenharmony_ci                SkBudgeted::kYes, GrProtected::kNo);
152cb93a386Sopenharmony_ci
153cb93a386Sopenharmony_ci        // Both RenderTarget and Texture - but via GrTextureProxy
154cb93a386Sopenharmony_ci        GrTextureProxy* tProxy = proxy->asTextureProxy();
155cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tProxy);
156cb93a386Sopenharmony_ci        GrRenderTargetProxy* rtProxy = tProxy->asRenderTargetProxy();
157cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, rtProxy);
158cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, rtProxy->asTextureProxy() == tProxy);
159cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy);
160cb93a386Sopenharmony_ci    }
161cb93a386Sopenharmony_ci
162cb93a386Sopenharmony_ci    {
163cb93a386Sopenharmony_ci        sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
164cb93a386Sopenharmony_ci                format, kDims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kApprox,
165cb93a386Sopenharmony_ci                SkBudgeted::kYes, GrProtected::kNo);
166cb93a386Sopenharmony_ci        // Texture-only
167cb93a386Sopenharmony_ci        GrTextureProxy* tProxy = proxy->asTextureProxy();
168cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tProxy);
169cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy);
170cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !tProxy->asRenderTargetProxy());
171cb93a386Sopenharmony_ci    }
172cb93a386Sopenharmony_ci}
173