| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkImage.h" | 10 #include "SkImage.h" |
| 11 #include "SkPixmap.h" |
| 11 #include "SkSpecialImage.h" | 12 #include "SkSpecialImage.h" |
| 12 #include "SkSpecialSurface.h" | 13 #include "SkSpecialSurface.h" |
| 13 #include "Test.h" | 14 #include "Test.h" |
| 14 #include "TestingSpecialImageAccess.h" | 15 #include "TestingSpecialImageAccess.h" |
| 15 | 16 |
| 16 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
| 17 #include "GrContext.h" | 18 #include "GrContext.h" |
| 18 #endif | 19 #endif |
| 19 | 20 |
| 20 | 21 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 41 | 42 |
| 42 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), | 43 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), |
| 43 SkIntToScalar(kSmallerSize), SkIntToScalar(kS
mallerSize)), | 44 SkIntToScalar(kSmallerSize), SkIntToScalar(kS
mallerSize)), |
| 44 p); | 45 p); |
| 45 | 46 |
| 46 return bm; | 47 return bm; |
| 47 } | 48 } |
| 48 | 49 |
| 49 // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels &
draw) | 50 // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels &
draw) |
| 50 static void test_image(SkSpecialImage* img, skiatest::Reporter* reporter, | 51 static void test_image(SkSpecialImage* img, skiatest::Reporter* reporter, |
| 51 bool peekPixelsSucceeds, bool peekTextureSucceeds) { | 52 bool peekPixelsSucceeds, bool peekTextureSucceeds, |
| 53 int offset, int size) { |
| 52 const SkIRect subset = TestingSpecialImageAccess::Subset(img); | 54 const SkIRect subset = TestingSpecialImageAccess::Subset(img); |
| 53 REPORTER_ASSERT(reporter, kPad == subset.left()); | 55 REPORTER_ASSERT(reporter, offset == subset.left()); |
| 54 REPORTER_ASSERT(reporter, kPad == subset.top()); | 56 REPORTER_ASSERT(reporter, offset == subset.top()); |
| 55 REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); | 57 REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); |
| 56 REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); | 58 REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); |
| 57 | 59 |
| 58 //-------------- | 60 //-------------- |
| 59 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!TestingSpecialImageAccess
::PeekTexture(img)); | 61 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!TestingSpecialImageAccess
::PeekTexture(img)); |
| 60 | 62 |
| 61 //-------------- | 63 //-------------- |
| 62 SkPixmap pixmap; | 64 SkPixmap pixmap; |
| 63 REPORTER_ASSERT(reporter, peekPixelsSucceeds == | 65 REPORTER_ASSERT(reporter, peekPixelsSucceeds == |
| 64 !!TestingSpecialImageAccess::PeekPixels(img, &pixm
ap)); | 66 !!TestingSpecialImageAccess::PeekPixels(img, &pixm
ap)); |
| 65 if (peekPixelsSucceeds) { | 67 if (peekPixelsSucceeds) { |
| 66 REPORTER_ASSERT(reporter, kFullSize == pixmap.width()); | 68 REPORTER_ASSERT(reporter, size == pixmap.width()); |
| 67 REPORTER_ASSERT(reporter, kFullSize == pixmap.height()); | 69 REPORTER_ASSERT(reporter, size == pixmap.height()); |
| 68 } | 70 } |
| 69 | 71 |
| 70 //-------------- | 72 //-------------- |
| 71 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlph
aType); | 73 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlph
aType); |
| 72 | 74 |
| 73 SkAutoTUnref<SkSpecialSurface> surf(img->newSurface(info)); | 75 SkAutoTUnref<SkSpecialSurface> surf(img->newSurface(info)); |
| 74 | 76 |
| 75 SkCanvas* canvas = surf->getCanvas(); | 77 SkCanvas* canvas = surf->getCanvas(); |
| 76 | 78 |
| 77 canvas->clear(SK_ColorBLUE); | 79 canvas->clear(SK_ColorBLUE); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 88 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); | 90 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); |
| 89 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, | 91 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, |
| 90 kSmallerSize+kPad-1)); | 92 kSmallerSize+kPad-1)); |
| 91 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, | 93 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, |
| 92 kSmallerSize+kPad)); | 94 kSmallerSize+kPad)); |
| 93 } | 95 } |
| 94 | 96 |
| 95 DEF_TEST(SpecialImage_Raster, reporter) { | 97 DEF_TEST(SpecialImage_Raster, reporter) { |
| 96 SkBitmap bm = create_bm(); | 98 SkBitmap bm = create_bm(); |
| 97 | 99 |
| 100 SkAutoTUnref<SkSpecialImage> fullSImage(SkSpecialImage::NewFromRaster( |
| 101 nullptr, |
| 102 SkIRect::MakeWH(kFul
lSize, kFullSize), |
| 103 bm)); |
| 104 |
| 98 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 105 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 99 | 106 |
| 100 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromRaster(nullptr, subs
et, bm)); | 107 { |
| 101 test_image(img, reporter, true, false); | 108 SkAutoTUnref<SkSpecialImage> subSImg1(SkSpecialImage::NewFromRaster(null
ptr, subset, bm)); |
| 109 test_image(subSImg1, reporter, true, false, kPad, kFullSize); |
| 110 } |
| 111 |
| 112 { |
| 113 SkAutoTUnref<SkSpecialImage> subSImg2(fullSImage->extractSubset(subset))
; |
| 114 test_image(subSImg2, reporter, true, false, 0, kSmallerSize); |
| 115 } |
| 102 } | 116 } |
| 103 | 117 |
| 104 DEF_TEST(SpecialImage_Image, reporter) { | 118 DEF_TEST(SpecialImage_Image, reporter) { |
| 105 SkBitmap bm = create_bm(); | 119 SkBitmap bm = create_bm(); |
| 106 | 120 |
| 107 SkAutoTUnref<SkImage> fullImage(SkImage::NewFromBitmap(bm)); | 121 SkAutoTUnref<SkImage> fullImage(SkImage::NewFromBitmap(bm)); |
| 108 | 122 |
| 123 SkAutoTUnref<SkSpecialImage> fullSImage(SkSpecialImage::NewFromImage( |
| 124 nullptr, |
| 125 SkIRect::MakeWH(kFul
lSize, kFullSize), |
| 126 fullImage)); |
| 127 |
| 109 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 128 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 110 | 129 |
| 111 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromImage(nullptr, subse
t, fullImage)); | 130 { |
| 112 test_image(img, reporter, true, false); | 131 SkAutoTUnref<SkSpecialImage> subSImg1(SkSpecialImage::NewFromImage(nullp
tr, |
| 132 subse
t, |
| 133 fullI
mage)); |
| 134 test_image(subSImg1, reporter, true, false, kPad, kFullSize); |
| 135 } |
| 136 |
| 137 { |
| 138 SkAutoTUnref<SkSpecialImage> subSImg2(fullSImage->extractSubset(subset))
; |
| 139 test_image(subSImg2, reporter, true, false, 0, kSmallerSize); |
| 140 } |
| 113 } | 141 } |
| 114 | 142 |
| 143 DEF_TEST(SpecialImage_Pixmap, reporter) { |
| 144 SkAutoPixmapStorage pixmap; |
| 145 |
| 146 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_
SkAlphaType); |
| 147 pixmap.alloc(info); |
| 148 pixmap.erase(SK_ColorGREEN); |
| 149 |
| 150 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 151 |
| 152 pixmap.erase(SK_ColorRED, subset); |
| 153 |
| 154 { |
| 155 // The SkAutoPixmapStorage keeps hold of the memory |
| 156 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromPixmap(nullptr,
subset, pixmap, |
| 157 nullptr,
nullptr)); |
| 158 test_image(img, reporter, true, false, kPad, kFullSize); |
| 159 } |
| 160 |
| 161 { |
| 162 // The image takes ownership of the memory |
| 163 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromPixmap( |
| 164 nullptr, subset, pixmap, |
| 165 [] (void* addr, void*) -> void {
sk_free(addr); }, |
| 166 nullptr)); |
| 167 pixmap.release(); |
| 168 test_image(img, reporter, true, false, kPad, kFullSize); |
| 169 } |
| 170 } |
| 171 |
| 172 |
| 115 #if SK_SUPPORT_GPU | 173 #if SK_SUPPORT_GPU |
| 116 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) { | 174 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) { |
| 117 SkBitmap bm = create_bm(); | 175 SkBitmap bm = create_bm(); |
| 118 | 176 |
| 119 GrSurfaceDesc desc; | 177 GrSurfaceDesc desc; |
| 120 desc.fConfig = kSkia8888_GrPixelConfig; | 178 desc.fConfig = kSkia8888_GrPixelConfig; |
| 121 desc.fFlags = kNone_GrSurfaceFlags; | 179 desc.fFlags = kNone_GrSurfaceFlags; |
| 122 desc.fWidth = kFullSize; | 180 desc.fWidth = kFullSize; |
| 123 desc.fHeight = kFullSize; | 181 desc.fHeight = kFullSize; |
| 124 | 182 |
| 125 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(de
sc, SkBudgeted::kNo, | 183 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(de
sc, SkBudgeted::kNo, |
| 126 bm
.getPixels(), 0)); | 184 bm
.getPixels(), 0)); |
| 127 if (!texture) { | 185 if (!texture) { |
| 128 return; | 186 return; |
| 129 } | 187 } |
| 130 | 188 |
| 189 SkAutoTUnref<SkSpecialImage> fullSImg(SkSpecialImage::NewFromGpu( |
| 190 nullptr, |
| 191 SkIRect::MakeWH(kFul
lSize, kFullSize), |
| 192 kNeedNewImageUniqueI
D_SpecialImage, |
| 193 texture)); |
| 194 |
| 131 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 195 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 132 | 196 |
| 133 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromGpu(nullptr, subset,
| 197 { |
| 134 kNeedNewImageUni
queID_SpecialImage, | 198 SkAutoTUnref<SkSpecialImage> subSImg1(SkSpecialImage::NewFromGpu( |
| 135 texture)); | 199 nullptr, subset, |
| 136 test_image(img, reporter, false, true); | 200 kNeedNewImageUniq
ueID_SpecialImage, |
| 201 texture)); |
| 202 test_image(subSImg1, reporter, false, true, kPad, kFullSize); |
| 203 } |
| 204 |
| 205 { |
| 206 SkAutoTUnref<SkSpecialImage> subSImg2(fullSImg->extractSubset(subset)); |
| 207 test_image(subSImg2, reporter, false, true, kPad, kFullSize); |
| 208 } |
| 137 } | 209 } |
| 138 | 210 |
| 139 #endif | 211 #endif |
| OLD | NEW |