Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / tests / GLBackendSurfaceTest.cpp
1 /*
2  * Copyright 2019 Google LLC
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 #include "include/core/SkTypes.h"
9
10 #ifdef SK_GL
11
12 #include "tests/Test.h"
13
14 #include "include/core/SkColorSpace.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkSurface.h"
17 #include "include/gpu/GrBackendSurface.h"
18 #include "include/gpu/GrDirectContext.h"
19 #include "include/gpu/gl/GrGLTypes.h"
20 #include "include/private/gpu/ganesh/GrGLTypesPriv.h"
21 #include "src/gpu/ganesh/GrDirectContextPriv.h"
22 #include "src/gpu/ganesh/GrTexture.h"
23 #include "src/gpu/ganesh/GrTextureProxy.h"
24 #include "src/gpu/ganesh/gl/GrGLCaps.h"
25 #include "src/gpu/ganesh/gl/GrGLTexture.h"
26 #include "src/image/SkImage_Base.h"
27 #include "tools/gpu/ProxyUtils.h"
28
29 static bool sampler_params_invalid(const GrGLTextureParameters& parameters) {
30     return SkScalarIsNaN(parameters.samplerOverriddenState().fMaxLOD);
31 }
32
33 static bool nonsampler_params_invalid(const GrGLTextureParameters& parameters) {
34     GrGLTextureParameters::NonsamplerState nsState = parameters.nonsamplerState();
35     GrGLTextureParameters::NonsamplerState invalidNSState;
36     invalidNSState.invalidate();
37     return nsState.fBaseMipMapLevel == invalidNSState.fBaseMipMapLevel &&
38            nsState.fMaxMipmapLevel  == invalidNSState.fMaxMipmapLevel  &&
39            nsState.fSwizzleIsRGBA   == invalidNSState.fSwizzleIsRGBA;
40 }
41
42 static bool params_invalid(const GrGLTextureParameters& parameters) {
43     return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters);
44 }
45
46 static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) {
47     if (nonsampler_params_invalid(parameters)) {
48         return false;
49     }
50     // We should only set the texture params that are equivalent to sampler param to valid if we're
51     // not using sampler objects.
52     return caps->useSamplerObjects() == sampler_params_invalid(parameters);
53 }
54
55 DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) {
56     auto dContext = ctxInfo.directContext();
57     auto caps = static_cast<const GrGLCaps*>(dContext->priv().caps());
58
59     GrBackendTexture backendTex = dContext->createBackendTexture(
60             1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
61     REPORTER_ASSERT(reporter, backendTex.isValid());
62
63     GrGLTextureInfo info;
64     REPORTER_ASSERT(reporter, backendTex.getGLTextureInfo(&info));
65
66     GrBackendTexture backendTexCopy = backendTex;
67     REPORTER_ASSERT(reporter, backendTexCopy.isSameTexture(backendTex));
68
69     sk_sp<SkImage> wrappedImage =
70             SkImage::MakeFromTexture(dContext, backendTex, kTopLeft_GrSurfaceOrigin,
71                                      kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
72     REPORTER_ASSERT(reporter, wrappedImage);
73
74     GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
75     REPORTER_ASSERT(reporter, proxy);
76     REPORTER_ASSERT(reporter, proxy->isInstantiated());
77     auto texture = static_cast<GrGLTexture*>(proxy->peekTexture());
78     REPORTER_ASSERT(reporter, texture);
79     auto parameters = texture->parameters();
80     REPORTER_ASSERT(reporter, parameters);
81     GrGLTextureParameters::SamplerOverriddenState invalidSState;
82     invalidSState.invalidate();
83     GrGLTextureParameters::NonsamplerState invalidNSState;
84     invalidNSState.invalidate();
85
86     auto surf = SkSurface::MakeRenderTarget(
87             dContext, SkBudgeted::kYes,
88             SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, nullptr);
89     REPORTER_ASSERT(reporter, surf);
90
91     // Test invalidating from the GL backend texture.
92     backendTex.glTextureParametersModified();
93     REPORTER_ASSERT(reporter, params_invalid(*parameters));
94
95     REPORTER_ASSERT(reporter, surf);
96     surf->getCanvas()->drawImage(wrappedImage, 0, 0);
97     surf->flushAndSubmit();
98     REPORTER_ASSERT(reporter, params_valid(*parameters, caps));
99
100     // Test invalidating from the copy.
101     backendTexCopy.glTextureParametersModified();
102     REPORTER_ASSERT(reporter, params_invalid(*parameters));
103
104     // Check that we can do things like assigning the backend texture to invalid one, assign an
105     // invalid one, assign a backend texture to itself etc. Success here is that we don't hit any
106     // of our ref counting asserts.
107     REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
108
109     GrBackendTexture invalidTexture;
110     REPORTER_ASSERT(reporter, !invalidTexture.isValid());
111     REPORTER_ASSERT(reporter,
112                     !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
113
114     backendTexCopy = invalidTexture;
115     REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
116     REPORTER_ASSERT(reporter,
117                     !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
118
119     invalidTexture = backendTex;
120     REPORTER_ASSERT(reporter, invalidTexture.isValid());
121     REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
122
123     invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
124     REPORTER_ASSERT(reporter, invalidTexture.isValid());
125     REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
126
127     wrappedImage.reset();
128     dContext->flush();
129     dContext->submit(true);
130     dContext->deleteBackendTexture(backendTex);
131 }
132 #endif