| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "skia/ext/pixel_ref_utils.h" | 5 #include "skia/ext/pixel_ref_utils.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "third_party/skia/include/core/SkBitmapDevice.h" | 9 #include "third_party/skia/include/core/SkBitmapDevice.h" |
| 10 #include "third_party/skia/include/core/SkCanvas.h" | 10 #include "third_party/skia/include/core/SkCanvas.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 private: | 44 private: |
| 45 std::vector<PixelRefUtils::PositionPixelRef>* pixel_refs_; | 45 std::vector<PixelRefUtils::PositionPixelRef>* pixel_refs_; |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 class GatherPixelRefDevice : public SkBitmapDevice { | 48 class GatherPixelRefDevice : public SkBitmapDevice { |
| 49 public: | 49 public: |
| 50 GatherPixelRefDevice(const SkBitmap& bm, | 50 GatherPixelRefDevice(const SkBitmap& bm, |
| 51 DiscardablePixelRefSet* pixel_ref_set) | 51 DiscardablePixelRefSet* pixel_ref_set) |
| 52 : SkBitmapDevice(bm), pixel_ref_set_(pixel_ref_set) {} | 52 : SkBitmapDevice(bm), pixel_ref_set_(pixel_ref_set) {} |
| 53 | 53 |
| 54 virtual void clear(SkColor color) SK_OVERRIDE {} | 54 virtual void clear(SkColor color) override {} |
| 55 virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) SK_OVERRIDE { | 55 virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) override { |
| 56 SkBitmap bitmap; | 56 SkBitmap bitmap; |
| 57 if (GetBitmapFromPaint(paint, &bitmap)) { | 57 if (GetBitmapFromPaint(paint, &bitmap)) { |
| 58 SkRect clip_rect = SkRect::Make(draw.fRC->getBounds()); | 58 SkRect clip_rect = SkRect::Make(draw.fRC->getBounds()); |
| 59 AddBitmap(bitmap, clip_rect); | 59 AddBitmap(bitmap, clip_rect); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 virtual void drawPoints(const SkDraw& draw, | 63 virtual void drawPoints(const SkDraw& draw, |
| 64 SkCanvas::PointMode mode, | 64 SkCanvas::PointMode mode, |
| 65 size_t count, | 65 size_t count, |
| 66 const SkPoint points[], | 66 const SkPoint points[], |
| 67 const SkPaint& paint) SK_OVERRIDE { | 67 const SkPaint& paint) override { |
| 68 SkBitmap bitmap; | 68 SkBitmap bitmap; |
| 69 if (!GetBitmapFromPaint(paint, &bitmap)) | 69 if (!GetBitmapFromPaint(paint, &bitmap)) |
| 70 return; | 70 return; |
| 71 | 71 |
| 72 if (count == 0) | 72 if (count == 0) |
| 73 return; | 73 return; |
| 74 | 74 |
| 75 SkPoint min_point = points[0]; | 75 SkPoint min_point = points[0]; |
| 76 SkPoint max_point = points[0]; | 76 SkPoint max_point = points[0]; |
| 77 for (size_t i = 1; i < count; ++i) { | 77 for (size_t i = 1; i < count; ++i) { |
| 78 const SkPoint& point = points[i]; | 78 const SkPoint& point = points[i]; |
| 79 min_point.set(std::min(min_point.x(), point.x()), | 79 min_point.set(std::min(min_point.x(), point.x()), |
| 80 std::min(min_point.y(), point.y())); | 80 std::min(min_point.y(), point.y())); |
| 81 max_point.set(std::max(max_point.x(), point.x()), | 81 max_point.set(std::max(max_point.x(), point.x()), |
| 82 std::max(max_point.y(), point.y())); | 82 std::max(max_point.y(), point.y())); |
| 83 } | 83 } |
| 84 | 84 |
| 85 SkRect bounds = SkRect::MakeLTRB( | 85 SkRect bounds = SkRect::MakeLTRB( |
| 86 min_point.x(), min_point.y(), max_point.x(), max_point.y()); | 86 min_point.x(), min_point.y(), max_point.x(), max_point.y()); |
| 87 | 87 |
| 88 GatherPixelRefDevice::drawRect(draw, bounds, paint); | 88 GatherPixelRefDevice::drawRect(draw, bounds, paint); |
| 89 } | 89 } |
| 90 virtual void drawRect(const SkDraw& draw, | 90 virtual void drawRect(const SkDraw& draw, |
| 91 const SkRect& rect, | 91 const SkRect& rect, |
| 92 const SkPaint& paint) SK_OVERRIDE { | 92 const SkPaint& paint) override { |
| 93 SkBitmap bitmap; | 93 SkBitmap bitmap; |
| 94 if (GetBitmapFromPaint(paint, &bitmap)) { | 94 if (GetBitmapFromPaint(paint, &bitmap)) { |
| 95 SkRect mapped_rect; | 95 SkRect mapped_rect; |
| 96 draw.fMatrix->mapRect(&mapped_rect, rect); | 96 draw.fMatrix->mapRect(&mapped_rect, rect); |
| 97 mapped_rect.intersect(SkRect::Make(draw.fRC->getBounds())); | 97 mapped_rect.intersect(SkRect::Make(draw.fRC->getBounds())); |
| 98 AddBitmap(bitmap, mapped_rect); | 98 AddBitmap(bitmap, mapped_rect); |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 virtual void drawOval(const SkDraw& draw, | 101 virtual void drawOval(const SkDraw& draw, |
| 102 const SkRect& rect, | 102 const SkRect& rect, |
| 103 const SkPaint& paint) SK_OVERRIDE { | 103 const SkPaint& paint) override { |
| 104 GatherPixelRefDevice::drawRect(draw, rect, paint); | 104 GatherPixelRefDevice::drawRect(draw, rect, paint); |
| 105 } | 105 } |
| 106 virtual void drawRRect(const SkDraw& draw, | 106 virtual void drawRRect(const SkDraw& draw, |
| 107 const SkRRect& rect, | 107 const SkRRect& rect, |
| 108 const SkPaint& paint) SK_OVERRIDE { | 108 const SkPaint& paint) override { |
| 109 GatherPixelRefDevice::drawRect(draw, rect.rect(), paint); | 109 GatherPixelRefDevice::drawRect(draw, rect.rect(), paint); |
| 110 } | 110 } |
| 111 virtual void drawPath(const SkDraw& draw, | 111 virtual void drawPath(const SkDraw& draw, |
| 112 const SkPath& path, | 112 const SkPath& path, |
| 113 const SkPaint& paint, | 113 const SkPaint& paint, |
| 114 const SkMatrix* pre_path_matrix, | 114 const SkMatrix* pre_path_matrix, |
| 115 bool path_is_mutable) SK_OVERRIDE { | 115 bool path_is_mutable) override { |
| 116 SkBitmap bitmap; | 116 SkBitmap bitmap; |
| 117 if (!GetBitmapFromPaint(paint, &bitmap)) | 117 if (!GetBitmapFromPaint(paint, &bitmap)) |
| 118 return; | 118 return; |
| 119 | 119 |
| 120 SkRect path_bounds = path.getBounds(); | 120 SkRect path_bounds = path.getBounds(); |
| 121 SkRect final_rect; | 121 SkRect final_rect; |
| 122 if (pre_path_matrix != NULL) | 122 if (pre_path_matrix != NULL) |
| 123 pre_path_matrix->mapRect(&final_rect, path_bounds); | 123 pre_path_matrix->mapRect(&final_rect, path_bounds); |
| 124 else | 124 else |
| 125 final_rect = path_bounds; | 125 final_rect = path_bounds; |
| 126 | 126 |
| 127 GatherPixelRefDevice::drawRect(draw, final_rect, paint); | 127 GatherPixelRefDevice::drawRect(draw, final_rect, paint); |
| 128 } | 128 } |
| 129 virtual void drawBitmap(const SkDraw& draw, | 129 virtual void drawBitmap(const SkDraw& draw, |
| 130 const SkBitmap& bitmap, | 130 const SkBitmap& bitmap, |
| 131 const SkMatrix& matrix, | 131 const SkMatrix& matrix, |
| 132 const SkPaint& paint) SK_OVERRIDE { | 132 const SkPaint& paint) override { |
| 133 SkMatrix total_matrix; | 133 SkMatrix total_matrix; |
| 134 total_matrix.setConcat(*draw.fMatrix, matrix); | 134 total_matrix.setConcat(*draw.fMatrix, matrix); |
| 135 | 135 |
| 136 SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); | 136 SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); |
| 137 SkRect mapped_rect; | 137 SkRect mapped_rect; |
| 138 total_matrix.mapRect(&mapped_rect, bitmap_rect); | 138 total_matrix.mapRect(&mapped_rect, bitmap_rect); |
| 139 AddBitmap(bitmap, mapped_rect); | 139 AddBitmap(bitmap, mapped_rect); |
| 140 | 140 |
| 141 SkBitmap paint_bitmap; | 141 SkBitmap paint_bitmap; |
| 142 if (GetBitmapFromPaint(paint, &paint_bitmap)) | 142 if (GetBitmapFromPaint(paint, &paint_bitmap)) |
| 143 AddBitmap(paint_bitmap, mapped_rect); | 143 AddBitmap(paint_bitmap, mapped_rect); |
| 144 } | 144 } |
| 145 virtual void drawBitmapRect(const SkDraw& draw, | 145 virtual void drawBitmapRect(const SkDraw& draw, |
| 146 const SkBitmap& bitmap, | 146 const SkBitmap& bitmap, |
| 147 const SkRect* src_or_null, | 147 const SkRect* src_or_null, |
| 148 const SkRect& dst, | 148 const SkRect& dst, |
| 149 const SkPaint& paint, | 149 const SkPaint& paint, |
| 150 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE { | 150 SkCanvas::DrawBitmapRectFlags flags) override { |
| 151 SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); | 151 SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); |
| 152 SkMatrix matrix; | 152 SkMatrix matrix; |
| 153 matrix.setRectToRect(bitmap_rect, dst, SkMatrix::kFill_ScaleToFit); | 153 matrix.setRectToRect(bitmap_rect, dst, SkMatrix::kFill_ScaleToFit); |
| 154 GatherPixelRefDevice::drawBitmap(draw, bitmap, matrix, paint); | 154 GatherPixelRefDevice::drawBitmap(draw, bitmap, matrix, paint); |
| 155 } | 155 } |
| 156 virtual void drawSprite(const SkDraw& draw, | 156 virtual void drawSprite(const SkDraw& draw, |
| 157 const SkBitmap& bitmap, | 157 const SkBitmap& bitmap, |
| 158 int x, | 158 int x, |
| 159 int y, | 159 int y, |
| 160 const SkPaint& paint) SK_OVERRIDE { | 160 const SkPaint& paint) override { |
| 161 // Sprites aren't affected by current matrix, so we can't reuse drawRect. | 161 // Sprites aren't affected by current matrix, so we can't reuse drawRect. |
| 162 SkMatrix matrix; | 162 SkMatrix matrix; |
| 163 matrix.setTranslate(x, y); | 163 matrix.setTranslate(x, y); |
| 164 | 164 |
| 165 SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); | 165 SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); |
| 166 SkRect mapped_rect; | 166 SkRect mapped_rect; |
| 167 matrix.mapRect(&mapped_rect, bitmap_rect); | 167 matrix.mapRect(&mapped_rect, bitmap_rect); |
| 168 | 168 |
| 169 AddBitmap(bitmap, mapped_rect); | 169 AddBitmap(bitmap, mapped_rect); |
| 170 SkBitmap paint_bitmap; | 170 SkBitmap paint_bitmap; |
| 171 if (GetBitmapFromPaint(paint, &paint_bitmap)) | 171 if (GetBitmapFromPaint(paint, &paint_bitmap)) |
| 172 AddBitmap(paint_bitmap, mapped_rect); | 172 AddBitmap(paint_bitmap, mapped_rect); |
| 173 } | 173 } |
| 174 virtual void drawText(const SkDraw& draw, | 174 virtual void drawText(const SkDraw& draw, |
| 175 const void* text, | 175 const void* text, |
| 176 size_t len, | 176 size_t len, |
| 177 SkScalar x, | 177 SkScalar x, |
| 178 SkScalar y, | 178 SkScalar y, |
| 179 const SkPaint& paint) SK_OVERRIDE { | 179 const SkPaint& paint) override { |
| 180 SkBitmap bitmap; | 180 SkBitmap bitmap; |
| 181 if (!GetBitmapFromPaint(paint, &bitmap)) | 181 if (!GetBitmapFromPaint(paint, &bitmap)) |
| 182 return; | 182 return; |
| 183 | 183 |
| 184 // Math is borrowed from SkBBoxRecord | 184 // Math is borrowed from SkBBoxRecord |
| 185 SkRect bounds; | 185 SkRect bounds; |
| 186 paint.measureText(text, len, &bounds); | 186 paint.measureText(text, len, &bounds); |
| 187 SkPaint::FontMetrics metrics; | 187 SkPaint::FontMetrics metrics; |
| 188 paint.getFontMetrics(&metrics); | 188 paint.getFontMetrics(&metrics); |
| 189 | 189 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 217 bounds.fBottom += y; | 217 bounds.fBottom += y; |
| 218 | 218 |
| 219 GatherPixelRefDevice::drawRect(draw, bounds, paint); | 219 GatherPixelRefDevice::drawRect(draw, bounds, paint); |
| 220 } | 220 } |
| 221 virtual void drawPosText(const SkDraw& draw, | 221 virtual void drawPosText(const SkDraw& draw, |
| 222 const void* text, | 222 const void* text, |
| 223 size_t len, | 223 size_t len, |
| 224 const SkScalar pos[], | 224 const SkScalar pos[], |
| 225 int scalars_per_pos, | 225 int scalars_per_pos, |
| 226 const SkPoint& offset, | 226 const SkPoint& offset, |
| 227 const SkPaint& paint) SK_OVERRIDE { | 227 const SkPaint& paint) override { |
| 228 SkBitmap bitmap; | 228 SkBitmap bitmap; |
| 229 if (!GetBitmapFromPaint(paint, &bitmap)) | 229 if (!GetBitmapFromPaint(paint, &bitmap)) |
| 230 return; | 230 return; |
| 231 | 231 |
| 232 if (len == 0) | 232 if (len == 0) |
| 233 return; | 233 return; |
| 234 | 234 |
| 235 // Similar to SkDraw asserts. | 235 // Similar to SkDraw asserts. |
| 236 SkASSERT(scalars_per_pos == 1 || scalars_per_pos == 2); | 236 SkASSERT(scalars_per_pos == 1 || scalars_per_pos == 2); |
| 237 | 237 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 261 bounds.fLeft += pad; | 261 bounds.fLeft += pad; |
| 262 bounds.fRight -= pad; | 262 bounds.fRight -= pad; |
| 263 | 263 |
| 264 GatherPixelRefDevice::drawRect(draw, bounds, paint); | 264 GatherPixelRefDevice::drawRect(draw, bounds, paint); |
| 265 } | 265 } |
| 266 virtual void drawTextOnPath(const SkDraw& draw, | 266 virtual void drawTextOnPath(const SkDraw& draw, |
| 267 const void* text, | 267 const void* text, |
| 268 size_t len, | 268 size_t len, |
| 269 const SkPath& path, | 269 const SkPath& path, |
| 270 const SkMatrix* matrix, | 270 const SkMatrix* matrix, |
| 271 const SkPaint& paint) SK_OVERRIDE { | 271 const SkPaint& paint) override { |
| 272 SkBitmap bitmap; | 272 SkBitmap bitmap; |
| 273 if (!GetBitmapFromPaint(paint, &bitmap)) | 273 if (!GetBitmapFromPaint(paint, &bitmap)) |
| 274 return; | 274 return; |
| 275 | 275 |
| 276 // Math is borrowed from SkBBoxRecord | 276 // Math is borrowed from SkBBoxRecord |
| 277 SkRect bounds = path.getBounds(); | 277 SkRect bounds = path.getBounds(); |
| 278 SkPaint::FontMetrics metrics; | 278 SkPaint::FontMetrics metrics; |
| 279 paint.getFontMetrics(&metrics); | 279 paint.getFontMetrics(&metrics); |
| 280 | 280 |
| 281 SkScalar pad = metrics.fTop; | 281 SkScalar pad = metrics.fTop; |
| 282 bounds.fLeft += pad; | 282 bounds.fLeft += pad; |
| 283 bounds.fRight -= pad; | 283 bounds.fRight -= pad; |
| 284 bounds.fTop += pad; | 284 bounds.fTop += pad; |
| 285 bounds.fBottom -= pad; | 285 bounds.fBottom -= pad; |
| 286 | 286 |
| 287 GatherPixelRefDevice::drawRect(draw, bounds, paint); | 287 GatherPixelRefDevice::drawRect(draw, bounds, paint); |
| 288 } | 288 } |
| 289 virtual void drawVertices(const SkDraw& draw, | 289 virtual void drawVertices(const SkDraw& draw, |
| 290 SkCanvas::VertexMode, | 290 SkCanvas::VertexMode, |
| 291 int vertex_count, | 291 int vertex_count, |
| 292 const SkPoint verts[], | 292 const SkPoint verts[], |
| 293 const SkPoint texs[], | 293 const SkPoint texs[], |
| 294 const SkColor colors[], | 294 const SkColor colors[], |
| 295 SkXfermode* xmode, | 295 SkXfermode* xmode, |
| 296 const uint16_t indices[], | 296 const uint16_t indices[], |
| 297 int index_count, | 297 int index_count, |
| 298 const SkPaint& paint) SK_OVERRIDE { | 298 const SkPaint& paint) override { |
| 299 GatherPixelRefDevice::drawPoints( | 299 GatherPixelRefDevice::drawPoints( |
| 300 draw, SkCanvas::kPolygon_PointMode, vertex_count, verts, paint); | 300 draw, SkCanvas::kPolygon_PointMode, vertex_count, verts, paint); |
| 301 } | 301 } |
| 302 virtual void drawDevice(const SkDraw&, | 302 virtual void drawDevice(const SkDraw&, |
| 303 SkBaseDevice*, | 303 SkBaseDevice*, |
| 304 int x, | 304 int x, |
| 305 int y, | 305 int y, |
| 306 const SkPaint&) SK_OVERRIDE {} | 306 const SkPaint&) override {} |
| 307 | 307 |
| 308 protected: | 308 protected: |
| 309 virtual bool onReadPixels(const SkImageInfo& info, | 309 virtual bool onReadPixels(const SkImageInfo& info, |
| 310 void* pixels, | 310 void* pixels, |
| 311 size_t rowBytes, | 311 size_t rowBytes, |
| 312 int x, | 312 int x, |
| 313 int y) SK_OVERRIDE { | 313 int y) override { |
| 314 return false; | 314 return false; |
| 315 } | 315 } |
| 316 | 316 |
| 317 virtual bool onWritePixels(const SkImageInfo& info, | 317 virtual bool onWritePixels(const SkImageInfo& info, |
| 318 const void* pixels, | 318 const void* pixels, |
| 319 size_t rowBytes, | 319 size_t rowBytes, |
| 320 int x, | 320 int x, |
| 321 int y) SK_OVERRIDE { | 321 int y) override { |
| 322 return false; | 322 return false; |
| 323 } | 323 } |
| 324 | 324 |
| 325 private: | 325 private: |
| 326 DiscardablePixelRefSet* pixel_ref_set_; | 326 DiscardablePixelRefSet* pixel_ref_set_; |
| 327 | 327 |
| 328 void AddBitmap(const SkBitmap& bm, const SkRect& rect) { | 328 void AddBitmap(const SkBitmap& bm, const SkRect& rect) { |
| 329 SkRect canvas_rect = SkRect::MakeWH(width(), height()); | 329 SkRect canvas_rect = SkRect::MakeWH(width(), height()); |
| 330 SkRect paint_rect = SkRect::MakeEmpty(); | 330 SkRect paint_rect = SkRect::MakeEmpty(); |
| 331 paint_rect.intersect(rect, canvas_rect); | 331 paint_rect.intersect(rect, canvas_rect); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 358 GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); | 358 GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); |
| 359 SkNoSaveLayerCanvas canvas(&device); | 359 SkNoSaveLayerCanvas canvas(&device); |
| 360 | 360 |
| 361 canvas.clipRect(SkRect::MakeWH(picture->width(), picture->height()), | 361 canvas.clipRect(SkRect::MakeWH(picture->width(), picture->height()), |
| 362 SkRegion::kIntersect_Op, | 362 SkRegion::kIntersect_Op, |
| 363 false); | 363 false); |
| 364 canvas.drawPicture(picture); | 364 canvas.drawPicture(picture); |
| 365 } | 365 } |
| 366 | 366 |
| 367 } // namespace skia | 367 } // namespace skia |
| OLD | NEW |