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