cblume | 33e0cb5 | 2016-08-30 12:09:23 -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" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 10 | #include "SkImage.h" |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 11 | #include "SkMipMap.h" |
| 12 | #include "Resources.h" |
| 13 | |
| 14 | #if SK_SUPPORT_GPU |
brianosman | 1dfec74 | 2016-09-15 07:19:53 -0700 | [diff] [blame] | 15 | #include "GrContext.h" |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 16 | |
Robert Phillips | b34727f | 2017-02-10 14:44:58 -0500 | [diff] [blame] | 17 | // Helper function that uploads the given SkImage using MakeFromDeferredTextureImageData and then |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 18 | // draws the uploaded version at the specified coordinates. |
| 19 | static void DrawDeferredTextureImageData(SkCanvas* canvas, |
| 20 | SkImage::DeferredTextureImageUsageParams* params) { |
| 21 | GrContext* context = canvas->getGrContext(); |
| 22 | if (!context) { |
| 23 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 24 | return; |
| 25 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 26 | sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | |
| 30 | sk_sp<SkImage> encodedImage = GetResourceAsImage("mandrill_512.png"); |
| 31 | if (!encodedImage) { |
| 32 | SkDebugf("\nCould not load resource.\n"); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | size_t requiredMemoryInBytes = encodedImage->getDeferredTextureImageData( |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 37 | *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 38 | if (requiredMemoryInBytes == 0) { |
| 39 | SkDebugf("\nCould not create DeferredTextureImageData.\n"); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | std::vector<uint8_t> memory; |
| 44 | memory.resize(requiredMemoryInBytes); |
| 45 | encodedImage->getDeferredTextureImageData( |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 46 | *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 47 | sk_sp<SkImage> uploadedEncodedImage = SkImage::MakeFromDeferredTextureImageData( |
| 48 | context, memory.data(), SkBudgeted::kNo); |
| 49 | |
| 50 | canvas->drawImage(uploadedEncodedImage, 10, 10); |
| 51 | |
| 52 | |
| 53 | |
| 54 | SkBitmap bitmap; |
| 55 | if (!GetResourceAsBitmap("mandrill_512.png", &bitmap)) { |
| 56 | SkDebugf("\nCould not decode resource.\n"); |
| 57 | return; |
| 58 | } |
| 59 | sk_sp<SkImage> decodedImage = SkImage::MakeFromBitmap(bitmap); |
| 60 | |
| 61 | requiredMemoryInBytes = decodedImage->getDeferredTextureImageData( |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 62 | *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 63 | if (requiredMemoryInBytes == 0) { |
| 64 | SkDebugf("\nCould not create DeferredTextureImageData.\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | memory.resize(requiredMemoryInBytes); |
| 69 | decodedImage->getDeferredTextureImageData( |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 70 | *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 71 | sk_sp<SkImage> uploadedDecodedImage = SkImage::MakeFromDeferredTextureImageData( |
| 72 | context, memory.data(), SkBudgeted::kNo); |
| 73 | |
| 74 | canvas->drawImage(uploadedDecodedImage, 512 + 20, 10); |
| 75 | } |
| 76 | |
| 77 | static void DrawDeferredTextureImageMipMapTree(SkCanvas* canvas, SkImage* image, |
| 78 | SkImage::DeferredTextureImageUsageParams* params) { |
| 79 | GrContext* context = canvas->getGrContext(); |
| 80 | if (!context) { |
| 81 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 82 | return; |
| 83 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 84 | sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 85 | |
| 86 | SkPaint paint; |
| 87 | paint.setFilterQuality(params->fQuality); |
| 88 | |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 89 | int mipLevelCount = SkMipMap::ComputeLevelCount(image->width(), image->height()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 90 | size_t requiredMemoryInBytes = image->getDeferredTextureImageData( |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 91 | *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 92 | if (requiredMemoryInBytes == 0) { |
| 93 | SkDebugf("\nCould not create DeferredTextureImageData.\n"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | std::vector<uint8_t> memory; |
| 98 | memory.resize(requiredMemoryInBytes); |
| 99 | image->getDeferredTextureImageData( |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 100 | *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 101 | sk_sp<SkImage> uploadedImage = SkImage::MakeFromDeferredTextureImageData( |
| 102 | context, memory.data(), SkBudgeted::kNo); |
| 103 | |
| 104 | // draw a column using deferred texture images |
| 105 | SkScalar offsetHeight = 10.f; |
| 106 | // handle base mipmap level |
| 107 | canvas->save(); |
| 108 | canvas->translate(10.f, offsetHeight); |
| 109 | canvas->drawImage(uploadedImage, 0, 0, &paint); |
| 110 | canvas->restore(); |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 111 | offsetHeight += image->height() + 10; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 112 | // handle generated mipmap levels |
| 113 | for (int i = 0; i < mipLevelCount; i++) { |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 114 | SkISize mipSize = SkMipMap::ComputeLevelSize(image->width(), image->height(), i); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 115 | canvas->save(); |
| 116 | canvas->translate(10.f, offsetHeight); |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 117 | canvas->scale(mipSize.width() / static_cast<float>(image->width()), |
| 118 | mipSize.height() / static_cast<float>(image->height())); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 119 | canvas->drawImage(uploadedImage, 0, 0, &paint); |
| 120 | canvas->restore(); |
| 121 | offsetHeight += mipSize.height() + 10; |
| 122 | } |
| 123 | |
| 124 | // draw a column using SkImage |
| 125 | offsetHeight = 10; |
| 126 | // handle base mipmap level |
| 127 | canvas->save(); |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 128 | canvas->translate(image->width() + 20.f, offsetHeight); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 129 | canvas->drawImage(image, 0, 0, &paint); |
| 130 | canvas->restore(); |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 131 | offsetHeight += image->height() + 10; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 132 | // handle generated mipmap levels |
| 133 | for (int i = 0; i < mipLevelCount; i++) { |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 134 | SkISize mipSize = SkMipMap::ComputeLevelSize(image->width(), image->height(), i); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 135 | canvas->save(); |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 136 | canvas->translate(image->width() + 20.f, offsetHeight); |
| 137 | canvas->scale(mipSize.width() / static_cast<float>(image->width()), |
| 138 | mipSize.height() / static_cast<float>(image->height())); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 139 | canvas->drawImage(image, 0, 0, &paint); |
| 140 | canvas->restore(); |
| 141 | offsetHeight += mipSize.height() + 10; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | DEF_SIMPLE_GM(deferred_texture_image_none, canvas, 512 + 512 + 30, 512 + 20) { |
| 146 | auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1.f, 1.f), |
| 147 | kNone_SkFilterQuality, 0); |
| 148 | DrawDeferredTextureImageData(canvas, ¶ms); |
| 149 | } |
| 150 | |
| 151 | DEF_SIMPLE_GM(deferred_texture_image_low, canvas, 512 + 512 + 30, 512 + 20) { |
| 152 | auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1.f, 1.f), |
| 153 | kLow_SkFilterQuality, 0); |
| 154 | DrawDeferredTextureImageData(canvas, ¶ms); |
| 155 | } |
| 156 | |
| 157 | DEF_SIMPLE_GM(deferred_texture_image_medium_encoded, canvas, 512 + 512 + 30, 1110) { |
| 158 | sk_sp<SkImage> encodedImage = GetResourceAsImage("mandrill_512.png"); |
| 159 | if (!encodedImage) { |
| 160 | SkDebugf("\nCould not load resource.\n"); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0.25f, 0.25f), |
| 165 | kMedium_SkFilterQuality, 0); |
| 166 | DrawDeferredTextureImageMipMapTree(canvas, encodedImage.get(), ¶ms); |
| 167 | } |
| 168 | |
| 169 | DEF_SIMPLE_GM(deferred_texture_image_medium_decoded, canvas, 512 + 512 + 30, 1110) { |
| 170 | SkBitmap bitmap; |
| 171 | if (!GetResourceAsBitmap("mandrill_512.png", &bitmap)) { |
| 172 | SkDebugf("\nCould not decode resource.\n"); |
| 173 | return; |
| 174 | } |
| 175 | sk_sp<SkImage> decodedImage = SkImage::MakeFromBitmap(bitmap); |
| 176 | |
| 177 | auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0.25f, 0.25f), |
| 178 | kMedium_SkFilterQuality, 0); |
| 179 | DrawDeferredTextureImageMipMapTree(canvas, decodedImage.get(), ¶ms); |
| 180 | } |
| 181 | |
| 182 | DEF_SIMPLE_GM(deferred_texture_image_high, canvas, 512 + 512 + 30, 512 + 20) { |
| 183 | auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1.f, 1.f), |
| 184 | kHigh_SkFilterQuality, 0); |
| 185 | DrawDeferredTextureImageData(canvas, ¶ms); |
| 186 | } |
| 187 | |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 188 | DEF_SIMPLE_GM(deferred_texture_image_medium_encoded_indexed, canvas, 128 + 128 + 30, 340) { |
| 189 | sk_sp<SkImage> encodedImage = GetResourceAsImage("color_wheel.gif"); |
| 190 | if (!encodedImage) { |
| 191 | SkDebugf("\nCould not load resource.\n"); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0.25f, 0.25f), |
| 196 | kMedium_SkFilterQuality, 0); |
| 197 | DrawDeferredTextureImageMipMapTree(canvas, encodedImage.get(), ¶ms); |
| 198 | } |
| 199 | |
| 200 | DEF_SIMPLE_GM(deferred_texture_image_medium_decoded_indexed, canvas, 128 + 128 + 30, 340) { |
| 201 | SkBitmap bitmap; |
| 202 | if (!GetResourceAsBitmap("color_wheel.gif", &bitmap)) { |
| 203 | SkDebugf("\nCould not decode resource.\n"); |
| 204 | return; |
| 205 | } |
| 206 | sk_sp<SkImage> decodedImage = SkImage::MakeFromBitmap(bitmap); |
| 207 | |
| 208 | auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0.25f, 0.25f), |
| 209 | kMedium_SkFilterQuality, 0); |
| 210 | DrawDeferredTextureImageMipMapTree(canvas, decodedImage.get(), ¶ms); |
| 211 | } |
| 212 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 213 | #endif |