blob: d3cc2f4f238354f26cc5a085662ae04cb0df776c [file] [log] [blame]
bsalomon@google.com686bcb82013-04-09 15:04:12 +00001/*
2 * Copyright 2013 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
bsalomona2c23232014-11-25 07:41:12 -08008#include "SkTypes.h"
9
bsalomon@google.com686bcb82013-04-09 15:04:12 +000010#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000011#include "GrContextPriv.h"
bsalomon091f60c2015-11-10 11:54:56 -080012#include "GrGpu.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050013#include "GrProxyProvider.h"
Robert Phillips2890fbf2017-07-26 15:48:41 -040014#include "GrRenderTarget.h"
Brian Osman32342f02017-03-04 08:12:46 -050015#include "GrResourceProvider.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000016#include "GrTexture.h"
Brian Osman48c99192017-06-02 08:45:06 -040017#include "SkMipMap.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000018#include "Test.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000019
bsalomona2c23232014-11-25 07:41:12 -080020// Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture
21// and render targets to GrSurface all work as expected.
bsalomon758586c2016-04-06 14:02:39 -070022DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrSurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070023 GrContext* context = ctxInfo.grContext();
Robert Phillips6be756b2018-01-16 15:07:54 -050024 auto resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050025 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillips6be756b2018-01-16 15:07:54 -050026
kkinnunen15302832015-12-01 04:35:26 -080027 GrSurfaceDesc desc;
kkinnunen15302832015-12-01 04:35:26 -080028 desc.fFlags = kRenderTarget_GrSurfaceFlag;
29 desc.fWidth = 256;
30 desc.fHeight = 256;
Robert Phillips16d8ec62017-07-27 16:16:25 -040031 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbdecacf2018-02-02 20:32:49 -050032 desc.fSampleCnt = 1;
Robert Phillips6be756b2018-01-16 15:07:54 -050033 sk_sp<GrSurface> texRT1 = resourceProvider->createTexture(desc, SkBudgeted::kNo);
bsalomona2c23232014-11-25 07:41:12 -080034
Robert Phillipse78b7252017-04-06 07:59:41 -040035 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget());
36 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080037 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
38 texRT1->asTexture());
39 REPORTER_ASSERT(reporter, texRT1->asRenderTarget() ==
40 static_cast<GrSurface*>(texRT1->asTexture()));
41 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
42 static_cast<GrSurface*>(texRT1->asTexture()));
bsalomona2c23232014-11-25 07:41:12 -080043
kkinnunen15302832015-12-01 04:35:26 -080044 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillips6be756b2018-01-16 15:07:54 -050045 sk_sp<GrTexture> tex1 = resourceProvider->createTexture(desc, SkBudgeted::kNo);
kkinnunen15302832015-12-01 04:35:26 -080046 REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget());
Robert Phillipse78b7252017-04-06 07:59:41 -040047 REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture());
48 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture());
bsalomon@google.com686bcb82013-04-09 15:04:12 +000049
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050050 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -040051 nullptr, 256, 256, GrColorType::kRGBA_8888, false, GrMipMapped::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080052
Brian Salomonbdecacf2018-02-02 20:32:49 -050053 sk_sp<GrSurface> texRT2 =
54 resourceProvider->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Greg Daniel7ef28f32017-04-20 16:41:55 +000055
bungeman6bd52842016-10-27 09:30:08 -070056 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget());
57 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080058 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
59 texRT2->asTexture());
60 REPORTER_ASSERT(reporter, texRT2->asRenderTarget() ==
61 static_cast<GrSurface*>(texRT2->asTexture()));
62 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
63 static_cast<GrSurface*>(texRT2->asTexture()));
bsalomon@google.com686bcb82013-04-09 15:04:12 +000064
Brian Salomon26102cb2018-03-09 09:33:19 -050065 gpu->deleteTestingOnlyBackendTexture(backendTex);
bsalomon@google.com686bcb82013-04-09 15:04:12 +000066}
67
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040068// This test checks that the isConfigTexturable and isConfigRenderable are
69// consistent with createTexture's result.
70DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) {
71 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050072 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050073 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Brian Salomonc7fe0f72018-05-11 10:14:21 -040074 const GrCaps* caps = context->contextPriv().caps();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040075
76 GrPixelConfig configs[] = {
77 kUnknown_GrPixelConfig,
78 kAlpha_8_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050079 kAlpha_8_as_Alpha_GrPixelConfig,
80 kAlpha_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040081 kGray_8_GrPixelConfig,
Greg Daniel7af060a2017-12-05 16:27:11 -050082 kGray_8_as_Lum_GrPixelConfig,
83 kGray_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040084 kRGB_565_GrPixelConfig,
85 kRGBA_4444_GrPixelConfig,
86 kRGBA_8888_GrPixelConfig,
Brian Salomon5fba7ad2018-03-22 10:01:16 -040087 kRGB_888_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040088 kBGRA_8888_GrPixelConfig,
89 kSRGBA_8888_GrPixelConfig,
90 kSBGRA_8888_GrPixelConfig,
Brian Osman10fc6fd2018-03-02 11:01:10 -050091 kRGBA_1010102_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040092 kRGBA_float_GrPixelConfig,
93 kRG_float_GrPixelConfig,
94 kAlpha_half_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050095 kAlpha_half_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040096 kRGBA_half_GrPixelConfig,
97 };
Brian Salomon5fba7ad2018-03-22 10:01:16 -040098 GR_STATIC_ASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs));
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040099
100 GrSurfaceDesc desc;
101 desc.fWidth = 64;
102 desc.fHeight = 64;
103
104 for (GrPixelConfig config : configs) {
105 for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
106 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400107 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500108 desc.fSampleCnt = 1;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400109
Robert Phillips3b3307f2017-05-24 07:44:02 -0400110 sk_sp<GrSurface> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomon1c80e992018-01-29 09:50:47 -0500111 bool ict = caps->isConfigTexturable(desc.fConfig);
112 REPORTER_ASSERT(reporter, SkToBool(tex) == ict,
113 "config:%d, tex:%d, isConfigTexturable:%d", config, SkToBool(tex), ict);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400114
Greg Daniel4065d452018-11-16 15:43:41 -0500115 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
116 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(config, &srgbEncoded);
117 const GrBackendFormat format =
118 caps->getBackendFormatFromGrColorType(colorType, srgbEncoded);
119
Brian Salomon2a4f9832018-03-03 22:43:43 -0500120 sk_sp<GrTextureProxy> proxy =
Greg Daniel4065d452018-11-16 15:43:41 -0500121 proxyProvider->createMipMapProxy(format, desc, origin, SkBudgeted::kNo);
Brian Osman48c99192017-06-02 08:45:06 -0400122 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) ==
123 (caps->isConfigTexturable(desc.fConfig) &&
Brian Salomon57111332018-02-05 15:55:54 -0500124 caps->mipMapSupport()));
Brian Osman48c99192017-06-02 08:45:06 -0400125
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400126 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400127 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 bool isRenderable = caps->isConfigRenderable(config);
129 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
130 "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex),
131 isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400132
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 desc.fSampleCnt = 2;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400134 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500135 isRenderable = SkToBool(caps->getRenderTargetSampleCount(2, config));
136 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
137 "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex),
138 isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400139 }
140 }
141}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400142
Brian Salomond17b4a62017-05-23 16:53:47 -0400143#include "GrDrawingManager.h"
144#include "GrSurfaceProxy.h"
145#include "GrTextureContext.h"
146
147DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) {
148 static constexpr int kSize = 100;
149 GrSurfaceDesc desc;
150 desc.fWidth = desc.fHeight = kSize;
151 std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500152
Brian Salomond17b4a62017-05-23 16:53:47 -0400153 GrContext* context = context_info.grContext();
Greg Daniel4065d452018-11-16 15:43:41 -0500154 const GrCaps* caps = context->contextPriv().caps();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500155 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
156
Brian Salomond17b4a62017-05-23 16:53:47 -0400157 for (int c = 0; c <= kLast_GrPixelConfig; ++c) {
158 desc.fConfig = static_cast<GrPixelConfig>(c);
Greg Daniel4065d452018-11-16 15:43:41 -0500159 if (!caps->isConfigTexturable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400160 continue;
161 }
162 desc.fFlags = kPerformInitialClear_GrSurfaceFlag;
163 for (bool rt : {false, true}) {
Greg Daniel4065d452018-11-16 15:43:41 -0500164 if (rt && !caps->isConfigRenderable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400165 continue;
166 }
167 desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel90f28ec2017-09-25 12:26:58 -0400168 for (GrSurfaceOrigin origin :
169 {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500170 for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400171 // Try directly creating the texture.
172 // Do this twice in an attempt to hit the cache on the second time through.
173 for (int i = 0; i < 2; ++i) {
Chris Daltond004e0b2018-09-27 09:28:03 -0600174 auto proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500175 desc, origin, fit, SkBudgeted::kYes);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500176 if (!proxy) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400177 continue;
178 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400179 auto texCtx = context->contextPriv().makeWrappedSurfaceContext(
Brian Osman9aa30c62018-07-02 15:21:46 -0400180 std::move(proxy));
Greg Daniel90f28ec2017-09-25 12:26:58 -0400181 SkImageInfo info = SkImageInfo::Make(
182 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
183 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
184 if (texCtx->readPixels(info, data.get(), 0, 0, 0)) {
185 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
186 for (int i = 0; i < kSize * kSize; ++i) {
187 if (cmp != data.get()[i]) {
188 ERRORF(reporter, "Failed on config %d", desc.fConfig);
189 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400190 }
191 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400192 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400193 memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t));
194 // Here we overwrite the texture so that the second time through we
195 // test against recycling without reclearing.
196 if (0 == i) {
197 texCtx->writePixels(info, data.get(), 0, 0, 0);
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400198 }
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400199 }
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500200 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400201
Greg Daniel4065d452018-11-16 15:43:41 -0500202 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
203 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(desc.fConfig,
204 &srgbEncoded);
205 const GrBackendFormat format =
206 caps->getBackendFormatFromGrColorType(colorType, srgbEncoded);
207
Greg Daniel90f28ec2017-09-25 12:26:58 -0400208 // Try creating the texture as a deferred proxy.
209 for (int i = 0; i < 2; ++i) {
210 auto surfCtx = context->contextPriv().makeDeferredSurfaceContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500211 format, desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes);
Greg Daniel90f28ec2017-09-25 12:26:58 -0400212 if (!surfCtx) {
213 continue;
214 }
215 SkImageInfo info = SkImageInfo::Make(
216 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
217 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
218 if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) {
219 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
220 for (int i = 0; i < kSize * kSize; ++i) {
221 if (cmp != data.get()[i]) {
222 ERRORF(reporter, "Failed on config %d", desc.fConfig);
223 break;
224 }
225 }
226 }
227 // Here we overwrite the texture so that the second time through we
228 // test against recycling without reclearing.
229 if (0 == i) {
230 surfCtx->writePixels(info, data.get(), 0, 0, 0);
231 }
232 }
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500233 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Brian Salomond17b4a62017-05-23 16:53:47 -0400234 }
235 }
236 }
237 }
238}