blob: 6d5441f3cde88b6221b7c20f52e6fc404c71e4be [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"
Adlai Hollera0693042020-10-14 11:23:11 -040011#include "src/gpu/GrDirectContextPriv.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040012#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#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 Salomon69100f02020-07-21 10:49:25 -040020 if (!context->priv().caps()->mipmapSupport()) {
Brian Osman7eae5d72019-03-26 11:21:55 -040021 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();
Brian Salomon4cfae3b2020-07-23 10:33:24 -040027 return GrMipmapped::kYes == texture->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()));
Brian Salomon4cfae3b2020-07-23 10:33:24 -040032 return image->getTexture()->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) {
Adlai Holler14dc7912020-08-11 15:48:49 +000068 auto dContext = ctxInfo.directContext();
69 if (!dContext->priv().caps()->mipmapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -040070 return;
71 }
72 static constexpr auto kCreateWithMipMaps = true;
73 auto surf = SkSurface::MakeRenderTarget(
Adlai Holler14dc7912020-08-11 15:48:49 +000074 dContext, SkBudgeted::kYes,
Brian Salomon930f9392018-06-20 16:25:26 -040075 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;
Adlai Holler14dc7912020-08-11 15:48:49 +000088 if (!SkImage::MakeBackendTextureFromSkImage(dContext, std::move(img), &btex, &texRelease)) {
Brian Salomon930f9392018-06-20 16:25:26 -040089 // Not all backends support stealing textures yet.
90 // ERRORF(reporter, "Could not turn image into texture");
91 return;
92 }
Brian Salomon40a40622020-07-21 10:32:07 -040093 REPORTER_ASSERT(reporter, btex.hasMipmaps());
Brian Salomon930f9392018-06-20 16:25:26 -040094 // Reimport the texture as an image and perform a downsampling draw with medium quality which
95 // should use the upper MIP levels.
Adlai Holler14dc7912020-08-11 15:48:49 +000096 img = SkImage::MakeFromTexture(dContext, btex, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
Brian Salomon930f9392018-06-20 16:25:26 -040097 kPremul_SkAlphaType, nullptr);
98 const auto singlePixelInfo =
99 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
Adlai Holler14dc7912020-08-11 15:48:49 +0000100 surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, singlePixelInfo, 1,
Brian Salomon930f9392018-06-20 16:25:26 -0400101 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}