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