2016-01-20 14:18:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-03-19 19:54:28 +00:00
|
|
|
// This test only works with the GL backend.
|
2016-01-20 14:18:10 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2020-03-19 19:54:28 +00:00
|
|
|
|
|
|
|
#ifdef SK_GL
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
2019-05-22 20:23:43 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
2020-07-01 18:45:24 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/gpu/GrTypes.h"
|
2020-06-19 23:51:21 +00:00
|
|
|
#include "src/core/SkAutoPixmapStorage.h"
|
2022-04-07 15:20:24 +00:00
|
|
|
#include "src/gpu/ganesh/GrDirectContextPriv.h"
|
|
|
|
#include "src/gpu/ganesh/GrGpu.h"
|
|
|
|
#include "src/gpu/ganesh/gl/GrGLCaps.h"
|
|
|
|
#include "src/gpu/ganesh/gl/GrGLDefines_impl.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
|
2016-01-20 14:18:10 +00:00
|
|
|
namespace skiagm {
|
2021-06-03 14:14:16 +00:00
|
|
|
class RectangleTexture : public GM {
|
2016-01-20 14:18:10 +00:00
|
|
|
public:
|
|
|
|
RectangleTexture() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
2020-02-24 02:29:01 +00:00
|
|
|
private:
|
|
|
|
enum class ImageType {
|
|
|
|
kGradientCircle,
|
|
|
|
k2x2
|
|
|
|
};
|
|
|
|
|
2016-01-20 14:18:10 +00:00
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("rectangle_texture");
|
|
|
|
}
|
|
|
|
|
2020-02-24 02:29:01 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(1180, 710); }
|
|
|
|
|
|
|
|
SkBitmap makeImagePixels(int size, ImageType type) {
|
|
|
|
auto ii = SkImageInfo::Make(size, size, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
|
|
|
|
switch (type) {
|
|
|
|
case ImageType::kGradientCircle: {
|
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocPixels(ii);
|
|
|
|
SkPaint paint;
|
|
|
|
SkCanvas canvas(bmp);
|
|
|
|
SkPoint pts[] = {{0, 0}, {0, SkIntToScalar(size)}};
|
|
|
|
SkColor colors0[] = {0xFF1060B0, 0xFF102030};
|
|
|
|
paint.setShader(
|
|
|
|
SkGradientShader::MakeLinear(pts, colors0, nullptr, 2, SkTileMode::kClamp));
|
|
|
|
canvas.drawPaint(paint);
|
|
|
|
SkColor colors1[] = {0xFFA07010, 0xFFA02080};
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setShader(
|
|
|
|
SkGradientShader::MakeLinear(pts, colors1, nullptr, 2, SkTileMode::kClamp));
|
|
|
|
canvas.drawCircle(size/2.f, size/2.f, 2.f*size/5, paint);
|
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
case ImageType::k2x2: {
|
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocPixels(ii);
|
|
|
|
*bmp.getAddr32(0, 0) = 0xFF0000FF;
|
|
|
|
*bmp.getAddr32(1, 0) = 0xFF00FF00;
|
|
|
|
*bmp.getAddr32(0, 1) = 0xFFFF0000;
|
|
|
|
*bmp.getAddr32(1, 1) = 0xFFFFFFFF;
|
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkUNREACHABLE;
|
2016-01-20 14:18:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 16:50:56 +00:00
|
|
|
sk_sp<SkImage> createRectangleTextureImg(GrDirectContext* dContext, GrSurfaceOrigin origin,
|
2020-02-24 02:29:01 +00:00
|
|
|
const SkBitmap content) {
|
|
|
|
SkASSERT(content.colorType() == kRGBA_8888_SkColorType);
|
2020-06-19 23:51:21 +00:00
|
|
|
auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
|
2020-07-29 16:50:56 +00:00
|
|
|
auto bet = dContext->createBackendTexture(content.width(), content.height(), format,
|
2020-08-04 18:07:40 +00:00
|
|
|
GrMipmapped::kNo, GrRenderable::kNo);
|
2020-06-19 23:51:21 +00:00
|
|
|
if (!bet.isValid()) {
|
2019-04-02 14:01:11 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-12-07 16:30:16 +00:00
|
|
|
if (!dContext->updateBackendTexture(bet, content.pixmap(), origin, nullptr, nullptr)) {
|
2020-07-29 16:50:56 +00:00
|
|
|
dContext->deleteBackendTexture(bet);
|
2016-01-20 14:18:10 +00:00
|
|
|
}
|
2020-07-29 16:50:56 +00:00
|
|
|
return SkImage::MakeFromAdoptedTexture(dContext, bet, origin, kRGBA_8888_SkColorType);
|
2016-01-20 14:18:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 18:45:24 +00:00
|
|
|
DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
|
2020-06-26 12:08:22 +00:00
|
|
|
if (!context || context->abandoned()) {
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
2020-06-19 23:51:21 +00:00
|
|
|
if (context->backend() != GrBackendApi::kOpenGL_GrBackend ||
|
|
|
|
!static_cast<const GrGLCaps*>(context->priv().caps())->rectangleTextureSupport()) {
|
2020-02-24 02:29:01 +00:00
|
|
|
*errorMsg = "This GM requires an OpenGL context that supports texture rectangles.";
|
2019-04-02 14:01:11 +00:00
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
2020-02-24 02:29:01 +00:00
|
|
|
auto gradCircle = this->makeImagePixels(50, ImageType::kGradientCircle);
|
2016-01-20 14:18:10 +00:00
|
|
|
|
2020-06-26 12:08:22 +00:00
|
|
|
fGradImgs[0] = this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin,
|
|
|
|
gradCircle);
|
|
|
|
fGradImgs[1] = this->createRectangleTextureImg(context, kBottomLeft_GrSurfaceOrigin,
|
|
|
|
gradCircle);
|
|
|
|
SkASSERT(SkToBool(fGradImgs[0]) == SkToBool(fGradImgs[1]));
|
|
|
|
if (!fGradImgs[0]) {
|
|
|
|
*errorMsg = "Could not create gradient rectangle texture images.";
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kFail;
|
2016-01-20 14:18:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 12:08:22 +00:00
|
|
|
fSmallImg = this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin,
|
|
|
|
this->makeImagePixels(2, ImageType::k2x2));
|
|
|
|
if (!fSmallImg) {
|
|
|
|
*errorMsg = "Could not create 2x2 rectangle texture image.";
|
|
|
|
return DrawResult::kFail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DrawResult::kOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onGpuTeardown() override {
|
|
|
|
fGradImgs[0] = fGradImgs[1] = nullptr;
|
|
|
|
fSmallImg = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-06-03 14:14:16 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2020-06-26 12:08:22 +00:00
|
|
|
SkASSERT(fGradImgs[0] && fGradImgs[1] && fSmallImg);
|
|
|
|
|
|
|
|
static constexpr SkScalar kPad = 5.f;
|
|
|
|
|
2021-01-28 02:21:08 +00:00
|
|
|
const SkSamplingOptions kSamplings[] = {
|
|
|
|
SkSamplingOptions(SkFilterMode::kNearest),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
|
2021-02-05 17:55:38 +00:00
|
|
|
SkSamplingOptions(SkCubicResampler::Mitchell()),
|
2016-01-20 14:18:10 +00:00
|
|
|
};
|
|
|
|
|
2018-12-06 20:33:02 +00:00
|
|
|
constexpr SkScalar kScales[] = {1.0f, 1.2f, 0.75f};
|
2016-01-20 14:18:10 +00:00
|
|
|
|
|
|
|
canvas->translate(kPad, kPad);
|
2020-06-26 12:08:22 +00:00
|
|
|
for (size_t i = 0; i < kNumGradImages; ++i) {
|
|
|
|
auto img = fGradImgs[i];
|
2020-02-24 02:29:01 +00:00
|
|
|
int w = img->width();
|
|
|
|
int h = img->height();
|
2021-08-13 02:33:57 +00:00
|
|
|
for (auto scale : kScales) {
|
2018-12-06 20:33:02 +00:00
|
|
|
canvas->save();
|
2021-08-13 02:33:57 +00:00
|
|
|
canvas->scale(scale, scale);
|
2021-01-28 02:21:08 +00:00
|
|
|
for (auto s : kSamplings) {
|
2018-12-06 20:33:02 +00:00
|
|
|
// drawImage
|
2021-01-28 02:21:08 +00:00
|
|
|
canvas->drawImage(img, 0, 0, s);
|
2020-02-24 02:29:01 +00:00
|
|
|
canvas->translate(w + kPad, 0);
|
2018-12-06 20:33:02 +00:00
|
|
|
|
|
|
|
// clamp/clamp shader
|
|
|
|
SkPaint clampPaint;
|
2021-01-28 02:21:08 +00:00
|
|
|
clampPaint.setShader(fGradImgs[i]->makeShader(s));
|
2020-02-24 02:29:01 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(1.5f*w, 1.5f*h), clampPaint);
|
|
|
|
canvas->translate(1.5f*w + kPad, 0);
|
2018-12-06 20:33:02 +00:00
|
|
|
|
|
|
|
// repeat/mirror shader
|
|
|
|
SkPaint repeatPaint;
|
2020-06-26 12:08:22 +00:00
|
|
|
repeatPaint.setShader(fGradImgs[i]->makeShader(SkTileMode::kRepeat,
|
2020-12-10 19:55:43 +00:00
|
|
|
SkTileMode::kMirror,
|
2021-01-28 02:21:08 +00:00
|
|
|
s));
|
2020-02-24 02:29:01 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(1.5f*w, 1.5f*h), repeatPaint);
|
|
|
|
canvas->translate(1.5f*w + kPad, 0);
|
2018-12-06 20:33:02 +00:00
|
|
|
|
|
|
|
// drawImageRect with kStrict
|
2020-02-24 02:29:01 +00:00
|
|
|
auto srcRect = SkRect::MakeXYWH(.25f*w, .25f*h, .50f*w, .50f*h);
|
|
|
|
auto dstRect = SkRect::MakeXYWH( 0, 0, .50f*w, .50f*h);
|
2021-01-28 02:21:08 +00:00
|
|
|
canvas->drawImageRect(fGradImgs[i], srcRect, dstRect, s, nullptr,
|
2018-12-06 20:33:02 +00:00
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2020-02-24 02:29:01 +00:00
|
|
|
canvas->translate(.5f*w + kPad, 0);
|
2018-12-06 20:33:02 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2021-08-13 02:33:57 +00:00
|
|
|
canvas->translate(0, kPad + 1.5f*h*scale);
|
2016-01-20 14:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 02:29:01 +00:00
|
|
|
|
|
|
|
static constexpr SkScalar kOutset = 25.f;
|
|
|
|
canvas->translate(kOutset, kOutset);
|
2020-06-26 12:08:22 +00:00
|
|
|
auto dstRect = SkRect::Make(fSmallImg->dimensions()).makeOutset(kOutset, kOutset);
|
2020-02-24 02:29:01 +00:00
|
|
|
|
2021-07-16 01:34:48 +00:00
|
|
|
const SkSamplingOptions gSamplings[] = {
|
|
|
|
SkSamplingOptions(SkFilterMode::kNearest),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
|
|
|
|
SkSamplingOptions(SkCubicResampler::Mitchell()),
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& sampling : gSamplings) {
|
|
|
|
if (!sampling.useCubic && sampling.mipmap != SkMipmapMode::kNone) {
|
2020-02-24 02:29:01 +00:00
|
|
|
// Medium is the same as Low for upscaling.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
canvas->save();
|
|
|
|
for (int ty = 0; ty < kSkTileModeCount; ++ty) {
|
|
|
|
canvas->save();
|
|
|
|
for (int tx = 0; tx < kSkTileModeCount; ++tx) {
|
|
|
|
SkMatrix lm;
|
|
|
|
lm.setRotate(45.f, 1, 1);
|
|
|
|
lm.postScale(6.5f, 6.5f);
|
|
|
|
SkPaint paint;
|
2020-12-10 19:55:43 +00:00
|
|
|
paint.setShader(fSmallImg->makeShader(static_cast<SkTileMode>(tx),
|
|
|
|
static_cast<SkTileMode>(ty),
|
2021-07-16 01:34:48 +00:00
|
|
|
sampling,
|
2020-12-10 19:55:43 +00:00
|
|
|
lm));
|
2020-02-24 02:29:01 +00:00
|
|
|
canvas->drawRect(dstRect, paint);
|
|
|
|
canvas->translate(dstRect.width() + kPad, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, dstRect.height() + kPad);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate((dstRect.width() + kPad)*kSkTileModeCount, 0);
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2016-01-20 14:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-06-26 12:08:22 +00:00
|
|
|
static const int kNumGradImages = 2;
|
|
|
|
|
|
|
|
sk_sp<SkImage> fGradImgs[kNumGradImages];
|
|
|
|
sk_sp<SkImage> fSmallImg;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2016-01-20 14:18:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new RectangleTexture;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|
2020-03-19 19:54:28 +00:00
|
|
|
#endif
|