Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: tests/DrawBitmapRectTest.cpp

Issue 83663006: Modify SkLazyPixelRef to use SkImageGenerator. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: reupload Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "Test.h" 8 #include "Test.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkData.h" 11 #include "SkData.h"
12 #include "SkPaint.h" 12 #include "SkPaint.h"
13 #include "SkShader.h" 13 #include "SkShader.h"
14 #include "SkSurface.h" 14 #include "SkSurface.h"
15 #include "SkRandom.h" 15 #include "SkRandom.h"
16 #include "SkMatrixUtils.h" 16 #include "SkMatrixUtils.h"
17 17
18 #include "SkLazyPixelRef.h" 18 #include "SkLazyPixelRef.h"
19 #include "SkLruImageCache.h" 19 #include "SkLruImageCache.h"
20 20
21 // A BitmapFactory that always fails when asked to return pixels. 21
22 static bool FailureDecoder(const void* data, size_t length, SkImageInfo* info, 22 namespace {
23 const SkBitmapFactory::Target* target) { 23 // A SkImageGenerator that always fails when asked to return pixels.
24 if (info) { 24 class FailureImageGenerator : public SkImageGenerator {
25 info->fWidth = info->fHeight = 100; 25 public:
26 info->fColorType = kRGBA_8888_SkColorType; 26 FailureImageGenerator() { }
27 info->fAlphaType = kPremul_SkAlphaType; 27 virtual ~FailureImageGenerator() { }
28 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE {
29 if (info) {
30 info->fWidth = info->fHeight = 100;
31 info->fColorType = kRGBA_8888_SkColorType;
32 info->fAlphaType = kPremul_SkAlphaType;
33 }
34 return true;
28 } 35 }
29 // this will deliberately return false if they are asking us to decode 36 virtual bool getPixels(const SkImageInfo&, void*, size_t) SK_OVERRIDE {
30 // into pixels. 37 return false;
31 return NULL == target; 38 }
32 } 39 };
40 } // namespace
33 41
34 // crbug.com/295895 42 // crbug.com/295895
35 // Crashing in skia when a pixelref fails in lockPixels 43 // Crashing in skia when a pixelref fails in lockPixels
36 // 44 //
37 static void test_faulty_pixelref(skiatest::Reporter* reporter) { 45 static void test_faulty_pixelref(skiatest::Reporter* reporter) {
38 // need a cache, but don't expect to use it, so the budget is not critical 46 // need a cache, but don't expect to use it, so the budget is not critical
39 SkLruImageCache cache(10 * 1000); 47 SkLruImageCache cache(10 * 1000);
40 // construct a garbage data represent "bad" encoded data. 48 // construct a garbage data represent "bad" encoded data.
41 SkAutoDataUnref data(SkData::NewFromMalloc(malloc(1000), 1000)); 49 SkAutoDataUnref data(SkData::NewFromMalloc(malloc(1000), 1000));
42 SkAutoTUnref<SkPixelRef> pr(new SkLazyPixelRef(data, FailureDecoder, &cache) );
43 50
51 FailureImageGenerator* gen(SkNEW(FailureImageGenerator));
44 SkBitmap bm; 52 SkBitmap bm;
45 bm.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); 53 bool lazyPixelRefSuccess = SkLazyPixelRef::Install(gen, &cache, &bm);
46 bm.setPixelRef(pr); 54 REPORTER_ASSERT(reporter, lazyPixelRefSuccess);
47 // now our bitmap has a pixelref, but we know it will fail to lock 55 // now our bitmap has a pixelref, but we know it will fail to lock
48 56
49 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(200, 200)); 57 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(200, 200));
50 SkCanvas* canvas = surface->getCanvas(); 58 SkCanvas* canvas = surface->getCanvas();
51 59
52 const SkPaint::FilterLevel levels[] = { 60 const SkPaint::FilterLevel levels[] = {
53 SkPaint::kNone_FilterLevel, 61 SkPaint::kNone_FilterLevel,
54 SkPaint::kLow_FilterLevel, 62 SkPaint::kLow_FilterLevel,
55 SkPaint::kMedium_FilterLevel, 63 SkPaint::kMedium_FilterLevel,
56 SkPaint::kHigh_FilterLevel, 64 SkPaint::kHigh_FilterLevel,
57 }; 65 };
58 66
59 SkPaint paint; 67 SkPaint paint;
60 canvas->scale(2, 2); // need a scale, otherwise we may ignore filtering 68 canvas->scale(2, 2); // need a scale, otherwise we may ignore filtering
61 for (size_t i = 0; i < SK_ARRAY_COUNT(levels); ++i) { 69 for (size_t i = 0; i < SK_ARRAY_COUNT(levels); ++i) {
62 paint.setFilterLevel(levels[i]); 70 paint.setFilterLevel(levels[i]);
63 canvas->drawBitmap(bm, 0, 0, &paint); 71 canvas->drawBitmap(bm, 0, 0, &paint);
64 } 72 }
65 } 73 }
66 74
67 /////////////////////////////////////////////////////////////////////////////// 75 ///////////////////////////////////////////////////////////////////////////////
68 76
69 static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) { 77 static void rand_matrix(SkMatrix* mat, SkRandom* rand, unsigned mask) {
70 mat->setIdentity(); 78 mat->setIdentity();
71 if (mask & SkMatrix::kTranslate_Mask) { 79 if (mask & SkMatrix::kTranslate_Mask) {
72 mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1()); 80 mat->postTranslate(rand->nextSScalar1(), rand->nextSScalar1());
73 } 81 }
74 if (mask & SkMatrix::kScale_Mask) { 82 if (mask & SkMatrix::kScale_Mask) {
75 mat->postScale(rand.nextSScalar1(), rand.nextSScalar1()); 83 mat->postScale(rand->nextSScalar1(), rand->nextSScalar1());
76 } 84 }
77 if (mask & SkMatrix::kAffine_Mask) { 85 if (mask & SkMatrix::kAffine_Mask) {
78 mat->postRotate(rand.nextSScalar1() * 360); 86 mat->postRotate(rand->nextSScalar1() * 360);
79 } 87 }
80 if (mask & SkMatrix::kPerspective_Mask) { 88 if (mask & SkMatrix::kPerspective_Mask) {
81 mat->setPerspX(rand.nextSScalar1()); 89 mat->setPerspX(rand->nextSScalar1());
82 mat->setPerspY(rand.nextSScalar1()); 90 mat->setPerspY(rand->nextSScalar1());
83 } 91 }
84 } 92 }
85 93
86 static void rand_size(SkISize* size, SkRandom& rand) { 94 static void rand_size(SkISize* size, SkRandom* rand) {
87 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF); 95 size->set(rand->nextU() & 0xFFFF, rand->nextU() & 0xFFFF);
88 } 96 }
89 97
90 static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size, 98 static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size,
91 unsigned bits) { 99 unsigned bits) {
92 return SkTreatAsSprite(mat, size.width(), size.height(), bits); 100 return SkTreatAsSprite(mat, size.width(), size.height(), bits);
93 } 101 }
94 102
95 static void test_treatAsSprite(skiatest::Reporter* reporter) { 103 static void test_treatAsSprite(skiatest::Reporter* reporter) {
96 const unsigned bilerBits = kSkSubPixelBitsForBilerp; 104 const unsigned bilerBits = kSkSubPixelBitsForBilerp;
97 105
98 SkMatrix mat; 106 SkMatrix mat;
99 SkISize size; 107 SkISize size;
100 SkRandom rand; 108 SkRandom rand;
101 109
102 // assert: translate-only no-filter can always be treated as sprite 110 // assert: translate-only no-filter can always be treated as sprite
103 for (int i = 0; i < 1000; ++i) { 111 for (int i = 0; i < 1000; ++i) {
104 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask); 112 rand_matrix(&mat, &rand, SkMatrix::kTranslate_Mask);
105 for (int j = 0; j < 1000; ++j) { 113 for (int j = 0; j < 1000; ++j) {
106 rand_size(&size, rand); 114 rand_size(&size, &rand);
107 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0)); 115 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0));
108 } 116 }
109 } 117 }
110 118
111 // assert: rotate/perspect is never treated as sprite 119 // assert: rotate/perspect is never treated as sprite
112 for (int i = 0; i < 1000; ++i) { 120 for (int i = 0; i < 1000; ++i) {
113 rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_ Mask); 121 rand_matrix(&mat, &rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective _Mask);
114 for (int j = 0; j < 1000; ++j) { 122 for (int j = 0; j < 1000; ++j) {
115 rand_size(&size, rand); 123 rand_size(&size, &rand);
116 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, 0)); 124 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, 0));
117 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); 125 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
118 } 126 }
119 } 127 }
120 128
121 size.set(500, 600); 129 size.set(500, 600);
122 130
123 const SkScalar tooMuchSubpixel = SkFloatToScalar(100.1f); 131 const SkScalar tooMuchSubpixel = SkFloatToScalar(100.1f);
124 mat.setTranslate(tooMuchSubpixel, 0); 132 mat.setTranslate(tooMuchSubpixel, 0);
125 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); 133 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 328
321 test_nan_antihair(); 329 test_nan_antihair();
322 test_giantrepeat_crbug118018(reporter); 330 test_giantrepeat_crbug118018(reporter);
323 331
324 test_treatAsSprite(reporter); 332 test_treatAsSprite(reporter);
325 test_faulty_pixelref(reporter); 333 test_faulty_pixelref(reporter);
326 } 334 }
327 335
328 #include "TestClassDef.h" 336 #include "TestClassDef.h"
329 DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect) 337 DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect)
OLDNEW
« src/lazy/SkLazyPixelRef.h ('K') | « tests/CachedDecodingPixelRefTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698