blob: 7e90e6d919551bb24736a3e6a0cfca96310abc57 [file] [log] [blame]
robertphillips@google.comaaa9b292013-07-25 21:34:00 +00001/*
2 * Copyright 2013 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#include "gm.h"
9#include "SkCanvas.h"
10
11#if SK_SUPPORT_GPU
12#include "GrContext.h"
13
14namespace skiagm {
15extern GrContext* GetGr();
16};
17
18void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
19 fMaxTextureSizeOverride = maxTextureSizeOverride;
20}
21#endif
22
skia.committer@gmail.com956b3102013-07-26 07:00:58 +000023// Create a black&white checked texture with a 1-pixel red ring
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000024// around the outside edge
25static void make_red_ringed_bitmap(SkBitmap* result, int width, int height) {
26 SkASSERT(0 == width % 2 && 0 == width % 2);
27
28 result->setConfig(SkBitmap::kARGB_8888_Config, width, height);
29 result->allocPixels();
30 SkAutoLockPixels lock(*result);
31
skia.committer@gmail.com956b3102013-07-26 07:00:58 +000032 SkPMColor* scanline = result->getAddr32(0, 0);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000033 for (int x = 0; x < width; ++x) {
34 scanline[x] = SK_ColorRED;
35 }
36
37 for (int y = 1; y < height/2; ++y) {
38 scanline = result->getAddr32(0, y);
39 scanline[0] = SK_ColorRED;
40 for (int x = 1; x < width/2; ++x) {
41 scanline[x] = SK_ColorBLACK;
42 }
43 for (int x = width/2; x < width-1; ++x) {
44 scanline[x] = SK_ColorWHITE;
45 }
46 scanline[width-1] = SK_ColorRED;
47 }
48
49 for (int y = height/2; y < height-1; ++y) {
50 scanline = result->getAddr32(0, y);
51 scanline[0] = SK_ColorRED;
52 for (int x = 1; x < width/2; ++x) {
53 scanline[x] = SK_ColorWHITE;
54 }
55 for (int x = width/2; x < width-1; ++x) {
56 scanline[x] = SK_ColorBLACK;
57 }
58 scanline[width-1] = SK_ColorRED;
59 }
60
61 scanline = result->getAddr32(0, height-1);
62 for (int x = 0; x < width; ++x) {
63 scanline[x] = SK_ColorRED;
64 }
65 result->setIsOpaque(true);
66 result->setImmutable();
67}
68
69// This GM exercises the drawBitmapRectToRect "bleed" flag
70class BleedGM : public skiagm::GM {
71public:
72 BleedGM() {}
73
74protected:
75 virtual SkString onShortName() SK_OVERRIDE {
76 return SkString("bleed");
77 }
78
79 virtual SkISize onISize() SK_OVERRIDE {
80 return SkISize::Make(kWidth, kHeight);
81 }
82
83 virtual void onOnceBeforeDraw() SK_OVERRIDE {
84 make_red_ringed_bitmap(&fBitmapSmall, kSmallTextureSize, kSmallTextureSize);
85
86 // To exercise the GPU's tiling path we need a texture
87 // too big for the GPU to handle in one go
88 make_red_ringed_bitmap(&fBitmapBig, 2*kMaxTextureSize, 2*kMaxTextureSize);
89 }
90
91 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
92#if SK_SUPPORT_GPU
93 GrContext* ctx = skiagm::GetGr();
94 int oldMaxTextureSize = 0;
95 if (NULL != ctx) {
96 // shrink the max texture size so all our textures can be reasonably sized
97 oldMaxTextureSize = ctx->getMaxTextureSize();
98 ctx->setMaxTextureSizeOverride(kMaxTextureSize);
skia.committer@gmail.com956b3102013-07-26 07:00:58 +000099 }
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000100#endif
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000101
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000102 canvas->clear(SK_ColorGRAY);
103
104 SkPaint paint;
105
106 // Bleeding only comes into play when filtering
107 paint.setFilterBitmap(true);
108
109 // carve out the center of the small bitmap
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000110 SkRect src = SkRect::MakeXYWH(1, 1,
111 kSmallTextureSize-2,
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000112 kSmallTextureSize-2);
113 SkRect dst = SkRect::MakeXYWH(10, 10, 100, 100);
114
115 // first draw without bleeding
116 canvas->drawBitmapRectToRect(fBitmapSmall, &src, dst, &paint);
117
118 // then draw with bleeding
119 dst = SkRect::MakeXYWH(120, 10, 100, 100);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000120 canvas->drawBitmapRectToRect(fBitmapSmall, &src, dst, &paint,
121 SkCanvas::kBleed_DrawBitmapRectFlag);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000122
123 // Next test out the GPU's tiling of large textures
124
125 // first draw almost the whole thing
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000126 src = SkRect::MakeXYWH(1, 1,
127 SkIntToScalar(fBitmapBig.width()-2),
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000128 SkIntToScalar(fBitmapBig.height()-2));
129 dst = SkRect::MakeXYWH(10, 120, 100, 100);
130
131 // first without bleeding
132 canvas->drawBitmapRectToRect(fBitmapBig, &src, dst, &paint);
133
134 // then with bleeding
135 dst = SkRect::MakeXYWH(120, 120, 100, 100);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000136 canvas->drawBitmapRectToRect(fBitmapBig, &src, dst, &paint,
137 SkCanvas::kBleed_DrawBitmapRectFlag);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000138
139 // next draw ~1/4 of the bitmap
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000140 src = SkRect::MakeXYWH(1, 1,
141 SkIntToScalar(fBitmapBig.width()/2-1),
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000142 SkIntToScalar(fBitmapBig.height()/2-1));
143 dst = SkRect::MakeXYWH(10, 230, 100, 100);
144
145 // first without bleeding
146 canvas->drawBitmapRectToRect(fBitmapBig, &src, dst, &paint);
147
148 // then with bleeding
149 dst = SkRect::MakeXYWH(120, 230, 100, 100);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000150 canvas->drawBitmapRectToRect(fBitmapBig, &src, dst, &paint,
151 SkCanvas::kBleed_DrawBitmapRectFlag);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000152
153#if SK_SUPPORT_GPU
154 if (NULL != ctx) {
155 ctx->setMaxTextureSizeOverride(oldMaxTextureSize);
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000156 }
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000157#endif
158 }
159
160private:
161 static const int kWidth = 230;
162 static const int kHeight = 340;
163
164 static const int kSmallTextureSize = 4;
165 static const int kMaxTextureSize = 32;
166
167 SkBitmap fBitmapSmall;
168 SkBitmap fBitmapBig;
169
170 typedef GM INHERITED;
171};
172
173DEF_GM( return new BleedGM(); )