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 | |
| 8 | #include "SkTypes.h" |
| 9 | |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 11 | #include "GrTexturePriv.h" |
| 12 | #include "SkCanvas.h" |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 13 | #include "SkImage_Base.h" |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 14 | #include "SkSurface.h" |
| 15 | #include "Test.h" |
| 16 | |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 17 | // 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] | 18 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrTextureMipMapInvalidationTest, reporter, ctxInfo) { |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 19 | auto isMipped = [] (SkSurface* surf) { |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 20 | const GrTexture* texture = surf->makeImageSnapshot()->getTexture(); |
| 21 | return GrMipMapped::kYes == texture->texturePriv().mipMapped(); |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | auto mipsAreDirty = [] (SkSurface* surf) { |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 25 | return surf->makeImageSnapshot()->getTexture()->texturePriv().mipMapsAreDirty(); |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 28 | GrContext* context = ctxInfo.grContext(); |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 29 | auto info = SkImageInfo::MakeN32Premul(256, 256); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 30 | for (auto allocateMips : {false, true}) { |
| 31 | auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0, |
| 32 | kBottomLeft_GrSurfaceOrigin, nullptr, |
| 33 | allocateMips); |
| 34 | auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info); |
| 35 | // Draw something just in case we ever had a solid color optimization |
| 36 | surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint()); |
| 37 | surf1->getCanvas()->flush(); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 38 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 39 | // No mipmaps initially |
| 40 | REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips); |
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 | // Painting with downscale and medium filter quality should result in mipmap creation |
| 43 | SkPaint paint; |
| 44 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 45 | surf2->getCanvas()->scale(0.2f, 0.2f); |
| 46 | surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint); |
| 47 | surf2->getCanvas()->flush(); |
| 48 | REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips); |
| 49 | REPORTER_ASSERT(reporter, !allocateMips || !mipsAreDirty(surf1.get())); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 50 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 51 | // Changing the contents of the surface should invalidate the mipmap, but not de-allocate |
| 52 | surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint()); |
| 53 | surf1->getCanvas()->flush(); |
| 54 | REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips); |
| 55 | REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get())); |
| 56 | } |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 57 | } |