OLD | NEW |
(Empty) | |
| 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 "SkMipMap.h" |
| 12 #include "Resources.h" |
| 13 |
| 14 #if SK_SUPPORT_GPU |
| 15 |
| 16 // Helper function that uploads the given SkImage using MakdeFromDeferredTexture
ImageData and then |
| 17 // draws the uploaded version at the specified coordinates. |
| 18 static void DrawDeferredTextureImageData(SkCanvas* canvas, |
| 19 SkImage::DeferredTextureImageUsageParam
s* params) { |
| 20 GrContext* context = canvas->getGrContext(); |
| 21 if (!context) { |
| 22 skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 23 return; |
| 24 } |
| 25 SkAutoTUnref<GrContextThreadSafeProxy> proxy(context->threadSafeProxy()); |
| 26 |
| 27 |
| 28 |
| 29 sk_sp<SkImage> encodedImage = GetResourceAsImage("mandrill_512.png"); |
| 30 if (!encodedImage) { |
| 31 SkDebugf("\nCould not load resource.\n"); |
| 32 return; |
| 33 } |
| 34 |
| 35 size_t requiredMemoryInBytes = encodedImage->getDeferredTextureImageData( |
| 36 *proxy, params, 1, nullptr, SkSourceGammaTreatment::kRespect); |
| 37 if (requiredMemoryInBytes == 0) { |
| 38 SkDebugf("\nCould not create DeferredTextureImageData.\n"); |
| 39 return; |
| 40 } |
| 41 |
| 42 std::vector<uint8_t> memory; |
| 43 memory.resize(requiredMemoryInBytes); |
| 44 encodedImage->getDeferredTextureImageData( |
| 45 *proxy, params, 1, memory.data(), SkSourceGammaTreatment::kRespect); |
| 46 sk_sp<SkImage> uploadedEncodedImage = SkImage::MakeFromDeferredTextureImageD
ata( |
| 47 context, memory.data(), SkBudgeted::kNo); |
| 48 |
| 49 canvas->drawImage(uploadedEncodedImage, 10, 10); |
| 50 |
| 51 |
| 52 |
| 53 SkBitmap bitmap; |
| 54 if (!GetResourceAsBitmap("mandrill_512.png", &bitmap)) { |
| 55 SkDebugf("\nCould not decode resource.\n"); |
| 56 return; |
| 57 } |
| 58 sk_sp<SkImage> decodedImage = SkImage::MakeFromBitmap(bitmap); |
| 59 |
| 60 requiredMemoryInBytes = decodedImage->getDeferredTextureImageData( |
| 61 *proxy, params, 1, nullptr, SkSourceGammaTreatment::kRespect); |
| 62 if (requiredMemoryInBytes == 0) { |
| 63 SkDebugf("\nCould not create DeferredTextureImageData.\n"); |
| 64 return; |
| 65 } |
| 66 |
| 67 memory.resize(requiredMemoryInBytes); |
| 68 decodedImage->getDeferredTextureImageData( |
| 69 *proxy, params, 1, memory.data(), SkSourceGammaTreatment::kRespect); |
| 70 sk_sp<SkImage> uploadedDecodedImage = SkImage::MakeFromDeferredTextureImageD
ata( |
| 71 context, memory.data(), SkBudgeted::kNo); |
| 72 |
| 73 canvas->drawImage(uploadedDecodedImage, 512 + 20, 10); |
| 74 } |
| 75 |
| 76 static void DrawDeferredTextureImageMipMapTree(SkCanvas* canvas, SkImage* image, |
| 77 SkImage::DeferredTextureImageUsag
eParams* params) { |
| 78 GrContext* context = canvas->getGrContext(); |
| 79 if (!context) { |
| 80 skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 81 return; |
| 82 } |
| 83 SkAutoTUnref<GrContextThreadSafeProxy> proxy(context->threadSafeProxy()); |
| 84 |
| 85 SkPaint paint; |
| 86 paint.setFilterQuality(params->fQuality); |
| 87 |
| 88 int mipLevelCount = SkMipMap::ComputeLevelCount(512, 512); |
| 89 size_t requiredMemoryInBytes = image->getDeferredTextureImageData( |
| 90 *proxy, params, 1, nullptr, SkSourceGammaTreatment::kRespect); |
| 91 if (requiredMemoryInBytes == 0) { |
| 92 SkDebugf("\nCould not create DeferredTextureImageData.\n"); |
| 93 return; |
| 94 } |
| 95 |
| 96 std::vector<uint8_t> memory; |
| 97 memory.resize(requiredMemoryInBytes); |
| 98 image->getDeferredTextureImageData( |
| 99 *proxy, params, 1, memory.data(), SkSourceGammaTreatment::kRespect); |
| 100 sk_sp<SkImage> uploadedImage = SkImage::MakeFromDeferredTextureImageData( |
| 101 context, memory.data(), SkBudgeted::kNo); |
| 102 |
| 103 // draw a column using deferred texture images |
| 104 SkScalar offsetHeight = 10.f; |
| 105 // handle base mipmap level |
| 106 canvas->save(); |
| 107 canvas->translate(10.f, offsetHeight); |
| 108 canvas->drawImage(uploadedImage, 0, 0, &paint); |
| 109 canvas->restore(); |
| 110 offsetHeight += 512 + 10; |
| 111 // handle generated mipmap levels |
| 112 for (int i = 0; i < mipLevelCount; i++) { |
| 113 SkISize mipSize = SkMipMap::ComputeLevelSize(512, 512, i); |
| 114 canvas->save(); |
| 115 canvas->translate(10.f, offsetHeight); |
| 116 canvas->scale(mipSize.width() / 512.f, mipSize.height() / 512.f); |
| 117 canvas->drawImage(uploadedImage, 0, 0, &paint); |
| 118 canvas->restore(); |
| 119 offsetHeight += mipSize.height() + 10; |
| 120 } |
| 121 |
| 122 // draw a column using SkImage |
| 123 offsetHeight = 10; |
| 124 // handle base mipmap level |
| 125 canvas->save(); |
| 126 canvas->translate(512.f + 20.f, offsetHeight); |
| 127 canvas->drawImage(image, 0, 0, &paint); |
| 128 canvas->restore(); |
| 129 offsetHeight += 512 + 10; |
| 130 // handle generated mipmap levels |
| 131 for (int i = 0; i < mipLevelCount; i++) { |
| 132 SkISize mipSize = SkMipMap::ComputeLevelSize(512, 512, i); |
| 133 canvas->save(); |
| 134 canvas->translate(512.f + 20.f, offsetHeight); |
| 135 canvas->scale(mipSize.width() / 512.f, mipSize.height() / 512.f); |
| 136 canvas->drawImage(image, 0, 0, &paint); |
| 137 canvas->restore(); |
| 138 offsetHeight += mipSize.height() + 10; |
| 139 } |
| 140 } |
| 141 |
| 142 DEF_SIMPLE_GM(deferred_texture_image_none, canvas, 512 + 512 + 30, 512 + 20) { |
| 143 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1
.f, 1.f), |
| 144 kNone_SkFilterQuality
, 0); |
| 145 DrawDeferredTextureImageData(canvas, ¶ms); |
| 146 } |
| 147 |
| 148 DEF_SIMPLE_GM(deferred_texture_image_low, canvas, 512 + 512 + 30, 512 + 20) { |
| 149 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1
.f, 1.f), |
| 150 kLow_SkFilterQuality,
0); |
| 151 DrawDeferredTextureImageData(canvas, ¶ms); |
| 152 } |
| 153 |
| 154 DEF_SIMPLE_GM(deferred_texture_image_medium_encoded, canvas, 512 + 512 + 30, 111
0) { |
| 155 sk_sp<SkImage> encodedImage = GetResourceAsImage("mandrill_512.png"); |
| 156 if (!encodedImage) { |
| 157 SkDebugf("\nCould not load resource.\n"); |
| 158 return; |
| 159 } |
| 160 |
| 161 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0
.25f, 0.25f), |
| 162 kMedium_SkFilterQuali
ty, 0); |
| 163 DrawDeferredTextureImageMipMapTree(canvas, encodedImage.get(), ¶ms); |
| 164 } |
| 165 |
| 166 DEF_SIMPLE_GM(deferred_texture_image_medium_decoded, canvas, 512 + 512 + 30, 111
0) { |
| 167 SkBitmap bitmap; |
| 168 if (!GetResourceAsBitmap("mandrill_512.png", &bitmap)) { |
| 169 SkDebugf("\nCould not decode resource.\n"); |
| 170 return; |
| 171 } |
| 172 sk_sp<SkImage> decodedImage = SkImage::MakeFromBitmap(bitmap); |
| 173 |
| 174 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0
.25f, 0.25f), |
| 175 kMedium_SkFilterQuali
ty, 0); |
| 176 DrawDeferredTextureImageMipMapTree(canvas, decodedImage.get(), ¶ms); |
| 177 } |
| 178 |
| 179 DEF_SIMPLE_GM(deferred_texture_image_high, canvas, 512 + 512 + 30, 512 + 20) { |
| 180 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1
.f, 1.f), |
| 181 kHigh_SkFilterQuality
, 0); |
| 182 DrawDeferredTextureImageData(canvas, ¶ms); |
| 183 } |
| 184 |
| 185 #endif |
OLD | NEW |