1/* 2 * Copyright 2016 Google Inc. 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" 13#include "GrTexturePriv.h" 14#include "SkCanvas.h" 15#include "SkImage_Base.h" 16#include "SkSurface.h" 17#include "Test.h" 18 19// Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures. 20DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrTextureMipMapInvalidationTest, reporter, ctxInfo) { 21 auto isMipped = [] (SkSurface* surf) { 22 const GrTexture* texture = surf->makeImageSnapshot()->getTexture(); 23 return GrMipMapped::kYes == texture->texturePriv().mipMapped(); 24 }; 25 26 auto mipsAreDirty = [] (SkSurface* surf) { 27 return surf->makeImageSnapshot()->getTexture()->texturePriv().mipMapsAreDirty(); 28 }; 29 30 GrContext* context = ctxInfo.grContext(); 31 auto info = SkImageInfo::MakeN32Premul(256, 256); 32 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info); 33 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info); 34 // Draw something just in case we ever had a solid color optimization 35 surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint()); 36 surf1->getCanvas()->flush(); 37 38 // No mipmaps initially 39 REPORTER_ASSERT(reporter, !isMipped(surf1.get())); 40 41 // Painting with downscale and medium filter quality should result in mipmap creation 42 SkPaint paint; 43 paint.setFilterQuality(kMedium_SkFilterQuality); 44 surf2->getCanvas()->scale(0.2f, 0.2f); 45 surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint); 46 surf2->getCanvas()->flush(); 47 REPORTER_ASSERT(reporter, isMipped(surf1.get())); 48 REPORTER_ASSERT(reporter, !mipsAreDirty(surf1.get())); 49 50 // Changing the contents of the surface should invalidate the mipmap, but not de-allocate 51 surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint()); 52 surf1->getCanvas()->flush(); 53 REPORTER_ASSERT(reporter, isMipped(surf1.get())); 54 REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get())); 55} 56 57#endif 58