blob: fb5706044f3c8610068330b712d6b9389bbe1f1c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
10#include "include/gpu/GrContext.h"
11#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrTexturePriv.h"
13#include "src/image/SkImage_Base.h"
14#include "tests/Test.h"
junovda5469d2015-06-15 09:48:15 -070015
bsalomonb1792f12016-07-14 09:24:08 -070016// Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures.
Brian Osman7eae5d72019-03-26 11:21:55 -040017DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTextureMipMapInvalidationTest, reporter, ctxInfo) {
18 GrContext* context = ctxInfo.grContext();
19 if (!context->priv().caps()->mipMapSupport()) {
20 return;
21 }
22
bsalomonb1792f12016-07-14 09:24:08 -070023 auto isMipped = [] (SkSurface* surf) {
Greg Daniele252f082017-10-23 16:05:23 -040024 const GrTexture* texture = surf->makeImageSnapshot()->getTexture();
25 return GrMipMapped::kYes == texture->texturePriv().mipMapped();
bsalomonb1792f12016-07-14 09:24:08 -070026 };
27
28 auto mipsAreDirty = [] (SkSurface* surf) {
Robert Phillips87444052017-06-23 14:09:30 -040029 return surf->makeImageSnapshot()->getTexture()->texturePriv().mipMapsAreDirty();
bsalomonb1792f12016-07-14 09:24:08 -070030 };
31
bsalomonb1792f12016-07-14 09:24:08 -070032 auto info = SkImageInfo::MakeN32Premul(256, 256);
Greg Daniel09c94002018-06-08 22:11:51 +000033 for (auto allocateMips : {false, true}) {
34 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0,
35 kBottomLeft_GrSurfaceOrigin, nullptr,
36 allocateMips);
37 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
38 // Draw something just in case we ever had a solid color optimization
39 surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint());
Robert Phillips9882dae2019-03-04 11:00:10 -050040 surf1->flush();
junovda5469d2015-06-15 09:48:15 -070041
Greg Daniel09c94002018-06-08 22:11:51 +000042 // No mipmaps initially
43 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
junovda5469d2015-06-15 09:48:15 -070044
Greg Daniel09c94002018-06-08 22:11:51 +000045 // Painting with downscale and medium filter quality should result in mipmap creation
Brian Salomon930f9392018-06-20 16:25:26 -040046 // Flush the context rather than the canvas as flushing the canvas triggers MIP level
47 // generation.
Greg Daniel09c94002018-06-08 22:11:51 +000048 SkPaint paint;
49 paint.setFilterQuality(kMedium_SkFilterQuality);
50 surf2->getCanvas()->scale(0.2f, 0.2f);
51 surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint);
Brian Salomon930f9392018-06-20 16:25:26 -040052 context->flush();
Greg Daniel09c94002018-06-08 22:11:51 +000053 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
54 REPORTER_ASSERT(reporter, !allocateMips || !mipsAreDirty(surf1.get()));
junovda5469d2015-06-15 09:48:15 -070055
Greg Daniel09c94002018-06-08 22:11:51 +000056 // Changing the contents of the surface should invalidate the mipmap, but not de-allocate
57 surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint());
Brian Salomon930f9392018-06-20 16:25:26 -040058 context->flush();
Greg Daniel09c94002018-06-08 22:11:51 +000059 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
60 REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get()));
61 }
junovda5469d2015-06-15 09:48:15 -070062}
Brian Salomon930f9392018-06-20 16:25:26 -040063
64DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReimportImageTextureWithMipLevels, reporter, ctxInfo) {
65 auto* ctx = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050066 if (!ctx->priv().caps()->mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -040067 return;
68 }
69 static constexpr auto kCreateWithMipMaps = true;
70 auto surf = SkSurface::MakeRenderTarget(
71 ctx, SkBudgeted::kYes,
72 SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
73 kTopLeft_GrSurfaceOrigin, nullptr, kCreateWithMipMaps);
74 if (!surf) {
75 return;
76 }
77 surf->getCanvas()->drawColor(SK_ColorDKGRAY);
78 auto img = surf->makeImageSnapshot();
79 if (!img) {
80 return;
81 }
82 surf.reset();
83 GrBackendTexture btex;
84 SkImage::BackendTextureReleaseProc texRelease;
85 if (!SkImage::MakeBackendTextureFromSkImage(ctx, std::move(img), &btex, &texRelease)) {
86 // Not all backends support stealing textures yet.
87 // ERRORF(reporter, "Could not turn image into texture");
88 return;
89 }
90 REPORTER_ASSERT(reporter, btex.hasMipMaps());
91 // Reimport the texture as an image and perform a downsampling draw with medium quality which
92 // should use the upper MIP levels.
93 img = SkImage::MakeFromTexture(ctx, btex, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
94 kPremul_SkAlphaType, nullptr);
95 const auto singlePixelInfo =
96 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
97 surf = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kYes, singlePixelInfo, 1,
98 kTopLeft_GrSurfaceOrigin, nullptr);
99 SkPaint paint;
100 paint.setFilterQuality(kMedium_SkFilterQuality);
101 surf->getCanvas()->drawImageRect(img, SkRect::MakeWH(1, 1), &paint);
102 uint32_t pixel;
103 surf->readPixels(singlePixelInfo, &pixel, sizeof(uint32_t), 0, 0);
104 REPORTER_ASSERT(reporter, pixel == SkPreMultiplyColor(SK_ColorDKGRAY));
105 img.reset();
106 texRelease(btex);
107}