junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 1 | /* |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 2 | * Copyright 2016 Google Inc. |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 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 | |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 8 | #include "GrContext.h" |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 9 | #include "GrContextPriv.h" |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 10 | #include "GrTexturePriv.h" |
| 11 | #include "SkCanvas.h" |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 12 | #include "SkImage_Base.h" |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 13 | #include "SkSurface.h" |
| 14 | #include "Test.h" |
| 15 | |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 16 | // Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures. |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 17 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrTextureMipMapInvalidationTest, reporter, ctxInfo) { |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 18 | auto isMipped = [] (SkSurface* surf) { |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 19 | const GrTexture* texture = surf->makeImageSnapshot()->getTexture(); |
| 20 | return GrMipMapped::kYes == texture->texturePriv().mipMapped(); |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | auto mipsAreDirty = [] (SkSurface* surf) { |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 24 | return surf->makeImageSnapshot()->getTexture()->texturePriv().mipMapsAreDirty(); |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 27 | GrContext* context = ctxInfo.grContext(); |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 28 | auto info = SkImageInfo::MakeN32Premul(256, 256); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 29 | for (auto allocateMips : {false, true}) { |
| 30 | auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0, |
| 31 | kBottomLeft_GrSurfaceOrigin, nullptr, |
| 32 | allocateMips); |
| 33 | auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info); |
| 34 | // Draw something just in case we ever had a solid color optimization |
| 35 | surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint()); |
| 36 | surf1->getCanvas()->flush(); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 37 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 38 | // No mipmaps initially |
| 39 | REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 40 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 41 | // Painting with downscale and medium filter quality should result in mipmap creation |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 42 | // Flush the context rather than the canvas as flushing the canvas triggers MIP level |
| 43 | // generation. |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 44 | SkPaint paint; |
| 45 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 46 | surf2->getCanvas()->scale(0.2f, 0.2f); |
| 47 | surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint); |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 48 | context->flush(); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 49 | REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips); |
| 50 | REPORTER_ASSERT(reporter, !allocateMips || !mipsAreDirty(surf1.get())); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 51 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 52 | // Changing the contents of the surface should invalidate the mipmap, but not de-allocate |
| 53 | surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint()); |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 54 | context->flush(); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips); |
| 56 | REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get())); |
| 57 | } |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 58 | } |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 59 | |
| 60 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReimportImageTextureWithMipLevels, reporter, ctxInfo) { |
| 61 | auto* ctx = ctxInfo.grContext(); |
| 62 | if (!ctx->contextPriv().caps()->mipMapSupport()) { |
| 63 | return; |
| 64 | } |
| 65 | static constexpr auto kCreateWithMipMaps = true; |
| 66 | auto surf = SkSurface::MakeRenderTarget( |
| 67 | ctx, SkBudgeted::kYes, |
| 68 | SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, |
| 69 | kTopLeft_GrSurfaceOrigin, nullptr, kCreateWithMipMaps); |
| 70 | if (!surf) { |
| 71 | return; |
| 72 | } |
| 73 | surf->getCanvas()->drawColor(SK_ColorDKGRAY); |
| 74 | auto img = surf->makeImageSnapshot(); |
| 75 | if (!img) { |
| 76 | return; |
| 77 | } |
| 78 | surf.reset(); |
| 79 | GrBackendTexture btex; |
| 80 | SkImage::BackendTextureReleaseProc texRelease; |
| 81 | if (!SkImage::MakeBackendTextureFromSkImage(ctx, std::move(img), &btex, &texRelease)) { |
| 82 | // Not all backends support stealing textures yet. |
| 83 | // ERRORF(reporter, "Could not turn image into texture"); |
| 84 | return; |
| 85 | } |
| 86 | REPORTER_ASSERT(reporter, btex.hasMipMaps()); |
| 87 | // Reimport the texture as an image and perform a downsampling draw with medium quality which |
| 88 | // should use the upper MIP levels. |
| 89 | img = SkImage::MakeFromTexture(ctx, btex, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 90 | kPremul_SkAlphaType, nullptr); |
| 91 | const auto singlePixelInfo = |
| 92 | SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr); |
| 93 | surf = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kYes, singlePixelInfo, 1, |
| 94 | kTopLeft_GrSurfaceOrigin, nullptr); |
| 95 | SkPaint paint; |
| 96 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 97 | surf->getCanvas()->drawImageRect(img, SkRect::MakeWH(1, 1), &paint); |
| 98 | uint32_t pixel; |
| 99 | surf->readPixels(singlePixelInfo, &pixel, sizeof(uint32_t), 0, 0); |
| 100 | REPORTER_ASSERT(reporter, pixel == SkPreMultiplyColor(SK_ColorDKGRAY)); |
| 101 | img.reset(); |
| 102 | texRelease(btex); |
| 103 | } |