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 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.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" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 16 | #if SK_SUPPORT_GPU |
| 17 | #include "SkGpuDevice.h" |
| 18 | #endif |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 19 | #include "SkImageEncoder.h" |
| 20 | #include "SkImageGenerator.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 21 | #include "SkPaint.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 22 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 23 | #include "SkPictureRecorder.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 24 | #include "SkPictureUtils.h" |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 25 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 26 | #include "SkRandom.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 27 | #include "SkShader.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 28 | #include "SkStream.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 29 | |
| 30 | #if SK_SUPPORT_GPU |
| 31 | #include "SkSurface.h" |
| 32 | #include "GrContextFactory.h" |
| 33 | #include "GrPictureUtils.h" |
| 34 | #endif |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 35 | #include "Test.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 36 | |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 37 | #include "SkLumaColorFilter.h" |
| 38 | #include "SkColorFilterImageFilter.h" |
| 39 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 40 | static const int gColorScale = 30; |
| 41 | static const int gColorOffset = 60; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 42 | |
| 43 | 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] | 44 | bm->allocN32Pixels(w, h); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 45 | bm->eraseColor(color); |
| 46 | if (immutable) { |
| 47 | bm->setImmutable(); |
| 48 | } |
| 49 | } |
| 50 | |
robertphillips@google.com | fe5824a | 2014-01-09 19:45:29 +0000 | [diff] [blame] | 51 | 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] | 52 | SkASSERT(w % 2 == 0); |
| 53 | SkASSERT(h % 2 == 0); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 54 | bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType, |
| 55 | kPremul_SkAlphaType)); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 56 | SkAutoLockPixels lock(*bm); |
| 57 | for (int y = 0; y < h; y += 2) { |
| 58 | uint8_t* s = bm->getAddr8(0, y); |
| 59 | for (int x = 0; x < w; x += 2) { |
| 60 | *s++ = 0xFF; |
| 61 | *s++ = 0x00; |
| 62 | } |
| 63 | s = bm->getAddr8(0, y + 1); |
| 64 | for (int x = 0; x < w; x += 2) { |
| 65 | *s++ = 0x00; |
| 66 | *s++ = 0xFF; |
| 67 | } |
| 68 | } |
| 69 | if (immutable) { |
| 70 | bm->setImmutable(); |
| 71 | } |
| 72 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 73 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 74 | static void init_paint(SkPaint* paint, const SkBitmap &bm) { |
| 75 | SkShader* shader = SkShader::CreateBitmapShader(bm, |
| 76 | SkShader::kClamp_TileMode, |
| 77 | SkShader::kClamp_TileMode); |
| 78 | paint->setShader(shader)->unref(); |
| 79 | } |
| 80 | |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 81 | typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 82 | const SkBitmap&, const SkPoint&, |
| 83 | SkTDArray<SkPixelRef*>* usedPixRefs); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 84 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 85 | static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 86 | const SkBitmap& altBM, const SkPoint& pos, |
| 87 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 88 | SkPaint paint; |
| 89 | init_paint(&paint, bm); |
| 90 | |
| 91 | canvas->drawPaint(paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 92 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 95 | static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 96 | const SkBitmap& altBM, const SkPoint& pos, |
| 97 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 98 | SkPaint paint; |
| 99 | init_paint(&paint, bm); |
| 100 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 101 | // draw a rect |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 102 | SkPoint points[5] = { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 103 | { pos.fX, pos.fY }, |
| 104 | { pos.fX + bm.width() - 1, pos.fY }, |
| 105 | { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 }, |
| 106 | { pos.fX, pos.fY + bm.height() - 1 }, |
| 107 | { pos.fX, pos.fY }, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 111 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 114 | static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 115 | const SkBitmap& altBM, const SkPoint& pos, |
| 116 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 117 | SkPaint paint; |
| 118 | init_paint(&paint, bm); |
| 119 | |
| 120 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 121 | r.offset(pos.fX, pos.fY); |
| 122 | |
| 123 | canvas->drawRect(r, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 124 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 125 | } |
| 126 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 127 | static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 128 | const SkBitmap& altBM, const SkPoint& pos, |
| 129 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 130 | SkPaint paint; |
| 131 | init_paint(&paint, bm); |
| 132 | |
| 133 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 134 | r.offset(pos.fX, pos.fY); |
| 135 | |
| 136 | canvas->drawOval(r, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 137 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 138 | } |
| 139 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 140 | static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 141 | const SkBitmap& altBM, const SkPoint& pos, |
| 142 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 143 | SkPaint paint; |
| 144 | init_paint(&paint, bm); |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 145 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 146 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 147 | r.offset(pos.fX, pos.fY); |
| 148 | |
| 149 | SkRRect rr; |
| 150 | rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4); |
| 151 | canvas->drawRRect(rr, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 152 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 153 | } |
| 154 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 155 | static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 156 | const SkBitmap& altBM, const SkPoint& pos, |
| 157 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 158 | SkPaint paint; |
| 159 | init_paint(&paint, bm); |
| 160 | |
| 161 | SkPath path; |
| 162 | path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height())); |
| 163 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 164 | path.close(); |
| 165 | path.offset(pos.fX, pos.fY); |
| 166 | |
| 167 | canvas->drawPath(path, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 168 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 169 | } |
| 170 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 171 | static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 172 | const SkBitmap& altBM, const SkPoint& pos, |
| 173 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 174 | canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 175 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 176 | } |
| 177 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 178 | static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 179 | const SkBitmap& altBM, const SkPoint& pos, |
| 180 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 181 | SkPaint paint; |
| 182 | init_paint(&paint, bm); |
| 183 | |
| 184 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 185 | canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 186 | *usedPixRefs->append() = bm.pixelRef(); |
| 187 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 188 | } |
| 189 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 190 | static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 191 | const SkBitmap& altBM, const SkPoint& pos, |
| 192 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 193 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 194 | |
| 195 | SkPoint p(pos); |
| 196 | ctm.mapPoints(&p, 1); |
| 197 | |
| 198 | canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 199 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | #if 0 |
| 203 | // Although specifiable, this case doesn't seem to make sense (i.e., the |
| 204 | // bitmap in the shader is never used). |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 205 | static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 206 | const SkBitmap& altBM, const SkPoint& pos, |
| 207 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 208 | SkPaint paint; |
| 209 | init_paint(&paint, bm); |
| 210 | |
| 211 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 212 | |
| 213 | SkPoint p(pos); |
| 214 | ctm.mapPoints(&p, 1); |
| 215 | |
| 216 | canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 217 | *usedPixRefs->append() = bm.pixelRef(); |
| 218 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 219 | } |
| 220 | #endif |
| 221 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 222 | static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 223 | const SkBitmap& altBM, const SkPoint& pos, |
| 224 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 225 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 226 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 227 | r.offset(pos.fX, pos.fY); |
| 228 | canvas->drawBitmapRectToRect(bm, NULL, r, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 229 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 230 | } |
| 231 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 232 | static void drawbitmaprect_withshader_proc(SkCanvas* canvas, |
| 233 | const SkBitmap& bm, |
| 234 | const SkBitmap& altBM, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 235 | const SkPoint& pos, |
| 236 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 237 | SkPaint paint; |
| 238 | init_paint(&paint, bm); |
| 239 | |
| 240 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 241 | r.offset(pos.fX, pos.fY); |
| 242 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 243 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 244 | canvas->drawBitmapRectToRect(altBM, NULL, r, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 245 | *usedPixRefs->append() = bm.pixelRef(); |
| 246 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 247 | } |
| 248 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 249 | static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 250 | const SkBitmap& altBM, const SkPoint& pos, |
| 251 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 252 | SkPaint paint; |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 253 | init_paint(&paint, bm); |
| 254 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 255 | |
| 256 | canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 257 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 258 | } |
| 259 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 260 | static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 261 | const SkBitmap& altBM, const SkPoint& pos, |
| 262 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 263 | SkPaint paint; |
| 264 | init_paint(&paint, bm); |
| 265 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 266 | |
| 267 | SkPoint point = { pos.fX, pos.fY + bm.height() }; |
| 268 | canvas->drawPosText("O", 1, &point, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 269 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 270 | } |
| 271 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 272 | static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 273 | const SkBitmap& altBM, const SkPoint& pos, |
| 274 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 275 | SkPaint paint; |
| 276 | |
| 277 | init_paint(&paint, bm); |
| 278 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 279 | |
| 280 | SkPath path; |
| 281 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 282 | path.offset(pos.fX, pos.fY+bm.height()); |
| 283 | |
| 284 | canvas->drawTextOnPath("O", 1, path, NULL, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 285 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 286 | } |
| 287 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 288 | static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 289 | const SkBitmap& altBM, const SkPoint& pos, |
| 290 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 291 | SkPaint paint; |
| 292 | init_paint(&paint, bm); |
| 293 | |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 294 | SkPoint verts[4] = { |
| 295 | { pos.fX, pos.fY }, |
| 296 | { pos.fX + bm.width(), pos.fY }, |
| 297 | { pos.fX + bm.width(), pos.fY + bm.height() }, |
| 298 | { pos.fX, pos.fY + bm.height() } |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 299 | }; |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 300 | SkPoint texs[4] = { { 0, 0 }, |
| 301 | { SkIntToScalar(bm.width()), 0 }, |
| 302 | { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 303 | { 0, SkIntToScalar(bm.height()) } }; |
| 304 | uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 }; |
| 305 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 306 | canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 307 | indices, 6, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 308 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // 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] | 312 | static SkPicture* record_bitmaps(const SkBitmap bm[], |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 313 | const SkPoint pos[], |
| 314 | SkTDArray<SkPixelRef*> analytic[], |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 315 | int count, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 316 | DrawBitmapProc proc) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 317 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 318 | SkCanvas* canvas = recorder.beginRecording(1000, 1000, NULL, 0); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 319 | for (int i = 0; i < count; ++i) { |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 320 | analytic[i].rewind(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 321 | canvas->save(); |
| 322 | SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, |
| 323 | SkIntToScalar(bm[i].width()), |
| 324 | SkIntToScalar(bm[i].height())); |
| 325 | canvas->clipRect(clipRect, SkRegion::kIntersect_Op); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 326 | proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 327 | canvas->restore(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 328 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 329 | return recorder.endRecording(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 330 | } |
| 331 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 332 | 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] | 333 | rect->fLeft = rand.nextRangeScalar(-W, 2*W); |
| 334 | rect->fTop = rand.nextRangeScalar(-H, 2*H); |
| 335 | rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); |
| 336 | rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); |
| 337 | |
| 338 | // we integralize rect to make our tests more predictable, since Gather is |
| 339 | // a little sloppy. |
| 340 | SkIRect ir; |
| 341 | rect->round(&ir); |
| 342 | rect->set(ir); |
| 343 | } |
| 344 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 345 | static void draw(SkPicture* pic, int width, int height, SkBitmap* result) { |
| 346 | make_bm(result, width, height, SK_ColorBLACK, false); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 347 | |
| 348 | SkCanvas canvas(*result); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 349 | canvas.drawPicture(*pic); |
| 350 | } |
| 351 | |
| 352 | template <typename T> int find_index(const T* array, T elem, int count) { |
| 353 | for (int i = 0; i < count; ++i) { |
| 354 | if (array[i] == elem) { |
| 355 | return i; |
| 356 | } |
| 357 | } |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | // Return true if 'ref' is found in array[] |
| 362 | static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) { |
| 363 | return find_index<const SkPixelRef*>(array, ref, count) >= 0; |
| 364 | } |
| 365 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 366 | // Look at each pixel that is inside 'subset', and if its color appears in |
| 367 | // 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] | 368 | // array, skipping duplicates of the same value. |
| 369 | // Note that gathering pixelRefs from rendered colors suffers from the problem |
| 370 | // that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color) |
| 371 | // isn't easy to reconstruct. |
| 372 | static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[], |
| 373 | int count, SkTDArray<SkPixelRef*>* array, |
| 374 | const SkRect& subset) { |
| 375 | SkIRect ir; |
| 376 | subset.roundOut(&ir); |
| 377 | |
| 378 | if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) { |
| 379 | return; |
| 380 | } |
| 381 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 382 | // Since we only want to return unique values in array, when we scan we just |
| 383 | // set a bit for each index'd color found. In practice we only have a few |
| 384 | // distinct colors, so we just use an int's bits as our array. Hence the |
| 385 | // assert that count <= number-of-bits-in-our-int. |
| 386 | SkASSERT((unsigned)count <= 32); |
| 387 | uint32_t bitarray = 0; |
| 388 | |
| 389 | SkAutoLockPixels alp(bm); |
| 390 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 391 | for (int y = ir.fTop; y < ir.fBottom; ++y) { |
| 392 | for (int x = ir.fLeft; x < ir.fRight; ++x) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 393 | SkPMColor pmc = *bm.getAddr32(x, y); |
| 394 | // the only good case where the color is not found would be if |
| 395 | // the color is transparent, meaning no bitmap was drawn in that |
| 396 | // pixel. |
| 397 | if (pmc) { |
bsalomon@google.com | c3d753e | 2013-01-08 17:24:44 +0000 | [diff] [blame] | 398 | uint32_t index = SkGetPackedR32(pmc); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 399 | SkASSERT(SkGetPackedG32(pmc) == index); |
| 400 | SkASSERT(SkGetPackedB32(pmc) == index); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 401 | if (0 == index) { |
| 402 | continue; // background color |
| 403 | } |
| 404 | SkASSERT(0 == (index - gColorOffset) % gColorScale); |
| 405 | index = (index - gColorOffset) / gColorScale; |
bsalomon@google.com | 5f429b0 | 2013-01-08 18:42:20 +0000 | [diff] [blame] | 406 | SkASSERT(static_cast<int>(index) < count); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 407 | bitarray |= 1 << index; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | for (int i = 0; i < count; ++i) { |
| 413 | if (bitarray & (1 << i)) { |
| 414 | *array->append() = refs[i]; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
robertphillips@google.com | 0e4ce14 | 2014-01-13 13:46:45 +0000 | [diff] [blame] | 419 | 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] | 420 | const SkTDArray<SkPixelRef*> analytic[], |
| 421 | int count, |
| 422 | SkTDArray<SkPixelRef*>* result, |
robertphillips@google.com | 0e4ce14 | 2014-01-13 13:46:45 +0000 | [diff] [blame] | 423 | const SkRect& subset) { |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 424 | for (int i = 0; i < count; ++i) { |
| 425 | SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); |
| 426 | |
| 427 | if (SkRect::Intersects(subset, rect)) { |
| 428 | result->append(analytic[i].count(), analytic[i].begin()); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 433 | |
| 434 | static const struct { |
| 435 | const DrawBitmapProc proc; |
| 436 | const char* const desc; |
| 437 | } gProcs[] = { |
| 438 | {drawpaint_proc, "drawpaint"}, |
| 439 | {drawpoints_proc, "drawpoints"}, |
| 440 | {drawrect_proc, "drawrect"}, |
| 441 | {drawoval_proc, "drawoval"}, |
| 442 | {drawrrect_proc, "drawrrect"}, |
| 443 | {drawpath_proc, "drawpath"}, |
| 444 | {drawbitmap_proc, "drawbitmap"}, |
| 445 | {drawbitmap_withshader_proc, "drawbitmap_withshader"}, |
| 446 | {drawsprite_proc, "drawsprite"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 447 | #if 0 |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 448 | {drawsprite_withshader_proc, "drawsprite_withshader"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 449 | #endif |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 450 | {drawbitmaprect_proc, "drawbitmaprect"}, |
| 451 | {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"}, |
| 452 | {drawtext_proc, "drawtext"}, |
| 453 | {drawpostext_proc, "drawpostext"}, |
| 454 | {drawtextonpath_proc, "drawtextonpath"}, |
| 455 | {drawverts_proc, "drawverts"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | 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] | 459 | // Our convention is that the color components contain an encoding of |
| 460 | // 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] | 461 | // reserved for the background |
| 462 | for (int i = 0; i < num; ++i) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 463 | make_bm(&bm[i], w, h, |
| 464 | SkColorSetARGB(0xFF, |
| 465 | gColorScale*i+gColorOffset, |
| 466 | gColorScale*i+gColorOffset, |
| 467 | gColorScale*i+gColorOffset), |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 468 | true); |
| 469 | refs[i] = bm[i].pixelRef(); |
| 470 | } |
| 471 | |
| 472 | // The A8 alternate bitmaps are all BW checkerboards |
| 473 | for (int i = 0; i < num; ++i) { |
| 474 | make_checkerboard(&bm[num+i], w, h, true); |
| 475 | refs[num+i] = bm[num+i].pixelRef(); |
| 476 | } |
| 477 | } |
| 478 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 479 | static void test_gatherpixelrefs(skiatest::Reporter* reporter) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 480 | const int IW = 32; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 481 | const int IH = IW; |
| 482 | const SkScalar W = SkIntToScalar(IW); |
| 483 | const SkScalar H = W; |
| 484 | |
| 485 | static const int N = 4; |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 486 | SkBitmap bm[2*N]; |
| 487 | SkPixelRef* refs[2*N]; |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 488 | SkTDArray<SkPixelRef*> analytic[N]; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 489 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 490 | const SkPoint pos[N] = { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 491 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 492 | }; |
| 493 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 494 | create_textures(bm, refs, N, IW, IH); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 495 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 496 | SkRandom rand; |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 497 | 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] | 498 | SkAutoTUnref<SkPicture> pic( |
| 499 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 500 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 501 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 502 | // 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] | 503 | // contain 1 or 2 bitmaps. |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 504 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 505 | SkRect r; |
| 506 | r.set(2, 2, W - 2, H - 2); |
| 507 | r.offset(pos[i].fX, pos[i].fY); |
| 508 | SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 509 | if (!data) { |
| 510 | ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned " |
| 511 | "NULL for %s.", gProcs[k].desc); |
| 512 | continue; |
| 513 | } |
| 514 | SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); |
| 515 | int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); |
| 516 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 517 | if (1 == count) { |
| 518 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 519 | } else if (2 == count) { |
| 520 | REPORTER_ASSERT(reporter, |
| 521 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 522 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
commit-bot@chromium.org | fe433c1 | 2013-07-09 16:04:32 +0000 | [diff] [blame] | 523 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 524 | } |
| 525 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 526 | SkBitmap image; |
| 527 | draw(pic, 2*IW, 2*IH, &image); |
| 528 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 529 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 530 | // with a deduced list of refs by looking at the colors drawn. |
| 531 | for (int j = 0; j < 100; ++j) { |
| 532 | SkRect r; |
| 533 | rand_rect(&r, rand, 2*W, 2*H); |
| 534 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 535 | SkTDArray<SkPixelRef*> fromImage; |
| 536 | gather_from_image(image, refs, N, &fromImage, r); |
| 537 | |
| 538 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 539 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 540 | |
| 541 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 542 | size_t dataSize = data ? data->size() : 0; |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 543 | int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 544 | SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); |
| 545 | SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; |
| 546 | SkAutoDataUnref adu(data); |
| 547 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 548 | // Everything that we saw drawn should appear in the analytic list |
| 549 | // but the analytic list may contain some pixelRefs that were not |
| 550 | // seen in the image (e.g., A8 textures used as masks) |
| 551 | for (int i = 0; i < fromImage.count(); ++i) { |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 552 | if (-1 == fromAnalytic.find(fromImage[i])) { |
| 553 | ERRORF(reporter, "PixelRef missing %d %s", |
| 554 | i, gProcs[k].desc); |
| 555 | } |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 556 | } |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 557 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 558 | /* |
| 559 | * GatherPixelRefs is conservative, so it can return more bitmaps |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 560 | * than are strictly required. Thus our check here is only that |
| 561 | * 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] | 562 | * a strict requirement on Gather, which is meant to be quick and |
| 563 | * only mostly-correct, but at the moment this test should work. |
| 564 | */ |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 565 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 566 | bool found = find(gatherRefs, fromAnalytic[i], gatherCount); |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 567 | if (!found) { |
| 568 | ERRORF(reporter, "PixelRef missing %d %s", |
| 569 | i, gProcs[k].desc); |
| 570 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 571 | #if 0 |
| 572 | // enable this block of code to debug failures, as it will rerun |
| 573 | // the case that failed. |
| 574 | if (!found) { |
| 575 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 576 | size_t dataSize = data ? data->size() : 0; |
| 577 | } |
| 578 | #endif |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 584 | static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { |
| 585 | const int IW = 32; |
| 586 | const int IH = IW; |
| 587 | const SkScalar W = SkIntToScalar(IW); |
| 588 | const SkScalar H = W; |
| 589 | |
| 590 | static const int N = 4; |
| 591 | SkBitmap bm[2*N]; |
| 592 | SkPixelRef* refs[2*N]; |
| 593 | SkTDArray<SkPixelRef*> analytic[N]; |
| 594 | |
| 595 | const SkPoint pos[N] = { |
| 596 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 597 | }; |
| 598 | |
| 599 | create_textures(bm, refs, N, IW, IH); |
| 600 | |
| 601 | SkRandom rand; |
| 602 | 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] | 603 | SkAutoTUnref<SkPicture> pic( |
| 604 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 605 | |
| 606 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
| 607 | |
| 608 | SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( |
| 609 | new SkPictureUtils::SkPixelRefsAndRectsList); |
| 610 | |
| 611 | SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); |
| 612 | |
| 613 | // quick check for a small piece of each quadrant, which should just |
| 614 | // contain 1 or 2 bitmaps. |
| 615 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 616 | SkRect r; |
| 617 | r.set(2, 2, W - 2, H - 2); |
| 618 | r.offset(pos[i].fX, pos[i].fY); |
| 619 | |
| 620 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 621 | prCont->query(r, &gatheredRefs); |
| 622 | |
| 623 | int count = gatheredRefs.count(); |
| 624 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 625 | if (1 == count) { |
| 626 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 627 | } else if (2 == count) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 628 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 629 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 630 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | SkBitmap image; |
| 635 | draw(pic, 2*IW, 2*IH, &image); |
| 636 | |
| 637 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 638 | // with the analytic results and the pixel refs seen in a rendering. |
| 639 | for (int j = 0; j < 100; ++j) { |
| 640 | SkRect r; |
| 641 | rand_rect(&r, rand, 2*W, 2*H); |
| 642 | |
| 643 | SkTDArray<SkPixelRef*> fromImage; |
| 644 | gather_from_image(image, refs, N, &fromImage, r); |
| 645 | |
| 646 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 647 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
| 648 | |
| 649 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 650 | prCont->query(r, &gatheredRefs); |
| 651 | |
| 652 | // Everything that we saw drawn should appear in the analytic list |
| 653 | // but the analytic list may contain some pixelRefs that were not |
| 654 | // seen in the image (e.g., A8 textures used as masks) |
| 655 | for (int i = 0; i < fromImage.count(); ++i) { |
| 656 | REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); |
| 657 | } |
| 658 | |
| 659 | // Everything in the analytic list should appear in the gathered |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 660 | // list. |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 661 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 662 | REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 668 | #ifdef SK_DEBUG |
| 669 | // Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only |
| 670 | // run in debug mode. |
| 671 | static void test_deleting_empty_playback() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 672 | SkPictureRecorder recorder; |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 673 | // Creates an SkPictureRecord |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 674 | recorder.beginRecording(0, 0, NULL, 0); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 675 | // Turns that into an SkPicturePlayback |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 676 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 677 | // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 678 | recorder.beginRecording(0, 0, NULL, 0); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 682 | static void test_serializing_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 683 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 684 | recorder.beginRecording(0, 0, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 685 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 686 | SkDynamicMemoryWStream stream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 687 | picture->serialize(&stream); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 688 | } |
| 689 | #endif |
| 690 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 691 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 692 | SkPaint paint; |
| 693 | SkRect rect = SkRect::MakeWH(50, 50); |
| 694 | |
| 695 | SkScalar unit = rand.nextUScalar1(); |
| 696 | if (unit <= 0.3) { |
| 697 | // SkDebugf("save\n"); |
| 698 | canvas->save(); |
| 699 | } else if (unit <= 0.6) { |
| 700 | // SkDebugf("restore\n"); |
| 701 | canvas->restore(); |
| 702 | } else if (unit <= 0.9) { |
| 703 | // SkDebugf("clip\n"); |
| 704 | canvas->clipRect(rect); |
| 705 | } else { |
| 706 | // SkDebugf("draw\n"); |
| 707 | canvas->drawPaint(paint); |
| 708 | } |
| 709 | } |
| 710 | |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 711 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 712 | static void test_gpu_veto(skiatest::Reporter* reporter) { |
| 713 | |
| 714 | SkPictureRecorder recorder; |
| 715 | |
| 716 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
| 717 | { |
| 718 | SkPath path; |
| 719 | path.moveTo(0, 0); |
| 720 | path.lineTo(50, 50); |
| 721 | |
| 722 | SkScalar intervals[] = { 1.0f, 1.0f }; |
| 723 | SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); |
| 724 | |
| 725 | SkPaint paint; |
| 726 | paint.setStyle(SkPaint::kStroke_Style); |
| 727 | paint.setPathEffect(dash); |
| 728 | |
| 729 | canvas->drawPath(path, paint); |
| 730 | } |
| 731 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 732 | // path effects currently render an SkPicture undesireable for GPU rendering |
| 733 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); |
| 734 | |
| 735 | canvas = recorder.beginRecording(100, 100, NULL, 0); |
| 736 | { |
| 737 | SkPath path; |
| 738 | |
| 739 | path.moveTo(0, 0); |
| 740 | path.lineTo(0, 50); |
| 741 | path.lineTo(25, 25); |
| 742 | path.lineTo(50, 50); |
| 743 | path.lineTo(50, 0); |
| 744 | path.close(); |
| 745 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 746 | |
| 747 | SkPaint paint; |
| 748 | paint.setAntiAlias(true); |
| 749 | for (int i = 0; i < 50; ++i) { |
| 750 | canvas->drawPath(path, paint); |
| 751 | } |
| 752 | } |
| 753 | picture.reset(recorder.endRecording()); |
| 754 | // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering |
| 755 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); |
| 756 | |
| 757 | canvas = recorder.beginRecording(100, 100, NULL, 0); |
| 758 | { |
| 759 | SkPath path; |
| 760 | |
| 761 | path.moveTo(0, 0); |
| 762 | path.lineTo(0, 50); |
| 763 | path.lineTo(25, 25); |
| 764 | path.lineTo(50, 50); |
| 765 | path.lineTo(50, 0); |
| 766 | path.close(); |
| 767 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 768 | |
| 769 | SkPaint paint; |
| 770 | paint.setAntiAlias(true); |
| 771 | paint.setStyle(SkPaint::kStroke_Style); |
| 772 | paint.setStrokeWidth(0); |
| 773 | for (int i = 0; i < 50; ++i) { |
| 774 | canvas->drawPath(path, paint); |
| 775 | } |
| 776 | } |
| 777 | picture.reset(recorder.endRecording()); |
| 778 | // hairline stroked AA concave paths are fine for GPU rendering |
| 779 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL)); |
| 780 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 781 | |
| 782 | static void test_gpu_picture_optimization(skiatest::Reporter* reporter, |
| 783 | GrContextFactory* factory) { |
| 784 | |
| 785 | GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); |
| 786 | |
| 787 | static const int kWidth = 100; |
| 788 | static const int kHeight = 100; |
| 789 | |
| 790 | SkAutoTUnref<SkPicture> pict; |
| 791 | |
| 792 | // create a picture with the structure: |
| 793 | // 1) |
| 794 | // SaveLayer |
| 795 | // Restore |
| 796 | // 2) |
| 797 | // SaveLayer |
skia.committer@gmail.com | a5b068c | 2014-05-07 03:04:15 +0000 | [diff] [blame] | 798 | // Translate |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 799 | // SaveLayer w/ bound |
| 800 | // Restore |
| 801 | // Restore |
| 802 | // 3) |
| 803 | // SaveLayer w/ copyable paint |
| 804 | // Restore |
| 805 | // 4) |
| 806 | // SaveLayer w/ non-copyable paint |
| 807 | // Restore |
| 808 | { |
| 809 | SkPictureRecorder recorder; |
| 810 | |
| 811 | SkCanvas* c = recorder.beginRecording(kWidth, kHeight, NULL, 0); |
| 812 | // 1) |
| 813 | c->saveLayer(NULL, NULL); |
| 814 | c->restore(); |
| 815 | |
| 816 | // 2) |
| 817 | c->saveLayer(NULL, NULL); |
| 818 | c->translate(kWidth/2, kHeight/2); |
| 819 | SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2); |
| 820 | c->saveLayer(&r, NULL); |
| 821 | c->restore(); |
| 822 | c->restore(); |
| 823 | |
| 824 | // 3) |
| 825 | { |
| 826 | SkPaint p; |
| 827 | p.setColor(SK_ColorRED); |
| 828 | c->saveLayer(NULL, &p); |
| 829 | c->restore(); |
| 830 | } |
| 831 | // 4) |
| 832 | // TODO: this case will need to be removed once the paint's are immutable |
| 833 | { |
| 834 | SkPaint p; |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 835 | SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create()); |
| 836 | p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref(); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 837 | c->saveLayer(NULL, &p); |
| 838 | c->restore(); |
| 839 | } |
| 840 | |
| 841 | pict.reset(recorder.endRecording()); |
| 842 | } |
| 843 | |
| 844 | // Now test out the SaveLayer extraction |
| 845 | { |
| 846 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 847 | |
| 848 | SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info)); |
| 849 | |
skia.committer@gmail.com | a5b068c | 2014-05-07 03:04:15 +0000 | [diff] [blame] | 850 | SkCanvas* canvas = surface->getCanvas(); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 851 | |
| 852 | canvas->EXPERIMENTAL_optimize(pict); |
| 853 | |
| 854 | SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey(); |
| 855 | |
| 856 | const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key); |
| 857 | REPORTER_ASSERT(reporter, NULL != data); |
| 858 | |
| 859 | const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data); |
| 860 | REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers()); |
| 861 | |
| 862 | const GPUAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0); |
| 863 | // The parent/child layer appear in reverse order |
| 864 | const GPUAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2); |
| 865 | const GPUAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1); |
| 866 | const GPUAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3); |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 867 | // const GPUAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 868 | |
| 869 | REPORTER_ASSERT(reporter, info0.fValid); |
| 870 | REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight); |
| 871 | REPORTER_ASSERT(reporter, info0.fCTM.isIdentity()); |
| 872 | REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY); |
| 873 | REPORTER_ASSERT(reporter, NULL != info0.fPaint); |
| 874 | REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers); |
| 875 | |
| 876 | REPORTER_ASSERT(reporter, info1.fValid); |
| 877 | REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight); |
| 878 | REPORTER_ASSERT(reporter, info1.fCTM.isIdentity()); |
| 879 | REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY); |
| 880 | REPORTER_ASSERT(reporter, NULL != info1.fPaint); |
| 881 | REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL |
| 882 | |
| 883 | REPORTER_ASSERT(reporter, info2.fValid); |
skia.committer@gmail.com | a5b068c | 2014-05-07 03:04:15 +0000 | [diff] [blame] | 884 | REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth && |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 885 | kHeight/2 == info2.fSize.fHeight); // bound reduces size |
| 886 | REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated |
skia.committer@gmail.com | 8a1e688 | 2014-05-09 03:05:30 +0000 | [diff] [blame] | 887 | REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX && |
commit-bot@chromium.org | f97d65d | 2014-05-08 23:24:05 +0000 | [diff] [blame] | 888 | kHeight/2 == info2.fOffset.fY); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 889 | REPORTER_ASSERT(reporter, NULL != info1.fPaint); |
| 890 | REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested |
| 891 | |
| 892 | REPORTER_ASSERT(reporter, info3.fValid); |
| 893 | REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight); |
| 894 | REPORTER_ASSERT(reporter, info3.fCTM.isIdentity()); |
| 895 | REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY); |
| 896 | REPORTER_ASSERT(reporter, NULL != info3.fPaint); |
| 897 | REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers); |
| 898 | |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 899 | #if 0 // needs more though for GrGatherCanvas |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 900 | REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable |
skia.committer@gmail.com | a5b068c | 2014-05-07 03:04:15 +0000 | [diff] [blame] | 901 | REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 902 | REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY); |
| 903 | REPORTER_ASSERT(reporter, info4.fCTM.isIdentity()); |
| 904 | REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable |
| 905 | REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers); |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 906 | #endif |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 910 | #endif |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 911 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 912 | static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
| 913 | canvas->restoreToCount(1); |
| 914 | canvas->save(); |
| 915 | canvas->save(); |
| 916 | canvas->save(); |
| 917 | } |
| 918 | |
| 919 | static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 920 | SkCanvas testCanvas(100, 100); |
| 921 | set_canvas_to_save_count_4(&testCanvas); |
| 922 | |
| 923 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 924 | |
| 925 | SkPaint paint; |
| 926 | SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 927 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 928 | SkPictureRecorder recorder; |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 929 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 930 | { |
| 931 | // Create picture with 2 unbalanced saves |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 932 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 933 | canvas->save(); |
| 934 | canvas->translate(10, 10); |
| 935 | canvas->drawRect(rect, paint); |
| 936 | canvas->save(); |
| 937 | canvas->translate(10, 10); |
| 938 | canvas->drawRect(rect, paint); |
| 939 | SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording()); |
| 940 | |
| 941 | testCanvas.drawPicture(*extraSavePicture); |
| 942 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 943 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 944 | |
| 945 | set_canvas_to_save_count_4(&testCanvas); |
| 946 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 947 | { |
| 948 | // Create picture with 2 unbalanced restores |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 949 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 950 | canvas->save(); |
| 951 | canvas->translate(10, 10); |
| 952 | canvas->drawRect(rect, paint); |
| 953 | canvas->save(); |
| 954 | canvas->translate(10, 10); |
| 955 | canvas->drawRect(rect, paint); |
| 956 | canvas->restore(); |
| 957 | canvas->restore(); |
| 958 | canvas->restore(); |
| 959 | canvas->restore(); |
| 960 | SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording()); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 961 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 962 | testCanvas.drawPicture(*extraRestorePicture); |
| 963 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 964 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 965 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 966 | set_canvas_to_save_count_4(&testCanvas); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 967 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 968 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 969 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 970 | canvas->translate(10, 10); |
| 971 | canvas->drawRect(rect, paint); |
| 972 | SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording()); |
| 973 | |
| 974 | testCanvas.drawPicture(*noSavePicture); |
| 975 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 976 | REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); |
| 977 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 978 | } |
| 979 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 980 | static void test_peephole() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 981 | SkRandom rand; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 982 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 983 | SkPictureRecorder recorder; |
| 984 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 985 | for (int j = 0; j < 100; j++) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 986 | SkRandom rand2(rand); // remember the seed |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 987 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 988 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 989 | |
| 990 | for (int i = 0; i < 1000; ++i) { |
| 991 | rand_op(canvas, rand); |
| 992 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 993 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 994 | |
| 995 | rand = rand2; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 999 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1000 | SkRect rect = SkRect::MakeWH(50, 50); |
skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 1001 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1002 | for (int i = 0; i < 100; ++i) { |
| 1003 | canvas->save(); |
| 1004 | } |
| 1005 | while (canvas->getSaveCount() > 1) { |
| 1006 | canvas->clipRect(rect); |
| 1007 | canvas->restore(); |
| 1008 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1009 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1013 | #ifndef SK_DEBUG |
| 1014 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 1015 | // should never do this. |
| 1016 | static void test_bad_bitmap() { |
| 1017 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 1018 | // fail. |
| 1019 | SkBitmap bm; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1020 | bm.setConfig(SkImageInfo::MakeN32Premul(100, 100)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1021 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1022 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, NULL, 0); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1023 | recordingCanvas->drawBitmap(bm, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1024 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1025 | |
| 1026 | SkCanvas canvas; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1027 | canvas.drawPicture(*picture); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1028 | } |
| 1029 | #endif |
| 1030 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 1031 | static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1032 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1036 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1037 | SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height(), NULL, 0); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1038 | canvas->drawBitmap(bitmap, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1039 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1040 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1041 | SkDynamicMemoryWStream wStream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1042 | picture->serialize(&wStream, &encode_bitmap_to_data); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1043 | return wStream.copyToData(); |
| 1044 | } |
| 1045 | |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1046 | struct ErrorContext { |
| 1047 | int fErrors; |
| 1048 | skiatest::Reporter* fReporter; |
| 1049 | }; |
| 1050 | |
| 1051 | static void assert_one_parse_error_cb(SkError error, void* context) { |
| 1052 | ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 1053 | errorContext->fErrors++; |
| 1054 | // This test only expects one error, and that is a kParseError. If there are others, |
| 1055 | // there is some unknown problem. |
| 1056 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 1057 | "This threw more errors than expected."); |
| 1058 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, |
| 1059 | SkGetLastErrorString()); |
| 1060 | } |
| 1061 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1062 | static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 1063 | // Create a bitmap that will be encoded. |
| 1064 | SkBitmap original; |
| 1065 | make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 1066 | SkDynamicMemoryWStream wStream; |
| 1067 | if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { |
| 1068 | return; |
| 1069 | } |
| 1070 | SkAutoDataUnref data(wStream.copyToData()); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1071 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1072 | SkBitmap bm; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 1073 | bool installSuccess = SkInstallDiscardablePixelRef( |
| 1074 | SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 1075 | REPORTER_ASSERT(reporter, installSuccess); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. |
| 1078 | // Flattening original will follow the old path of performing an encode, while flattening bm |
| 1079 | // will use the already encoded data. |
| 1080 | SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 1081 | SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 1082 | REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1083 | // Now test that a parse error was generated when trying to create a new SkPicture without |
| 1084 | // providing a function to decode the bitmap. |
| 1085 | ErrorContext context; |
| 1086 | context.fErrors = 0; |
| 1087 | context.fReporter = reporter; |
| 1088 | SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 1089 | SkMemoryStream pictureStream(picture1); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1090 | SkClearLastError(); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 1091 | SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); |
| 1092 | REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1093 | SkClearLastError(); |
| 1094 | SkSetErrorCallback(NULL, NULL); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 1097 | static void test_clone_empty(skiatest::Reporter* reporter) { |
| 1098 | // This is a regression test for crbug.com/172062 |
| 1099 | // Before the fix, we used to crash accessing a null pointer when we |
| 1100 | // had a picture with no paints. This test passes by not crashing. |
| 1101 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1102 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1103 | recorder.beginRecording(1, 1, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1104 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1105 | SkAutoTUnref<SkPicture> destPicture(picture->clone()); |
junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 1106 | REPORTER_ASSERT(reporter, NULL != destPicture); |
junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1110 | static void test_draw_empty(skiatest::Reporter* reporter) { |
| 1111 | SkBitmap result; |
| 1112 | make_bm(&result, 2, 2, SK_ColorBLACK, false); |
| 1113 | |
| 1114 | SkCanvas canvas(result); |
| 1115 | |
| 1116 | { |
| 1117 | // stock SkPicture |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1118 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1119 | recorder.beginRecording(1, 1, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1120 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1121 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1122 | canvas.drawPicture(*picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | { |
| 1126 | // tile grid |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1127 | SkTileGridFactory::TileGridInfo gridInfo; |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1128 | gridInfo.fMargin.setEmpty(); |
| 1129 | gridInfo.fOffset.setZero(); |
| 1130 | gridInfo.fTileInterval.set(1, 1); |
| 1131 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1132 | SkTileGridFactory factory(gridInfo); |
| 1133 | SkPictureRecorder recorder; |
| 1134 | recorder.beginRecording(1, 1, &factory, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1135 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1136 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1137 | canvas.drawPicture(*picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | { |
| 1141 | // RTree |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1142 | SkRTreeFactory factory; |
| 1143 | SkPictureRecorder recorder; |
| 1144 | recorder.beginRecording(1, 1, &factory, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1145 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1146 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1147 | canvas.drawPicture(*picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | { |
| 1151 | // quad tree |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1152 | SkQuadTreeFactory factory; |
| 1153 | SkPictureRecorder recorder; |
| 1154 | recorder.beginRecording(1, 1, &factory, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1155 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1156 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1157 | canvas.drawPicture(*picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1161 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 1162 | // Test for crbug.com/229011 |
| 1163 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 1164 | SkIntToScalar(2), SkIntToScalar(2)); |
| 1165 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 1166 | SkIntToScalar(1), SkIntToScalar(1)); |
| 1167 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 1168 | SkIntToScalar(1), SkIntToScalar(1)); |
| 1169 | |
| 1170 | SkPath invPath; |
| 1171 | invPath.addOval(rect1); |
| 1172 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 1173 | SkPath path; |
| 1174 | path.addOval(rect2); |
| 1175 | SkPath path2; |
| 1176 | path2.addOval(rect3); |
| 1177 | SkIRect clipBounds; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1178 | SkPictureRecorder recorder; |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1179 | // Minimalist test set for 100% code coverage of |
| 1180 | // SkPictureRecord::updateClipConservativelyUsingBounds |
| 1181 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1182 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1183 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1184 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 1185 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1186 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1187 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1188 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1189 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1190 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1191 | } |
| 1192 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1193 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1194 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1195 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1196 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 1197 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1198 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1199 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1200 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1201 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1202 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1203 | } |
| 1204 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1205 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1206 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1207 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1208 | canvas->clipPath(invPath, SkRegion::kUnion_Op); |
| 1209 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1210 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1211 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1212 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1213 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1214 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1215 | } |
| 1216 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1217 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1218 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1219 | canvas->clipPath(path, SkRegion::kDifference_Op); |
| 1220 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1221 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1222 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1223 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1224 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1225 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1226 | } |
| 1227 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1228 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1229 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1230 | canvas->clipPath(path, SkRegion::kReverseDifference_Op); |
| 1231 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1232 | // True clip is actually empty in this case, but the best |
| 1233 | // determination we can make using only bounds as input is that the |
| 1234 | // clip is included in the bounds of 'path'. |
| 1235 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1236 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1237 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1238 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1239 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1240 | } |
| 1241 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1242 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1243 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1244 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1245 | canvas->clipPath(path2, SkRegion::kXOR_Op); |
| 1246 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1247 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1248 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 1249 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 1250 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1251 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1252 | } |
| 1253 | } |
| 1254 | |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1255 | /** |
| 1256 | * A canvas that records the number of clip commands. |
| 1257 | */ |
| 1258 | class ClipCountingCanvas : public SkCanvas { |
| 1259 | public: |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1260 | explicit ClipCountingCanvas(int width, int height) |
| 1261 | : INHERITED(width, height) |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1262 | , fClipCount(0){ |
| 1263 | } |
| 1264 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1265 | virtual void onClipRect(const SkRect& r, |
| 1266 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1267 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1268 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1269 | this->INHERITED::onClipRect(r, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1272 | virtual void onClipRRect(const SkRRect& rrect, |
| 1273 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1274 | ClipEdgeStyle edgeStyle)SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1275 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1276 | this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1279 | virtual void onClipPath(const SkPath& path, |
| 1280 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1281 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1282 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1283 | this->INHERITED::onClipPath(path, op, edgeStyle); |
| 1284 | } |
| 1285 | |
| 1286 | virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE { |
| 1287 | fClipCount += 1; |
| 1288 | this->INHERITED::onClipRegion(deviceRgn, op); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | unsigned getClipCount() const { return fClipCount; } |
| 1292 | |
| 1293 | private: |
| 1294 | unsigned fClipCount; |
| 1295 | |
| 1296 | typedef SkCanvas INHERITED; |
| 1297 | }; |
| 1298 | |
| 1299 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1300 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1301 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1302 | |
| 1303 | canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); |
| 1304 | // The following expanding clip should not be skipped. |
| 1305 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); |
| 1306 | // Draw something so the optimizer doesn't just fold the world. |
| 1307 | SkPaint p; |
| 1308 | p.setColor(SK_ColorBLUE); |
| 1309 | canvas->drawPaint(p); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1310 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1311 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1312 | ClipCountingCanvas testCanvas(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1313 | picture->draw(&testCanvas); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1314 | |
| 1315 | // Both clips should be present on playback. |
| 1316 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 1317 | } |
| 1318 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1319 | static void test_hierarchical(skiatest::Reporter* reporter) { |
| 1320 | SkBitmap bm; |
| 1321 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1322 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1323 | SkPictureRecorder recorder; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1324 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1325 | recorder.beginRecording(10, 10, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1326 | SkAutoTUnref<SkPicture> childPlain(recorder.endRecording()); |
| 1327 | REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1328 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1329 | recorder.beginRecording(10, 10, NULL, 0)->drawBitmap(bm, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1330 | SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording()); |
| 1331 | REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1332 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1333 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1334 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1335 | canvas->drawPicture(*childPlain); |
| 1336 | SkAutoTUnref<SkPicture> parentPP(recorder.endRecording()); |
| 1337 | REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 |
| 1338 | } |
| 1339 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1340 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1341 | canvas->drawPicture(*childWithBitmap); |
| 1342 | SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording()); |
| 1343 | REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 |
| 1344 | } |
| 1345 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1346 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1347 | canvas->drawBitmap(bm, 0, 0); |
| 1348 | canvas->drawPicture(*childPlain); |
| 1349 | SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording()); |
| 1350 | REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 |
| 1351 | } |
| 1352 | { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1353 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1354 | canvas->drawBitmap(bm, 0, 0); |
| 1355 | canvas->drawPicture(*childWithBitmap); |
| 1356 | SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording()); |
| 1357 | REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 |
| 1358 | } |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1361 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 1362 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1363 | SkPicture empty; |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1364 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1365 | // Empty pictures should still have a valid ID |
| 1366 | REPORTER_ASSERT(reporter, empty.uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1367 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1368 | SkPictureRecorder recorder; |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1369 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1370 | SkCanvas* canvas = recorder.beginRecording(1, 1, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1371 | canvas->drawARGB(255, 255, 255, 255); |
| 1372 | SkAutoTUnref<SkPicture> hasData(recorder.endRecording()); |
| 1373 | // picture should have a non-zero id after recording |
| 1374 | REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1375 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1376 | // both pictures should have different ids |
| 1377 | REPORTER_ASSERT(reporter, hasData->uniqueID() != empty.uniqueID()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1378 | |
| 1379 | // test out copy constructor |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1380 | SkPicture copyWithData(*hasData); |
| 1381 | REPORTER_ASSERT(reporter, hasData->uniqueID() == copyWithData.uniqueID()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1382 | |
| 1383 | SkPicture emptyCopy(empty); |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 1384 | REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID()); |
skia.committer@gmail.com | a915772 | 2014-04-03 03:04:26 +0000 | [diff] [blame] | 1385 | |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1386 | // test out swap |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1387 | { |
| 1388 | SkPicture swapWithData; |
| 1389 | uint32_t beforeID1 = swapWithData.uniqueID(); |
| 1390 | uint32_t beforeID2 = copyWithData.uniqueID(); |
| 1391 | swapWithData.swap(copyWithData); |
| 1392 | REPORTER_ASSERT(reporter, copyWithData.uniqueID() == beforeID1); |
| 1393 | REPORTER_ASSERT(reporter, swapWithData.uniqueID() == beforeID2); |
| 1394 | } |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1395 | |
| 1396 | // test out clone |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1397 | { |
| 1398 | SkAutoTUnref<SkPicture> cloneWithData(hasData->clone()); |
| 1399 | REPORTER_ASSERT(reporter, hasData->uniqueID() == cloneWithData->uniqueID()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1400 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1401 | SkAutoTUnref<SkPicture> emptyClone(empty.clone()); |
| 1402 | REPORTER_ASSERT(reporter, empty.uniqueID() != emptyClone->uniqueID()); |
| 1403 | } |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 1406 | DEF_TEST(Picture, reporter) { |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1407 | #ifdef SK_DEBUG |
| 1408 | test_deleting_empty_playback(); |
| 1409 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1410 | #else |
| 1411 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1412 | #endif |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1413 | test_unbalanced_save_restores(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1414 | test_peephole(); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1415 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 1416 | test_gpu_veto(reporter); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1417 | #endif |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1418 | test_gatherpixelrefs(reporter); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 1419 | test_gatherpixelrefsandrects(reporter); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1420 | test_bitmap_with_encoded_data(reporter); |
junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 1421 | test_clone_empty(reporter); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1422 | test_draw_empty(reporter); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1423 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1424 | test_clip_expansion(reporter); |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1425 | test_hierarchical(reporter); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1426 | test_gen_id(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1427 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1428 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1429 | #if SK_SUPPORT_GPU |
| 1430 | DEF_GPUTEST(GPUPicture, reporter, factory) { |
| 1431 | test_gpu_picture_optimization(reporter, factory); |
| 1432 | } |
| 1433 | #endif |
| 1434 | |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1435 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 1436 | const SkPaint paint; |
| 1437 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 1438 | const SkIRect irect = { 2, 2, 3, 3 }; |
| 1439 | |
| 1440 | // Don't care what these record, as long as they're legal. |
| 1441 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
| 1442 | canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag); |
| 1443 | canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint); |
| 1444 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
| 1445 | canvas->drawSprite(bitmap, 1, 1); |
| 1446 | } |
| 1447 | |
| 1448 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 1449 | SkBitmap empty; |
| 1450 | draw_bitmaps(empty, canvas); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1451 | empty.setConfig(SkImageInfo::MakeN32Premul(10, 10)); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1452 | draw_bitmaps(empty, canvas); |
| 1453 | } |
| 1454 | |
| 1455 | DEF_TEST(Picture_EmptyBitmap, r) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1456 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1457 | test_draw_bitmaps(recorder.beginRecording(10, 10, NULL, 0)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1458 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 1462 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1463 | dst.allocN32Pixels(10, 10); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1464 | SkCanvas canvas(dst); |
| 1465 | |
| 1466 | test_draw_bitmaps(&canvas); |
| 1467 | } |