blob: 8b2d95faad0871688f3c8865c65f8a40e061898d [file] [log] [blame]
junovda5469d2015-06-15 09:48:15 -07001/*
bsalomonb1792f12016-07-14 09:24:08 -07002 * Copyright 2016 Google Inc.
junovda5469d2015-06-15 09:48:15 -07003 *
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"
junovda5469d2015-06-15 09:48:15 -070013#include "GrTexturePriv.h"
14#include "SkCanvas.h"
bsalomonb1792f12016-07-14 09:24:08 -070015#include "SkImage_Base.h"
junovda5469d2015-06-15 09:48:15 -070016#include "SkSurface.h"
17#include "Test.h"
18
bsalomonb1792f12016-07-14 09:24:08 -070019// Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures.
bsalomon758586c2016-04-06 14:02:39 -070020DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrTextureMipMapInvalidationTest, reporter, ctxInfo) {
bsalomonb1792f12016-07-14 09:24:08 -070021 auto isMipped = [] (SkSurface* surf) {
Robert Phillips87444052017-06-23 14:09:30 -040022 return surf->makeImageSnapshot()->getTexture()->texturePriv().hasMipMaps();
bsalomonb1792f12016-07-14 09:24:08 -070023 };
24
25 auto mipsAreDirty = [] (SkSurface* surf) {
Robert Phillips87444052017-06-23 14:09:30 -040026 return surf->makeImageSnapshot()->getTexture()->texturePriv().mipMapsAreDirty();
bsalomonb1792f12016-07-14 09:24:08 -070027 };
28
bsalomon8b7451a2016-05-11 06:33:06 -070029 GrContext* context = ctxInfo.grContext();
bsalomonb1792f12016-07-14 09:24:08 -070030 auto info = SkImageInfo::MakeN32Premul(256, 256);
31 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
32 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
33 // Draw something just in case we ever had a solid color optimization
34 surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint());
35 surf1->getCanvas()->flush();
junovda5469d2015-06-15 09:48:15 -070036
kkinnunen15302832015-12-01 04:35:26 -080037 // No mipmaps initially
bsalomonb1792f12016-07-14 09:24:08 -070038 REPORTER_ASSERT(reporter, !isMipped(surf1.get()));
junovda5469d2015-06-15 09:48:15 -070039
kkinnunen15302832015-12-01 04:35:26 -080040 // Painting with downscale and medium filter quality should result in mipmap creation
kkinnunen15302832015-12-01 04:35:26 -080041 SkPaint paint;
42 paint.setFilterQuality(kMedium_SkFilterQuality);
bsalomonb1792f12016-07-14 09:24:08 -070043 surf2->getCanvas()->scale(0.2f, 0.2f);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -040044 surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint);
bsalomonb1792f12016-07-14 09:24:08 -070045 surf2->getCanvas()->flush();
46 REPORTER_ASSERT(reporter, isMipped(surf1.get()));
47 REPORTER_ASSERT(reporter, !mipsAreDirty(surf1.get()));
junovda5469d2015-06-15 09:48:15 -070048
bsalomonb1792f12016-07-14 09:24:08 -070049 // Changing the contents of the surface should invalidate the mipmap, but not de-allocate
50 surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint());
51 surf1->getCanvas()->flush();
52 REPORTER_ASSERT(reporter, isMipped(surf1.get()));
53 REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get()));
junovda5469d2015-06-15 09:48:15 -070054}
55
56#endif