blob: bdec795a43c8b07a0373d2c6c004115d4177a6f6 [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
Brian Salomon2a4f9832018-03-03 22:43:43 -0500115 sk_sp<GrTextureProxy> proxy =
116 proxyProvider->createMipMapProxy(desc, origin, SkBudgeted::kNo);
Brian Osman48c99192017-06-02 08:45:06 -0400117 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) ==
118 (caps->isConfigTexturable(desc.fConfig) &&
Brian Salomon57111332018-02-05 15:55:54 -0500119 caps->mipMapSupport()));
Brian Osman48c99192017-06-02 08:45:06 -0400120
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400121 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400122 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500123 bool isRenderable = caps->isConfigRenderable(config);
124 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
125 "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex),
126 isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400127
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 desc.fSampleCnt = 2;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400129 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500130 isRenderable = SkToBool(caps->getRenderTargetSampleCount(2, config));
131 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
132 "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex),
133 isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400134 }
135 }
136}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400137
Brian Salomond17b4a62017-05-23 16:53:47 -0400138#include "GrDrawingManager.h"
139#include "GrSurfaceProxy.h"
140#include "GrTextureContext.h"
141
142DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) {
143 static constexpr int kSize = 100;
144 GrSurfaceDesc desc;
145 desc.fWidth = desc.fHeight = kSize;
146 std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500147
Brian Salomond17b4a62017-05-23 16:53:47 -0400148 GrContext* context = context_info.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500149 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
150
Brian Salomond17b4a62017-05-23 16:53:47 -0400151 for (int c = 0; c <= kLast_GrPixelConfig; ++c) {
152 desc.fConfig = static_cast<GrPixelConfig>(c);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400153 if (!context->contextPriv().caps()->isConfigTexturable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400154 continue;
155 }
156 desc.fFlags = kPerformInitialClear_GrSurfaceFlag;
157 for (bool rt : {false, true}) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400158 if (rt && !context->contextPriv().caps()->isConfigRenderable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400159 continue;
160 }
161 desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel90f28ec2017-09-25 12:26:58 -0400162 for (GrSurfaceOrigin origin :
163 {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500164 for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400165 // Try directly creating the texture.
166 // Do this twice in an attempt to hit the cache on the second time through.
167 for (int i = 0; i < 2; ++i) {
Chris Daltond004e0b2018-09-27 09:28:03 -0600168 auto proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500169 desc, origin, fit, SkBudgeted::kYes);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500170 if (!proxy) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400171 continue;
172 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400173 auto texCtx = context->contextPriv().makeWrappedSurfaceContext(
Brian Osman9aa30c62018-07-02 15:21:46 -0400174 std::move(proxy));
Greg Daniel90f28ec2017-09-25 12:26:58 -0400175 SkImageInfo info = SkImageInfo::Make(
176 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
177 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
178 if (texCtx->readPixels(info, data.get(), 0, 0, 0)) {
179 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
180 for (int i = 0; i < kSize * kSize; ++i) {
181 if (cmp != data.get()[i]) {
182 ERRORF(reporter, "Failed on config %d", desc.fConfig);
183 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400184 }
185 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400186 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400187 memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t));
188 // Here we overwrite the texture so that the second time through we
189 // test against recycling without reclearing.
190 if (0 == i) {
191 texCtx->writePixels(info, data.get(), 0, 0, 0);
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400192 }
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400193 }
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500194 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400195
196 // Try creating the texture as a deferred proxy.
197 for (int i = 0; i < 2; ++i) {
198 auto surfCtx = context->contextPriv().makeDeferredSurfaceContext(
Brian Osman9aa30c62018-07-02 15:21:46 -0400199 desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes);
Greg Daniel90f28ec2017-09-25 12:26:58 -0400200 if (!surfCtx) {
201 continue;
202 }
203 SkImageInfo info = SkImageInfo::Make(
204 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
205 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
206 if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) {
207 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
208 for (int i = 0; i < kSize * kSize; ++i) {
209 if (cmp != data.get()[i]) {
210 ERRORF(reporter, "Failed on config %d", desc.fConfig);
211 break;
212 }
213 }
214 }
215 // Here we overwrite the texture so that the second time through we
216 // test against recycling without reclearing.
217 if (0 == i) {
218 surfCtx->writePixels(info, data.get(), 0, 0, 0);
219 }
220 }
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500221 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Brian Salomond17b4a62017-05-23 16:53:47 -0400222 }
223 }
224 }
225 }
226}