Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: gm/image.cpp

Issue 19729007: Add SkImage->draw() call with src and dst rects. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | include/core/SkImage.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 17 matching lines...) Expand all
28 void* mem = sk_malloc_throw(size); 28 void* mem = sk_malloc_throw(size);
29 stream.read(mem, size); 29 stream.read(mem, size);
30 return SkData::NewFromMalloc(mem, size); 30 return SkData::NewFromMalloc(mem, size);
31 } 31 }
32 32
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);
reed1 2013/07/22 18:19:49 SkAutoTUnref<SkImage>
39
39 if (image) { 40 if (image) {
41 SkRect src, dst;
42 src.iset(0, 0, image->width(), image->height());
43 dst.iset(0, 0, size.width(), size.height());
44 image->draw(canvas, &src, dst, NULL);
45
40 SkAutoCanvasRestore acr(canvas, true); 46 SkAutoCanvasRestore acr(canvas, true);
41 canvas->scale(size.width() * 1.0f / image->width(), 47 canvas->scale(size.width() * 1.0f / image->width(),
42 size.height() * 1.0f / image->height()); 48 size.height() * 1.0f / image->height());
43 image->draw(canvas,0, 0, NULL); 49 image->draw(canvas, 0, 0, NULL);
reed1 2013/07/22 18:19:49 Does this call draw on-top-of the previous call, o
44 image->unref(); 50 image->unref();
reed1 2013/07/22 18:19:49 shouldn't need this with SkAutoTUnref
45 } 51 }
46 } 52 }
47 53
48 static void drawContents(SkSurface* surface, SkColor fillC) { 54 static void drawContents(SkSurface* surface, SkColor fillC) {
49 SkSize size = SkSize::Make(SkIntToScalar(surface->width()), 55 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
50 SkIntToScalar(surface->height())); 56 SkIntToScalar(surface->height()));
51 SkCanvas* canvas = surface->getCanvas(); 57 SkCanvas* canvas = surface->getCanvas();
52 58
53 SkScalar stroke = size.fWidth / 10; 59 SkScalar stroke = size.fWidth / 10;
54 SkScalar radius = (size.fWidth - stroke) / 2; 60 SkScalar radius = (size.fWidth - stroke) / 2;
55 61
56 SkPaint paint; 62 SkPaint paint;
57 63
58 paint.setAntiAlias(true); 64 paint.setAntiAlias(true);
59 paint.setColor(fillC); 65 paint.setColor(fillC);
60 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); 66 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
61 67
62 paint.setStyle(SkPaint::kStroke_Style); 68 paint.setStyle(SkPaint::kStroke_Style);
63 paint.setStrokeWidth(stroke); 69 paint.setStrokeWidth(stroke);
64 paint.setColor(SK_ColorBLACK); 70 paint.setColor(SK_ColorBLACK);
65 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); 71 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
66 } 72 }
67 73
68 static void test_surface(SkCanvas* canvas, SkSurface* surf) { 74 static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
69 drawContents(surf, SK_ColorRED); 75 drawContents(surf, SK_ColorRED);
70 SkImage* imgR = surf->newImageSnapshot(); 76 SkImage* imgR = surf->newImageSnapshot();
71 77
72 if (true) { 78 if (true) {
73 SkImage* imgR2 = surf->newImageSnapshot(); 79 SkImage* imgR2 = surf->newImageSnapshot();
74 SkASSERT(imgR == imgR2); 80 SkASSERT(imgR == imgR2);
75 imgR2->unref(); 81 imgR2->unref();
76 } 82 }
77 83
78 drawContents(surf, SK_ColorGREEN); 84 drawContents(surf, SK_ColorGREEN);
79 SkImage* imgG = surf->newImageSnapshot(); 85 SkImage* imgG = surf->newImageSnapshot();
80 86
81 // since we've drawn after we snapped imgR, imgG will be a different obj 87 // since we've drawn after we snapped imgR, imgG will be a different obj
82 SkASSERT(imgR != imgG); 88 SkASSERT(imgR != imgG);
83 89
84 drawContents(surf, SK_ColorBLUE); 90 drawContents(surf, SK_ColorBLUE);
85 91
86 SkPaint paint; 92 SkPaint paint;
87 // paint.setFilterBitmap(true); 93 // paint.setFilterBitmap(true);
88 // paint.setAlpha(0x80); 94 // paint.setAlpha(0x80);
89 95
90 imgR->draw(canvas, 0, 0, &paint); 96 imgR->draw(canvas, 0, 0, usePaint ? &paint : NULL);
91 imgG->draw(canvas, 0, 80, &paint); 97 imgG->draw(canvas, 0, 80, usePaint ? &paint : NULL);
92 surf->draw(canvas, 0, 160, &paint); 98 surf->draw(canvas, 0, 160, usePaint ? &paint : NULL);
99
100 SkRect src1, src2, src3;
101 src1.iset(0, 0, surf->width(), surf->height());
102 src2.iset(-surf->width() / 2, -surf->height() / 2,
103 surf->width(), surf->height());
104 src3.iset(0, 0, surf->width() / 2, surf->height() / 2);
105
106 SkRect dst1, dst2, dst3, dst4;
107 dst1.set(0, 240, 65, 305);
108 dst2.set(0, 320, 65, 385);
109 dst3.set(10, 410, 45, 445);
110 dst4.set(0, 480, 65, 545);
111
112 imgR->draw(canvas, &src1, dst1, usePaint ? &paint : NULL);
113 imgG->draw(canvas, &src2, dst2, usePaint ? &paint : NULL);
114 imgR->draw(canvas, &src3, dst3, usePaint ? &paint : NULL);
115 imgG->draw(canvas, NULL, dst4, usePaint ? &paint : NULL);
93 116
94 imgG->unref(); 117 imgG->unref();
95 imgR->unref(); 118 imgR->unref();
96 } 119 }
97 120
98 class ImageGM : public skiagm::GM { 121 class ImageGM : public skiagm::GM {
99 void* fBuffer; 122 void* fBuffer;
100 size_t fBufferSize; 123 size_t fBufferSize;
101 SkSize fSize; 124 SkSize fSize;
102 enum { 125 enum {
(...skipping 12 matching lines...) Expand all
115 sk_free(fBuffer); 138 sk_free(fBuffer);
116 } 139 }
117 140
118 141
119 protected: 142 protected:
120 virtual SkString onShortName() { 143 virtual SkString onShortName() {
121 return SkString("image-surface"); 144 return SkString("image-surface");
122 } 145 }
123 146
124 virtual SkISize onISize() { 147 virtual SkISize onISize() {
125 return SkISize::Make(800, 500); 148 return SkISize::Make(960, 1200);
126 } 149 }
127 150
128 virtual void onDraw(SkCanvas* canvas) { 151 virtual void onDraw(SkCanvas* canvas) {
129 drawJpeg(canvas, this->getISize()); 152 drawJpeg(canvas, this->getISize());
130 153
131 canvas->scale(2, 2); 154 canvas->scale(2, 2);
132 155
133 static const char* kLabel1 = "Original Img"; 156 static const char* kLabel1 = "Original Img";
134 static const char* kLabel2 = "Modified Img"; 157 static const char* kLabel2 = "Modified Img";
135 static const char* kLabel3 = "Cur Surface"; 158 static const char* kLabel3 = "Cur Surface";
159 static const char* kLabel4 = "Full Crop";
160 static const char* kLabel5 = "Over-crop";
161 static const char* kLabel6 = "Upper-left";
162 static const char* kLabel7 = "No Crop";
136 163
137 static const char* kLabel4 = "Pre-Alloc Img"; 164 static const char* kLabel8 = "Pre-Alloc Img";
138 static const char* kLabel5 = "New Alloc Img"; 165 static const char* kLabel9 = "New Alloc Img";
139 static const char* kLabel6 = "SkPicture"; 166 static const char* kLabel10 = "SkPicture";
140 static const char* kLabel7 = "GPU"; 167 static const char* kLabel11 = "Null Paint";
168 static const char* kLabel12 = "GPU";
141 169
142 SkPaint textPaint; 170 SkPaint textPaint;
143 171
144 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint); 172 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
145 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint); 173 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
146 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint); 174 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
175 canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
176 canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
177 canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
178 canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
147 179
148 canvas->drawText(kLabel4, strlen(kLabel4), 80, 10, textPaint); 180 canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint);
149 canvas->drawText(kLabel5, strlen(kLabel5), 160, 10, textPaint); 181 canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
150 canvas->drawText(kLabel6, strlen(kLabel6), 250, 10, textPaint); 182 canvas->drawText(kLabel10, strlen(kLabel10), 250, 10, textPaint);
151 canvas->drawText(kLabel7, strlen(kLabel7), 340, 10, textPaint); 183 canvas->drawText(kLabel11, strlen(kLabel11), 320, 10, textPaint);
184 canvas->drawText(kLabel12, strlen(kLabel12), 410, 10, textPaint);
152 185
153 canvas->translate(80, 20); 186 canvas->translate(80, 20);
154 187
155 // since we draw into this directly, we need to start fresh 188 // since we draw into this directly, we need to start fresh
156 sk_bzero(fBuffer, fBufferSize); 189 sk_bzero(fBuffer, fBufferSize);
157 190
158 SkImage::Info info; 191 SkImage::Info info;
159 192
160 info.fWidth = W; 193 info.fWidth = W;
161 info.fHeight = H; 194 info.fHeight = H;
162 info.fColorType = SkImage::kPMColor_ColorType; 195 info.fColorType = SkImage::kPMColor_ColorType;
163 info.fAlphaType = SkImage::kPremul_AlphaType; 196 info.fAlphaType = SkImage::kPremul_AlphaType;
164 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB)); 197 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
165 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info)); 198 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
166 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fH eight)); 199 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fH eight));
200 SkAutoTUnref<SkSurface> surf3(SkSurface::NewPicture(info.fWidth, info.fH eight));
167 #if SK_SUPPORT_GPU 201 #if SK_SUPPORT_GPU
168 GrContext* ctx = skiagm::GetGr(); 202 GrContext* ctx = skiagm::GetGr();
169 203
170 SkAutoTUnref<SkSurface> surf3(SkSurface::NewRenderTarget(ctx, info, 0)); 204 SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info, 0));
171 #endif 205 #endif
172 206
173 test_surface(canvas, surf0); 207 test_surface(canvas, surf0, true);
174 canvas->translate(80, 0); 208 canvas->translate(80, 0);
175 test_surface(canvas, surf1); 209 test_surface(canvas, surf1, true);
176 canvas->translate(80, 0); 210 canvas->translate(80, 0);
177 test_surface(canvas, surf2); 211 test_surface(canvas, surf2, true);
212 canvas->translate(80, 0);
213 test_surface(canvas, surf3, false);
178 #if SK_SUPPORT_GPU 214 #if SK_SUPPORT_GPU
179 if (NULL != ctx) { 215 if (NULL != ctx) {
180 canvas->translate(80, 0); 216 canvas->translate(80, 0);
181 test_surface(canvas, surf3); 217 test_surface(canvas, surf4, true);
182 } 218 }
183 #endif 219 #endif
184 } 220 }
185 221
186 virtual uint32_t onGetFlags() const SK_OVERRIDE { 222 virtual uint32_t onGetFlags() const SK_OVERRIDE {
187 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; 223 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
188 } 224 }
189 225
190 private: 226 private:
191 typedef skiagm::GM INHERITED; 227 typedef skiagm::GM INHERITED;
192 }; 228 };
193 229
194 ////////////////////////////////////////////////////////////////////////////// 230 //////////////////////////////////////////////////////////////////////////////
195 231
196 static skiagm::GM* MyFactory(void*) { return new ImageGM; } 232 static skiagm::GM* MyFactory(void*) { return new ImageGM; }
197 static skiagm::GMRegistry reg(MyFactory); 233 static skiagm::GMRegistry reg(MyFactory);
OLDNEW
« no previous file with comments | « no previous file | include/core/SkImage.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698