blob: 0889063a1f1d9916f340f00faf997d3e58089002 [file] [log] [blame]
bsalomone179a912016-01-20 06:18:10 -08001
2/*
3 * Copyright 2016 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9// This test only works with the GPU backend.
10
11#include "gm.h"
12
13#if SK_SUPPORT_GPU
14
15#include "GrContext.h"
16#include "GrTest.h"
17#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 };
45 paint.setShader(SkGradientShader::CreateLinear(pts, colors0, nullptr, 2,
46 SkShader::kClamp_TileMode))->unref();
47 canvas.drawPaint(paint);
48
49 SkColor colors1[] = { 0xFFA07010 , 0xFFA02080 };
50 paint.setAntiAlias(true);
51 paint.setShader(SkGradientShader::CreateLinear(pts, colors1, nullptr, 2,
52 SkShader::kClamp_TileMode))->unref();
53 canvas.drawCircle(SkIntToScalar(width) / 2, SkIntToScalar(height) / 2,
54 SkIntToScalar(width + height) / 5, paint);
55 }
56
57 SkImage* createRectangleTextureImg(GrContext* context, int width, int height, void* pixels) {
58 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
89 GrGLuint id;
90 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,
97 GR_GL_CLAMP_TO_EDGE));
98 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);
114 if (SkImage* image = SkImage::NewFromAdoptedTexture(context, desc)) {
115 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);
135 SkAutoTUnref<SkImage> rectImg(this->createRectangleTextureImg(context, kWidth, kHeight,
136 pixels));
137
138 if (!rectImg) {
139 SkPaint paint;
140 paint.setAntiAlias(true);
141 static const char* kMsg = "Could not create rectangle texture image.";
142 canvas->drawText(kMsg, strlen(kMsg), 10, 100, paint);
143 return;
144 }
145
146 static const SkFilterQuality kQualities[] = {
147 kNone_SkFilterQuality,
148 kLow_SkFilterQuality,
149 kMedium_SkFilterQuality,
150 kHigh_SkFilterQuality,
151 };
152
153 static const SkScalar kScales[] = { 1.0f, 1.2f, 0.75f };
154
155 canvas->translate(kPad, kPad);
156 for (auto s : kScales) {
157 canvas->save();
158 canvas->scale(s, s);
159 for (auto q : kQualities) {
160 SkPaint plainPaint;
161 plainPaint.setFilterQuality(q);
162 canvas->drawImage(rectImg, 0, 0, &plainPaint);
163 canvas->translate(kWidth + kPad, 0);
164
165 SkPaint clampPaint;
166 clampPaint.setFilterQuality(q);
167 clampPaint.setShader(rectImg->newShader(SkShader::kClamp_TileMode,
168 SkShader::kClamp_TileMode))->unref();
169 canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), clampPaint);
170 canvas->translate(kWidth * 1.5f + kPad, 0);
171
172 SkPaint repeatPaint;
173 repeatPaint.setFilterQuality(q);
174 repeatPaint.setShader(rectImg->newShader(SkShader::kRepeat_TileMode,
175 SkShader::kMirror_TileMode))->unref();
176 canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), repeatPaint);
177 canvas->translate(1.5f * kWidth + kPad, 0);
178 }
179 canvas->restore();
180 canvas->translate(0, kPad + 1.5f * kHeight * s);
181 }
182 }
183
184private:
185 typedef GM INHERITED;
186};
187
188DEF_GM(return new RectangleTexture;)
189}
190
191#endif