blob: 58faec4f8bd433e649c3362aa3793c1adc2aa759 [file] [log] [blame]
Greg Daniel177e6952017-10-12 12:27:11 -04001/*
2 * Copyright 2017 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#include "SkTypes.h"
9
10#if SK_SUPPORT_GPU
11
12#include "GrBackendSurface.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040013#include "GrBackendTextureImageGenerator.h"
Greg Daniel177e6952017-10-12 12:27:11 -040014#include "GrContext.h"
15#include "GrContextPriv.h"
16#include "GrGpu.h"
17#include "GrRenderTargetContext.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040018#include "GrSemaphore.h"
Greg Daniel177e6952017-10-12 12:27:11 -040019#include "GrSurfaceProxyPriv.h"
20#include "GrTest.h"
21#include "GrTexturePriv.h"
22#include "GrTextureProxy.h"
23#include "SkCanvas.h"
24#include "SkImage_Base.h"
25#include "SkGpuDevice.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040026#include "SkPoint.h"
Greg Daniel177e6952017-10-12 12:27:11 -040027#include "SkSurface.h"
28#include "SkSurface_Gpu.h"
29#include "Test.h"
30
Greg Daniel45d63032017-10-30 13:41:26 -040031static constexpr int kSize = 8;
32
Greg Daniel177e6952017-10-12 12:27:11 -040033// Test that the correct mip map states are on the GrTextures when wrapping GrBackendTextures in
34// SkImages and SkSurfaces
35DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrWrappedMipMappedTest, reporter, ctxInfo) {
36 GrContext* context = ctxInfo.grContext();
Greg Daniel261b8aa2017-10-23 09:37:36 -040037 if (!context->caps()->mipMapSupport()) {
38 return;
39 }
Greg Daniel177e6952017-10-12 12:27:11 -040040 for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) {
41 for (auto isRT : {false, true}) {
42 // CreateTestingOnlyBackendTexture currently doesn't support uploading data to mip maps
43 // so we don't send any. However, we pretend there is data for the checks below which is
44 // fine since we are never actually using these textures for any work on the gpu.
Robert Phillipsd21b2a52017-12-12 13:01:25 -050045 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
Robert Phillips57e08282017-11-16 14:59:48 -050046 nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, isRT, mipMapped);
Greg Daniel177e6952017-10-12 12:27:11 -040047
Robert Phillipse0070c02017-11-13 12:47:24 -050048 sk_sp<GrTextureProxy> proxy;
Greg Daniel177e6952017-10-12 12:27:11 -040049 sk_sp<SkImage> image;
50 if (isRT) {
51 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(
52 context,
53 backendTex,
54 kTopLeft_GrSurfaceOrigin,
55 0,
56 nullptr,
57 nullptr);
58
59 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
Robert Phillipse0070c02017-11-13 12:47:24 -050060 proxy = device->accessRenderTargetContext()->asTextureProxyRef();
Greg Daniel177e6952017-10-12 12:27:11 -040061 } else {
62 image = SkImage::MakeFromTexture(context, backendTex,
63 kTopLeft_GrSurfaceOrigin,
64 kPremul_SkAlphaType, nullptr);
Robert Phillipse0070c02017-11-13 12:47:24 -050065 proxy = as_IB(image)->asTextureProxyRef();
Greg Daniel177e6952017-10-12 12:27:11 -040066 }
67 REPORTER_ASSERT(reporter, proxy);
68 if (!proxy) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050069 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040070 return;
71 }
72
73 REPORTER_ASSERT(reporter, proxy->priv().isInstantiated());
74
75 GrTexture* texture = proxy->priv().peekTexture();
76 REPORTER_ASSERT(reporter, texture);
77 if (!texture) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050078 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040079 return;
80 }
81
82 if (GrMipMapped::kYes == mipMapped) {
Greg Daniele252f082017-10-23 16:05:23 -040083 REPORTER_ASSERT(reporter, GrMipMapped::kYes == texture->texturePriv().mipMapped());
Greg Daniel177e6952017-10-12 12:27:11 -040084 if (isRT) {
85 REPORTER_ASSERT(reporter, texture->texturePriv().mipMapsAreDirty());
86 } else {
87 REPORTER_ASSERT(reporter, !texture->texturePriv().mipMapsAreDirty());
88 }
89 } else {
Greg Daniele252f082017-10-23 16:05:23 -040090 REPORTER_ASSERT(reporter, GrMipMapped::kNo == texture->texturePriv().mipMapped());
Greg Daniel177e6952017-10-12 12:27:11 -040091 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -050092 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040093 }
94 }
95}
96
Greg Daniel261b8aa2017-10-23 09:37:36 -040097// Test that we correctly copy or don't copy GrBackendTextures in the GrBackendTextureImageGenerator
98// based on if we will use mips in the draw and the mip status of the GrBackendTexture.
99DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrBackendTextureImageMipMappedTest, reporter, ctxInfo) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400100 GrContext* context = ctxInfo.grContext();
101 if (!context->caps()->mipMapSupport()) {
102 return;
103 }
104 for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) {
105 for (auto willUseMips : {false, true}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500106 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
Greg Daniel261b8aa2017-10-23 09:37:36 -0400107 nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, false, mipMapped);
108
Greg Daniel261b8aa2017-10-23 09:37:36 -0400109 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backendTex,
110 kTopLeft_GrSurfaceOrigin,
111 kPremul_SkAlphaType, nullptr);
112
113 GrTextureProxy* proxy = as_IB(image)->peekProxy();
114 REPORTER_ASSERT(reporter, proxy);
115 if (!proxy) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500116 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400117 return;
118 }
119
120 REPORTER_ASSERT(reporter, proxy->priv().isInstantiated());
121
122 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
123 REPORTER_ASSERT(reporter, texture);
124 if (!texture) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500125 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400126 return;
127 }
128
129 std::unique_ptr<SkImageGenerator> imageGen = GrBackendTextureImageGenerator::Make(
130 texture, kTopLeft_GrSurfaceOrigin, nullptr, kPremul_SkAlphaType, nullptr);
131 REPORTER_ASSERT(reporter, imageGen);
132 if (!imageGen) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500133 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400134 return;
135 }
136
137 SkIPoint origin = SkIPoint::Make(0,0);
138 // The transfer function behavior isn't used in the generator so set we set it
139 // arbitrarily here.
140 SkTransferFunctionBehavior behavior = SkTransferFunctionBehavior::kIgnore;
141 SkImageInfo imageInfo = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
142 kPremul_SkAlphaType);
143 sk_sp<GrTextureProxy> genProxy = imageGen->generateTexture(context, imageInfo,
144 origin, behavior,
145 willUseMips);
146
147 REPORTER_ASSERT(reporter, genProxy);
148 if (!genProxy) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500149 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400150 return;
151 }
152
153 REPORTER_ASSERT(reporter, genProxy->priv().isInstantiated());
154
155 GrTexture* genTexture = genProxy->priv().peekTexture();
156 REPORTER_ASSERT(reporter, genTexture);
157 if (!genTexture) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500158 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400159 return;
160 }
161
Robert Phillipsb67821d2017-12-13 15:00:45 -0500162 GrBackendTexture genBackendTex = genTexture->getBackendTexture();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400163
Robert Phillipsb67821d2017-12-13 15:00:45 -0500164 if (const GrGLTextureInfo* genTexInfo = genBackendTex.getGLTextureInfo()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400165 const GrGLTextureInfo* origTexInfo = backendTex.getGLTextureInfo();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400166 if (willUseMips && GrMipMapped::kNo == mipMapped) {
167 // We did a copy so the texture IDs should be different
168 REPORTER_ASSERT(reporter, origTexInfo->fID != genTexInfo->fID);
169 } else {
170 REPORTER_ASSERT(reporter, origTexInfo->fID == genTexInfo->fID);
171 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400172#ifdef SK_VULKAN
Robert Phillipsb67821d2017-12-13 15:00:45 -0500173 } else if (const GrVkImageInfo* genImageInfo = genBackendTex.getVkImageInfo()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400174 const GrVkImageInfo* origImageInfo = backendTex.getVkImageInfo();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400175 if (willUseMips && GrMipMapped::kNo == mipMapped) {
176 // We did a copy so the texture IDs should be different
177 REPORTER_ASSERT(reporter, origImageInfo->fImage != genImageInfo->fImage);
178 } else {
179 REPORTER_ASSERT(reporter, origImageInfo->fImage == genImageInfo->fImage);
180 }
181#endif
Greg Daniel261b8aa2017-10-23 09:37:36 -0400182 } else {
183 REPORTER_ASSERT(reporter, false);
184 }
185
186 // Must make sure the uses of the backend texture have finished (we possibly have a
187 // queued up copy) before we delete the backend texture. Thus we use readPixels here
188 // just to force the synchronization.
189 sk_sp<GrSurfaceContext> surfContext =
190 context->contextPriv().makeWrappedSurfaceContext(genProxy, nullptr);
191
192 SkBitmap bitmap;
193 bitmap.allocPixels(imageInfo);
194 surfContext->readPixels(imageInfo, bitmap.getPixels(), 0, 0, 0, 0);
195
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500196 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400197 }
198 }
199}
200
Greg Daniel45d63032017-10-30 13:41:26 -0400201// Test that when we call makeImageSnapshot on an SkSurface we retains the same mip status as the
202// resource we took the snapshot of.
203DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxInfo) {
204 GrContext* context = ctxInfo.grContext();
205 if (!context->caps()->mipMapSupport()) {
206 return;
207 }
208
209 for (auto willUseMips : {false, true}) {
210 for (auto isWrapped : {false, true}) {
211 GrMipMapped mipMapped = willUseMips ? GrMipMapped::kYes : GrMipMapped::kNo;
212 sk_sp<SkSurface> surface;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500213 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
214 nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, true, mipMapped);
Greg Daniel45d63032017-10-30 13:41:26 -0400215 if (isWrapped) {
Greg Daniel45d63032017-10-30 13:41:26 -0400216 surface = SkSurface::MakeFromBackendTexture(context,
217 backendTex,
218 kTopLeft_GrSurfaceOrigin,
219 0,
220 nullptr,
221 nullptr);
222 } else {
223 SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
224 kPremul_SkAlphaType);
225 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0,
226 kTopLeft_GrSurfaceOrigin, nullptr,
227 willUseMips);
228 }
229 REPORTER_ASSERT(reporter, surface);
230 if (!surface) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500231 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400232 }
233 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
234 GrTextureProxy* texProxy = device->accessRenderTargetContext()->asTextureProxy();
235 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped());
236
237 texProxy->instantiate(context->resourceProvider());
238 GrTexture* texture = texProxy->priv().peekTexture();
239 REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped());
240
241 sk_sp<SkImage> image = surface->makeImageSnapshot();
242 REPORTER_ASSERT(reporter, image);
243 if (!image) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500244 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400245 }
246 texProxy = as_IB(image)->peekProxy();
247 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped());
248
249 texProxy->instantiate(context->resourceProvider());
250 texture = texProxy->priv().peekTexture();
251 REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped());
252
253 // Must flush the context to make sure all the cmds (copies, etc.) from above are sent
254 // to the gpu before we delete the backendHandle.
255 context->flush();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500256 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400257 }
258 }
259}
Greg Daniel261b8aa2017-10-23 09:37:36 -0400260
Greg Daniel177e6952017-10-12 12:27:11 -0400261#endif