scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 8 | #include "SkBBoxHierarchy.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 9 | #include "SkBlurImageFilter.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 12 | #include "SkDashPathEffect.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 13 | #include "SkData.h" |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 14 | #include "SkDecodingImageGenerator.h" |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 15 | #include "SkError.h" |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 16 | #include "SkImageEncoder.h" |
| 17 | #include "SkImageGenerator.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 18 | #include "SkPaint.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 19 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 20 | #include "SkPictureRecorder.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 21 | #include "SkPictureUtils.h" |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 22 | #include "SkPixelRef.h" |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 23 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 24 | #include "SkRandom.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 25 | #include "SkShader.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 26 | #include "SkStream.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 27 | |
| 28 | #if SK_SUPPORT_GPU |
| 29 | #include "SkSurface.h" |
| 30 | #include "GrContextFactory.h" |
| 31 | #include "GrPictureUtils.h" |
| 32 | #endif |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 33 | #include "Test.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 34 | |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 35 | #include "SkLumaColorFilter.h" |
| 36 | #include "SkColorFilterImageFilter.h" |
| 37 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 38 | static const int gColorScale = 30; |
| 39 | static const int gColorOffset = 60; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 40 | |
| 41 | static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 42 | bm->allocN32Pixels(w, h); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 43 | bm->eraseColor(color); |
| 44 | if (immutable) { |
| 45 | bm->setImmutable(); |
| 46 | } |
| 47 | } |
| 48 | |
robertphillips@google.com | fe5824a | 2014-01-09 19:45:29 +0000 | [diff] [blame] | 49 | static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 50 | SkASSERT(w % 2 == 0); |
| 51 | SkASSERT(h % 2 == 0); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 52 | bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType, |
| 53 | kPremul_SkAlphaType)); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 54 | SkAutoLockPixels lock(*bm); |
| 55 | for (int y = 0; y < h; y += 2) { |
| 56 | uint8_t* s = bm->getAddr8(0, y); |
| 57 | for (int x = 0; x < w; x += 2) { |
| 58 | *s++ = 0xFF; |
| 59 | *s++ = 0x00; |
| 60 | } |
| 61 | s = bm->getAddr8(0, y + 1); |
| 62 | for (int x = 0; x < w; x += 2) { |
| 63 | *s++ = 0x00; |
| 64 | *s++ = 0xFF; |
| 65 | } |
| 66 | } |
| 67 | if (immutable) { |
| 68 | bm->setImmutable(); |
| 69 | } |
| 70 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 71 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 72 | static void init_paint(SkPaint* paint, const SkBitmap &bm) { |
| 73 | SkShader* shader = SkShader::CreateBitmapShader(bm, |
| 74 | SkShader::kClamp_TileMode, |
| 75 | SkShader::kClamp_TileMode); |
| 76 | paint->setShader(shader)->unref(); |
| 77 | } |
| 78 | |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 79 | typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 80 | const SkBitmap&, const SkPoint&, |
| 81 | SkTDArray<SkPixelRef*>* usedPixRefs); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 82 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 83 | static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 84 | const SkBitmap& altBM, const SkPoint& pos, |
| 85 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 86 | SkPaint paint; |
| 87 | init_paint(&paint, bm); |
| 88 | |
| 89 | canvas->drawPaint(paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 90 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 91 | } |
| 92 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 93 | static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 94 | const SkBitmap& altBM, const SkPoint& pos, |
| 95 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 96 | SkPaint paint; |
| 97 | init_paint(&paint, bm); |
| 98 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 99 | // draw a rect |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 100 | SkPoint points[5] = { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 101 | { pos.fX, pos.fY }, |
| 102 | { pos.fX + bm.width() - 1, pos.fY }, |
| 103 | { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 }, |
| 104 | { pos.fX, pos.fY + bm.height() - 1 }, |
| 105 | { pos.fX, pos.fY }, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 109 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 110 | } |
| 111 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 112 | static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 113 | const SkBitmap& altBM, const SkPoint& pos, |
| 114 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 115 | SkPaint paint; |
| 116 | init_paint(&paint, bm); |
| 117 | |
| 118 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 119 | r.offset(pos.fX, pos.fY); |
| 120 | |
| 121 | canvas->drawRect(r, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 122 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 123 | } |
| 124 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 125 | static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 126 | const SkBitmap& altBM, const SkPoint& pos, |
| 127 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 128 | SkPaint paint; |
| 129 | init_paint(&paint, bm); |
| 130 | |
| 131 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 132 | r.offset(pos.fX, pos.fY); |
| 133 | |
| 134 | canvas->drawOval(r, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 135 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 136 | } |
| 137 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 138 | static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 139 | const SkBitmap& altBM, const SkPoint& pos, |
| 140 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 141 | SkPaint paint; |
| 142 | init_paint(&paint, bm); |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 143 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 144 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 145 | r.offset(pos.fX, pos.fY); |
| 146 | |
| 147 | SkRRect rr; |
| 148 | rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4); |
| 149 | canvas->drawRRect(rr, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 150 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 151 | } |
| 152 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 153 | static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 154 | const SkBitmap& altBM, const SkPoint& pos, |
| 155 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 156 | SkPaint paint; |
| 157 | init_paint(&paint, bm); |
| 158 | |
| 159 | SkPath path; |
| 160 | path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height())); |
| 161 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 162 | path.close(); |
| 163 | path.offset(pos.fX, pos.fY); |
| 164 | |
| 165 | canvas->drawPath(path, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 166 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 167 | } |
| 168 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 169 | static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 170 | const SkBitmap& altBM, const SkPoint& pos, |
| 171 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 172 | canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 173 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 174 | } |
| 175 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 176 | static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 177 | const SkBitmap& altBM, const SkPoint& pos, |
| 178 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 179 | SkPaint paint; |
| 180 | init_paint(&paint, bm); |
| 181 | |
| 182 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 183 | canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 184 | *usedPixRefs->append() = bm.pixelRef(); |
| 185 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 186 | } |
| 187 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 188 | static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 189 | const SkBitmap& altBM, const SkPoint& pos, |
| 190 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 191 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 192 | |
| 193 | SkPoint p(pos); |
| 194 | ctm.mapPoints(&p, 1); |
| 195 | |
| 196 | canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 197 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | #if 0 |
| 201 | // Although specifiable, this case doesn't seem to make sense (i.e., the |
| 202 | // bitmap in the shader is never used). |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 203 | static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 204 | const SkBitmap& altBM, const SkPoint& pos, |
| 205 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 206 | SkPaint paint; |
| 207 | init_paint(&paint, bm); |
| 208 | |
| 209 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 210 | |
| 211 | SkPoint p(pos); |
| 212 | ctm.mapPoints(&p, 1); |
| 213 | |
| 214 | canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 215 | *usedPixRefs->append() = bm.pixelRef(); |
| 216 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 217 | } |
| 218 | #endif |
| 219 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 220 | static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 221 | const SkBitmap& altBM, const SkPoint& pos, |
| 222 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 223 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 224 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 225 | r.offset(pos.fX, pos.fY); |
| 226 | canvas->drawBitmapRectToRect(bm, NULL, r, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 227 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 228 | } |
| 229 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 230 | static void drawbitmaprect_withshader_proc(SkCanvas* canvas, |
| 231 | const SkBitmap& bm, |
| 232 | const SkBitmap& altBM, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 233 | const SkPoint& pos, |
| 234 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 235 | SkPaint paint; |
| 236 | init_paint(&paint, bm); |
| 237 | |
| 238 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 239 | r.offset(pos.fX, pos.fY); |
| 240 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 241 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 242 | canvas->drawBitmapRectToRect(altBM, NULL, r, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 243 | *usedPixRefs->append() = bm.pixelRef(); |
| 244 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 245 | } |
| 246 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 247 | static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 248 | const SkBitmap& altBM, const SkPoint& pos, |
| 249 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 250 | SkPaint paint; |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 251 | init_paint(&paint, bm); |
| 252 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 253 | |
| 254 | canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 255 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 256 | } |
| 257 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 258 | static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 259 | const SkBitmap& altBM, const SkPoint& pos, |
| 260 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 261 | SkPaint paint; |
| 262 | init_paint(&paint, bm); |
| 263 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 264 | |
| 265 | SkPoint point = { pos.fX, pos.fY + bm.height() }; |
| 266 | canvas->drawPosText("O", 1, &point, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 267 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 268 | } |
| 269 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 270 | static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 271 | const SkBitmap& altBM, const SkPoint& pos, |
| 272 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 273 | SkPaint paint; |
| 274 | |
| 275 | init_paint(&paint, bm); |
| 276 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 277 | |
| 278 | SkPath path; |
| 279 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 280 | path.offset(pos.fX, pos.fY+bm.height()); |
| 281 | |
| 282 | canvas->drawTextOnPath("O", 1, path, NULL, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 283 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 284 | } |
| 285 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 286 | static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 287 | const SkBitmap& altBM, const SkPoint& pos, |
| 288 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 289 | SkPaint paint; |
| 290 | init_paint(&paint, bm); |
| 291 | |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 292 | SkPoint verts[4] = { |
| 293 | { pos.fX, pos.fY }, |
| 294 | { pos.fX + bm.width(), pos.fY }, |
| 295 | { pos.fX + bm.width(), pos.fY + bm.height() }, |
| 296 | { pos.fX, pos.fY + bm.height() } |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 297 | }; |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 298 | SkPoint texs[4] = { { 0, 0 }, |
| 299 | { SkIntToScalar(bm.width()), 0 }, |
| 300 | { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 301 | { 0, SkIntToScalar(bm.height()) } }; |
| 302 | uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 }; |
| 303 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 304 | canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 305 | indices, 6, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 306 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Return a picture with the bitmaps drawn at the specified positions. |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 310 | static SkPicture* record_bitmaps(const SkBitmap bm[], |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 311 | const SkPoint pos[], |
| 312 | SkTDArray<SkPixelRef*> analytic[], |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 313 | int count, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 314 | DrawBitmapProc proc) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 315 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 316 | SkCanvas* canvas = recorder.beginRecording(1000, 1000); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 317 | for (int i = 0; i < count; ++i) { |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 318 | analytic[i].rewind(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 319 | canvas->save(); |
| 320 | SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, |
| 321 | SkIntToScalar(bm[i].width()), |
| 322 | SkIntToScalar(bm[i].height())); |
| 323 | canvas->clipRect(clipRect, SkRegion::kIntersect_Op); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 324 | proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 325 | canvas->restore(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 326 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 327 | return recorder.endRecording(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 328 | } |
| 329 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 330 | static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 331 | rect->fLeft = rand.nextRangeScalar(-W, 2*W); |
| 332 | rect->fTop = rand.nextRangeScalar(-H, 2*H); |
| 333 | rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); |
| 334 | rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); |
| 335 | |
| 336 | // we integralize rect to make our tests more predictable, since Gather is |
| 337 | // a little sloppy. |
| 338 | SkIRect ir; |
| 339 | rect->round(&ir); |
| 340 | rect->set(ir); |
| 341 | } |
| 342 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 343 | static void draw(SkPicture* pic, int width, int height, SkBitmap* result) { |
| 344 | make_bm(result, width, height, SK_ColorBLACK, false); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 345 | |
| 346 | SkCanvas canvas(*result); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 347 | canvas.drawPicture(pic); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | template <typename T> int find_index(const T* array, T elem, int count) { |
| 351 | for (int i = 0; i < count; ++i) { |
| 352 | if (array[i] == elem) { |
| 353 | return i; |
| 354 | } |
| 355 | } |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | // Return true if 'ref' is found in array[] |
| 360 | static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) { |
| 361 | return find_index<const SkPixelRef*>(array, ref, count) >= 0; |
| 362 | } |
| 363 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 364 | // Look at each pixel that is inside 'subset', and if its color appears in |
| 365 | // colors[], find the corresponding value in refs[] and append that ref into |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 366 | // array, skipping duplicates of the same value. |
| 367 | // Note that gathering pixelRefs from rendered colors suffers from the problem |
| 368 | // that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color) |
| 369 | // isn't easy to reconstruct. |
| 370 | static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[], |
| 371 | int count, SkTDArray<SkPixelRef*>* array, |
| 372 | const SkRect& subset) { |
| 373 | SkIRect ir; |
| 374 | subset.roundOut(&ir); |
| 375 | |
| 376 | if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) { |
| 377 | return; |
| 378 | } |
| 379 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 380 | // Since we only want to return unique values in array, when we scan we just |
| 381 | // set a bit for each index'd color found. In practice we only have a few |
| 382 | // distinct colors, so we just use an int's bits as our array. Hence the |
| 383 | // assert that count <= number-of-bits-in-our-int. |
| 384 | SkASSERT((unsigned)count <= 32); |
| 385 | uint32_t bitarray = 0; |
| 386 | |
| 387 | SkAutoLockPixels alp(bm); |
| 388 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 389 | for (int y = ir.fTop; y < ir.fBottom; ++y) { |
| 390 | for (int x = ir.fLeft; x < ir.fRight; ++x) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 391 | SkPMColor pmc = *bm.getAddr32(x, y); |
| 392 | // the only good case where the color is not found would be if |
| 393 | // the color is transparent, meaning no bitmap was drawn in that |
| 394 | // pixel. |
| 395 | if (pmc) { |
bsalomon@google.com | c3d753e | 2013-01-08 17:24:44 +0000 | [diff] [blame] | 396 | uint32_t index = SkGetPackedR32(pmc); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 397 | SkASSERT(SkGetPackedG32(pmc) == index); |
| 398 | SkASSERT(SkGetPackedB32(pmc) == index); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 399 | if (0 == index) { |
| 400 | continue; // background color |
| 401 | } |
| 402 | SkASSERT(0 == (index - gColorOffset) % gColorScale); |
| 403 | index = (index - gColorOffset) / gColorScale; |
bsalomon@google.com | 5f429b0 | 2013-01-08 18:42:20 +0000 | [diff] [blame] | 404 | SkASSERT(static_cast<int>(index) < count); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 405 | bitarray |= 1 << index; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | for (int i = 0; i < count; ++i) { |
| 411 | if (bitarray & (1 << i)) { |
| 412 | *array->append() = refs[i]; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
robertphillips@google.com | 0e4ce14 | 2014-01-13 13:46:45 +0000 | [diff] [blame] | 417 | static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h, |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 418 | const SkTDArray<SkPixelRef*> analytic[], |
| 419 | int count, |
| 420 | SkTDArray<SkPixelRef*>* result, |
robertphillips@google.com | 0e4ce14 | 2014-01-13 13:46:45 +0000 | [diff] [blame] | 421 | const SkRect& subset) { |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 422 | for (int i = 0; i < count; ++i) { |
| 423 | SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); |
| 424 | |
| 425 | if (SkRect::Intersects(subset, rect)) { |
| 426 | result->append(analytic[i].count(), analytic[i].begin()); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 431 | |
| 432 | static const struct { |
| 433 | const DrawBitmapProc proc; |
| 434 | const char* const desc; |
| 435 | } gProcs[] = { |
| 436 | {drawpaint_proc, "drawpaint"}, |
| 437 | {drawpoints_proc, "drawpoints"}, |
| 438 | {drawrect_proc, "drawrect"}, |
| 439 | {drawoval_proc, "drawoval"}, |
| 440 | {drawrrect_proc, "drawrrect"}, |
| 441 | {drawpath_proc, "drawpath"}, |
| 442 | {drawbitmap_proc, "drawbitmap"}, |
| 443 | {drawbitmap_withshader_proc, "drawbitmap_withshader"}, |
| 444 | {drawsprite_proc, "drawsprite"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 445 | #if 0 |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 446 | {drawsprite_withshader_proc, "drawsprite_withshader"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 447 | #endif |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 448 | {drawbitmaprect_proc, "drawbitmaprect"}, |
| 449 | {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"}, |
| 450 | {drawtext_proc, "drawtext"}, |
| 451 | {drawpostext_proc, "drawpostext"}, |
| 452 | {drawtextonpath_proc, "drawtextonpath"}, |
| 453 | {drawverts_proc, "drawverts"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 457 | // Our convention is that the color components contain an encoding of |
| 458 | // the index of their corresponding bitmap/pixelref. (0,0,0,0) is |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 459 | // reserved for the background |
| 460 | for (int i = 0; i < num; ++i) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 461 | make_bm(&bm[i], w, h, |
| 462 | SkColorSetARGB(0xFF, |
| 463 | gColorScale*i+gColorOffset, |
| 464 | gColorScale*i+gColorOffset, |
| 465 | gColorScale*i+gColorOffset), |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 466 | true); |
| 467 | refs[i] = bm[i].pixelRef(); |
| 468 | } |
| 469 | |
| 470 | // The A8 alternate bitmaps are all BW checkerboards |
| 471 | for (int i = 0; i < num; ++i) { |
| 472 | make_checkerboard(&bm[num+i], w, h, true); |
| 473 | refs[num+i] = bm[num+i].pixelRef(); |
| 474 | } |
| 475 | } |
| 476 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 477 | static void test_gatherpixelrefs(skiatest::Reporter* reporter) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 478 | const int IW = 32; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 479 | const int IH = IW; |
| 480 | const SkScalar W = SkIntToScalar(IW); |
| 481 | const SkScalar H = W; |
| 482 | |
| 483 | static const int N = 4; |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 484 | SkBitmap bm[2*N]; |
| 485 | SkPixelRef* refs[2*N]; |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 486 | SkTDArray<SkPixelRef*> analytic[N]; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 487 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 488 | const SkPoint pos[N] = { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 489 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 490 | }; |
| 491 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 492 | create_textures(bm, refs, N, IW, IH); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 493 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 494 | SkRandom rand; |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 495 | for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 496 | SkAutoTUnref<SkPicture> pic( |
| 497 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 498 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 499 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 500 | // quick check for a small piece of each quadrant, which should just |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 501 | // contain 1 or 2 bitmaps. |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 502 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 503 | SkRect r; |
| 504 | r.set(2, 2, W - 2, H - 2); |
| 505 | r.offset(pos[i].fX, pos[i].fY); |
| 506 | SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 507 | if (!data) { |
| 508 | ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned " |
| 509 | "NULL for %s.", gProcs[k].desc); |
| 510 | continue; |
| 511 | } |
| 512 | SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); |
| 513 | int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); |
| 514 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 515 | if (1 == count) { |
| 516 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 517 | } else if (2 == count) { |
| 518 | REPORTER_ASSERT(reporter, |
| 519 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 520 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
commit-bot@chromium.org | fe433c1 | 2013-07-09 16:04:32 +0000 | [diff] [blame] | 521 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 522 | } |
| 523 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 524 | SkBitmap image; |
| 525 | draw(pic, 2*IW, 2*IH, &image); |
| 526 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 527 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 528 | // with a deduced list of refs by looking at the colors drawn. |
| 529 | for (int j = 0; j < 100; ++j) { |
| 530 | SkRect r; |
| 531 | rand_rect(&r, rand, 2*W, 2*H); |
| 532 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 533 | SkTDArray<SkPixelRef*> fromImage; |
| 534 | gather_from_image(image, refs, N, &fromImage, r); |
| 535 | |
| 536 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 537 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 538 | |
| 539 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 540 | size_t dataSize = data ? data->size() : 0; |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 541 | int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 542 | SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); |
| 543 | SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; |
| 544 | SkAutoDataUnref adu(data); |
| 545 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 546 | // Everything that we saw drawn should appear in the analytic list |
| 547 | // but the analytic list may contain some pixelRefs that were not |
| 548 | // seen in the image (e.g., A8 textures used as masks) |
| 549 | for (int i = 0; i < fromImage.count(); ++i) { |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 550 | if (-1 == fromAnalytic.find(fromImage[i])) { |
| 551 | ERRORF(reporter, "PixelRef missing %d %s", |
| 552 | i, gProcs[k].desc); |
| 553 | } |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 554 | } |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 555 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 556 | /* |
| 557 | * GatherPixelRefs is conservative, so it can return more bitmaps |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 558 | * than are strictly required. Thus our check here is only that |
| 559 | * Gather didn't miss any that we actually needed. Even that isn't |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 560 | * a strict requirement on Gather, which is meant to be quick and |
| 561 | * only mostly-correct, but at the moment this test should work. |
| 562 | */ |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 563 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 564 | bool found = find(gatherRefs, fromAnalytic[i], gatherCount); |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 565 | if (!found) { |
| 566 | ERRORF(reporter, "PixelRef missing %d %s", |
| 567 | i, gProcs[k].desc); |
| 568 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 569 | #if 0 |
| 570 | // enable this block of code to debug failures, as it will rerun |
| 571 | // the case that failed. |
| 572 | if (!found) { |
| 573 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 574 | size_t dataSize = data ? data->size() : 0; |
| 575 | } |
| 576 | #endif |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 582 | #define GENERATE_CANVAS(recorder, x) \ |
| 583 | (x) ? recorder.EXPERIMENTAL_beginRecording(100, 100) \ |
mtklein | c92e550 | 2014-08-21 13:07:27 -0700 | [diff] [blame] | 584 | : recorder. DEPRECATED_beginRecording(100,100); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 585 | |
| 586 | /* Hit a few SkPicture::Analysis cases not handled elsewhere. */ |
| 587 | static void test_analysis(skiatest::Reporter* reporter, bool useNewPath) { |
| 588 | SkPictureRecorder recorder; |
| 589 | |
| 590 | SkCanvas* canvas = GENERATE_CANVAS(recorder, useNewPath); |
| 591 | { |
| 592 | canvas->drawRect(SkRect::MakeWH(10, 10), SkPaint ()); |
| 593 | } |
| 594 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 595 | REPORTER_ASSERT(reporter, !picture->willPlayBackBitmaps()); |
| 596 | |
| 597 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
| 598 | { |
| 599 | SkPaint paint; |
| 600 | // CreateBitmapShader is too smart for us; an empty (or 1x1) bitmap shader |
| 601 | // gets optimized into a non-bitmap form, so we create a 2x2 bitmap here. |
| 602 | SkBitmap bitmap; |
| 603 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2)); |
| 604 | bitmap.eraseColor(SK_ColorBLUE); |
| 605 | *(bitmap.getAddr32(0, 0)) = SK_ColorGREEN; |
| 606 | SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode, |
| 607 | SkShader::kClamp_TileMode); |
| 608 | paint.setShader(shader)->unref(); |
| 609 | REPORTER_ASSERT(reporter, |
| 610 | shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType); |
| 611 | |
| 612 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 613 | } |
| 614 | picture.reset(recorder.endRecording()); |
| 615 | REPORTER_ASSERT(reporter, picture->willPlayBackBitmaps()); |
| 616 | } |
| 617 | |
| 618 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 619 | static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { |
| 620 | const int IW = 32; |
| 621 | const int IH = IW; |
| 622 | const SkScalar W = SkIntToScalar(IW); |
| 623 | const SkScalar H = W; |
| 624 | |
| 625 | static const int N = 4; |
| 626 | SkBitmap bm[2*N]; |
| 627 | SkPixelRef* refs[2*N]; |
| 628 | SkTDArray<SkPixelRef*> analytic[N]; |
| 629 | |
| 630 | const SkPoint pos[N] = { |
| 631 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 632 | }; |
| 633 | |
| 634 | create_textures(bm, refs, N, IW, IH); |
| 635 | |
| 636 | SkRandom rand; |
| 637 | for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 638 | SkAutoTUnref<SkPicture> pic( |
| 639 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 640 | |
| 641 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
| 642 | |
| 643 | SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( |
| 644 | new SkPictureUtils::SkPixelRefsAndRectsList); |
| 645 | |
| 646 | SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); |
| 647 | |
| 648 | // quick check for a small piece of each quadrant, which should just |
| 649 | // contain 1 or 2 bitmaps. |
| 650 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 651 | SkRect r; |
| 652 | r.set(2, 2, W - 2, H - 2); |
| 653 | r.offset(pos[i].fX, pos[i].fY); |
| 654 | |
| 655 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 656 | prCont->query(r, &gatheredRefs); |
| 657 | |
| 658 | int count = gatheredRefs.count(); |
| 659 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 660 | if (1 == count) { |
| 661 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 662 | } else if (2 == count) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 663 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 664 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 665 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | SkBitmap image; |
| 670 | draw(pic, 2*IW, 2*IH, &image); |
| 671 | |
| 672 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 673 | // with the analytic results and the pixel refs seen in a rendering. |
| 674 | for (int j = 0; j < 100; ++j) { |
| 675 | SkRect r; |
| 676 | rand_rect(&r, rand, 2*W, 2*H); |
| 677 | |
| 678 | SkTDArray<SkPixelRef*> fromImage; |
| 679 | gather_from_image(image, refs, N, &fromImage, r); |
| 680 | |
| 681 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 682 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
| 683 | |
| 684 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 685 | prCont->query(r, &gatheredRefs); |
| 686 | |
| 687 | // Everything that we saw drawn should appear in the analytic list |
| 688 | // but the analytic list may contain some pixelRefs that were not |
| 689 | // seen in the image (e.g., A8 textures used as masks) |
| 690 | for (int i = 0; i < fromImage.count(); ++i) { |
| 691 | REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); |
| 692 | } |
| 693 | |
| 694 | // Everything in the analytic list should appear in the gathered |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 695 | // list. |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 696 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 697 | REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 703 | #ifdef SK_DEBUG |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 704 | // Ensure that deleting an empty SkPicture does not assert. Asserts only fire |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 705 | // in debug mode, so only run in debug mode. |
| 706 | static void test_deleting_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 707 | SkPictureRecorder recorder; |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 708 | // Creates an SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 709 | recorder.beginRecording(0, 0); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 710 | // Turns that into an SkPicture |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 711 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 712 | // Ceates a new SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 713 | recorder.beginRecording(0, 0); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 717 | static void test_serializing_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 718 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 719 | recorder.beginRecording(0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 720 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 721 | SkDynamicMemoryWStream stream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 722 | picture->serialize(&stream); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 723 | } |
| 724 | #endif |
| 725 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 726 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 727 | SkPaint paint; |
| 728 | SkRect rect = SkRect::MakeWH(50, 50); |
| 729 | |
| 730 | SkScalar unit = rand.nextUScalar1(); |
| 731 | if (unit <= 0.3) { |
| 732 | // SkDebugf("save\n"); |
| 733 | canvas->save(); |
| 734 | } else if (unit <= 0.6) { |
| 735 | // SkDebugf("restore\n"); |
| 736 | canvas->restore(); |
| 737 | } else if (unit <= 0.9) { |
| 738 | // SkDebugf("clip\n"); |
| 739 | canvas->clipRect(rect); |
| 740 | } else { |
| 741 | // SkDebugf("draw\n"); |
| 742 | canvas->drawPaint(paint); |
| 743 | } |
| 744 | } |
| 745 | |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 746 | #if SK_SUPPORT_GPU |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 747 | |
| 748 | static void test_gpu_veto(skiatest::Reporter* reporter, |
| 749 | bool useNewPath) { |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 750 | |
| 751 | SkPictureRecorder recorder; |
| 752 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 753 | SkCanvas* canvas = GENERATE_CANVAS(recorder, useNewPath); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 754 | { |
| 755 | SkPath path; |
| 756 | path.moveTo(0, 0); |
| 757 | path.lineTo(50, 50); |
| 758 | |
| 759 | SkScalar intervals[] = { 1.0f, 1.0f }; |
| 760 | SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); |
| 761 | |
| 762 | SkPaint paint; |
| 763 | paint.setStyle(SkPaint::kStroke_Style); |
| 764 | paint.setPathEffect(dash); |
| 765 | |
| 766 | canvas->drawPath(path, paint); |
| 767 | } |
| 768 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 769 | // path effects currently render an SkPicture undesireable for GPU rendering |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 770 | |
| 771 | const char *reason = NULL; |
| 772 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason)); |
| 773 | REPORTER_ASSERT(reporter, NULL != reason); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 774 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 775 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 776 | { |
| 777 | SkPath path; |
| 778 | |
| 779 | path.moveTo(0, 0); |
| 780 | path.lineTo(0, 50); |
| 781 | path.lineTo(25, 25); |
| 782 | path.lineTo(50, 50); |
| 783 | path.lineTo(50, 0); |
| 784 | path.close(); |
| 785 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 786 | |
| 787 | SkPaint paint; |
| 788 | paint.setAntiAlias(true); |
| 789 | for (int i = 0; i < 50; ++i) { |
| 790 | canvas->drawPath(path, paint); |
| 791 | } |
| 792 | } |
| 793 | picture.reset(recorder.endRecording()); |
| 794 | // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering |
| 795 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); |
| 796 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 797 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 798 | { |
| 799 | SkPath path; |
| 800 | |
| 801 | path.moveTo(0, 0); |
| 802 | path.lineTo(0, 50); |
| 803 | path.lineTo(25, 25); |
| 804 | path.lineTo(50, 50); |
| 805 | path.lineTo(50, 0); |
| 806 | path.close(); |
| 807 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 808 | |
| 809 | SkPaint paint; |
| 810 | paint.setAntiAlias(true); |
| 811 | paint.setStyle(SkPaint::kStroke_Style); |
| 812 | paint.setStrokeWidth(0); |
| 813 | for (int i = 0; i < 50; ++i) { |
| 814 | canvas->drawPath(path, paint); |
| 815 | } |
| 816 | } |
| 817 | picture.reset(recorder.endRecording()); |
| 818 | // hairline stroked AA concave paths are fine for GPU rendering |
| 819 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL)); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 820 | |
| 821 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
| 822 | { |
| 823 | SkPaint paint; |
| 824 | SkScalar intervals [] = { 10, 20 }; |
| 825 | SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25); |
| 826 | paint.setPathEffect(pe)->unref(); |
| 827 | |
| 828 | SkPoint points [2] = { { 0, 0 }, { 100, 0 } }; |
| 829 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, points, paint); |
| 830 | } |
| 831 | picture.reset(recorder.endRecording()); |
| 832 | // fast-path dashed effects are fine for GPU rendering ... |
| 833 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL)); |
| 834 | |
| 835 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
| 836 | { |
| 837 | SkPaint paint; |
| 838 | SkScalar intervals [] = { 10, 20 }; |
| 839 | SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25); |
| 840 | paint.setPathEffect(pe)->unref(); |
| 841 | |
| 842 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 843 | } |
| 844 | picture.reset(recorder.endRecording()); |
| 845 | // ... but only when applied to drawPoint() calls |
| 846 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 847 | |
| 848 | // Nest the previous picture inside a new one. |
| 849 | // This doesn't work in the old backend. |
| 850 | if (useNewPath) { |
| 851 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
| 852 | { |
| 853 | canvas->drawPicture(picture.get()); |
| 854 | } |
| 855 | picture.reset(recorder.endRecording()); |
| 856 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); |
| 857 | } |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 858 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 859 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 860 | #undef GENERATE_CANVAS |
| 861 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 862 | static void test_gpu_picture_optimization(skiatest::Reporter* reporter, |
| 863 | GrContextFactory* factory) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 864 | for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 865 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 866 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 867 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 868 | continue; |
| 869 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 870 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 871 | GrContext* context = factory->get(glCtxType); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 872 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 873 | if (NULL == context) { |
| 874 | continue; |
| 875 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 876 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 877 | static const int kWidth = 100; |
| 878 | static const int kHeight = 100; |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 879 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 880 | SkAutoTUnref<SkPicture> pict, child; |
| 881 | |
| 882 | { |
| 883 | SkPictureRecorder recorder; |
| 884 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 885 | SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 886 | |
| 887 | c->saveLayer(NULL, NULL); |
| 888 | c->restore(); |
| 889 | |
| 890 | child.reset(recorder.endRecording()); |
| 891 | } |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 892 | |
| 893 | // create a picture with the structure: |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 894 | // 1) |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 895 | // SaveLayer |
| 896 | // Restore |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 897 | // 2) |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 898 | // SaveLayer |
| 899 | // Translate |
| 900 | // SaveLayer w/ bound |
| 901 | // Restore |
| 902 | // Restore |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 903 | // 3) |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 904 | // SaveLayer w/ copyable paint |
| 905 | // Restore |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 906 | // 4) |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 907 | // SaveLayer |
| 908 | // DrawPicture (which has a SaveLayer/Restore pair) |
| 909 | // Restore |
| 910 | // 5) |
| 911 | // SaveLayer |
| 912 | // DrawPicture with Matrix & Paint (with SaveLayer/Restore pair) |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 913 | // Restore |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 914 | { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 915 | SkPictureRecorder recorder; |
| 916 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 917 | SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth), |
| 918 | SkIntToScalar(kHeight)); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 919 | // 1) |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 920 | c->saveLayer(NULL, NULL); // layer #0 |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 921 | c->restore(); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 922 | |
| 923 | // 2) |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 924 | c->saveLayer(NULL, NULL); // layer #1 |
| 925 | c->translate(kWidth/2.0f, kHeight/2.0f); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 926 | SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 927 | c->saveLayer(&r, NULL); // layer #2 |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 928 | c->restore(); |
| 929 | c->restore(); |
| 930 | |
| 931 | // 3) |
| 932 | { |
| 933 | SkPaint p; |
| 934 | p.setColor(SK_ColorRED); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 935 | c->saveLayer(NULL, &p); // layer #3 |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 936 | c->restore(); |
| 937 | } |
| 938 | // 4) |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 939 | { |
| 940 | c->saveLayer(NULL, NULL); // layer #4 |
| 941 | c->drawPicture(child); // layer #5 inside picture |
| 942 | c->restore(); |
| 943 | } |
| 944 | // 5 |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 945 | { |
| 946 | SkPaint p; |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 947 | SkMatrix trans; |
| 948 | trans.setTranslate(10, 10); |
| 949 | |
| 950 | c->saveLayer(NULL, NULL); // layer #6 |
| 951 | c->drawPicture(child, &trans, &p); // layer #7 inside picture |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 952 | c->restore(); |
| 953 | } |
| 954 | |
| 955 | pict.reset(recorder.endRecording()); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 956 | } |
| 957 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 958 | // Now test out the SaveLayer extraction |
| 959 | { |
| 960 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 961 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 962 | SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info)); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 963 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 964 | SkCanvas* canvas = surface->getCanvas(); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 965 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 966 | canvas->EXPERIMENTAL_optimize(pict); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 967 | |
robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 968 | SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 969 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 970 | const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key); |
| 971 | REPORTER_ASSERT(reporter, NULL != data); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 972 | |
robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 973 | const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 974 | REPORTER_ASSERT(reporter, 8 == gpuData->numSaveLayers()); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 975 | |
robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 976 | const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 977 | // The parent/child layers appear in reverse order |
robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 978 | const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2); |
| 979 | const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 980 | |
robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 981 | const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 982 | |
| 983 | // The parent/child layers appear in reverse order |
| 984 | const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(5); |
| 985 | const GrAccelData::SaveLayerInfo& info5 = gpuData->saveLayerInfo(4); |
| 986 | |
| 987 | // The parent/child layers appear in reverse order |
| 988 | const GrAccelData::SaveLayerInfo& info6 = gpuData->saveLayerInfo(7); |
| 989 | const GrAccelData::SaveLayerInfo& info7 = gpuData->saveLayerInfo(6); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 990 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 991 | REPORTER_ASSERT(reporter, info0.fValid); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 992 | REPORTER_ASSERT(reporter, pict->uniqueID() == info0.fPictureID); |
| 993 | REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && |
| 994 | kHeight == info0.fSize.fHeight); |
| 995 | REPORTER_ASSERT(reporter, info0.fOriginXform.isIdentity()); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 996 | REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 997 | REPORTER_ASSERT(reporter, NULL == info0.fPaint); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 998 | REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 999 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1000 | REPORTER_ASSERT(reporter, info1.fValid); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 1001 | REPORTER_ASSERT(reporter, pict->uniqueID() == info1.fPictureID); |
| 1002 | REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && |
| 1003 | kHeight == info1.fSize.fHeight); |
| 1004 | REPORTER_ASSERT(reporter, info1.fOriginXform.isIdentity()); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1005 | REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 1006 | REPORTER_ASSERT(reporter, NULL == info1.fPaint); |
| 1007 | REPORTER_ASSERT(reporter, !info1.fIsNested && |
| 1008 | info1.fHasNestedLayers); // has a nested SL |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1009 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1010 | REPORTER_ASSERT(reporter, info2.fValid); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 1011 | REPORTER_ASSERT(reporter, pict->uniqueID() == info2.fPictureID); |
| 1012 | REPORTER_ASSERT(reporter, kWidth / 2 == info2.fSize.fWidth && |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1013 | kHeight/2 == info2.fSize.fHeight); // bound reduces size |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 1014 | REPORTER_ASSERT(reporter, info2.fOriginXform.isIdentity()); |
| 1015 | REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX && // translated |
| 1016 | kHeight/2 == info2.fOffset.fY); |
| 1017 | REPORTER_ASSERT(reporter, NULL == info1.fPaint); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1018 | REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1019 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1020 | REPORTER_ASSERT(reporter, info3.fValid); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 1021 | REPORTER_ASSERT(reporter, pict->uniqueID() == info3.fPictureID); |
| 1022 | REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && |
| 1023 | kHeight == info3.fSize.fHeight); |
| 1024 | REPORTER_ASSERT(reporter, info3.fOriginXform.isIdentity()); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1025 | REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY); |
| 1026 | REPORTER_ASSERT(reporter, NULL != info3.fPaint); |
| 1027 | REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1028 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 1029 | REPORTER_ASSERT(reporter, info4.fValid); |
| 1030 | REPORTER_ASSERT(reporter, pict->uniqueID() == info4.fPictureID); |
| 1031 | REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && |
| 1032 | kHeight == info4.fSize.fHeight); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1033 | REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 1034 | REPORTER_ASSERT(reporter, info4.fOriginXform.isIdentity()); |
| 1035 | REPORTER_ASSERT(reporter, NULL == info4.fPaint); |
| 1036 | REPORTER_ASSERT(reporter, !info4.fIsNested && |
| 1037 | info4.fHasNestedLayers); // has a nested SL |
| 1038 | |
| 1039 | REPORTER_ASSERT(reporter, info5.fValid); |
| 1040 | REPORTER_ASSERT(reporter, child->uniqueID() == info5.fPictureID); // in a child picture |
| 1041 | REPORTER_ASSERT(reporter, kWidth == info5.fSize.fWidth && |
| 1042 | kHeight == info5.fSize.fHeight); |
| 1043 | REPORTER_ASSERT(reporter, 0 == info5.fOffset.fX && 0 == info5.fOffset.fY); |
| 1044 | REPORTER_ASSERT(reporter, info5.fOriginXform.isIdentity()); |
| 1045 | REPORTER_ASSERT(reporter, NULL == info5.fPaint); |
| 1046 | REPORTER_ASSERT(reporter, info5.fIsNested && !info5.fHasNestedLayers); // is nested |
| 1047 | |
| 1048 | REPORTER_ASSERT(reporter, info6.fValid); |
| 1049 | REPORTER_ASSERT(reporter, pict->uniqueID() == info6.fPictureID); |
| 1050 | REPORTER_ASSERT(reporter, kWidth == info6.fSize.fWidth && |
| 1051 | kHeight == info6.fSize.fHeight); |
| 1052 | REPORTER_ASSERT(reporter, 0 == info6.fOffset.fX && 0 == info6.fOffset.fY); |
| 1053 | REPORTER_ASSERT(reporter, info6.fOriginXform.isIdentity()); |
| 1054 | REPORTER_ASSERT(reporter, NULL == info6.fPaint); |
| 1055 | REPORTER_ASSERT(reporter, !info6.fIsNested && |
| 1056 | info6.fHasNestedLayers); // has a nested SL |
| 1057 | |
| 1058 | REPORTER_ASSERT(reporter, info7.fValid); |
| 1059 | REPORTER_ASSERT(reporter, child->uniqueID() == info7.fPictureID); // in a child picture |
| 1060 | REPORTER_ASSERT(reporter, kWidth == info7.fSize.fWidth && |
| 1061 | kHeight == info7.fSize.fHeight); |
| 1062 | REPORTER_ASSERT(reporter, 0 == info7.fOffset.fX && 0 == info7.fOffset.fY); |
| 1063 | REPORTER_ASSERT(reporter, info7.fOriginXform.isIdentity()); |
| 1064 | REPORTER_ASSERT(reporter, NULL == info7.fPaint); |
| 1065 | REPORTER_ASSERT(reporter, info7.fIsNested && !info7.fHasNestedLayers); // is nested |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 1066 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1070 | #endif |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 1071 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1072 | static void test_has_text(skiatest::Reporter* reporter, bool useNewPath) { |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1073 | SkPictureRecorder recorder; |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1074 | #define BEGIN_RECORDING useNewPath ? recorder.EXPERIMENTAL_beginRecording(100, 100) \ |
mtklein | c92e550 | 2014-08-21 13:07:27 -0700 | [diff] [blame] | 1075 | : recorder. DEPRECATED_beginRecording(100, 100) |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1076 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1077 | SkCanvas* canvas = BEGIN_RECORDING; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1078 | { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1079 | canvas->drawRect(SkRect::MakeWH(20, 20), SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1080 | } |
| 1081 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1082 | REPORTER_ASSERT(reporter, !picture->hasText()); |
| 1083 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1084 | SkPoint point = SkPoint::Make(10, 10); |
| 1085 | canvas = BEGIN_RECORDING; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1086 | { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1087 | canvas->drawText("Q", 1, point.fX, point.fY, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1088 | } |
| 1089 | picture.reset(recorder.endRecording()); |
| 1090 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1091 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1092 | canvas = BEGIN_RECORDING; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1093 | { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1094 | canvas->drawPosText("Q", 1, &point, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1095 | } |
| 1096 | picture.reset(recorder.endRecording()); |
| 1097 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1098 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1099 | canvas = BEGIN_RECORDING; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1100 | { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1101 | canvas->drawPosTextH("Q", 1, &point.fX, point.fY, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1102 | } |
| 1103 | picture.reset(recorder.endRecording()); |
| 1104 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1105 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1106 | canvas = BEGIN_RECORDING; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1107 | { |
| 1108 | SkPath path; |
| 1109 | path.moveTo(0, 0); |
| 1110 | path.lineTo(50, 50); |
| 1111 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1112 | canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1113 | } |
| 1114 | picture.reset(recorder.endRecording()); |
| 1115 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1116 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1117 | canvas = BEGIN_RECORDING; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1118 | { |
| 1119 | SkPath path; |
| 1120 | path.moveTo(0, 0); |
| 1121 | path.lineTo(50, 50); |
| 1122 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1123 | canvas->drawTextOnPath("Q", 1, path, NULL, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1124 | } |
| 1125 | picture.reset(recorder.endRecording()); |
| 1126 | REPORTER_ASSERT(reporter, picture->hasText()); |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 1127 | |
| 1128 | // Nest the previous picture inside a new one. |
| 1129 | // This doesn't work in the old backend. |
| 1130 | if (useNewPath) { |
| 1131 | canvas = BEGIN_RECORDING; |
| 1132 | { |
| 1133 | canvas->drawPicture(picture.get()); |
| 1134 | } |
| 1135 | picture.reset(recorder.endRecording()); |
| 1136 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1137 | } |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1138 | #undef BEGIN_RECORDING |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1141 | static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
| 1142 | canvas->restoreToCount(1); |
| 1143 | canvas->save(); |
| 1144 | canvas->save(); |
| 1145 | canvas->save(); |
| 1146 | } |
| 1147 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1148 | /** |
| 1149 | * A canvas that records the number of saves, saveLayers and restores. |
| 1150 | */ |
| 1151 | class SaveCountingCanvas : public SkCanvas { |
| 1152 | public: |
| 1153 | SaveCountingCanvas(int width, int height) |
| 1154 | : INHERITED(width, height) |
| 1155 | , fSaveCount(0) |
| 1156 | , fSaveLayerCount(0) |
| 1157 | , fRestoreCount(0){ |
| 1158 | } |
| 1159 | |
| 1160 | virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint, |
| 1161 | SaveFlags flags) SK_OVERRIDE { |
| 1162 | ++fSaveLayerCount; |
| 1163 | return this->INHERITED::willSaveLayer(bounds, paint, flags); |
| 1164 | } |
| 1165 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 1166 | virtual void willSave() SK_OVERRIDE { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1167 | ++fSaveCount; |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 1168 | this->INHERITED::willSave(); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | virtual void willRestore() SK_OVERRIDE { |
| 1172 | ++fRestoreCount; |
| 1173 | this->INHERITED::willRestore(); |
| 1174 | } |
| 1175 | |
| 1176 | unsigned int getSaveCount() const { return fSaveCount; } |
| 1177 | unsigned int getSaveLayerCount() const { return fSaveLayerCount; } |
| 1178 | unsigned int getRestoreCount() const { return fRestoreCount; } |
| 1179 | |
| 1180 | private: |
| 1181 | unsigned int fSaveCount; |
| 1182 | unsigned int fSaveLayerCount; |
| 1183 | unsigned int fRestoreCount; |
| 1184 | |
| 1185 | typedef SkCanvas INHERITED; |
| 1186 | }; |
| 1187 | |
skia.committer@gmail.com | 8e7d37d | 2014-05-28 03:06:06 +0000 | [diff] [blame] | 1188 | void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1189 | unsigned int numSaves, unsigned int numSaveLayers, |
| 1190 | unsigned int numRestores) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 1191 | SaveCountingCanvas canvas(SkScalarCeilToInt(picture->cullRect().width()), |
| 1192 | SkScalarCeilToInt(picture->cullRect().height())); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1193 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame^] | 1194 | picture->playback(&canvas); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1195 | |
| 1196 | REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount()); |
| 1197 | REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount()); |
| 1198 | REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount()); |
| 1199 | } |
| 1200 | |
| 1201 | // This class exists so SkPicture can friend it and give it access to |
| 1202 | // the 'partialReplay' method. |
| 1203 | class SkPictureRecorderReplayTester { |
| 1204 | public: |
| 1205 | static SkPicture* Copy(SkPictureRecorder* recorder) { |
| 1206 | SkPictureRecorder recorder2; |
| 1207 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1208 | SkCanvas* canvas = recorder2.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1209 | |
| 1210 | recorder->partialReplay(canvas); |
| 1211 | |
| 1212 | return recorder2.endRecording(); |
| 1213 | } |
| 1214 | }; |
| 1215 | |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 1216 | static void create_imbalance(SkCanvas* canvas) { |
| 1217 | SkRect clipRect = SkRect::MakeWH(2, 2); |
| 1218 | SkRect drawRect = SkRect::MakeWH(10, 10); |
| 1219 | canvas->save(); |
| 1220 | canvas->clipRect(clipRect, SkRegion::kReplace_Op); |
| 1221 | canvas->translate(1.0f, 1.0f); |
| 1222 | SkPaint p; |
| 1223 | p.setColor(SK_ColorGREEN); |
| 1224 | canvas->drawRect(drawRect, p); |
| 1225 | // no restore |
| 1226 | } |
| 1227 | |
| 1228 | // This tests that replaying a potentially unbalanced picture into a canvas |
| 1229 | // doesn't affect the canvas' save count or matrix/clip state. |
| 1230 | static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) { |
| 1231 | SkBitmap bm; |
| 1232 | bm.allocN32Pixels(4, 3); |
| 1233 | SkCanvas canvas(bm); |
| 1234 | |
| 1235 | int beforeSaveCount = canvas.getSaveCount(); |
| 1236 | |
| 1237 | SkMatrix beforeMatrix = canvas.getTotalMatrix(); |
| 1238 | |
| 1239 | SkRect beforeClip; |
| 1240 | |
| 1241 | canvas.getClipBounds(&beforeClip); |
| 1242 | |
| 1243 | canvas.drawPicture(picture); |
| 1244 | |
| 1245 | REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount()); |
| 1246 | REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix()); |
| 1247 | |
| 1248 | SkRect afterClip; |
| 1249 | |
| 1250 | canvas.getClipBounds(&afterClip); |
| 1251 | |
| 1252 | REPORTER_ASSERT(reporter, afterClip == beforeClip); |
| 1253 | } |
| 1254 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1255 | // Test out SkPictureRecorder::partialReplay |
| 1256 | DEF_TEST(PictureRecorder_replay, reporter) { |
| 1257 | // check save/saveLayer state |
| 1258 | { |
| 1259 | SkPictureRecorder recorder; |
| 1260 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1261 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1262 | |
| 1263 | canvas->saveLayer(NULL, NULL); |
| 1264 | |
| 1265 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1266 | |
| 1267 | // The extra save and restore comes from the Copy process. |
| 1268 | check_save_state(reporter, copy, 2, 1, 3); |
| 1269 | |
| 1270 | canvas->saveLayer(NULL, NULL); |
| 1271 | |
| 1272 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1273 | |
| 1274 | check_save_state(reporter, final, 1, 2, 3); |
| 1275 | |
| 1276 | // The copy shouldn't pick up any operations added after it was made |
| 1277 | check_save_state(reporter, copy, 2, 1, 3); |
| 1278 | } |
| 1279 | |
| 1280 | // (partially) check leakage of draw ops |
| 1281 | { |
| 1282 | SkPictureRecorder recorder; |
| 1283 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1284 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1285 | |
| 1286 | SkRect r = SkRect::MakeWH(5, 5); |
| 1287 | SkPaint p; |
| 1288 | |
| 1289 | canvas->drawRect(r, p); |
| 1290 | |
| 1291 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1292 | |
| 1293 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 1294 | |
| 1295 | SkBitmap bm; |
| 1296 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1297 | |
| 1298 | r.offset(5.0f, 5.0f); |
| 1299 | canvas->drawBitmapRectToRect(bm, NULL, r); |
| 1300 | |
| 1301 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1302 | REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); |
| 1303 | |
| 1304 | REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); |
| 1305 | |
| 1306 | // The snapshot shouldn't pick up any operations added after it was made |
| 1307 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 1308 | } |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 1309 | |
| 1310 | // Recreate the Android partialReplay test case |
| 1311 | { |
| 1312 | SkPictureRecorder recorder; |
| 1313 | |
| 1314 | SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0); |
| 1315 | create_imbalance(canvas); |
| 1316 | |
| 1317 | int expectedSaveCount = canvas->getSaveCount(); |
| 1318 | |
| 1319 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1320 | check_balance(reporter, copy); |
| 1321 | |
| 1322 | REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount()); |
| 1323 | |
| 1324 | // End the recording of source to test the picture finalization |
| 1325 | // process isn't complicated by the partialReplay step |
| 1326 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1327 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1328 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1329 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1330 | static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 1331 | SkCanvas testCanvas(100, 100); |
| 1332 | set_canvas_to_save_count_4(&testCanvas); |
| 1333 | |
| 1334 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1335 | |
| 1336 | SkPaint paint; |
| 1337 | SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 1338 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1339 | SkPictureRecorder recorder; |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1340 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1341 | { |
| 1342 | // Create picture with 2 unbalanced saves |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1343 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1344 | canvas->save(); |
| 1345 | canvas->translate(10, 10); |
| 1346 | canvas->drawRect(rect, paint); |
| 1347 | canvas->save(); |
| 1348 | canvas->translate(10, 10); |
| 1349 | canvas->drawRect(rect, paint); |
| 1350 | SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording()); |
| 1351 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1352 | testCanvas.drawPicture(extraSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1353 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1354 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1355 | |
| 1356 | set_canvas_to_save_count_4(&testCanvas); |
| 1357 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1358 | { |
| 1359 | // Create picture with 2 unbalanced restores |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1360 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1361 | canvas->save(); |
| 1362 | canvas->translate(10, 10); |
| 1363 | canvas->drawRect(rect, paint); |
| 1364 | canvas->save(); |
| 1365 | canvas->translate(10, 10); |
| 1366 | canvas->drawRect(rect, paint); |
| 1367 | canvas->restore(); |
| 1368 | canvas->restore(); |
| 1369 | canvas->restore(); |
| 1370 | canvas->restore(); |
| 1371 | SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording()); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1372 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1373 | testCanvas.drawPicture(extraRestorePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1374 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1375 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1376 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1377 | set_canvas_to_save_count_4(&testCanvas); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1378 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1379 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1380 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1381 | canvas->translate(10, 10); |
| 1382 | canvas->drawRect(rect, paint); |
| 1383 | SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording()); |
| 1384 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1385 | testCanvas.drawPicture(noSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1386 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1387 | REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); |
| 1388 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1391 | static void test_peephole() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1392 | SkRandom rand; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1393 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1394 | SkPictureRecorder recorder; |
| 1395 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1396 | for (int j = 0; j < 100; j++) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1397 | SkRandom rand2(rand); // remember the seed |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1398 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1399 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1400 | |
| 1401 | for (int i = 0; i < 1000; ++i) { |
| 1402 | rand_op(canvas, rand); |
| 1403 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1404 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 1405 | |
| 1406 | rand = rand2; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1410 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1411 | SkRect rect = SkRect::MakeWH(50, 50); |
skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 1412 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1413 | for (int i = 0; i < 100; ++i) { |
| 1414 | canvas->save(); |
| 1415 | } |
| 1416 | while (canvas->getSaveCount() > 1) { |
| 1417 | canvas->clipRect(rect); |
| 1418 | canvas->restore(); |
| 1419 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1420 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1424 | #ifndef SK_DEBUG |
| 1425 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 1426 | // should never do this. |
| 1427 | static void test_bad_bitmap() { |
| 1428 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 1429 | // fail. |
| 1430 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1431 | bm.setInfo(SkImageInfo::MakeN32Premul(100, 100)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1432 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1433 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1434 | recordingCanvas->drawBitmap(bm, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1435 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1436 | |
| 1437 | SkCanvas canvas; |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1438 | canvas.drawPicture(picture); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1439 | } |
| 1440 | #endif |
| 1441 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 1442 | static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1443 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1447 | SkPictureRecorder recorder; |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 1448 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bitmap.width()), |
| 1449 | SkIntToScalar(bitmap.height())); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1450 | canvas->drawBitmap(bitmap, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1451 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1452 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1453 | SkDynamicMemoryWStream wStream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1454 | picture->serialize(&wStream, &encode_bitmap_to_data); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1455 | return wStream.copyToData(); |
| 1456 | } |
| 1457 | |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1458 | struct ErrorContext { |
| 1459 | int fErrors; |
| 1460 | skiatest::Reporter* fReporter; |
| 1461 | }; |
| 1462 | |
| 1463 | static void assert_one_parse_error_cb(SkError error, void* context) { |
| 1464 | ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 1465 | errorContext->fErrors++; |
| 1466 | // This test only expects one error, and that is a kParseError. If there are others, |
| 1467 | // there is some unknown problem. |
| 1468 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 1469 | "This threw more errors than expected."); |
| 1470 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, |
| 1471 | SkGetLastErrorString()); |
| 1472 | } |
| 1473 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1474 | static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 1475 | // Create a bitmap that will be encoded. |
| 1476 | SkBitmap original; |
| 1477 | make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 1478 | SkDynamicMemoryWStream wStream; |
| 1479 | if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { |
| 1480 | return; |
| 1481 | } |
| 1482 | SkAutoDataUnref data(wStream.copyToData()); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1483 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1484 | SkBitmap bm; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 1485 | bool installSuccess = SkInstallDiscardablePixelRef( |
commit-bot@chromium.org | 2d970b5 | 2014-05-27 14:14:22 +0000 | [diff] [blame] | 1486 | SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 1487 | REPORTER_ASSERT(reporter, installSuccess); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1488 | |
| 1489 | // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. |
| 1490 | // Flattening original will follow the old path of performing an encode, while flattening bm |
| 1491 | // will use the already encoded data. |
| 1492 | SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 1493 | SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 1494 | REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1495 | // Now test that a parse error was generated when trying to create a new SkPicture without |
| 1496 | // providing a function to decode the bitmap. |
| 1497 | ErrorContext context; |
| 1498 | context.fErrors = 0; |
| 1499 | context.fReporter = reporter; |
| 1500 | SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 1501 | SkMemoryStream pictureStream(picture1); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1502 | SkClearLastError(); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 1503 | SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); |
| 1504 | REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1505 | SkClearLastError(); |
| 1506 | SkSetErrorCallback(NULL, NULL); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1509 | static void test_draw_empty(skiatest::Reporter* reporter) { |
| 1510 | SkBitmap result; |
| 1511 | make_bm(&result, 2, 2, SK_ColorBLACK, false); |
| 1512 | |
| 1513 | SkCanvas canvas(result); |
| 1514 | |
| 1515 | { |
| 1516 | // stock SkPicture |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1517 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1518 | recorder.beginRecording(1, 1); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1519 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1520 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1521 | canvas.drawPicture(picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | { |
| 1525 | // tile grid |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1526 | SkTileGridFactory::TileGridInfo gridInfo; |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1527 | gridInfo.fMargin.setEmpty(); |
| 1528 | gridInfo.fOffset.setZero(); |
| 1529 | gridInfo.fTileInterval.set(1, 1); |
| 1530 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1531 | SkTileGridFactory factory(gridInfo); |
| 1532 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1533 | recorder.beginRecording(1, 1, &factory); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1534 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1535 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1536 | canvas.drawPicture(picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | { |
| 1540 | // RTree |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1541 | SkRTreeFactory factory; |
| 1542 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1543 | recorder.beginRecording(1, 1, &factory); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1544 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1545 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1546 | canvas.drawPicture(picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1547 | } |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1550 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 1551 | // Test for crbug.com/229011 |
| 1552 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 1553 | SkIntToScalar(2), SkIntToScalar(2)); |
| 1554 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 1555 | SkIntToScalar(1), SkIntToScalar(1)); |
| 1556 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 1557 | SkIntToScalar(1), SkIntToScalar(1)); |
| 1558 | |
| 1559 | SkPath invPath; |
| 1560 | invPath.addOval(rect1); |
| 1561 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 1562 | SkPath path; |
| 1563 | path.addOval(rect2); |
| 1564 | SkPath path2; |
| 1565 | path2.addOval(rect3); |
| 1566 | SkIRect clipBounds; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1567 | SkPictureRecorder recorder; |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1568 | // Minimalist test set for 100% code coverage of |
| 1569 | // SkPictureRecord::updateClipConservativelyUsingBounds |
| 1570 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1571 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1572 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 1573 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1574 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1575 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1576 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1577 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1578 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1579 | } |
| 1580 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1581 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1582 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1583 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 1584 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1585 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1586 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1587 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1588 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1589 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1590 | } |
| 1591 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1592 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1593 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1594 | canvas->clipPath(invPath, SkRegion::kUnion_Op); |
| 1595 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1596 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1597 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1598 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1599 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1600 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1601 | } |
| 1602 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1603 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1604 | canvas->clipPath(path, SkRegion::kDifference_Op); |
| 1605 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1606 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1607 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1608 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1609 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1610 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1611 | } |
| 1612 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1613 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1614 | canvas->clipPath(path, SkRegion::kReverseDifference_Op); |
| 1615 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1616 | // True clip is actually empty in this case, but the best |
| 1617 | // determination we can make using only bounds as input is that the |
| 1618 | // clip is included in the bounds of 'path'. |
| 1619 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1620 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1621 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1622 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1623 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1624 | } |
| 1625 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1626 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1627 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1628 | canvas->clipPath(path2, SkRegion::kXOR_Op); |
| 1629 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1630 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1631 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 1632 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 1633 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1634 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1635 | } |
| 1636 | } |
| 1637 | |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1638 | /** |
| 1639 | * A canvas that records the number of clip commands. |
| 1640 | */ |
| 1641 | class ClipCountingCanvas : public SkCanvas { |
| 1642 | public: |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1643 | ClipCountingCanvas(int width, int height) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1644 | : INHERITED(width, height) |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1645 | , fClipCount(0){ |
| 1646 | } |
| 1647 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1648 | virtual void onClipRect(const SkRect& r, |
| 1649 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1650 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1651 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1652 | this->INHERITED::onClipRect(r, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1655 | virtual void onClipRRect(const SkRRect& rrect, |
| 1656 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1657 | ClipEdgeStyle edgeStyle)SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1658 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1659 | this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1662 | virtual void onClipPath(const SkPath& path, |
| 1663 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1664 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1665 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1666 | this->INHERITED::onClipPath(path, op, edgeStyle); |
| 1667 | } |
| 1668 | |
| 1669 | virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE { |
| 1670 | fClipCount += 1; |
| 1671 | this->INHERITED::onClipRegion(deviceRgn, op); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | unsigned getClipCount() const { return fClipCount; } |
| 1675 | |
| 1676 | private: |
| 1677 | unsigned fClipCount; |
| 1678 | |
| 1679 | typedef SkCanvas INHERITED; |
| 1680 | }; |
| 1681 | |
| 1682 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1683 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1684 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1685 | |
| 1686 | canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); |
| 1687 | // The following expanding clip should not be skipped. |
| 1688 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); |
| 1689 | // Draw something so the optimizer doesn't just fold the world. |
| 1690 | SkPaint p; |
| 1691 | p.setColor(SK_ColorBLUE); |
| 1692 | canvas->drawPaint(p); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1693 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1694 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1695 | ClipCountingCanvas testCanvas(10, 10); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame^] | 1696 | picture->playback(&testCanvas); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1697 | |
| 1698 | // Both clips should be present on playback. |
| 1699 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 1700 | } |
| 1701 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1702 | static void test_hierarchical(skiatest::Reporter* reporter) { |
| 1703 | SkBitmap bm; |
| 1704 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1705 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1706 | SkPictureRecorder recorder; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1707 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1708 | recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1709 | SkAutoTUnref<SkPicture> childPlain(recorder.endRecording()); |
| 1710 | REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1711 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1712 | recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1713 | SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording()); |
| 1714 | REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1715 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1716 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1717 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1718 | canvas->drawPicture(childPlain); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1719 | SkAutoTUnref<SkPicture> parentPP(recorder.endRecording()); |
| 1720 | REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 |
| 1721 | } |
| 1722 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1723 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1724 | canvas->drawPicture(childWithBitmap); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1725 | SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording()); |
| 1726 | REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 |
| 1727 | } |
| 1728 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1729 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1730 | canvas->drawBitmap(bm, 0, 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1731 | canvas->drawPicture(childPlain); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1732 | SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording()); |
| 1733 | REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 |
| 1734 | } |
| 1735 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1736 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1737 | canvas->drawBitmap(bm, 0, 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1738 | canvas->drawPicture(childWithBitmap); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1739 | SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording()); |
| 1740 | REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 |
| 1741 | } |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1744 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 1745 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1746 | SkPictureRecorder recorder; |
| 1747 | recorder.beginRecording(0, 0); |
| 1748 | SkAutoTUnref<SkPicture> empty(recorder.endRecording()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1749 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1750 | // Empty pictures should still have a valid ID |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1751 | REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1752 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1753 | SkCanvas* canvas = recorder.beginRecording(1, 1); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1754 | canvas->drawARGB(255, 255, 255, 255); |
| 1755 | SkAutoTUnref<SkPicture> hasData(recorder.endRecording()); |
| 1756 | // picture should have a non-zero id after recording |
| 1757 | REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1758 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1759 | // both pictures should have different ids |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1760 | REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 1763 | DEF_TEST(Picture, reporter) { |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1764 | #ifdef SK_DEBUG |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 1765 | test_deleting_empty_picture(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1766 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1767 | #else |
| 1768 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1769 | #endif |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1770 | test_unbalanced_save_restores(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1771 | test_peephole(); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1772 | #if SK_SUPPORT_GPU |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 1773 | test_gpu_veto(reporter, false); |
| 1774 | test_gpu_veto(reporter, true); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1775 | #endif |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 1776 | test_has_text(reporter, false); |
| 1777 | test_has_text(reporter, true); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 1778 | test_analysis(reporter, false); |
| 1779 | test_analysis(reporter, true); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1780 | test_gatherpixelrefs(reporter); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 1781 | test_gatherpixelrefsandrects(reporter); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1782 | test_bitmap_with_encoded_data(reporter); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1783 | test_draw_empty(reporter); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1784 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1785 | test_clip_expansion(reporter); |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1786 | test_hierarchical(reporter); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1787 | test_gen_id(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1788 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1789 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1790 | #if SK_SUPPORT_GPU |
| 1791 | DEF_GPUTEST(GPUPicture, reporter, factory) { |
| 1792 | test_gpu_picture_optimization(reporter, factory); |
| 1793 | } |
| 1794 | #endif |
| 1795 | |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1796 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 1797 | const SkPaint paint; |
| 1798 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 1799 | const SkIRect irect = { 2, 2, 3, 3 }; |
| 1800 | |
| 1801 | // Don't care what these record, as long as they're legal. |
| 1802 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
| 1803 | canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag); |
| 1804 | canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint); |
| 1805 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
| 1806 | canvas->drawSprite(bitmap, 1, 1); |
| 1807 | } |
| 1808 | |
| 1809 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 1810 | SkBitmap empty; |
| 1811 | draw_bitmaps(empty, canvas); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1812 | empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1813 | draw_bitmaps(empty, canvas); |
| 1814 | } |
| 1815 | |
| 1816 | DEF_TEST(Picture_EmptyBitmap, r) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1817 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1818 | test_draw_bitmaps(recorder.beginRecording(10, 10)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1819 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 1823 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1824 | dst.allocN32Pixels(10, 10); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1825 | SkCanvas canvas(dst); |
| 1826 | |
| 1827 | test_draw_bitmaps(&canvas); |
| 1828 | } |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 1829 | |
| 1830 | DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { |
| 1831 | // This test is from crbug.com/344987. |
| 1832 | // The commands are: |
| 1833 | // saveLayer with paint that modifies alpha |
| 1834 | // drawBitmapRectToRect |
| 1835 | // drawBitmapRectToRect |
| 1836 | // restore |
| 1837 | // The bug was that this structure was modified so that: |
| 1838 | // - The saveLayer and restore were eliminated |
| 1839 | // - The alpha was only applied to the first drawBitmapRectToRect |
| 1840 | |
| 1841 | // This test draws blue and red squares inside a 50% transparent |
| 1842 | // layer. Both colours should show up muted. |
| 1843 | // When the bug is present, the red square (the second bitmap) |
| 1844 | // shows upwith full opacity. |
| 1845 | |
| 1846 | SkBitmap blueBM; |
| 1847 | make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); |
| 1848 | SkBitmap redBM; |
| 1849 | make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); |
| 1850 | SkPaint semiTransparent; |
| 1851 | semiTransparent.setAlpha(0x80); |
| 1852 | |
| 1853 | SkPictureRecorder recorder; |
| 1854 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 1855 | canvas->drawARGB(0, 0, 0, 0); |
| 1856 | |
| 1857 | canvas->saveLayer(0, &semiTransparent); |
| 1858 | canvas->drawBitmap(blueBM, 25, 25); |
| 1859 | canvas->drawBitmap(redBM, 50, 50); |
| 1860 | canvas->restore(); |
| 1861 | |
| 1862 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1863 | |
| 1864 | // Now replay the picture back on another canvas |
| 1865 | // and check a couple of its pixels. |
| 1866 | SkBitmap replayBM; |
| 1867 | make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); |
| 1868 | SkCanvas replayCanvas(replayBM); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame^] | 1869 | picture->playback(&replayCanvas); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 1870 | replayCanvas.flush(); |
| 1871 | |
| 1872 | // With the bug present, at (55, 55) we would get a fully opaque red |
| 1873 | // intead of a dark red. |
| 1874 | REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); |
| 1875 | REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); |
| 1876 | } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1877 | |
| 1878 | struct CountingBBH : public SkBBoxHierarchy { |
| 1879 | mutable int searchCalls; |
| 1880 | |
| 1881 | CountingBBH() : searchCalls(0) {} |
| 1882 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1883 | virtual void search(const SkRect& query, SkTDArray<void*>* results) const { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1884 | this->searchCalls++; |
| 1885 | } |
| 1886 | |
| 1887 | // All other methods unimplemented. |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1888 | virtual void insert(void* data, const SkRect& bounds, bool defer) {} |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1889 | virtual void flushDeferredInserts() {} |
| 1890 | virtual void clear() {} |
| 1891 | virtual int getCount() const { return 0; } |
| 1892 | virtual int getDepth() const { return 0; } |
| 1893 | virtual void rewindInserts() {} |
| 1894 | }; |
| 1895 | |
| 1896 | class SpoonFedBBHFactory : public SkBBHFactory { |
| 1897 | public: |
| 1898 | explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {} |
| 1899 | virtual SkBBoxHierarchy* operator()(int width, int height) const { |
| 1900 | return SkRef(fBBH); |
| 1901 | } |
| 1902 | private: |
| 1903 | SkBBoxHierarchy* fBBH; |
| 1904 | }; |
| 1905 | |
| 1906 | // When the canvas clip covers the full picture, we don't need to call the BBH. |
| 1907 | DEF_TEST(Picture_SkipBBH, r) { |
| 1908 | CountingBBH bbh; |
| 1909 | SpoonFedBBHFactory factory(&bbh); |
| 1910 | |
| 1911 | SkPictureRecorder recorder; |
| 1912 | recorder.beginRecording(320, 240, &factory); |
| 1913 | SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); |
| 1914 | |
| 1915 | SkCanvas big(640, 480), small(300, 200); |
| 1916 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame^] | 1917 | picture->playback(&big); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1918 | REPORTER_ASSERT(r, bbh.searchCalls == 0); |
| 1919 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame^] | 1920 | picture->playback(&small); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1921 | REPORTER_ASSERT(r, bbh.searchCalls == 1); |
| 1922 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1923 | |
| 1924 | DEF_TEST(Picture_BitmapLeak, r) { |
| 1925 | SkBitmap mut, immut; |
| 1926 | mut.allocN32Pixels(300, 200); |
| 1927 | immut.allocN32Pixels(300, 200); |
| 1928 | immut.setImmutable(); |
| 1929 | SkASSERT(!mut.isImmutable()); |
| 1930 | SkASSERT(immut.isImmutable()); |
| 1931 | |
| 1932 | // No one can hold a ref on our pixels yet. |
| 1933 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1934 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 1935 | |
| 1936 | SkPictureRecorder rec; |
| 1937 | SkCanvas* canvas = rec.beginRecording(1920, 1200); |
| 1938 | canvas->drawBitmap(mut, 0, 0); |
| 1939 | canvas->drawBitmap(immut, 800, 600); |
| 1940 | SkAutoTDelete<const SkPicture> pic(rec.endRecording()); |
| 1941 | |
| 1942 | // The picture shares the immutable pixels but copies the mutable ones. |
| 1943 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1944 | REPORTER_ASSERT(r, !immut.pixelRef()->unique()); |
| 1945 | |
| 1946 | // When the picture goes away, it's just our bitmaps holding the refs. |
| 1947 | pic.reset(NULL); |
| 1948 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1949 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 1950 | } |