halcanary | e9d55c5 | 2016-08-09 17:46:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "gm.h" |
| 10 | #include "GrContext.h" |
| 11 | #include "Resources.h" |
| 12 | #include "SkImage.h" |
| 13 | |
| 14 | #if SK_SUPPORT_GPU |
| 15 | |
| 16 | // Helper function that uploads the given SkImage using MakdeFromDeferredTextureImageData and then |
| 17 | // draws the uploaded version at the specified coordinates. |
| 18 | static bool DrawDeferredTextureImageData(GrContext* context, SkCanvas* canvas, SkImage* image, |
| 19 | SkImage::DeferredTextureImageUsageParams* params, |
| 20 | SkScalar x, SkScalar y) { |
| 21 | SkAutoTUnref<GrContextThreadSafeProxy> proxy(context->threadSafeProxy()); |
| 22 | size_t deferredSize = image->getDeferredTextureImageData(*proxy, params, 1, nullptr); |
| 23 | if (deferredSize == 0) { |
| 24 | SkDebugf("\nCould not create DeferredTextureImageData.\n"); |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | std::vector<uint8_t> memory; |
| 29 | memory.resize(deferredSize); |
| 30 | image->getDeferredTextureImageData(*proxy, params, 1, memory.data()); |
| 31 | sk_sp<SkImage> uploadedImage = |
| 32 | SkImage::MakeFromDeferredTextureImageData(context, memory.data(), SkBudgeted::kNo); |
| 33 | canvas->drawImage(uploadedImage, x, y); |
| 34 | |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | DEF_SIMPLE_GM(deferred_texture_image_data, canvas, 60, 10) { |
| 39 | GrContext* context = canvas->getGrContext(); |
| 40 | if (!context) { |
| 41 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | sk_sp<SkImage> encodedImage = GetResourceAsImage("randPixels.png"); |
| 46 | if (!encodedImage) { |
| 47 | SkDebugf("\nCould not load resource.\n"); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | SkBitmap bitmap; |
| 52 | if (!GetResourceAsBitmap("randPixels.png", &bitmap)) { |
| 53 | SkDebugf("\nCould not decode resource.\n"); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | sk_sp<SkImage> decodedImage = SkImage::MakeFromBitmap(bitmap); |
| 58 | |
| 59 | // Draw both encoded and decoded image via deferredTextureImageData. |
| 60 | SkImage::DeferredTextureImageUsageParams params; |
| 61 | DrawDeferredTextureImageData(context, canvas, encodedImage.get(), ¶ms, 0, 0); |
| 62 | DrawDeferredTextureImageData(context, canvas, decodedImage.get(), ¶ms, 10, 0); |
| 63 | |
| 64 | // Draw 50% scaled versions of the encoded and decoded images at medium quality. |
| 65 | SkImage::DeferredTextureImageUsageParams mediumScaledParams; |
| 66 | mediumScaledParams.fPreScaleMipLevel = 1; |
| 67 | mediumScaledParams.fQuality = kMedium_SkFilterQuality; |
| 68 | |
| 69 | DrawDeferredTextureImageData(context, canvas, encodedImage.get(), &mediumScaledParams, 20, 0); |
| 70 | DrawDeferredTextureImageData(context, canvas, decodedImage.get(), &mediumScaledParams, 30, 0); |
| 71 | |
| 72 | // Draw 50% scaled versions of the encoded and decoded images at none quality. |
| 73 | SkImage::DeferredTextureImageUsageParams noneScaledParams; |
| 74 | noneScaledParams.fPreScaleMipLevel = 1; |
| 75 | noneScaledParams.fQuality = kNone_SkFilterQuality; |
| 76 | |
| 77 | DrawDeferredTextureImageData(context, canvas, encodedImage.get(), &noneScaledParams, 40, 0); |
| 78 | DrawDeferredTextureImageData(context, canvas, decodedImage.get(), &noneScaledParams, 50, 0); |
| 79 | } |
| 80 | |
| 81 | #endif |