blob: e8f6bcce6a31df2351b14c3a890ef7b4082c14b0 [file] [log] [blame]
junovda5469d2015-06-15 09:48:15 -07001/*
2 * Copyright 2013 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"
junovda5469d2015-06-15 09:48:15 -070013#include "GrTexture.h"
14#include "GrTexturePriv.h"
15#include "SkCanvas.h"
16#include "SkGr.h"
17#include "SkSurface.h"
18#include "Test.h"
19
20// Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture
21// and render targets to GrSurface all work as expected.
bsalomon758586c2016-04-06 14:02:39 -070022DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrTextureMipMapInvalidationTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070023 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080024 GrSurfaceDesc desc;
25 desc.fConfig = kSkia8888_GrPixelConfig;
26 desc.fFlags = kRenderTarget_GrSurfaceFlag;
27 desc.fWidth = 256;
28 desc.fHeight = 256;
29 desc.fSampleCnt = 0;
bsalomon5ec26ae2016-02-25 08:33:02 -080030 GrSurface* texRT1 = context->textureProvider()->createTexture(
31 desc, SkBudgeted::kNo, nullptr, 0);
32 GrSurface* texRT2 = context->textureProvider()->createTexture(
33 desc, SkBudgeted::kNo, nullptr, 0);
kkinnunen15302832015-12-01 04:35:26 -080034 REPORTER_ASSERT(reporter, nullptr != texRT1);
35 REPORTER_ASSERT(reporter, nullptr != texRT2);
36 GrTexture* tex = texRT1->asTexture();
37 REPORTER_ASSERT(reporter, nullptr != tex);
38 SkBitmap bitmap;
39 GrWrapTextureInBitmap(tex, 256, 256, false, &bitmap);
junovda5469d2015-06-15 09:48:15 -070040
kkinnunen15302832015-12-01 04:35:26 -080041 // No mipmaps initially
42 REPORTER_ASSERT(reporter, false == tex->texturePriv().hasMipMaps());
junovda5469d2015-06-15 09:48:15 -070043
kkinnunen15302832015-12-01 04:35:26 -080044 // Painting with downscale and medium filter quality should result in mipmap creation
reede8f30622016-03-23 18:59:25 -070045 auto surface = SkSurface::MakeRenderTargetDirect(texRT2->asRenderTarget());
kkinnunen15302832015-12-01 04:35:26 -080046 SkPaint paint;
47 paint.setFilterQuality(kMedium_SkFilterQuality);
48 surface->getCanvas()->scale(0.2f, 0.2f);
49 surface->getCanvas()->drawBitmap(bitmap, 0, 0, &paint);
50 context->flush();
junovda5469d2015-06-15 09:48:15 -070051
kkinnunen15302832015-12-01 04:35:26 -080052 REPORTER_ASSERT(reporter, true == tex->texturePriv().hasMipMaps());
53 REPORTER_ASSERT(reporter, false == tex->texturePriv().mipMapsAreDirty());
junovda5469d2015-06-15 09:48:15 -070054
kkinnunen15302832015-12-01 04:35:26 -080055 // Invalidating the contents of the bitmap should invalidate the mipmap, but not de-allocate
56 bitmap.notifyPixelsChanged();
57 REPORTER_ASSERT(reporter, true == tex->texturePriv().hasMipMaps());
58 REPORTER_ASSERT(reporter, true == tex->texturePriv().mipMapsAreDirty());
junovda5469d2015-06-15 09:48:15 -070059
kkinnunen15302832015-12-01 04:35:26 -080060 texRT1->unref();
61 texRT2->unref();
junovda5469d2015-06-15 09:48:15 -070062}
63
64#endif