blob: 91fba5f919c3352571feb411688e74de9605ad60 [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,
Greg Danielf5d87582017-12-18 14:48:15 -050064 kRGBA_8888_SkColorType,
65 kPremul_SkAlphaType, nullptr,
66 nullptr, nullptr);
Robert Phillipse0070c02017-11-13 12:47:24 -050067 proxy = as_IB(image)->asTextureProxyRef();
Greg Daniel177e6952017-10-12 12:27:11 -040068 }
69 REPORTER_ASSERT(reporter, proxy);
70 if (!proxy) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050071 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040072 return;
73 }
74
75 REPORTER_ASSERT(reporter, proxy->priv().isInstantiated());
76
77 GrTexture* texture = proxy->priv().peekTexture();
78 REPORTER_ASSERT(reporter, texture);
79 if (!texture) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050080 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040081 return;
82 }
83
84 if (GrMipMapped::kYes == mipMapped) {
Greg Daniele252f082017-10-23 16:05:23 -040085 REPORTER_ASSERT(reporter, GrMipMapped::kYes == texture->texturePriv().mipMapped());
Greg Daniel177e6952017-10-12 12:27:11 -040086 if (isRT) {
87 REPORTER_ASSERT(reporter, texture->texturePriv().mipMapsAreDirty());
88 } else {
89 REPORTER_ASSERT(reporter, !texture->texturePriv().mipMapsAreDirty());
90 }
91 } else {
Greg Daniele252f082017-10-23 16:05:23 -040092 REPORTER_ASSERT(reporter, GrMipMapped::kNo == texture->texturePriv().mipMapped());
Greg Daniel177e6952017-10-12 12:27:11 -040093 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -050094 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel177e6952017-10-12 12:27:11 -040095 }
96 }
97}
98
Greg Daniel261b8aa2017-10-23 09:37:36 -040099// Test that we correctly copy or don't copy GrBackendTextures in the GrBackendTextureImageGenerator
100// based on if we will use mips in the draw and the mip status of the GrBackendTexture.
101DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrBackendTextureImageMipMappedTest, reporter, ctxInfo) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400102 GrContext* context = ctxInfo.grContext();
103 if (!context->caps()->mipMapSupport()) {
104 return;
105 }
106 for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) {
107 for (auto willUseMips : {false, true}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500108 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
Greg Daniel261b8aa2017-10-23 09:37:36 -0400109 nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, false, mipMapped);
110
Greg Daniel261b8aa2017-10-23 09:37:36 -0400111 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backendTex,
112 kTopLeft_GrSurfaceOrigin,
Greg Danielf5d87582017-12-18 14:48:15 -0500113 kRGBA_8888_SkColorType,
114 kPremul_SkAlphaType, nullptr,
115 nullptr, nullptr);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400116
117 GrTextureProxy* proxy = as_IB(image)->peekProxy();
118 REPORTER_ASSERT(reporter, proxy);
119 if (!proxy) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500120 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400121 return;
122 }
123
124 REPORTER_ASSERT(reporter, proxy->priv().isInstantiated());
125
126 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
127 REPORTER_ASSERT(reporter, texture);
128 if (!texture) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500129 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400130 return;
131 }
132
133 std::unique_ptr<SkImageGenerator> imageGen = GrBackendTextureImageGenerator::Make(
134 texture, kTopLeft_GrSurfaceOrigin, nullptr, kPremul_SkAlphaType, nullptr);
135 REPORTER_ASSERT(reporter, imageGen);
136 if (!imageGen) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500137 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400138 return;
139 }
140
141 SkIPoint origin = SkIPoint::Make(0,0);
142 // The transfer function behavior isn't used in the generator so set we set it
143 // arbitrarily here.
144 SkTransferFunctionBehavior behavior = SkTransferFunctionBehavior::kIgnore;
145 SkImageInfo imageInfo = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
146 kPremul_SkAlphaType);
147 sk_sp<GrTextureProxy> genProxy = imageGen->generateTexture(context, imageInfo,
148 origin, behavior,
149 willUseMips);
150
151 REPORTER_ASSERT(reporter, genProxy);
152 if (!genProxy) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500153 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400154 return;
155 }
156
157 REPORTER_ASSERT(reporter, genProxy->priv().isInstantiated());
158
159 GrTexture* genTexture = genProxy->priv().peekTexture();
160 REPORTER_ASSERT(reporter, genTexture);
161 if (!genTexture) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500162 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400163 return;
164 }
165
Robert Phillipsb67821d2017-12-13 15:00:45 -0500166 GrBackendTexture genBackendTex = genTexture->getBackendTexture();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400167
Robert Phillipsb67821d2017-12-13 15:00:45 -0500168 if (const GrGLTextureInfo* genTexInfo = genBackendTex.getGLTextureInfo()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400169 const GrGLTextureInfo* origTexInfo = backendTex.getGLTextureInfo();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400170 if (willUseMips && GrMipMapped::kNo == mipMapped) {
171 // We did a copy so the texture IDs should be different
172 REPORTER_ASSERT(reporter, origTexInfo->fID != genTexInfo->fID);
173 } else {
174 REPORTER_ASSERT(reporter, origTexInfo->fID == genTexInfo->fID);
175 }
Greg Daniel261b8aa2017-10-23 09:37:36 -0400176#ifdef SK_VULKAN
Robert Phillipsb67821d2017-12-13 15:00:45 -0500177 } else if (const GrVkImageInfo* genImageInfo = genBackendTex.getVkImageInfo()) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400178 const GrVkImageInfo* origImageInfo = backendTex.getVkImageInfo();
Greg Daniel261b8aa2017-10-23 09:37:36 -0400179 if (willUseMips && GrMipMapped::kNo == mipMapped) {
180 // We did a copy so the texture IDs should be different
181 REPORTER_ASSERT(reporter, origImageInfo->fImage != genImageInfo->fImage);
182 } else {
183 REPORTER_ASSERT(reporter, origImageInfo->fImage == genImageInfo->fImage);
184 }
185#endif
Greg Daniel261b8aa2017-10-23 09:37:36 -0400186 } else {
187 REPORTER_ASSERT(reporter, false);
188 }
189
190 // Must make sure the uses of the backend texture have finished (we possibly have a
191 // queued up copy) before we delete the backend texture. Thus we use readPixels here
192 // just to force the synchronization.
193 sk_sp<GrSurfaceContext> surfContext =
194 context->contextPriv().makeWrappedSurfaceContext(genProxy, nullptr);
195
196 SkBitmap bitmap;
197 bitmap.allocPixels(imageInfo);
198 surfContext->readPixels(imageInfo, bitmap.getPixels(), 0, 0, 0, 0);
199
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500200 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel261b8aa2017-10-23 09:37:36 -0400201 }
202 }
203}
204
Greg Daniel45d63032017-10-30 13:41:26 -0400205// Test that when we call makeImageSnapshot on an SkSurface we retains the same mip status as the
206// resource we took the snapshot of.
207DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxInfo) {
208 GrContext* context = ctxInfo.grContext();
209 if (!context->caps()->mipMapSupport()) {
210 return;
211 }
212
213 for (auto willUseMips : {false, true}) {
214 for (auto isWrapped : {false, true}) {
215 GrMipMapped mipMapped = willUseMips ? GrMipMapped::kYes : GrMipMapped::kNo;
216 sk_sp<SkSurface> surface;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500217 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
218 nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, true, mipMapped);
Greg Daniel45d63032017-10-30 13:41:26 -0400219 if (isWrapped) {
Greg Daniel45d63032017-10-30 13:41:26 -0400220 surface = SkSurface::MakeFromBackendTexture(context,
221 backendTex,
222 kTopLeft_GrSurfaceOrigin,
223 0,
224 nullptr,
225 nullptr);
226 } else {
227 SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
228 kPremul_SkAlphaType);
229 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0,
230 kTopLeft_GrSurfaceOrigin, nullptr,
231 willUseMips);
232 }
233 REPORTER_ASSERT(reporter, surface);
234 if (!surface) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500235 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400236 }
237 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
238 GrTextureProxy* texProxy = device->accessRenderTargetContext()->asTextureProxy();
239 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped());
240
241 texProxy->instantiate(context->resourceProvider());
242 GrTexture* texture = texProxy->priv().peekTexture();
243 REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped());
244
245 sk_sp<SkImage> image = surface->makeImageSnapshot();
246 REPORTER_ASSERT(reporter, image);
247 if (!image) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500248 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400249 }
250 texProxy = as_IB(image)->peekProxy();
251 REPORTER_ASSERT(reporter, mipMapped == texProxy->mipMapped());
252
253 texProxy->instantiate(context->resourceProvider());
254 texture = texProxy->priv().peekTexture();
255 REPORTER_ASSERT(reporter, mipMapped == texture->texturePriv().mipMapped());
256
257 // Must flush the context to make sure all the cmds (copies, etc.) from above are sent
258 // to the gpu before we delete the backendHandle.
259 context->flush();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500260 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel45d63032017-10-30 13:41:26 -0400261 }
262 }
263}
Greg Daniel261b8aa2017-10-23 09:37:36 -0400264
Greg Daniel177e6952017-10-12 12:27:11 -0400265#endif