Flutter Engine
The Flutter Engine
GrTextureMipMapInvalidationTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
16#include "include/core/SkRect.h"
24#include "include/gpu/GrTypes.h"
32#include "tests/Test.h"
34
35#include <cstdint>
36#include <functional>
37#include <initializer_list>
38#include <utility>
39
40struct GrContextOptions;
41
42// Tests that MIP maps are created and invalidated as expected when drawing to and from GrTextures.
43DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(GrTextureMipMapInvalidationTest,
45 ctxInfo,
47 auto context = ctxInfo.directContext();
48 if (!context->priv().caps()->mipmapSupport()) {
49 return;
50 }
51
52 auto isMipped = [reporter](SkSurface* surf) {
53 sk_sp<SkImage> image = surf->makeImageSnapshot();
55 surf->recordingContext());
56 bool proxyIsMipmapped = proxy->mipmapped() == skgpu::Mipmapped::kYes;
57 REPORTER_ASSERT(reporter, proxyIsMipmapped == image->hasMipmaps());
58 return image->hasMipmaps();
59 };
60
61 auto mipsAreDirty = [](SkSurface* surf) {
62 sk_sp<SkImage> image = surf->makeImageSnapshot();
64 surf->recordingContext());
65 return proxy->peekTexture()->mipmapsAreDirty();
66 };
67
68 auto info = SkImageInfo::MakeN32Premul(256, 256);
69 for (auto allocateMips : {false, true}) {
70 auto surf1 = SkSurfaces::RenderTarget(context,
72 info,
73 0,
75 nullptr,
76 allocateMips);
78 // Draw something just in case we ever had a solid color optimization
79 surf1->getCanvas()->drawCircle(128, 128, 50, SkPaint());
80 context->flushAndSubmit(surf1.get(), GrSyncCpu::kNo);
81
82 // No mipmaps initially
83 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
84
85 // Painting with downscale and medium filter quality should result in mipmap creation
86 // Flush the context rather than the canvas as flushing the canvas triggers MIP level
87 // generation.
89
90 surf2->getCanvas()->scale(0.2f, 0.2f);
91 surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, sampling);
92 context->flushAndSubmit();
93 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
94 REPORTER_ASSERT(reporter, !allocateMips || !mipsAreDirty(surf1.get()));
95
96 // Changing the contents of the surface should invalidate the mipmap, but not de-allocate
97 surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint());
98 context->flushAndSubmit();
99 REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
100 REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get()));
101 }
102}
103
104DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ReimportImageTextureWithMipLevels,
105 reporter,
106 ctxInfo,
108 auto dContext = ctxInfo.directContext();
109 if (!dContext->priv().caps()->mipmapSupport()) {
110 return;
111 }
112 static constexpr auto kCreateWithMipMaps = true;
113 auto surf = SkSurfaces::RenderTarget(
114 dContext,
117 1,
119 nullptr,
120 kCreateWithMipMaps);
121 if (!surf) {
122 return;
123 }
124 surf->getCanvas()->drawColor(SK_ColorDKGRAY);
125 auto img = surf->makeImageSnapshot();
126 if (!img) {
127 return;
128 }
129 surf.reset();
130 GrBackendTexture btex;
132 if (!SkImages::MakeBackendTextureFromImage(dContext, std::move(img), &btex, &texRelease)) {
133 // Not all backends support stealing textures yet.
134 // ERRORF(reporter, "Could not turn image into texture");
135 return;
136 }
138 // Reimport the texture as an image and perform a downsampling draw with medium quality which
139 // should use the upper MIP levels.
140 img = SkImages::BorrowTextureFrom(dContext,
141 btex,
145 nullptr);
146 const auto singlePixelInfo =
149 dContext, skgpu::Budgeted::kYes, singlePixelInfo, 1, kTopLeft_GrSurfaceOrigin, nullptr);
150
151 surf->getCanvas()->drawImageRect(img, SkRect::MakeWH(1, 1),
154 uint32_t pixel;
155 surf->readPixels(singlePixelInfo, &pixel, sizeof(uint32_t), 0, 0);
157 img.reset();
158 texRelease(btex);
159}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
reporter
Definition: FontMgrTest.cpp:39
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(GrTextureMipMapInvalidationTest, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
@ kBottomLeft_GrSurfaceOrigin
Definition: GrTypes.h:149
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
SK_API SkPMColor SkPreMultiplyColor(SkColor c)
Definition: SkColor.cpp:21
constexpr SkColor SK_ColorDKGRAY
Definition: SkColor.h:108
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
bool hasMipmaps() const
GrTexture * peekTexture() const
skgpu::Mipmapped mipmapped() const
bool mipmapsAreDirty() const
Definition: GrTexture.h:65
bool hasMipmaps() const
Definition: SkImage.cpp:292
T * get() const
Definition: SkRefCnt.h:303
std::function< void(GrBackendTexture)> BackendTextureReleaseProc
Definition: SkImageGanesh.h:43
SK_API sk_sp< SkImage > BorrowTextureFrom(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
SK_API bool MakeBackendTextureFromImage(GrDirectContext *context, sk_sp< SkImage > image, GrBackendTexture *backendTexture, BackendTextureReleaseProc *backendTextureReleaseProc)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SkSamplingOptions sampling
Definition: SkRecords.h:337
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
GrTextureProxy * GetTextureImageProxy(SkImage *image, GrRecordingContext *rContext)
Definition: ProxyUtils.cpp:32
SkSamplingOptions(SkFilterMode::kLinear))
static SkImageInfo MakeN32Premul(int width, int height)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609