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