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 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | |
| 12 | #include "GrContext.h" |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 13 | #include "GrTexture.h" |
| 14 | #include "GrTexturePriv.h" |
| 15 | #include "SkCanvas.h" |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 16 | #include "SkImage_Base.h" |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 17 | #include "SkSurface.h" |
| 18 | #include "Test.h" |
| 19 | |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 20 | // 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] | 21 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrTextureMipMapInvalidationTest, reporter, ctxInfo) { |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 22 | auto isMipped = [] (SkSurface* surf) { |
| 23 | return as_IB(surf->makeImageSnapshot(SkBudgeted::kYes))-> |
| 24 | peekTexture()->texturePriv().hasMipMaps(); |
| 25 | }; |
| 26 | |
| 27 | auto mipsAreDirty = [] (SkSurface* surf) { |
| 28 | return as_IB(surf->makeImageSnapshot(SkBudgeted::kYes))-> |
| 29 | peekTexture()->texturePriv().mipMapsAreDirty(); |
| 30 | }; |
| 31 | |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 32 | GrContext* context = ctxInfo.grContext(); |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 33 | auto info = SkImageInfo::MakeN32Premul(256, 256); |
| 34 | auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info); |
| 35 | auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info); |
| 36 | // Draw something just in case we ever had a solid color optimization |
| 37 | surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint()); |
| 38 | surf1->getCanvas()->flush(); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 39 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 40 | // No mipmaps initially |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, !isMipped(surf1.get())); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 42 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 43 | // Painting with downscale and medium filter quality should result in mipmap creation |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 44 | SkPaint paint; |
| 45 | paint.setFilterQuality(kMedium_SkFilterQuality); |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [diff] [blame] | 46 | surf2->getCanvas()->scale(0.2f, 0.2f); |
| 47 | surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(SkBudgeted::kYes), 0, 0, &paint); |
| 48 | surf2->getCanvas()->flush(); |
| 49 | REPORTER_ASSERT(reporter, isMipped(surf1.get())); |
| 50 | REPORTER_ASSERT(reporter, !mipsAreDirty(surf1.get())); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 51 | |
bsalomon | b1792f1 | 2016-07-14 09:24:08 -0700 | [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()); |
| 54 | surf1->getCanvas()->flush(); |
| 55 | REPORTER_ASSERT(reporter, isMipped(surf1.get())); |
| 56 | REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get())); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | #endif |