blob: 47390e2e1911e496983cc311e059771f570211c6 [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
12#if SK_SUPPORT_GPU
13
14#include "GrContext.h"
15#include "GrTest.h"
16#include "SkBitmap.h"
17#include "SkGradientShader.h"
18#include "SkImage.h"
19
20namespace skiagm {
21class RectangleTexture : public GM {
22public:
23 RectangleTexture() {
24 this->setBGColor(0xFFFFFFFF);
25 }
26
27protected:
28 SkString onShortName() override {
29 return SkString("rectangle_texture");
30 }
31
32 SkISize onISize() override {
33 return SkISize::Make(1035, 240);
34 }
35
36 void fillPixels(int width, int height, void *pixels) {
37 SkBitmap bmp;
38 bmp.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType), width * 4);
39 bmp.setPixels(pixels);
40 SkPaint paint;
41 SkCanvas canvas(bmp);
42 SkPoint pts[] = { {0, 0}, {0, SkIntToScalar(height)} };
43 SkColor colors0[] = { 0xFF1060B0 , 0xFF102030 };
reed1a9b9642016-03-13 14:13:58 -070044 paint.setShader(SkGradientShader::MakeLinear(pts, colors0, nullptr, 2,
45 SkShader::kClamp_TileMode));
bsalomone179a912016-01-20 06:18:10 -080046 canvas.drawPaint(paint);
47
48 SkColor colors1[] = { 0xFFA07010 , 0xFFA02080 };
49 paint.setAntiAlias(true);
reed1a9b9642016-03-13 14:13:58 -070050 paint.setShader(SkGradientShader::MakeLinear(pts, colors1, nullptr, 2,
51 SkShader::kClamp_TileMode));
bsalomone179a912016-01-20 06:18:10 -080052 canvas.drawCircle(SkIntToScalar(width) / 2, SkIntToScalar(height) / 2,
53 SkIntToScalar(width + height) / 5, paint);
54 }
55
reed9ce9d672016-03-17 10:51:11 -070056 sk_sp<SkImage> createRectangleTextureImg(GrContext* context, int width, int height,
57 void* pixels) {
bsalomone179a912016-01-20 06:18:10 -080058 if (!context) {
59 return nullptr;
60 }
61 GrGpu* gpu = context->getGpu();
62 if (!gpu) {
63 return nullptr;
64 }
65 const GrGLContext* glCtx = gpu->glContextForTesting();
66 if (!glCtx) {
67 return nullptr;
68 }
69
70 if (!(kGL_GrGLStandard == glCtx->standard() && glCtx->version() >= GR_GL_VER(3, 1)) &&
71 !glCtx->hasExtension("GL_ARB_texture_rectangle")) {
72 return nullptr;
73 }
74
75 // We will always create the GL texture as GL_RGBA, however the pixels uploaded may be
76 // be RGBA or BGRA, depending on how SkPMColor was compiled.
77 GrGLenum format;
78 if (kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
79 format = GR_GL_BGRA;
80 } else {
81 SkASSERT(kSkia8888_GrPixelConfig == kRGBA_8888_GrPixelConfig);
82 format = GR_GL_RGBA;
83 }
84
85 const GrGLInterface* gl = glCtx->interface();
86// Useful for debugging whether errors result from use of RECTANGLE
87// #define TARGET GR_GL_TEXTURE_2D
88#define TARGET GR_GL_TEXTURE_RECTANGLE
bsalomon3c481002016-03-21 09:04:26 -070089 GrGLuint id = 0;
bsalomone179a912016-01-20 06:18:10 -080090 GR_GL_CALL(gl, GenTextures(1, &id));
91 GR_GL_CALL(gl, BindTexture(TARGET, id));
92 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MAG_FILTER,
93 GR_GL_NEAREST));
94 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MIN_FILTER,
95 GR_GL_NEAREST));
96 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_S,
halcanary9d524f22016-03-29 09:03:52 -070097 GR_GL_CLAMP_TO_EDGE));
bsalomone179a912016-01-20 06:18:10 -080098 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_T,
99 GR_GL_CLAMP_TO_EDGE));
100 GR_GL_CALL(gl, TexImage2D(TARGET, 0, GR_GL_RGBA, width, height, 0,
101 format, GR_GL_UNSIGNED_BYTE, pixels));
102
103
104 context->resetContext();
105 GrGLTextureInfo info;
106 info.fID = id;
107 info.fTarget = TARGET;
108 GrBackendTextureDesc desc;
109 desc.fConfig = kRGBA_8888_GrPixelConfig;
110 desc.fWidth = width;
111 desc.fHeight = height;
112 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
113 desc.fTextureHandle = reinterpret_cast<GrBackendObject>(&info);
reed9ce9d672016-03-17 10:51:11 -0700114 if (sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(context, desc)) {
bsalomone179a912016-01-20 06:18:10 -0800115 return image;
116 }
117 GR_GL_CALL(gl, DeleteTextures(1, &id));
118 return nullptr;
119 }
120
121 void onDraw(SkCanvas* canvas) override {
122 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
123 GrContext* context;
124 if (!rt || !(context = rt->getContext())) {
125 skiagm::GM::DrawGpuOnlyMessage(canvas);
126 return;
127 }
128
129 static const int kWidth = 50;
130 static const int kHeight = 50;
131 static const SkScalar kPad = 5.f;
132
133 SkPMColor pixels[kWidth * kHeight];
134 this->fillPixels(kWidth, kHeight, pixels);
reed9ce9d672016-03-17 10:51:11 -0700135 sk_sp<SkImage> rectImg(this->createRectangleTextureImg(context, kWidth, kHeight, pixels));
bsalomone179a912016-01-20 06:18:10 -0800136
137 if (!rectImg) {
138 SkPaint paint;
139 paint.setAntiAlias(true);
140 static const char* kMsg = "Could not create rectangle texture image.";
141 canvas->drawText(kMsg, strlen(kMsg), 10, 100, paint);
142 return;
143 }
144
145 static const SkFilterQuality kQualities[] = {
146 kNone_SkFilterQuality,
147 kLow_SkFilterQuality,
148 kMedium_SkFilterQuality,
149 kHigh_SkFilterQuality,
150 };
151
152 static const SkScalar kScales[] = { 1.0f, 1.2f, 0.75f };
153
154 canvas->translate(kPad, kPad);
155 for (auto s : kScales) {
156 canvas->save();
157 canvas->scale(s, s);
158 for (auto q : kQualities) {
reed9ce9d672016-03-17 10:51:11 -0700159 SkPaint plainPaint;
160 plainPaint.setFilterQuality(q);
161 canvas->drawImage(rectImg.get(), 0, 0, &plainPaint);
162 canvas->translate(kWidth + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800163
reed9ce9d672016-03-17 10:51:11 -0700164 SkPaint clampPaint;
165 clampPaint.setFilterQuality(q);
166 clampPaint.setShader(rectImg->makeShader(SkShader::kClamp_TileMode,
167 SkShader::kClamp_TileMode));
168 canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), clampPaint);
169 canvas->translate(kWidth * 1.5f + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800170
reed9ce9d672016-03-17 10:51:11 -0700171 SkPaint repeatPaint;
172 repeatPaint.setFilterQuality(q);
173 repeatPaint.setShader(rectImg->makeShader(SkShader::kRepeat_TileMode,
174 SkShader::kMirror_TileMode));
175 canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), repeatPaint);
176 canvas->translate(1.5f * kWidth + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800177 }
178 canvas->restore();
179 canvas->translate(0, kPad + 1.5f * kHeight * s);
180 }
181 }
182
183private:
184 typedef GM INHERITED;
185};
186
187DEF_GM(return new RectangleTexture;)
188}
189
190#endif