| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "../src/image/SkImagePriv.h" | 8 #include "../src/image/SkImagePriv.h" |
| 9 #include "../src/image/SkSurface_Base.h" | 9 #include "../src/image/SkSurface_Base.h" |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| 11 #include "SkBitmapDevice.h" | 11 #include "SkBitmapDevice.h" |
| 12 #include "SkBitmapProcShader.h" | 12 #include "SkBitmapProcShader.h" |
| 13 #include "SkDeferredCanvas.h" | 13 #include "SkDeferredCanvas.h" |
| 14 #include "SkGradientShader.h" | 14 #include "SkGradientShader.h" |
| 15 #include "SkShader.h" | 15 #include "SkShader.h" |
| 16 #include "SkSurface.h" | 16 #include "SkSurface.h" |
| 17 #include "Test.h" | 17 #include "Test.h" |
| 18 #include "sk_tool_utils.h" | 18 #include "sk_tool_utils.h" |
| 19 | 19 |
| 20 #if SK_SUPPORT_GPU | 20 #if SK_SUPPORT_GPU |
| 21 #include "GrContextFactory.h" | 21 #include "GrContextFactory.h" |
| 22 #else | 22 #else |
| 23 class GrContextFactory; | 23 class GrContextFactory; |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 static const int gWidth = 2; | 26 static const int gWidth = 2; |
| 27 static const int gHeight = 2; | 27 static const int gHeight = 2; |
| 28 | 28 |
| 29 static void callWritePixels(SkCanvas* canvas, const SkBitmap& src, int x, int y, | |
| 30 SkCanvas::Config8888 config) { | |
| 31 SkBitmap bm(src); | |
| 32 bm.lockPixels(); | |
| 33 | |
| 34 SkImageInfo info = bm.info(); | |
| 35 sk_tool_utils::config8888_to_imagetypes(config, &info.fColorType, &info.fAlp
haType); | |
| 36 | |
| 37 if (src.isOpaque()) { | |
| 38 info.fAlphaType = kOpaque_SkAlphaType; | |
| 39 } | |
| 40 | |
| 41 canvas->writePixels(info, bm.getPixels(), bm.rowBytes(), x, y); | |
| 42 } | |
| 43 | |
| 44 static void create(SkBitmap* bm, SkColor color) { | 29 static void create(SkBitmap* bm, SkColor color) { |
| 45 bm->allocN32Pixels(gWidth, gHeight); | 30 bm->allocN32Pixels(gWidth, gHeight); |
| 46 bm->eraseColor(color); | 31 bm->eraseColor(color); |
| 47 } | 32 } |
| 48 | 33 |
| 49 static SkSurface* createSurface(SkColor color) { | 34 static SkSurface* createSurface(SkColor color) { |
| 50 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight); | 35 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight); |
| 51 surface->getCanvas()->clear(color); | 36 surface->getCanvas()->clear(color); |
| 52 return surface; | 37 return surface; |
| 53 } | 38 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 100 |
| 116 int fDiscardCount, fRetainCount; | 101 int fDiscardCount, fRetainCount; |
| 117 SkBitmap fBitmap; | 102 SkBitmap fBitmap; |
| 118 }; | 103 }; |
| 119 | 104 |
| 120 static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter)
{ | 105 static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter)
{ |
| 121 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10))); | 106 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10))); |
| 122 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); | 107 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); |
| 123 | 108 |
| 124 SkBitmap srcBitmap; | 109 SkBitmap srcBitmap; |
| 125 srcBitmap.allocN32Pixels(10, 10); | 110 srcBitmap.allocPixels(SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kUnp
remul_SkAlphaType)); |
| 126 srcBitmap.eraseColor(SK_ColorGREEN); | 111 srcBitmap.eraseColor(SK_ColorGREEN); |
| 127 // Tests below depend on this bitmap being recognized as opaque | 112 // Tests below depend on this bitmap being recognized as opaque |
| 128 | 113 |
| 129 // Preliminary sanity check: no copy on write if no active snapshot | 114 // Preliminary sanity check: no copy on write if no active snapshot |
| 130 surface->clearCounts(); | 115 surface->clearCounts(); |
| 131 canvas->clear(SK_ColorWHITE); | 116 canvas->clear(SK_ColorWHITE); |
| 132 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 117 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 133 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 118 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 134 | 119 |
| 135 surface->clearCounts(); | 120 surface->clearCounts(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 152 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 168 | 153 |
| 169 // Case 4: unpremultiplied opaque writePixels that entirely | 154 // Case 4: unpremultiplied opaque writePixels that entirely |
| 170 // covers the canvas | 155 // covers the canvas |
| 171 surface->clearCounts(); | 156 surface->clearCounts(); |
| 172 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot()); | 157 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot()); |
| 173 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 158 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 174 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 159 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 175 | 160 |
| 176 surface->clearCounts(); | 161 surface->clearCounts(); |
| 177 callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888
); | 162 canvas->writePixels(srcBitmap, 0, 0); |
| 178 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); | 163 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); |
| 179 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 164 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 180 | 165 |
| 181 surface->clearCounts(); | 166 surface->clearCounts(); |
| 182 canvas->flush(); | 167 canvas->flush(); |
| 183 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 168 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 184 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 169 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 185 | 170 |
| 186 // Case 5: unpremultiplied opaque writePixels that partially | 171 // Case 5: unpremultiplied opaque writePixels that partially |
| 187 // covers the canvas | 172 // covers the canvas |
| 188 surface->clearCounts(); | 173 surface->clearCounts(); |
| 189 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot()); | 174 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot()); |
| 190 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 175 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 191 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 176 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 192 | 177 |
| 193 surface->clearCounts(); | 178 surface->clearCounts(); |
| 194 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888
); | 179 canvas->writePixels(srcBitmap, 5, 0); |
| 195 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 180 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 196 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); | 181 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); |
| 197 | 182 |
| 198 surface->clearCounts(); | 183 surface->clearCounts(); |
| 199 canvas->flush(); | 184 canvas->flush(); |
| 200 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 185 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 201 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 186 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 202 | 187 |
| 203 // Case 6: unpremultiplied opaque writePixels that entirely | 188 // Case 6: unpremultiplied opaque writePixels that entirely |
| 204 // covers the canvas, preceded by clear | 189 // covers the canvas, preceded by clear |
| 205 surface->clearCounts(); | 190 surface->clearCounts(); |
| 206 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot()); | 191 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot()); |
| 207 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 192 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 208 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 193 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 209 | 194 |
| 210 surface->clearCounts(); | 195 surface->clearCounts(); |
| 211 canvas->clear(SK_ColorWHITE); | 196 canvas->clear(SK_ColorWHITE); |
| 212 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 197 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 213 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 198 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 214 | 199 |
| 215 surface->clearCounts(); | 200 surface->clearCounts(); |
| 216 callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888
); | 201 canvas->writePixels(srcBitmap, 0, 0); |
| 217 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); | 202 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); |
| 218 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 203 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 219 | 204 |
| 220 surface->clearCounts(); | 205 surface->clearCounts(); |
| 221 canvas->flush(); | 206 canvas->flush(); |
| 222 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 207 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 223 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 208 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 224 | 209 |
| 225 // Case 7: unpremultiplied opaque writePixels that partially | 210 // Case 7: unpremultiplied opaque writePixels that partially |
| 226 // covers the canvas, preceeded by a clear | 211 // covers the canvas, preceeded by a clear |
| 227 surface->clearCounts(); | 212 surface->clearCounts(); |
| 228 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot()); | 213 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot()); |
| 229 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 214 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 230 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 215 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 231 | 216 |
| 232 surface->clearCounts(); | 217 surface->clearCounts(); |
| 233 canvas->clear(SK_ColorWHITE); | 218 canvas->clear(SK_ColorWHITE); |
| 234 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 219 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 235 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 220 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 236 | 221 |
| 237 surface->clearCounts(); | 222 surface->clearCounts(); |
| 238 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888
); | 223 canvas->writePixels(srcBitmap, 5, 0); |
| 239 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the cl
ear | 224 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the cl
ear |
| 240 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 225 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 241 | 226 |
| 242 surface->clearCounts(); | 227 surface->clearCounts(); |
| 243 canvas->flush(); | 228 canvas->flush(); |
| 244 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 229 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 245 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 230 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 246 | 231 |
| 247 // Case 8: unpremultiplied opaque writePixels that partially | 232 // Case 8: unpremultiplied opaque writePixels that partially |
| 248 // covers the canvas, preceeded by a drawREct that partially | 233 // covers the canvas, preceeded by a drawREct that partially |
| 249 // covers the canvas | 234 // covers the canvas |
| 250 surface->clearCounts(); | 235 surface->clearCounts(); |
| 251 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot()); | 236 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot()); |
| 252 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 237 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 253 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 238 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 254 | 239 |
| 255 surface->clearCounts(); | 240 surface->clearCounts(); |
| 256 SkPaint paint; | 241 SkPaint paint; |
| 257 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint); | 242 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint); |
| 258 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 243 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 259 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 244 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 260 | 245 |
| 261 surface->clearCounts(); | 246 surface->clearCounts(); |
| 262 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888
); | 247 canvas->writePixels(srcBitmap, 5, 0); |
| 263 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 248 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 264 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); | 249 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); |
| 265 | 250 |
| 266 surface->clearCounts(); | 251 surface->clearCounts(); |
| 267 canvas->flush(); | 252 canvas->flush(); |
| 268 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 253 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 269 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 254 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 270 } | 255 } |
| 271 | 256 |
| 272 static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) { | 257 static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) { |
| (...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 TestDeferredCanvasBitmapSizeThreshold(reporter); | 814 TestDeferredCanvasBitmapSizeThreshold(reporter); |
| 830 TestDeferredCanvasCreateCompatibleDevice(reporter); | 815 TestDeferredCanvasCreateCompatibleDevice(reporter); |
| 831 TestDeferredCanvasWritePixelsToSurface(reporter); | 816 TestDeferredCanvasWritePixelsToSurface(reporter); |
| 832 TestDeferredCanvasSurface(reporter, NULL); | 817 TestDeferredCanvasSurface(reporter, NULL); |
| 833 TestDeferredCanvasSetSurface(reporter, NULL); | 818 TestDeferredCanvasSetSurface(reporter, NULL); |
| 834 if (NULL != factory) { | 819 if (NULL != factory) { |
| 835 TestDeferredCanvasSurface(reporter, factory); | 820 TestDeferredCanvasSurface(reporter, factory); |
| 836 TestDeferredCanvasSetSurface(reporter, factory); | 821 TestDeferredCanvasSetSurface(reporter, factory); |
| 837 } | 822 } |
| 838 } | 823 } |
| OLD | NEW |