blob: 325e29ac0948032241abd38527fd38761a2a6541 [file] [log] [blame]
bsalomone179a912016-01-20 06:18:10 -08001/*
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
8// This test only works with the GPU backend.
9
10#include "gm.h"
11
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrBackendSurface.h"
bsalomone179a912016-01-20 06:18:10 -080013#include "GrContext.h"
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050014#include "GrContextPriv.h"
robertphillips87f15c82016-05-20 11:14:33 -070015#include "GrGpu.h"
robertphillips87f15c82016-05-20 11:14:33 -070016#include "gl/GrGLContext.h"
bsalomone179a912016-01-20 06:18:10 -080017#include "SkBitmap.h"
18#include "SkGradientShader.h"
19#include "SkImage.h"
20
21namespace skiagm {
22class RectangleTexture : public GM {
23public:
24 RectangleTexture() {
25 this->setBGColor(0xFFFFFFFF);
26 }
27
28protected:
29 SkString onShortName() override {
30 return SkString("rectangle_texture");
31 }
32
33 SkISize onISize() override {
34 return SkISize::Make(1035, 240);
35 }
36
37 void fillPixels(int width, int height, void *pixels) {
38 SkBitmap bmp;
39 bmp.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType), width * 4);
40 bmp.setPixels(pixels);
41 SkPaint paint;
42 SkCanvas canvas(bmp);
43 SkPoint pts[] = { {0, 0}, {0, SkIntToScalar(height)} };
44 SkColor colors0[] = { 0xFF1060B0 , 0xFF102030 };
reed1a9b9642016-03-13 14:13:58 -070045 paint.setShader(SkGradientShader::MakeLinear(pts, colors0, nullptr, 2,
46 SkShader::kClamp_TileMode));
bsalomone179a912016-01-20 06:18:10 -080047 canvas.drawPaint(paint);
48
49 SkColor colors1[] = { 0xFFA07010 , 0xFFA02080 };
50 paint.setAntiAlias(true);
reed1a9b9642016-03-13 14:13:58 -070051 paint.setShader(SkGradientShader::MakeLinear(pts, colors1, nullptr, 2,
52 SkShader::kClamp_TileMode));
bsalomone179a912016-01-20 06:18:10 -080053 canvas.drawCircle(SkIntToScalar(width) / 2, SkIntToScalar(height) / 2,
54 SkIntToScalar(width + height) / 5, paint);
55 }
56
reed9ce9d672016-03-17 10:51:11 -070057 sk_sp<SkImage> createRectangleTextureImg(GrContext* context, int width, int height,
58 void* pixels) {
bsalomone179a912016-01-20 06:18:10 -080059 if (!context) {
60 return nullptr;
61 }
Khushalc421ca12018-06-26 14:38:34 -070062 if (context->abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -040063 return nullptr;
64 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050065 GrGpu* gpu = context->contextPriv().getGpu();
bsalomone179a912016-01-20 06:18:10 -080066 if (!gpu) {
67 return nullptr;
68 }
69 const GrGLContext* glCtx = gpu->glContextForTesting();
70 if (!glCtx) {
71 return nullptr;
72 }
73
74 if (!(kGL_GrGLStandard == glCtx->standard() && glCtx->version() >= GR_GL_VER(3, 1)) &&
Weiliang Chen1e04b362018-07-12 17:13:35 -040075 !(glCtx->hasExtension("GL_ARB_texture_rectangle") ||
76 glCtx->hasExtension("GL_ANGLE_texture_rectangle"))) {
bsalomone179a912016-01-20 06:18:10 -080077 return nullptr;
78 }
79
80 // We will always create the GL texture as GL_RGBA, however the pixels uploaded may be
81 // be RGBA or BGRA, depending on how SkPMColor was compiled.
82 GrGLenum format;
83 if (kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
84 format = GR_GL_BGRA;
85 } else {
86 SkASSERT(kSkia8888_GrPixelConfig == kRGBA_8888_GrPixelConfig);
87 format = GR_GL_RGBA;
88 }
89
90 const GrGLInterface* gl = glCtx->interface();
91// Useful for debugging whether errors result from use of RECTANGLE
92// #define TARGET GR_GL_TEXTURE_2D
93#define TARGET GR_GL_TEXTURE_RECTANGLE
bsalomon3c481002016-03-21 09:04:26 -070094 GrGLuint id = 0;
bsalomone179a912016-01-20 06:18:10 -080095 GR_GL_CALL(gl, GenTextures(1, &id));
96 GR_GL_CALL(gl, BindTexture(TARGET, id));
97 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MAG_FILTER,
98 GR_GL_NEAREST));
99 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MIN_FILTER,
100 GR_GL_NEAREST));
101 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_S,
halcanary9d524f22016-03-29 09:03:52 -0700102 GR_GL_CLAMP_TO_EDGE));
bsalomone179a912016-01-20 06:18:10 -0800103 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_T,
104 GR_GL_CLAMP_TO_EDGE));
105 GR_GL_CALL(gl, TexImage2D(TARGET, 0, GR_GL_RGBA, width, height, 0,
106 format, GR_GL_UNSIGNED_BYTE, pixels));
107
108
109 context->resetContext();
110 GrGLTextureInfo info;
111 info.fID = id;
112 info.fTarget = TARGET;
Greg Danielf5d87582017-12-18 14:48:15 -0500113 info.fFormat = GR_GL_RGBA8;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000114
Greg Danielf5d87582017-12-18 14:48:15 -0500115 GrBackendTexture rectangleTex(width, height, GrMipMapped::kNo, info);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000116
117 if (sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(context, rectangleTex,
Greg Danielf5d87582017-12-18 14:48:15 -0500118 kTopLeft_GrSurfaceOrigin,
119 kRGBA_8888_SkColorType)) {
bsalomone179a912016-01-20 06:18:10 -0800120 return image;
121 }
122 GR_GL_CALL(gl, DeleteTextures(1, &id));
123 return nullptr;
124 }
125
126 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700127 GrContext *context = canvas->getGrContext();
128 if (!context) {
bsalomone179a912016-01-20 06:18:10 -0800129 skiagm::GM::DrawGpuOnlyMessage(canvas);
130 return;
131 }
132
mtkleindbfd7ab2016-09-01 11:24:54 -0700133 constexpr int kWidth = 50;
134 constexpr int kHeight = 50;
135 constexpr SkScalar kPad = 5.f;
bsalomone179a912016-01-20 06:18:10 -0800136
137 SkPMColor pixels[kWidth * kHeight];
138 this->fillPixels(kWidth, kHeight, pixels);
reed9ce9d672016-03-17 10:51:11 -0700139 sk_sp<SkImage> rectImg(this->createRectangleTextureImg(context, kWidth, kHeight, pixels));
bsalomone179a912016-01-20 06:18:10 -0800140
141 if (!rectImg) {
142 SkPaint paint;
143 paint.setAntiAlias(true);
mtkleindbfd7ab2016-09-01 11:24:54 -0700144 const char* kMsg = "Could not create rectangle texture image.";
Cary Clark2a475ea2017-04-28 15:35:12 -0400145 canvas->drawString(kMsg, 10, 100, paint);
bsalomone179a912016-01-20 06:18:10 -0800146 return;
147 }
148
mtkleindbfd7ab2016-09-01 11:24:54 -0700149 constexpr SkFilterQuality kQualities[] = {
bsalomone179a912016-01-20 06:18:10 -0800150 kNone_SkFilterQuality,
151 kLow_SkFilterQuality,
152 kMedium_SkFilterQuality,
153 kHigh_SkFilterQuality,
154 };
155
mtkleindbfd7ab2016-09-01 11:24:54 -0700156 constexpr SkScalar kScales[] = { 1.0f, 1.2f, 0.75f };
bsalomone179a912016-01-20 06:18:10 -0800157
158 canvas->translate(kPad, kPad);
159 for (auto s : kScales) {
160 canvas->save();
161 canvas->scale(s, s);
162 for (auto q : kQualities) {
reed9ce9d672016-03-17 10:51:11 -0700163 SkPaint plainPaint;
164 plainPaint.setFilterQuality(q);
165 canvas->drawImage(rectImg.get(), 0, 0, &plainPaint);
166 canvas->translate(kWidth + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800167
reed9ce9d672016-03-17 10:51:11 -0700168 SkPaint clampPaint;
169 clampPaint.setFilterQuality(q);
Mike Reed0acd7952017-04-28 11:12:19 -0400170 clampPaint.setShader(rectImg->makeShader());
reed9ce9d672016-03-17 10:51:11 -0700171 canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), clampPaint);
172 canvas->translate(kWidth * 1.5f + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800173
reed9ce9d672016-03-17 10:51:11 -0700174 SkPaint repeatPaint;
175 repeatPaint.setFilterQuality(q);
176 repeatPaint.setShader(rectImg->makeShader(SkShader::kRepeat_TileMode,
177 SkShader::kMirror_TileMode));
178 canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), repeatPaint);
179 canvas->translate(1.5f * kWidth + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800180 }
181 canvas->restore();
182 canvas->translate(0, kPad + 1.5f * kHeight * s);
183 }
184 }
185
186private:
187 typedef GM INHERITED;
188};
189
190DEF_GM(return new RectangleTexture;)
191}