OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkSurface.h" | 9 #include "SkSurface.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 static void drawJpeg(SkCanvas* canvas, const SkISize& size) { | 33 static void drawJpeg(SkCanvas* canvas, const SkISize& size) { |
34 // TODO: Make this draw a file that is checked in, so it can | 34 // TODO: Make this draw a file that is checked in, so it can |
35 // be exercised on machines other than mike's. Will require a | 35 // be exercised on machines other than mike's. Will require a |
36 // rebaseline. | 36 // rebaseline. |
37 SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg")); | 37 SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg")); |
38 SkImage* image = SkImage::NewEncodedData(data); | 38 SkImage* image = SkImage::NewEncodedData(data); |
39 if (image) { | 39 if (image) { |
40 SkAutoCanvasRestore acr(canvas, true); | 40 SkAutoCanvasRestore acr(canvas, true); |
41 canvas->scale(size.width() * 1.0f / image->width(), | 41 canvas->scale(size.width() * 1.0f / image->width(), |
42 size.height() * 1.0f / image->height()); | 42 size.height() * 1.0f / image->height()); |
43 image->draw(canvas,0, 0, NULL); | 43 image->draw(canvas, 0, 0, NULL); |
44 image->unref(); | 44 image->unref(); |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 static void drawContents(SkSurface* surface, SkColor fillC) { | 48 static void drawContents(SkSurface* surface, SkColor fillC) { |
49 SkSize size = SkSize::Make(SkIntToScalar(surface->width()), | 49 SkSize size = SkSize::Make(SkIntToScalar(surface->width()), |
50 SkIntToScalar(surface->height())); | 50 SkIntToScalar(surface->height())); |
51 SkCanvas* canvas = surface->getCanvas(); | 51 SkCanvas* canvas = surface->getCanvas(); |
52 | 52 |
53 SkScalar stroke = size.fWidth / 10; | 53 SkScalar stroke = size.fWidth / 10; |
54 SkScalar radius = (size.fWidth - stroke) / 2; | 54 SkScalar radius = (size.fWidth - stroke) / 2; |
55 | 55 |
56 SkPaint paint; | 56 SkPaint paint; |
57 | 57 |
58 paint.setAntiAlias(true); | 58 paint.setAntiAlias(true); |
59 paint.setColor(fillC); | 59 paint.setColor(fillC); |
60 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); | 60 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); |
61 | 61 |
62 paint.setStyle(SkPaint::kStroke_Style); | 62 paint.setStyle(SkPaint::kStroke_Style); |
63 paint.setStrokeWidth(stroke); | 63 paint.setStrokeWidth(stroke); |
64 paint.setColor(SK_ColorBLACK); | 64 paint.setColor(SK_ColorBLACK); |
65 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); | 65 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); |
66 } | 66 } |
67 | 67 |
68 static void test_surface(SkCanvas* canvas, SkSurface* surf) { | 68 static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) { |
69 drawContents(surf, SK_ColorRED); | 69 drawContents(surf, SK_ColorRED); |
70 SkImage* imgR = surf->newImageSnapshot(); | 70 SkImage* imgR = surf->newImageSnapshot(); |
71 | 71 |
72 if (true) { | 72 if (true) { |
73 SkImage* imgR2 = surf->newImageSnapshot(); | 73 SkImage* imgR2 = surf->newImageSnapshot(); |
74 SkASSERT(imgR == imgR2); | 74 SkASSERT(imgR == imgR2); |
75 imgR2->unref(); | 75 imgR2->unref(); |
76 } | 76 } |
77 | 77 |
78 drawContents(surf, SK_ColorGREEN); | 78 drawContents(surf, SK_ColorGREEN); |
79 SkImage* imgG = surf->newImageSnapshot(); | 79 SkImage* imgG = surf->newImageSnapshot(); |
80 | 80 |
81 // since we've drawn after we snapped imgR, imgG will be a different obj | 81 // since we've drawn after we snapped imgR, imgG will be a different obj |
82 SkASSERT(imgR != imgG); | 82 SkASSERT(imgR != imgG); |
83 | 83 |
84 drawContents(surf, SK_ColorBLUE); | 84 drawContents(surf, SK_ColorBLUE); |
85 | 85 |
86 SkPaint paint; | 86 SkPaint paint; |
87 // paint.setFilterBitmap(true); | 87 // paint.setFilterBitmap(true); |
88 // paint.setAlpha(0x80); | 88 // paint.setAlpha(0x80); |
89 | 89 |
90 imgR->draw(canvas, 0, 0, &paint); | 90 imgR->draw(canvas, 0, 0, usePaint ? &paint : NULL); |
91 imgG->draw(canvas, 0, 80, &paint); | 91 imgG->draw(canvas, 0, 80, usePaint ? &paint : NULL); |
92 surf->draw(canvas, 0, 160, &paint); | 92 surf->draw(canvas, 0, 160, usePaint ? &paint : NULL); |
| 93 |
| 94 SkRect src1, src2, src3; |
| 95 src1.iset(0, 0, surf->width(), surf->height()); |
| 96 src2.iset(-surf->width() / 2, -surf->height() / 2, |
| 97 surf->width(), surf->height()); |
| 98 src3.iset(0, 0, surf->width() / 2, surf->height() / 2); |
| 99 |
| 100 SkRect dst1, dst2, dst3, dst4; |
| 101 dst1.set(0, 240, 65, 305); |
| 102 dst2.set(0, 320, 65, 385); |
| 103 dst3.set(0, 400, 65, 465); |
| 104 dst4.set(0, 480, 65, 545); |
| 105 |
| 106 imgR->draw(canvas, &src1, dst1, usePaint ? &paint : NULL); |
| 107 imgG->draw(canvas, &src2, dst2, usePaint ? &paint : NULL); |
| 108 imgR->draw(canvas, &src3, dst3, usePaint ? &paint : NULL); |
| 109 imgG->draw(canvas, NULL, dst4, usePaint ? &paint : NULL); |
93 | 110 |
94 imgG->unref(); | 111 imgG->unref(); |
95 imgR->unref(); | 112 imgR->unref(); |
96 } | 113 } |
97 | 114 |
98 class ImageGM : public skiagm::GM { | 115 class ImageGM : public skiagm::GM { |
99 void* fBuffer; | 116 void* fBuffer; |
100 size_t fBufferSize; | 117 size_t fBufferSize; |
101 SkSize fSize; | 118 SkSize fSize; |
102 enum { | 119 enum { |
(...skipping 12 matching lines...) Expand all Loading... |
115 sk_free(fBuffer); | 132 sk_free(fBuffer); |
116 } | 133 } |
117 | 134 |
118 | 135 |
119 protected: | 136 protected: |
120 virtual SkString onShortName() { | 137 virtual SkString onShortName() { |
121 return SkString("image-surface"); | 138 return SkString("image-surface"); |
122 } | 139 } |
123 | 140 |
124 virtual SkISize onISize() { | 141 virtual SkISize onISize() { |
125 return SkISize::Make(800, 500); | 142 return SkISize::Make(960, 1200); |
126 } | 143 } |
127 | 144 |
128 virtual void onDraw(SkCanvas* canvas) { | 145 virtual void onDraw(SkCanvas* canvas) { |
129 drawJpeg(canvas, this->getISize()); | 146 drawJpeg(canvas, this->getISize()); |
130 | 147 |
131 canvas->scale(2, 2); | 148 canvas->scale(2, 2); |
132 | 149 |
133 static const char* kLabel1 = "Original Img"; | 150 static const char* kLabel1 = "Original Img"; |
134 static const char* kLabel2 = "Modified Img"; | 151 static const char* kLabel2 = "Modified Img"; |
135 static const char* kLabel3 = "Cur Surface"; | 152 static const char* kLabel3 = "Cur Surface"; |
| 153 static const char* kLabel4 = "Full Crop"; |
| 154 static const char* kLabel5 = "Over-crop"; |
| 155 static const char* kLabel6 = "Upper-left"; |
| 156 static const char* kLabel7 = "No Crop"; |
136 | 157 |
137 static const char* kLabel4 = "Pre-Alloc Img"; | 158 static const char* kLabel8 = "Pre-Alloc Img"; |
138 static const char* kLabel5 = "New Alloc Img"; | 159 static const char* kLabel9 = "New Alloc Img"; |
139 static const char* kLabel6 = "SkPicture"; | 160 static const char* kLabel10 = "SkPicture"; |
140 static const char* kLabel7 = "GPU"; | 161 static const char* kLabel11 = "Null Paint"; |
| 162 static const char* kLabel12 = "GPU"; |
141 | 163 |
142 SkPaint textPaint; | 164 SkPaint textPaint; |
143 | 165 |
144 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint); | 166 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint); |
145 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint); | 167 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint); |
146 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint); | 168 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint); |
| 169 canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint); |
| 170 canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint); |
| 171 canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint); |
| 172 canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint); |
147 | 173 |
148 canvas->drawText(kLabel4, strlen(kLabel4), 80, 10, textPaint); | 174 canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint); |
149 canvas->drawText(kLabel5, strlen(kLabel5), 160, 10, textPaint); | 175 canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint); |
150 canvas->drawText(kLabel6, strlen(kLabel6), 250, 10, textPaint); | 176 canvas->drawText(kLabel10, strlen(kLabel10), 250, 10, textPaint); |
151 canvas->drawText(kLabel7, strlen(kLabel7), 340, 10, textPaint); | 177 canvas->drawText(kLabel11, strlen(kLabel11), 320, 10, textPaint); |
| 178 canvas->drawText(kLabel12, strlen(kLabel12), 410, 10, textPaint); |
152 | 179 |
153 canvas->translate(80, 20); | 180 canvas->translate(80, 20); |
154 | 181 |
155 // since we draw into this directly, we need to start fresh | 182 // since we draw into this directly, we need to start fresh |
156 sk_bzero(fBuffer, fBufferSize); | 183 sk_bzero(fBuffer, fBufferSize); |
157 | 184 |
158 SkImage::Info info; | 185 SkImage::Info info; |
159 | 186 |
160 info.fWidth = W; | 187 info.fWidth = W; |
161 info.fHeight = H; | 188 info.fHeight = H; |
162 info.fColorType = SkImage::kPMColor_ColorType; | 189 info.fColorType = SkImage::kPMColor_ColorType; |
163 info.fAlphaType = SkImage::kPremul_AlphaType; | 190 info.fAlphaType = SkImage::kPremul_AlphaType; |
164 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer,
RB)); | 191 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer,
RB)); |
165 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info)); | 192 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info)); |
166 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fH
eight)); | 193 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fH
eight)); |
| 194 SkAutoTUnref<SkSurface> surf3(SkSurface::NewPicture(info.fWidth, info.fH
eight)); |
167 #if SK_SUPPORT_GPU | 195 #if SK_SUPPORT_GPU |
168 GrContext* ctx = skiagm::GetGr(); | 196 GrContext* ctx = skiagm::GetGr(); |
169 | 197 |
170 SkAutoTUnref<SkSurface> surf3(SkSurface::NewRenderTarget(ctx, info, 0)); | 198 SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info, 0)); |
171 #endif | 199 #endif |
172 | 200 |
173 test_surface(canvas, surf0); | 201 test_surface(canvas, surf0, true); |
174 canvas->translate(80, 0); | 202 canvas->translate(80, 0); |
175 test_surface(canvas, surf1); | 203 test_surface(canvas, surf1, true); |
176 canvas->translate(80, 0); | 204 canvas->translate(80, 0); |
177 test_surface(canvas, surf2); | 205 test_surface(canvas, surf2, true); |
| 206 canvas->translate(80, 0); |
| 207 test_surface(canvas, surf3, false); |
178 #if SK_SUPPORT_GPU | 208 #if SK_SUPPORT_GPU |
179 if (NULL != ctx) { | 209 if (NULL != ctx) { |
180 canvas->translate(80, 0); | 210 canvas->translate(80, 0); |
181 test_surface(canvas, surf3); | 211 test_surface(canvas, surf4, true); |
182 } | 212 } |
183 #endif | 213 #endif |
184 } | 214 } |
185 | 215 |
186 virtual uint32_t onGetFlags() const SK_OVERRIDE { | 216 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
187 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; | 217 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; |
188 } | 218 } |
189 | 219 |
190 private: | 220 private: |
191 typedef skiagm::GM INHERITED; | 221 typedef skiagm::GM INHERITED; |
192 }; | 222 }; |
193 | 223 |
194 ////////////////////////////////////////////////////////////////////////////// | 224 ////////////////////////////////////////////////////////////////////////////// |
195 | 225 |
196 static skiagm::GM* MyFactory(void*) { return new ImageGM; } | 226 static skiagm::GM* MyFactory(void*) { return new ImageGM; } |
197 static skiagm::GMRegistry reg(MyFactory); | 227 static skiagm::GMRegistry reg(MyFactory); |
OLD | NEW |