| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkCanvas.h" | 8 #include "SkCanvas.h" |
| 9 #include "SkData.h" | 9 #include "SkData.h" |
| 10 #include "SkDevice.h" | 10 #include "SkDevice.h" |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 SkData* fData; | 216 SkData* fData; |
| 217 | 217 |
| 218 static void Release(const void* pixels, void* context) { | 218 static void Release(const void* pixels, void* context) { |
| 219 ReleaseDataContext* state = (ReleaseDataContext*)context; | 219 ReleaseDataContext* state = (ReleaseDataContext*)context; |
| 220 REPORTER_ASSERT(state->fReporter, state->fData); | 220 REPORTER_ASSERT(state->fReporter, state->fData); |
| 221 state->fData->unref(); | 221 state->fData->unref(); |
| 222 state->fData = NULL; | 222 state->fData = NULL; |
| 223 } | 223 } |
| 224 }; | 224 }; |
| 225 | 225 |
| 226 static SkImage* create_image(ImageType imageType, GrContext* context, SkColor co
lor, | 226 static void test_texture_handle(skiatest::Reporter* reporter, SkSurface* surf) { |
| 227 SkAutoTUnref<SkImage> image0(surf->newImageSnapshot()); |
| 228 GrBackendObject obj = surf->getTextureHandle(SkSurface::kFlushRead_TextureHa
ndleAccess); |
| 229 REPORTER_ASSERT(reporter, obj != 0); |
| 230 SkAutoTUnref<SkImage> image1(surf->newImageSnapshot()); |
| 231 // just read access should not affect the snapshot |
| 232 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID()); |
| 233 |
| 234 obj = surf->getTextureHandle(SkSurface::kFlushWrite_TextureHandleAccess); |
| 235 REPORTER_ASSERT(reporter, obj != 0); |
| 236 SkAutoTUnref<SkImage> image2(surf->newImageSnapshot()); |
| 237 // expect a new image, since we claimed we would write |
| 238 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID()); |
| 239 |
| 240 obj = surf->getTextureHandle(SkSurface::kDiscardWrite_TextureHandleAccess); |
| 241 REPORTER_ASSERT(reporter, obj != 0); |
| 242 SkAutoTUnref<SkImage> image3(surf->newImageSnapshot()); |
| 243 // expect a new(er) image, since we claimed we would write |
| 244 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID()); |
| 245 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID()); |
| 246 } |
| 247 |
| 248 static SkImage* create_image(skiatest::Reporter* reporter, |
| 249 ImageType imageType, GrContext* context, SkColor co
lor, |
| 227 ReleaseDataContext* releaseContext) { | 250 ReleaseDataContext* releaseContext) { |
| 228 const SkPMColor pmcolor = SkPreMultiplyColor(color); | 251 const SkPMColor pmcolor = SkPreMultiplyColor(color); |
| 229 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); | 252 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 230 const size_t rowBytes = info.minRowBytes(); | 253 const size_t rowBytes = info.minRowBytes(); |
| 231 const size_t size = rowBytes * info.height(); | 254 const size_t size = rowBytes * info.height(); |
| 232 | 255 |
| 233 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); | 256 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); |
| 234 void* addr = data->writable_data(); | 257 void* addr = data->writable_data(); |
| 235 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2)); | 258 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2)); |
| 236 | 259 |
| 237 switch (imageType) { | 260 switch (imageType) { |
| 238 case kRasterCopy_ImageType: | 261 case kRasterCopy_ImageType: |
| 239 return SkImage::NewRasterCopy(info, addr, rowBytes); | 262 return SkImage::NewRasterCopy(info, addr, rowBytes); |
| 240 case kRasterData_ImageType: | 263 case kRasterData_ImageType: |
| 241 return SkImage::NewRasterData(info, data, rowBytes); | 264 return SkImage::NewRasterData(info, data, rowBytes); |
| 242 case kRasterProc_ImageType: | 265 case kRasterProc_ImageType: |
| 243 SkASSERT(releaseContext); | 266 SkASSERT(releaseContext); |
| 244 releaseContext->fData = SkRef(data.get()); | 267 releaseContext->fData = SkRef(data.get()); |
| 245 return SkImage::NewFromRaster(info, addr, rowBytes, | 268 return SkImage::NewFromRaster(info, addr, rowBytes, |
| 246 ReleaseDataContext::Release, releaseCo
ntext); | 269 ReleaseDataContext::Release, releaseCo
ntext); |
| 247 case kGpu_ImageType: { | 270 case kGpu_ImageType: { |
| 248 SkAutoTUnref<SkSurface> surf( | 271 SkAutoTUnref<SkSurface> surf( |
| 249 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, inf
o, 0)); | 272 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, inf
o, 0)); |
| 250 surf->getCanvas()->clear(color); | 273 surf->getCanvas()->clear(color); |
| 274 // test our backing texture while were here... |
| 275 test_texture_handle(reporter, surf); |
| 276 // redraw so our returned image looks as expected. |
| 277 surf->getCanvas()->clear(color); |
| 251 return surf->newImageSnapshot(); | 278 return surf->newImageSnapshot(); |
| 252 } | 279 } |
| 253 case kCodec_ImageType: { | 280 case kCodec_ImageType: { |
| 254 SkBitmap bitmap; | 281 SkBitmap bitmap; |
| 255 bitmap.installPixels(info, addr, rowBytes); | 282 bitmap.installPixels(info, addr, rowBytes); |
| 256 SkAutoTUnref<SkData> src( | 283 SkAutoTUnref<SkData> src( |
| 257 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 1
00)); | 284 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 1
00)); |
| 258 return SkImage::NewFromEncoded(src); | 285 return SkImage::NewFromEncoded(src); |
| 259 } | 286 } |
| 260 } | 287 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 #endif | 371 #endif |
| 345 | 372 |
| 346 ReleaseDataContext releaseCtx; | 373 ReleaseDataContext releaseCtx; |
| 347 releaseCtx.fReporter = reporter; | 374 releaseCtx.fReporter = reporter; |
| 348 | 375 |
| 349 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { | 376 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 350 SkImageInfo info; | 377 SkImageInfo info; |
| 351 size_t rowBytes; | 378 size_t rowBytes; |
| 352 | 379 |
| 353 releaseCtx.fData = NULL; | 380 releaseCtx.fData = NULL; |
| 354 SkAutoTUnref<SkImage> image(create_image(gRec[i].fType, ctx, color, &rel
easeCtx)); | 381 SkAutoTUnref<SkImage> image(create_image(reporter, gRec[i].fType, ctx, c
olor, &releaseCtx)); |
| 355 if (!image.get()) { | 382 if (!image.get()) { |
| 356 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName); | 383 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName); |
| 357 continue; // gpu may not be enabled | 384 continue; // gpu may not be enabled |
| 358 } | 385 } |
| 359 if (kRasterProc_ImageType == gRec[i].fType) { | 386 if (kRasterProc_ImageType == gRec[i].fType) { |
| 360 REPORTER_ASSERT(reporter, NULL != releaseCtx.fData); // we are trac
king the data | 387 REPORTER_ASSERT(reporter, NULL != releaseCtx.fData); // we are trac
king the data |
| 361 } else { | 388 } else { |
| 362 REPORTER_ASSERT(reporter, NULL == releaseCtx.fData); // we ignored
the context | 389 REPORTER_ASSERT(reporter, NULL == releaseCtx.fData); // we ignored
the context |
| 363 } | 390 } |
| 364 | 391 |
| (...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 891 // We expect the ref'd image to see the new color, but cpy'd one should stil
l see the old color | 918 // We expect the ref'd image to see the new color, but cpy'd one should stil
l see the old color |
| 892 test_image_color(reporter, refImg, expected1); | 919 test_image_color(reporter, refImg, expected1); |
| 893 test_image_color(reporter, cpyImg, expected0); | 920 test_image_color(reporter, cpyImg, expected0); |
| 894 | 921 |
| 895 // Now exercise the release proc | 922 // Now exercise the release proc |
| 896 REPORTER_ASSERT(reporter, !releaseCtx.fIsReleased); | 923 REPORTER_ASSERT(reporter, !releaseCtx.fIsReleased); |
| 897 refImg.reset(NULL); // force a release of the image | 924 refImg.reset(NULL); // force a release of the image |
| 898 REPORTER_ASSERT(reporter, releaseCtx.fIsReleased); | 925 REPORTER_ASSERT(reporter, releaseCtx.fIsReleased); |
| 899 } | 926 } |
| 900 #endif | 927 #endif |
| OLD | NEW |