blob: 8da79c9565830bb0243bc0e3fddf92dbf31f7dfc [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
junovda5469d2015-06-15 09:48:15 -070010#include "GrContext.h"
junovda5469d2015-06-15 09:48:15 -070011#include "GrTexturePriv.h"
12#include "SkCanvas.h"
bsalomonb1792f12016-07-14 09:24:08 -070013#include "SkImage_Base.h"
junovda5469d2015-06-15 09:48:15 -070014#include "SkSurface.h"
15#include "Test.h"
16
bsalomonb1792f12016-07-14 09:24:08 -070017// Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures.
bsalomon758586c2016-04-06 14:02:39 -070018DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrTextureMipMapInvalidationTest, reporter, ctxInfo) {
bsalomonb1792f12016-07-14 09:24:08 -070019 auto isMipped = [] (SkSurface* surf) {
Greg Daniele252f082017-10-23 16:05:23 -040020 const GrTexture* texture = surf->makeImageSnapshot()->getTexture();
21 return GrMipMapped::kYes == texture->texturePriv().mipMapped();
bsalomonb1792f12016-07-14 09:24:08 -070022 };
23
24 auto mipsAreDirty = [] (SkSurface* surf) {
Robert Phillips87444052017-06-23 14:09:30 -040025 return surf->makeImageSnapshot()->getTexture()->texturePriv().mipMapsAreDirty();
bsalomonb1792f12016-07-14 09:24:08 -070026 };
27
bsalomon8b7451a2016-05-11 06:33:06 -070028 GrContext* context = ctxInfo.grContext();
bsalomonb1792f12016-07-14 09:24:08 -070029 auto info = SkImageInfo::MakeN32Premul(256, 256);
Greg Daniel09c94002018-06-08 22:11:51 +000030 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();
junovda5469d2015-06-15 09:48:15 -070038
Greg Daniel09c94002018-06-08 22:11:51 +000039 // No mipmaps initially
40 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
junovda5469d2015-06-15 09:48:15 -070041
Greg Daniel09c94002018-06-08 22:11:51 +000042 // 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()));
junovda5469d2015-06-15 09:48:15 -070050
Greg Daniel09c94002018-06-08 22:11:51 +000051 // 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 }
junovda5469d2015-06-15 09:48:15 -070057}