2015-06-15 16:48:15 +00:00
|
|
|
/*
|
2016-07-14 16:24:08 +00:00
|
|
|
* Copyright 2016 Google Inc.
|
2015-06-15 16:48:15 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2020-07-23 14:33:24 +00:00
|
|
|
#include "src/gpu/GrTexture.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/image/SkImage_Base.h"
|
2020-03-06 18:07:10 +00:00
|
|
|
#include "src/image/SkImage_GpuBase.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2015-06-15 16:48:15 +00:00
|
|
|
|
2016-07-14 16:24:08 +00:00
|
|
|
// Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures.
|
2019-03-26 15:21:55 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTextureMipMapInvalidationTest, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
auto context = ctxInfo.directContext();
|
2020-07-21 14:49:25 +00:00
|
|
|
if (!context->priv().caps()->mipmapSupport()) {
|
2019-03-26 15:21:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-14 16:24:08 +00:00
|
|
|
auto isMipped = [] (SkSurface* surf) {
|
2020-03-06 18:07:10 +00:00
|
|
|
SkImage_GpuBase* image = static_cast<SkImage_GpuBase*>(as_IB(surf->makeImageSnapshot()));
|
|
|
|
const GrTexture* texture = image->getTexture();
|
2020-07-23 14:33:24 +00:00
|
|
|
return GrMipmapped::kYes == texture->mipmapped();
|
2016-07-14 16:24:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto mipsAreDirty = [] (SkSurface* surf) {
|
2020-03-06 18:07:10 +00:00
|
|
|
SkImage_GpuBase* image = static_cast<SkImage_GpuBase*>(as_IB(surf->makeImageSnapshot()));
|
2020-07-23 14:33:24 +00:00
|
|
|
return image->getTexture()->mipmapsAreDirty();
|
2016-07-14 16:24:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto info = SkImageInfo::MakeN32Premul(256, 256);
|
2018-06-08 22:11:51 +00:00
|
|
|
for (auto allocateMips : {false, true}) {
|
|
|
|
auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0,
|
|
|
|
kBottomLeft_GrSurfaceOrigin, nullptr,
|
|
|
|
allocateMips);
|
|
|
|
auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
|
|
|
|
// Draw something just in case we ever had a solid color optimization
|
|
|
|
surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint());
|
2020-05-14 19:45:44 +00:00
|
|
|
surf1->flushAndSubmit();
|
2018-06-08 22:11:51 +00:00
|
|
|
|
|
|
|
// No mipmaps initially
|
|
|
|
REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
|
|
|
|
|
|
|
|
// Painting with downscale and medium filter quality should result in mipmap creation
|
2018-06-20 20:25:26 +00:00
|
|
|
// Flush the context rather than the canvas as flushing the canvas triggers MIP level
|
|
|
|
// generation.
|
2018-06-08 22:11:51 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(kMedium_SkFilterQuality);
|
|
|
|
surf2->getCanvas()->scale(0.2f, 0.2f);
|
|
|
|
surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint);
|
2020-05-14 19:45:44 +00:00
|
|
|
context->flushAndSubmit();
|
2018-06-08 22:11:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
|
|
|
|
REPORTER_ASSERT(reporter, !allocateMips || !mipsAreDirty(surf1.get()));
|
|
|
|
|
|
|
|
// Changing the contents of the surface should invalidate the mipmap, but not de-allocate
|
|
|
|
surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint());
|
2020-05-14 19:45:44 +00:00
|
|
|
context->flushAndSubmit();
|
2018-06-08 22:11:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
|
|
|
|
REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get()));
|
|
|
|
}
|
2015-06-15 16:48:15 +00:00
|
|
|
}
|
2018-06-20 20:25:26 +00:00
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReimportImageTextureWithMipLevels, reporter, ctxInfo) {
|
2020-08-11 15:48:49 +00:00
|
|
|
auto dContext = ctxInfo.directContext();
|
|
|
|
if (!dContext->priv().caps()->mipmapSupport()) {
|
2018-06-20 20:25:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
static constexpr auto kCreateWithMipMaps = true;
|
|
|
|
auto surf = SkSurface::MakeRenderTarget(
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext, SkBudgeted::kYes,
|
2018-06-20 20:25:26 +00:00
|
|
|
SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
|
|
|
|
kTopLeft_GrSurfaceOrigin, nullptr, kCreateWithMipMaps);
|
|
|
|
if (!surf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
surf->getCanvas()->drawColor(SK_ColorDKGRAY);
|
|
|
|
auto img = surf->makeImageSnapshot();
|
|
|
|
if (!img) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
surf.reset();
|
|
|
|
GrBackendTexture btex;
|
|
|
|
SkImage::BackendTextureReleaseProc texRelease;
|
2020-08-11 15:48:49 +00:00
|
|
|
if (!SkImage::MakeBackendTextureFromSkImage(dContext, std::move(img), &btex, &texRelease)) {
|
2018-06-20 20:25:26 +00:00
|
|
|
// Not all backends support stealing textures yet.
|
|
|
|
// ERRORF(reporter, "Could not turn image into texture");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-21 14:32:07 +00:00
|
|
|
REPORTER_ASSERT(reporter, btex.hasMipmaps());
|
2018-06-20 20:25:26 +00:00
|
|
|
// Reimport the texture as an image and perform a downsampling draw with medium quality which
|
|
|
|
// should use the upper MIP levels.
|
2020-08-11 15:48:49 +00:00
|
|
|
img = SkImage::MakeFromTexture(dContext, btex, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
|
2018-06-20 20:25:26 +00:00
|
|
|
kPremul_SkAlphaType, nullptr);
|
|
|
|
const auto singlePixelInfo =
|
|
|
|
SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
|
2020-08-11 15:48:49 +00:00
|
|
|
surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, singlePixelInfo, 1,
|
2018-06-20 20:25:26 +00:00
|
|
|
kTopLeft_GrSurfaceOrigin, nullptr);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(kMedium_SkFilterQuality);
|
|
|
|
surf->getCanvas()->drawImageRect(img, SkRect::MakeWH(1, 1), &paint);
|
|
|
|
uint32_t pixel;
|
|
|
|
surf->readPixels(singlePixelInfo, &pixel, sizeof(uint32_t), 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, pixel == SkPreMultiplyColor(SK_ColorDKGRAY));
|
|
|
|
img.reset();
|
|
|
|
texRelease(btex);
|
|
|
|
}
|