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"
|
|
|
|
#include "include/core/SkFilterQuality.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"
|
|
|
|
#include "include/gpu/GrContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/gpu/GrTypes.h"
|
|
|
|
#include "include/gpu/gl/GrGLFunctions.h"
|
|
|
|
#include "include/gpu/gl/GrGLInterface.h"
|
|
|
|
#include "include/gpu/gl/GrGLTypes.h"
|
|
|
|
#include "include/private/GrTypesPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrGpu.h"
|
|
|
|
#include "src/gpu/gl/GrGLContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/gl/GrGLDefines.h"
|
|
|
|
#include "src/gpu/gl/GrGLUtil.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class GrRenderTargetContext;
|
2016-01-20 14:18:10 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
2019-02-07 22:23:36 +00:00
|
|
|
class RectangleTexture : public GpuGM {
|
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
|
|
|
}
|
|
|
|
|
2019-04-02 14:01:11 +00:00
|
|
|
static const GrGLContext* GetGLContextIfSupported(GrContext* context) {
|
2019-05-13 14:52:41 +00:00
|
|
|
if (context->backend() != GrBackendApi::kOpenGL) {
|
2016-01-20 14:18:10 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-05-13 14:52:41 +00:00
|
|
|
auto* caps = static_cast<const GrGLCaps*>(context->priv().caps());
|
|
|
|
if (!caps->rectangleTextureSupport()) {
|
2019-04-02 18:54:51 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-05-13 14:52:41 +00:00
|
|
|
return context->priv().getGpu()->glContextForTesting();
|
2019-04-02 14:01:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 02:29:01 +00:00
|
|
|
sk_sp<SkImage> createRectangleTextureImg(GrContext* context, GrSurfaceOrigin origin,
|
|
|
|
const SkBitmap content) {
|
|
|
|
SkASSERT(content.colorType() == kRGBA_8888_SkColorType);
|
|
|
|
|
2019-04-02 14:01:11 +00:00
|
|
|
const GrGLContext* glCtx = GetGLContextIfSupported(context);
|
|
|
|
if (!glCtx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-01-20 14:18:10 +00:00
|
|
|
|
2020-02-24 16:36:15 +00:00
|
|
|
const GrGLInterface* gl = glCtx->glInterface();
|
2018-12-06 20:33:02 +00:00
|
|
|
// Useful for debugging whether errors result from use of RECTANGLE
|
|
|
|
// static constexpr GrGLenum kTarget = GR_GL_TEXTURE_2D;
|
|
|
|
static constexpr GrGLenum kTarget = GR_GL_TEXTURE_RECTANGLE;
|
2016-03-21 16:04:26 +00:00
|
|
|
GrGLuint id = 0;
|
2016-01-20 14:18:10 +00:00
|
|
|
GR_GL_CALL(gl, GenTextures(1, &id));
|
2018-12-06 20:33:02 +00:00
|
|
|
GR_GL_CALL(gl, BindTexture(kTarget, id));
|
|
|
|
GR_GL_CALL(gl, TexParameteri(kTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
|
|
|
|
GR_GL_CALL(gl, TexParameteri(kTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
|
|
|
|
GR_GL_CALL(gl, TexParameteri(kTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
|
|
|
|
GR_GL_CALL(gl, TexParameteri(kTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
|
|
|
|
std::unique_ptr<uint32_t[]> tempPixels;
|
2020-02-24 02:29:01 +00:00
|
|
|
auto src = content.getAddr32(0, 0);
|
|
|
|
int h = content.height();
|
|
|
|
int w = content.width();
|
2018-12-06 20:33:02 +00:00
|
|
|
if (origin == kBottomLeft_GrSurfaceOrigin) {
|
2020-02-24 02:29:01 +00:00
|
|
|
tempPixels.reset(new uint32_t[w * h]);
|
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
std::copy_n(src + w*(h - y - 1), w, tempPixels.get() + w*y);
|
2018-12-06 20:33:02 +00:00
|
|
|
}
|
2020-02-24 02:29:01 +00:00
|
|
|
src = tempPixels.get();
|
2018-12-06 20:33:02 +00:00
|
|
|
}
|
2020-02-24 02:29:01 +00:00
|
|
|
GR_GL_CALL(gl, TexImage2D(kTarget, 0, GR_GL_RGBA, w, h, 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
|
|
|
|
src));
|
2016-01-20 14:18:10 +00:00
|
|
|
|
|
|
|
context->resetContext();
|
|
|
|
GrGLTextureInfo info;
|
|
|
|
info.fID = id;
|
2018-12-06 20:33:02 +00:00
|
|
|
info.fTarget = kTarget;
|
2017-12-18 19:48:15 +00:00
|
|
|
info.fFormat = GR_GL_RGBA8;
|
Revert "Revert "Plumb GrBackendTexture throughout skia.""
This reverts commit 7fa5c31c2c9af834bee66d5fcf476e250076c8d6.
Reason for revert: Relanding this change now that other fixes have landed.
Original change's description:
> Revert "Plumb GrBackendTexture throughout skia."
>
> This reverts commit 7da62b9059f3c1d31624a0e4da96ee5f908f9c12.
>
> Reason for revert: fix android roll
>
> Original change's description:
> > Plumb GrBackendTexture throughout skia.
> >
> > Bug: skia:
> > Change-Id: I1bae6768ee7229818a83ba608035a1f7867e6875
> > Reviewed-on: https://skia-review.googlesource.com/13645
> > Commit-Queue: Greg Daniel <egdaniel@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
> >
>
> TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com,brianosman@google.com,reviews@skia.org,stani@google.com
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
>
> Change-Id: I5cb8763cc837c83ebc6d10366fe2dd3efe35fb89
> Reviewed-on: https://skia-review.googlesource.com/13773
> Reviewed-by: Stan Iliev <stani@google.com>
> Commit-Queue: Stan Iliev <stani@google.com>
>
TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com,reviews@skia.org,brianosman@google.com,stani@google.com
# Not skipping CQ checks because original CL landed > 1 day ago.
Change-Id: I92bc074e4fe37fa5c83186afadc472c03802e8f2
Reviewed-on: https://skia-review.googlesource.com/13975
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
2017-04-20 16:41:55 +00:00
|
|
|
|
2020-02-24 02:29:01 +00:00
|
|
|
GrBackendTexture rectangleTex(w, h, GrMipMapped::kNo, info);
|
Revert "Revert "Plumb GrBackendTexture throughout skia.""
This reverts commit 7fa5c31c2c9af834bee66d5fcf476e250076c8d6.
Reason for revert: Relanding this change now that other fixes have landed.
Original change's description:
> Revert "Plumb GrBackendTexture throughout skia."
>
> This reverts commit 7da62b9059f3c1d31624a0e4da96ee5f908f9c12.
>
> Reason for revert: fix android roll
>
> Original change's description:
> > Plumb GrBackendTexture throughout skia.
> >
> > Bug: skia:
> > Change-Id: I1bae6768ee7229818a83ba608035a1f7867e6875
> > Reviewed-on: https://skia-review.googlesource.com/13645
> > Commit-Queue: Greg Daniel <egdaniel@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
> >
>
> TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com,brianosman@google.com,reviews@skia.org,stani@google.com
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
>
> Change-Id: I5cb8763cc837c83ebc6d10366fe2dd3efe35fb89
> Reviewed-on: https://skia-review.googlesource.com/13773
> Reviewed-by: Stan Iliev <stani@google.com>
> Commit-Queue: Stan Iliev <stani@google.com>
>
TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com,reviews@skia.org,brianosman@google.com,stani@google.com
# Not skipping CQ checks because original CL landed > 1 day ago.
Change-Id: I92bc074e4fe37fa5c83186afadc472c03802e8f2
Reviewed-on: https://skia-review.googlesource.com/13975
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
2017-04-20 16:41:55 +00:00
|
|
|
|
2020-02-24 02:29:01 +00:00
|
|
|
if (sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(
|
|
|
|
context, rectangleTex, origin, content.colorType(), content.alphaType())) {
|
2016-01-20 14:18:10 +00:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
GR_GL_CALL(gl, DeleteTextures(1, &id));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas,
|
|
|
|
SkString* errorMsg) override {
|
2019-04-02 14:01:11 +00:00
|
|
|
if (!GetGLContextIfSupported(context)) {
|
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);
|
|
|
|
static constexpr SkScalar kPad = 5.f;
|
2016-01-20 14:18:10 +00:00
|
|
|
|
2020-02-24 02:29:01 +00:00
|
|
|
sk_sp<SkImage> gradImgs[] = {
|
|
|
|
this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin, gradCircle),
|
|
|
|
this->createRectangleTextureImg(context, kBottomLeft_GrSurfaceOrigin, gradCircle),
|
2018-12-06 20:33:02 +00:00
|
|
|
};
|
2020-02-24 02:29:01 +00:00
|
|
|
SkASSERT(SkToBool(gradImgs[0]) == SkToBool(gradImgs[1]));
|
|
|
|
if (!gradImgs[0]) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Could not create rectangle texture image.";
|
|
|
|
return DrawResult::kFail;
|
2016-01-20 14:18:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkFilterQuality kQualities[] = {
|
2018-12-06 20:33:02 +00:00
|
|
|
kNone_SkFilterQuality,
|
|
|
|
kLow_SkFilterQuality,
|
|
|
|
kMedium_SkFilterQuality,
|
|
|
|
kHigh_SkFilterQuality,
|
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-02-24 02:29:01 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gradImgs); ++i) {
|
|
|
|
auto img = gradImgs[i];
|
|
|
|
int w = img->width();
|
|
|
|
int h = img->height();
|
2018-12-06 20:33:02 +00:00
|
|
|
for (auto s : kScales) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(s, s);
|
|
|
|
for (auto q : kQualities) {
|
|
|
|
// drawImage
|
|
|
|
SkPaint plainPaint;
|
|
|
|
plainPaint.setFilterQuality(q);
|
2020-02-24 02:29:01 +00:00
|
|
|
canvas->drawImage(img, 0, 0, &plainPaint);
|
|
|
|
canvas->translate(w + kPad, 0);
|
2018-12-06 20:33:02 +00:00
|
|
|
|
|
|
|
// clamp/clamp shader
|
|
|
|
SkPaint clampPaint;
|
|
|
|
clampPaint.setFilterQuality(q);
|
2020-02-24 02:29:01 +00:00
|
|
|
clampPaint.setShader(gradImgs[i]->makeShader());
|
|
|
|
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;
|
|
|
|
repeatPaint.setFilterQuality(q);
|
2020-02-24 02:29:01 +00:00
|
|
|
repeatPaint.setShader(gradImgs[i]->makeShader(SkTileMode::kRepeat,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror));
|
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);
|
|
|
|
canvas->drawImageRect(gradImgs[i], srcRect, dstRect, &plainPaint,
|
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();
|
2020-02-24 02:29:01 +00:00
|
|
|
canvas->translate(0, kPad + 1.5f*h*s);
|
2016-01-20 14:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 02:29:01 +00:00
|
|
|
|
|
|
|
auto smallImg = this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin,
|
|
|
|
this->makeImagePixels(2, ImageType::k2x2));
|
|
|
|
static constexpr SkScalar kOutset = 25.f;
|
|
|
|
canvas->translate(kOutset, kOutset);
|
|
|
|
auto dstRect = SkRect::Make(smallImg->dimensions()).makeOutset(kOutset, kOutset);
|
|
|
|
|
|
|
|
for (int fq = kNone_SkFilterQuality; fq <= kLast_SkFilterQuality; ++fq) {
|
|
|
|
if (fq == kMedium_SkFilterQuality) {
|
|
|
|
// 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);
|
|
|
|
auto shader = smallImg->makeShader(static_cast<SkTileMode>(tx),
|
|
|
|
static_cast<SkTileMode>(ty), &lm);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(std::move(shader));
|
|
|
|
paint.setFilterQuality(static_cast<SkFilterQuality>(fq));
|
|
|
|
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:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new RectangleTexture;)
|
|
|
|
}
|
2020-03-19 19:54:28 +00:00
|
|
|
#endif
|