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