blob: ea2d0de107044581f27a18e9681d84f93f2a04ea [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
junovda5469d2015-06-15 09:48:15 -07008#include "GrContext.h"
Brian Salomon930f9392018-06-20 16:25:26 -04009#include "GrContextPriv.h"
junovda5469d2015-06-15 09:48:15 -070010#include "GrTexturePriv.h"
11#include "SkCanvas.h"
bsalomonb1792f12016-07-14 09:24:08 -070012#include "SkImage_Base.h"
junovda5469d2015-06-15 09:48:15 -070013#include "SkSurface.h"
14#include "Test.h"
15
bsalomonb1792f12016-07-14 09:24:08 -070016// Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures.
bsalomon758586c2016-04-06 14:02:39 -070017DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrTextureMipMapInvalidationTest, reporter, ctxInfo) {
bsalomonb1792f12016-07-14 09:24:08 -070018 auto isMipped = [] (SkSurface* surf) {
Greg Daniele252f082017-10-23 16:05:23 -040019 const GrTexture* texture = surf->makeImageSnapshot()->getTexture();
20 return GrMipMapped::kYes == texture->texturePriv().mipMapped();
bsalomonb1792f12016-07-14 09:24:08 -070021 };
22
23 auto mipsAreDirty = [] (SkSurface* surf) {
Robert Phillips87444052017-06-23 14:09:30 -040024 return surf->makeImageSnapshot()->getTexture()->texturePriv().mipMapsAreDirty();
bsalomonb1792f12016-07-14 09:24:08 -070025 };
26
bsalomon8b7451a2016-05-11 06:33:06 -070027 GrContext* context = ctxInfo.grContext();
bsalomonb1792f12016-07-14 09:24:08 -070028 auto info = SkImageInfo::MakeN32Premul(256, 256);
Greg Daniel09c94002018-06-08 22:11:51 +000029 for (auto allocateMips : {false, true}) {
30 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0,
31 kBottomLeft_GrSurfaceOrigin, nullptr,
32 allocateMips);
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();
junovda5469d2015-06-15 09:48:15 -070037
Greg Daniel09c94002018-06-08 22:11:51 +000038 // No mipmaps initially
39 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
junovda5469d2015-06-15 09:48:15 -070040
Greg Daniel09c94002018-06-08 22:11:51 +000041 // Painting with downscale and medium filter quality should result in mipmap creation
Brian Salomon930f9392018-06-20 16:25:26 -040042 // Flush the context rather than the canvas as flushing the canvas triggers MIP level
43 // generation.
Greg Daniel09c94002018-06-08 22:11:51 +000044 SkPaint paint;
45 paint.setFilterQuality(kMedium_SkFilterQuality);
46 surf2->getCanvas()->scale(0.2f, 0.2f);
47 surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint);
Brian Salomon930f9392018-06-20 16:25:26 -040048 context->flush();
Greg Daniel09c94002018-06-08 22:11:51 +000049 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
50 REPORTER_ASSERT(reporter, !allocateMips || !mipsAreDirty(surf1.get()));
junovda5469d2015-06-15 09:48:15 -070051
Greg Daniel09c94002018-06-08 22:11:51 +000052 // Changing the contents of the surface should invalidate the mipmap, but not de-allocate
53 surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint());
Brian Salomon930f9392018-06-20 16:25:26 -040054 context->flush();
Greg Daniel09c94002018-06-08 22:11:51 +000055 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
56 REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get()));
57 }
junovda5469d2015-06-15 09:48:15 -070058}
Brian Salomon930f9392018-06-20 16:25:26 -040059
60DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReimportImageTextureWithMipLevels, reporter, ctxInfo) {
61 auto* ctx = ctxInfo.grContext();
62 if (!ctx->contextPriv().caps()->mipMapSupport()) {
63 return;
64 }
65 static constexpr auto kCreateWithMipMaps = true;
66 auto surf = SkSurface::MakeRenderTarget(
67 ctx, SkBudgeted::kYes,
68 SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
69 kTopLeft_GrSurfaceOrigin, nullptr, kCreateWithMipMaps);
70 if (!surf) {
71 return;
72 }
73 surf->getCanvas()->drawColor(SK_ColorDKGRAY);
74 auto img = surf->makeImageSnapshot();
75 if (!img) {
76 return;
77 }
78 surf.reset();
79 GrBackendTexture btex;
80 SkImage::BackendTextureReleaseProc texRelease;
81 if (!SkImage::MakeBackendTextureFromSkImage(ctx, std::move(img), &btex, &texRelease)) {
82 // Not all backends support stealing textures yet.
83 // ERRORF(reporter, "Could not turn image into texture");
84 return;
85 }
86 REPORTER_ASSERT(reporter, btex.hasMipMaps());
87 // Reimport the texture as an image and perform a downsampling draw with medium quality which
88 // should use the upper MIP levels.
89 img = SkImage::MakeFromTexture(ctx, btex, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
90 kPremul_SkAlphaType, nullptr);
91 const auto singlePixelInfo =
92 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
93 surf = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kYes, singlePixelInfo, 1,
94 kTopLeft_GrSurfaceOrigin, nullptr);
95 SkPaint paint;
96 paint.setFilterQuality(kMedium_SkFilterQuality);
97 surf->getCanvas()->drawImageRect(img, SkRect::MakeWH(1, 1), &paint);
98 uint32_t pixel;
99 surf->readPixels(singlePixelInfo, &pixel, sizeof(uint32_t), 0, 0);
100 REPORTER_ASSERT(reporter, pixel == SkPreMultiplyColor(SK_ColorDKGRAY));
101 img.reset();
102 texRelease(btex);
103}