blob: a01b8877906722e9b231a3bd55471d23a47fb4eb [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"
Robert Phillips6d344c32020-07-06 10:56:46 -040010#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrTexturePriv.h"
13#include "src/image/SkImage_Base.h"
Greg Daniel7c902112020-03-06 13:07:10 -050014#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
junovda5469d2015-06-15 09:48:15 -070016
bsalomonb1792f12016-07-14 09:24:08 -070017// Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures.
Brian Osman7eae5d72019-03-26 11:21:55 -040018DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTextureMipMapInvalidationTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040019 auto context = ctxInfo.directContext();
Brian Osman7eae5d72019-03-26 11:21:55 -040020 if (!context->priv().caps()->mipMapSupport()) {
21 return;
22 }
23
bsalomonb1792f12016-07-14 09:24:08 -070024 auto isMipped = [] (SkSurface* surf) {
Greg Daniel7c902112020-03-06 13:07:10 -050025 SkImage_GpuBase* image = static_cast<SkImage_GpuBase*>(as_IB(surf->makeImageSnapshot()));
26 const GrTexture* texture = image->getTexture();
Greg Daniele252f082017-10-23 16:05:23 -040027 return GrMipMapped::kYes == texture->texturePriv().mipMapped();
bsalomonb1792f12016-07-14 09:24:08 -070028 };
29
30 auto mipsAreDirty = [] (SkSurface* surf) {
Greg Daniel7c902112020-03-06 13:07:10 -050031 SkImage_GpuBase* image = static_cast<SkImage_GpuBase*>(as_IB(surf->makeImageSnapshot()));
32 return image->getTexture()->texturePriv().mipMapsAreDirty();
bsalomonb1792f12016-07-14 09:24:08 -070033 };
34
bsalomonb1792f12016-07-14 09:24:08 -070035 auto info = SkImageInfo::MakeN32Premul(256, 256);
Greg Daniel09c94002018-06-08 22:11:51 +000036 for (auto allocateMips : {false, true}) {
37 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0,
38 kBottomLeft_GrSurfaceOrigin, nullptr,
39 allocateMips);
40 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
41 // Draw something just in case we ever had a solid color optimization
42 surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint());
Greg Daniel0a2464f2020-05-14 15:45:44 -040043 surf1->flushAndSubmit();
junovda5469d2015-06-15 09:48:15 -070044
Greg Daniel09c94002018-06-08 22:11:51 +000045 // No mipmaps initially
46 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
junovda5469d2015-06-15 09:48:15 -070047
Greg Daniel09c94002018-06-08 22:11:51 +000048 // Painting with downscale and medium filter quality should result in mipmap creation
Brian Salomon930f9392018-06-20 16:25:26 -040049 // Flush the context rather than the canvas as flushing the canvas triggers MIP level
50 // generation.
Greg Daniel09c94002018-06-08 22:11:51 +000051 SkPaint paint;
52 paint.setFilterQuality(kMedium_SkFilterQuality);
53 surf2->getCanvas()->scale(0.2f, 0.2f);
54 surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint);
Greg Daniel0a2464f2020-05-14 15:45:44 -040055 context->flushAndSubmit();
Greg Daniel09c94002018-06-08 22:11:51 +000056 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
57 REPORTER_ASSERT(reporter, !allocateMips || !mipsAreDirty(surf1.get()));
junovda5469d2015-06-15 09:48:15 -070058
Greg Daniel09c94002018-06-08 22:11:51 +000059 // Changing the contents of the surface should invalidate the mipmap, but not de-allocate
60 surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint());
Greg Daniel0a2464f2020-05-14 15:45:44 -040061 context->flushAndSubmit();
Greg Daniel09c94002018-06-08 22:11:51 +000062 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
63 REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get()));
64 }
junovda5469d2015-06-15 09:48:15 -070065}
Brian Salomon930f9392018-06-20 16:25:26 -040066
67DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReimportImageTextureWithMipLevels, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040068 auto ctx = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050069 if (!ctx->priv().caps()->mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -040070 return;
71 }
72 static constexpr auto kCreateWithMipMaps = true;
73 auto surf = SkSurface::MakeRenderTarget(
74 ctx, SkBudgeted::kYes,
75 SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
76 kTopLeft_GrSurfaceOrigin, nullptr, kCreateWithMipMaps);
77 if (!surf) {
78 return;
79 }
80 surf->getCanvas()->drawColor(SK_ColorDKGRAY);
81 auto img = surf->makeImageSnapshot();
82 if (!img) {
83 return;
84 }
85 surf.reset();
86 GrBackendTexture btex;
87 SkImage::BackendTextureReleaseProc texRelease;
88 if (!SkImage::MakeBackendTextureFromSkImage(ctx, std::move(img), &btex, &texRelease)) {
89 // Not all backends support stealing textures yet.
90 // ERRORF(reporter, "Could not turn image into texture");
91 return;
92 }
93 REPORTER_ASSERT(reporter, btex.hasMipMaps());
94 // Reimport the texture as an image and perform a downsampling draw with medium quality which
95 // should use the upper MIP levels.
96 img = SkImage::MakeFromTexture(ctx, btex, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
97 kPremul_SkAlphaType, nullptr);
98 const auto singlePixelInfo =
99 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
100 surf = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kYes, singlePixelInfo, 1,
101 kTopLeft_GrSurfaceOrigin, nullptr);
102 SkPaint paint;
103 paint.setFilterQuality(kMedium_SkFilterQuality);
104 surf->getCanvas()->drawImageRect(img, SkRect::MakeWH(1, 1), &paint);
105 uint32_t pixel;
106 surf->readPixels(singlePixelInfo, &pixel, sizeof(uint32_t), 0, 0);
107 REPORTER_ASSERT(reporter, pixel == SkPreMultiplyColor(SK_ColorDKGRAY));
108 img.reset();
109 texRelease(btex);
110}