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

Side by Side Diff: src/utils/SkGatherPixelRefsAndRects.h

Issue 134473002: Pull in Chromium's version of GatherPixelRefs (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: cleaned up Created 6 years, 11 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 | « include/utils/SkPictureUtils.h ('k') | src/utils/SkGatherPixelRefsAndRects.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 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 #ifndef SkGatherPixelRefsAndRects_DEFINED
9 #define SkGatherPixelRefsAndRects_DEFINED
10
11 #include "SkBitmap.h"
12 #include "SkDevice.h"
13 #include "SkDraw.h"
14 #include "SkPictureUtils.h"
15 #include "SkRasterClip.h"
16 #include "SkRefCnt.h"
17 #include "SkRRect.h"
18 #include "SkTypes.h"
19
20 // This GatherPixelRefs device passes all discovered pixel refs and their
21 // device bounds to the user provided SkPixelRefContainer-derived object
22 class SkGatherPixelRefsAndRectsDevice : public SkBaseDevice {
23 public:
24 SK_DECLARE_INST_COUNT(SkGatherPixelRefsAndRectsDevice)
25
26 SkGatherPixelRefsAndRectsDevice(int width, int height,
27 SkPictureUtils::SkPixelRefContainer* prCont) {
28 fSize.set(width, height);
29 fPRCont = prCont;
30 SkSafeRef(fPRCont);
31 fEmptyBitmap.setConfig(SkBitmap::kNo_Config, width, height);
32 }
33
34 virtual ~SkGatherPixelRefsAndRectsDevice() {
35 SkSafeUnref(fPRCont);
36 }
37
38 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; }
39
40 virtual int width() const SK_OVERRIDE { return fSize.width(); }
41 virtual int height() const SK_OVERRIDE { return fSize.height(); }
42 virtual bool isOpaque() const SK_OVERRIDE { return false; }
43 virtual SkBitmap::Config config() const SK_OVERRIDE {
44 return SkBitmap::kNo_Config;
45 }
46 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
47 SkCanvas::Config8888 config8888) SK_OVERRIDE {
48 NotSupported();
49 }
50 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
51
52 protected:
53 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
54 return false;
55 }
56 virtual void clear(SkColor color) SK_OVERRIDE {
57 NothingToDo();
58 }
59 virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) SK_OVERRIDE {
60 SkBitmap bm;
61
62 if (GetBitmapFromPaint(paint, &bm)) {
63 SkRect clipRect = SkRect::Make(draw.fRC->getBounds());
64 fPRCont->add(bm.pixelRef(), clipRect);
65 }
66 }
67 virtual void drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, size_t count,
68 const SkPoint points[], const SkPaint& paint) SK_OVE RRIDE {
69 SkBitmap bm;
70 if (!GetBitmapFromPaint(paint, &bm)) {
71 return;
72 }
73
74 if (0 == count) {
75 return;
76 }
77
78 SkPoint min = points[0];
79 SkPoint max = points[0];
80 for (size_t i = 1; i < count; ++i) {
81 const SkPoint& point = points[i];
82
83 min.set(SkMinScalar(min.x(), point.x()), SkMinScalar(min.y(), point. y()));
84 max.set(SkMaxScalar(max.x(), point.x()), SkMaxScalar(max.y(), point. y()));
85 }
86
87 SkRect bounds = SkRect::MakeLTRB(min.x(), min.y(), max.x()+1, max.y()+1) ;
88
89 this->drawRect(draw, bounds, paint);
90 }
91 virtual void drawRect(const SkDraw& draw, const SkRect& rect,
92 const SkPaint& paint) SK_OVERRIDE {
93 SkBitmap bm;
94 if (GetBitmapFromPaint(paint, &bm)) {
95 SkRect mappedRect;
96 draw.fMatrix->mapRect(&mappedRect, rect);
97 SkRect clipRect = SkRect::Make(draw.fRC->getBounds());
98 mappedRect.intersect(clipRect);
99 fPRCont->add(bm.pixelRef(), mappedRect);
100 }
101 }
102 virtual void drawOval(const SkDraw& draw, const SkRect& rect,
103 const SkPaint& paint) SK_OVERRIDE {
104 this->drawRect(draw, rect, paint);
105 }
106 virtual void drawRRect(const SkDraw& draw, const SkRRect& rrect,
107 const SkPaint& paint) SK_OVERRIDE {
108 this->drawRect(draw, rrect.rect(), paint);
109 }
110 virtual void drawPath(const SkDraw& draw, const SkPath& path,
111 const SkPaint& paint, const SkMatrix* prePathMatrix,
112 bool pathIsMutable) SK_OVERRIDE {
113 SkBitmap bm;
114 if (!GetBitmapFromPaint(paint, &bm)) {
115 return;
116 }
117
118 SkRect pathBounds = path.getBounds();
119 if (NULL != prePathMatrix) {
120 prePathMatrix->mapRect(&pathBounds);
121 }
122
123 this->drawRect(draw, pathBounds, paint);
124 }
125 virtual void drawBitmap(const SkDraw& draw, const SkBitmap& bitmap,
126 const SkMatrix& matrix, const SkPaint& paint) SK_OVE RRIDE {
127 SkMatrix totMatrix;
128 totMatrix.setConcat(*draw.fMatrix, matrix);
129
130 SkRect bitmapRect = SkRect::MakeWH(SkIntToScalar(bitmap.width()),
131 SkIntToScalar(bitmap.height()));
132 SkRect mappedRect;
133 totMatrix.mapRect(&mappedRect, bitmapRect);
134 fPRCont->add(bitmap.pixelRef(), mappedRect);
135
136 SkBitmap paintBitmap;
137 if (GetBitmapFromPaint(paint, &paintBitmap)) {
138 fPRCont->add(paintBitmap.pixelRef(), mappedRect);
139 }
140 }
141 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
142 int x, int y, const SkPaint& paint) SK_OVERRIDE {
143 // Sprites aren't affected by current matrix, so we can't reuse drawRect .
144 SkMatrix matrix;
145 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
146
147 SkRect bitmapRect = SkRect::MakeWH(SkIntToScalar(bitmap.width()),
148 SkIntToScalar(bitmap.height()));
149 SkRect mappedRect;
150 matrix.mapRect(&mappedRect, bitmapRect);
151 fPRCont->add(bitmap.pixelRef(), mappedRect);
152
153 SkBitmap paintBitmap;
154 if (GetBitmapFromPaint(paint, &paintBitmap)) {
155 fPRCont->add(paintBitmap.pixelRef(), mappedRect);
156 }
157 }
158 virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
159 const SkRect* srcOrNull, const SkRect& dst,
160 const SkPaint& paint,
161 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
162 SkRect bitmapRect = SkRect::MakeWH(SkIntToScalar(bitmap.width()),
163 SkIntToScalar(bitmap.height()));
164 SkMatrix matrix;
165 matrix.setRectToRect(bitmapRect, dst, SkMatrix::kFill_ScaleToFit);
166 this->drawBitmap(draw, bitmap, matrix, paint);
167 }
168 virtual void drawText(const SkDraw& draw, const void* text, size_t len,
169 SkScalar x, SkScalar y,
170 const SkPaint& paint) SK_OVERRIDE {
171 SkBitmap bitmap;
172 if (!GetBitmapFromPaint(paint, &bitmap)) {
173 return;
174 }
175
176 // Math is borrowed from SkBBoxRecord
177 SkRect bounds;
178 paint.measureText(text, len, &bounds);
179 SkPaint::FontMetrics metrics;
180 paint.getFontMetrics(&metrics);
181
182 if (paint.isVerticalText()) {
183 SkScalar h = bounds.fBottom - bounds.fTop;
184 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
185 bounds.fTop -= h / 2;
186 bounds.fBottom -= h / 2;
187 }
188 bounds.fBottom += metrics.fBottom;
189 bounds.fTop += metrics.fTop;
190 } else {
191 SkScalar w = bounds.fRight - bounds.fLeft;
192 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
193 bounds.fLeft -= w / 2;
194 bounds.fRight -= w / 2;
195 } else if (paint.getTextAlign() == SkPaint::kRight_Align) {
196 bounds.fLeft -= w;
197 bounds.fRight -= w;
198 }
199 bounds.fTop = metrics.fTop;
200 bounds.fBottom = metrics.fBottom;
201 }
202
203 SkScalar pad = (metrics.fBottom - metrics.fTop) / 2;
204 bounds.fLeft -= pad;
205 bounds.fRight += pad;
206 bounds.offset(x, y);
207
208 this->drawRect(draw, bounds, paint);
209 }
210 virtual void drawPosText(const SkDraw& draw, const void* text, size_t len,
211 const SkScalar pos[], SkScalar constY,
212 int scalarsPerPos, const SkPaint& paint) SK_OVERRID E {
213 SkBitmap bitmap;
214 if (!GetBitmapFromPaint(paint, &bitmap)) {
215 return;
216 }
217
218 if (0 == len) {
219 return;
220 }
221
222 // Similar to SkDraw asserts.
223 SkASSERT(scalarsPerPos == 1 || scalarsPerPos == 2);
224
225 SkPoint min, max;
226 if (1 == scalarsPerPos) {
227 min.set(pos[0], constY);
228 max.set(pos[0], constY);
229 } else if (2 == scalarsPerPos) {
230 min.set(pos[0], constY + pos[1]);
231 max.set(pos[0], constY + pos[1]);
232 }
233
234 for (size_t i = 1; i < len; ++i) {
235 SkScalar x = pos[i * scalarsPerPos];
236 SkScalar y = constY;
237 if (2 == scalarsPerPos) {
238 y += pos[i * scalarsPerPos + 1];
239 }
240
241 min.set(SkMinScalar(x, min.x()), SkMinScalar(y, min.y()));
242 max.set(SkMaxScalar(x, max.x()), SkMaxScalar(y, max.y()));
243 }
244
245 SkRect bounds = SkRect::MakeLTRB(min.x(), min.y(), max.x(), max.y());
246
247 // Math is borrowed from SkBBoxRecord
248 SkPaint::FontMetrics metrics;
249 paint.getFontMetrics(&metrics);
250
251 bounds.fTop += metrics.fTop;
252 bounds.fBottom += metrics.fBottom;
253
254 SkScalar pad = (metrics.fTop - metrics.fBottom) / 2;
255 bounds.fLeft -= pad;
256 bounds.fRight += pad;
257
258 this->drawRect(draw, bounds, paint);
259 }
260 virtual void drawTextOnPath(const SkDraw& draw, const void* text, size_t len ,
261 const SkPath& path, const SkMatrix* matrix,
262 const SkPaint& paint) SK_OVERRIDE {
263 SkBitmap bitmap;
264 if (!GetBitmapFromPaint(paint, &bitmap)) {
265 return;
266 }
267
268 // Math is borrowed from SkBBoxRecord
269 SkRect bounds = path.getBounds();
270 SkPaint::FontMetrics metrics;
271 paint.getFontMetrics(&metrics);
272
273 SkScalar pad = metrics.fTop;
274 // TODO: inset?!
275 bounds.fLeft += pad;
276 bounds.fRight -= pad;
277 bounds.fTop += pad;
278 bounds.fBottom -= pad;
279
280 this->drawRect(draw, bounds, paint);
281 }
282 virtual void drawVertices(const SkDraw& draw, SkCanvas::VertexMode, int vert exCount,
283 const SkPoint verts[], const SkPoint texs[],
284 const SkColor colors[], SkXfermode* xmode,
285 const uint16_t indices[], int indexCount,
286 const SkPaint& paint) SK_OVERRIDE {
287 this->drawPoints(draw, SkCanvas::kPolygon_PointMode, vertexCount, verts, paint);
288 }
289 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
290 const SkPaint&) SK_OVERRIDE {
291 NothingToDo();
292 }
293 // TODO: allow this call to return failure, or move to SkBitmapDevice only.
294 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
295 return fEmptyBitmap;
296 }
297 virtual bool onReadPixels(const SkBitmap& bitmap,
298 int x, int y,
299 SkCanvas::Config8888 config8888) SK_OVERRIDE {
300 NotSupported();
301 return false;
302 }
303 virtual void lockPixels() SK_OVERRIDE { NothingToDo(); }
304 virtual void unlockPixels() SK_OVERRIDE { NothingToDo(); }
305 virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE { return false; }
306 virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE { return false ; }
307 virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&,
308 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
309 return false;
310 }
311
312 private:
313 SkPictureUtils::SkPixelRefContainer* fPRCont;
314 SkISize fSize;
315
316 SkBitmap fEmptyBitmap; // legacy -- need to remo ve
317
318 static bool GetBitmapFromPaint(const SkPaint &paint, SkBitmap* bitmap) {
319 SkShader* shader = paint.getShader();
320 if (NULL != shader) {
321 if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) {
322 return SkShader::kNone_BitmapType != shader->asABitmap(bitmap, N ULL, NULL);
323 }
324 }
325 return false;
326 }
327
328 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRI DE {
329 NotSupported();
330 }
331
332 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
333 int width, int height,
334 bool isOpaque,
335 Usage usage) SK_OVERRIDE {
336 // we expect to only get called via savelayer, in which case it is fine.
337 SkASSERT(kSaveLayer_Usage == usage);
338 return SkNEW_ARGS(SkGatherPixelRefsAndRectsDevice, (width, height, fPRCo nt));
339 }
340
341 virtual void flush() SK_OVERRIDE {}
342
343 static void NotSupported() {
344 SkDEBUGFAIL("this method should never be called");
345 }
346
347 static void NothingToDo() {}
348
349 typedef SkBaseDevice INHERITED;
350 };
351
352 #endif // SkGatherPixelRefsAndRects_DEFINED
353
OLDNEW
« no previous file with comments | « include/utils/SkPictureUtils.h ('k') | src/utils/SkGatherPixelRefsAndRects.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698