blob: c88b878ccb6e1d09bc0f2110ee22499aabb6c861 [file] [log] [blame]
cblume33e0cb52016-08-30 12:09:23 -07001/*
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"
cblume33e0cb52016-08-30 12:09:23 -070010#include "SkMipMap.h"
11#include "Resources.h"
12
13#if SK_SUPPORT_GPU
brianosman1dfec742016-09-15 07:19:53 -070014#include "GrContext.h"
cblume33e0cb52016-08-30 12:09:23 -070015
Robert Phillipsb34727f2017-02-10 14:44:58 -050016// Helper function that uploads the given SkImage using MakeFromDeferredTextureImageData and then
cblume33e0cb52016-08-30 12:09:23 -070017// draws the uploaded version at the specified coordinates.
18static void DrawDeferredTextureImageData(SkCanvas* canvas,
19 SkImage::DeferredTextureImageUsageParams* params) {
20 GrContext* context = canvas->getGrContext();
21 if (!context) {
22 skiagm::GM::DrawGpuOnlyMessage(canvas);
23 return;
24 }
bungeman6bd52842016-10-27 09:30:08 -070025 sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy());
cblume33e0cb52016-08-30 12:09:23 -070026
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(
Brian Osman6c15cc72016-10-17 09:51:37 -040036 *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070037 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(
Brian Osman6c15cc72016-10-17 09:51:37 -040045 *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070046 sk_sp<SkImage> uploadedEncodedImage = SkImage::MakeFromDeferredTextureImageData(
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(
Brian Osman6c15cc72016-10-17 09:51:37 -040061 *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070062 if (requiredMemoryInBytes == 0) {
63 SkDebugf("\nCould not create DeferredTextureImageData.\n");
64 return;
65 }
66
67 memory.resize(requiredMemoryInBytes);
68 decodedImage->getDeferredTextureImageData(
Brian Osman6c15cc72016-10-17 09:51:37 -040069 *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070070 sk_sp<SkImage> uploadedDecodedImage = SkImage::MakeFromDeferredTextureImageData(
71 context, memory.data(), SkBudgeted::kNo);
72
73 canvas->drawImage(uploadedDecodedImage, 512 + 20, 10);
74}
75
76static void DrawDeferredTextureImageMipMapTree(SkCanvas* canvas, SkImage* image,
77 SkImage::DeferredTextureImageUsageParams* params) {
78 GrContext* context = canvas->getGrContext();
79 if (!context) {
80 skiagm::GM::DrawGpuOnlyMessage(canvas);
81 return;
82 }
bungeman6bd52842016-10-27 09:30:08 -070083 sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy());
cblume33e0cb52016-08-30 12:09:23 -070084
85 SkPaint paint;
86 paint.setFilterQuality(params->fQuality);
87
Brian Osmanaaedae72017-01-20 13:21:56 -050088 int mipLevelCount = SkMipMap::ComputeLevelCount(image->width(), image->height());
cblume33e0cb52016-08-30 12:09:23 -070089 size_t requiredMemoryInBytes = image->getDeferredTextureImageData(
Brian Osman6c15cc72016-10-17 09:51:37 -040090 *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070091 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(
Brian Osman6c15cc72016-10-17 09:51:37 -040099 *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -0700100 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();
Brian Osmanaaedae72017-01-20 13:21:56 -0500110 offsetHeight += image->height() + 10;
cblume33e0cb52016-08-30 12:09:23 -0700111 // handle generated mipmap levels
112 for (int i = 0; i < mipLevelCount; i++) {
Brian Osmanaaedae72017-01-20 13:21:56 -0500113 SkISize mipSize = SkMipMap::ComputeLevelSize(image->width(), image->height(), i);
cblume33e0cb52016-08-30 12:09:23 -0700114 canvas->save();
115 canvas->translate(10.f, offsetHeight);
Brian Osmanaaedae72017-01-20 13:21:56 -0500116 canvas->scale(mipSize.width() / static_cast<float>(image->width()),
117 mipSize.height() / static_cast<float>(image->height()));
cblume33e0cb52016-08-30 12:09:23 -0700118 canvas->drawImage(uploadedImage, 0, 0, &paint);
119 canvas->restore();
120 offsetHeight += mipSize.height() + 10;
121 }
122
123 // draw a column using SkImage
124 offsetHeight = 10;
125 // handle base mipmap level
126 canvas->save();
Brian Osmanaaedae72017-01-20 13:21:56 -0500127 canvas->translate(image->width() + 20.f, offsetHeight);
cblume33e0cb52016-08-30 12:09:23 -0700128 canvas->drawImage(image, 0, 0, &paint);
129 canvas->restore();
Brian Osmanaaedae72017-01-20 13:21:56 -0500130 offsetHeight += image->height() + 10;
cblume33e0cb52016-08-30 12:09:23 -0700131 // handle generated mipmap levels
132 for (int i = 0; i < mipLevelCount; i++) {
Brian Osmanaaedae72017-01-20 13:21:56 -0500133 SkISize mipSize = SkMipMap::ComputeLevelSize(image->width(), image->height(), i);
cblume33e0cb52016-08-30 12:09:23 -0700134 canvas->save();
Brian Osmanaaedae72017-01-20 13:21:56 -0500135 canvas->translate(image->width() + 20.f, offsetHeight);
136 canvas->scale(mipSize.width() / static_cast<float>(image->width()),
137 mipSize.height() / static_cast<float>(image->height()));
cblume33e0cb52016-08-30 12:09:23 -0700138 canvas->drawImage(image, 0, 0, &paint);
139 canvas->restore();
140 offsetHeight += mipSize.height() + 10;
141 }
142}
143
144DEF_SIMPLE_GM(deferred_texture_image_none, canvas, 512 + 512 + 30, 512 + 20) {
145 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1.f, 1.f),
146 kNone_SkFilterQuality, 0);
147 DrawDeferredTextureImageData(canvas, &params);
148}
149
150DEF_SIMPLE_GM(deferred_texture_image_low, canvas, 512 + 512 + 30, 512 + 20) {
151 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1.f, 1.f),
152 kLow_SkFilterQuality, 0);
153 DrawDeferredTextureImageData(canvas, &params);
154}
155
156DEF_SIMPLE_GM(deferred_texture_image_medium_encoded, canvas, 512 + 512 + 30, 1110) {
157 sk_sp<SkImage> encodedImage = GetResourceAsImage("mandrill_512.png");
158 if (!encodedImage) {
159 SkDebugf("\nCould not load resource.\n");
160 return;
161 }
162
163 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0.25f, 0.25f),
164 kMedium_SkFilterQuality, 0);
165 DrawDeferredTextureImageMipMapTree(canvas, encodedImage.get(), &params);
166}
167
168DEF_SIMPLE_GM(deferred_texture_image_medium_decoded, canvas, 512 + 512 + 30, 1110) {
169 SkBitmap bitmap;
170 if (!GetResourceAsBitmap("mandrill_512.png", &bitmap)) {
171 SkDebugf("\nCould not decode resource.\n");
172 return;
173 }
174 sk_sp<SkImage> decodedImage = SkImage::MakeFromBitmap(bitmap);
175
176 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0.25f, 0.25f),
177 kMedium_SkFilterQuality, 0);
178 DrawDeferredTextureImageMipMapTree(canvas, decodedImage.get(), &params);
179}
180
181DEF_SIMPLE_GM(deferred_texture_image_high, canvas, 512 + 512 + 30, 512 + 20) {
182 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(1.f, 1.f),
183 kHigh_SkFilterQuality, 0);
184 DrawDeferredTextureImageData(canvas, &params);
185}
186
Brian Osmanaaedae72017-01-20 13:21:56 -0500187DEF_SIMPLE_GM(deferred_texture_image_medium_encoded_indexed, canvas, 128 + 128 + 30, 340) {
188 sk_sp<SkImage> encodedImage = GetResourceAsImage("color_wheel.gif");
189 if (!encodedImage) {
190 SkDebugf("\nCould not load resource.\n");
191 return;
192 }
193
194 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0.25f, 0.25f),
195 kMedium_SkFilterQuality, 0);
196 DrawDeferredTextureImageMipMapTree(canvas, encodedImage.get(), &params);
197}
198
199DEF_SIMPLE_GM(deferred_texture_image_medium_decoded_indexed, canvas, 128 + 128 + 30, 340) {
200 SkBitmap bitmap;
201 if (!GetResourceAsBitmap("color_wheel.gif", &bitmap)) {
202 SkDebugf("\nCould not decode resource.\n");
203 return;
204 }
205 sk_sp<SkImage> decodedImage = SkImage::MakeFromBitmap(bitmap);
206
207 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(0.25f, 0.25f),
208 kMedium_SkFilterQuality, 0);
209 DrawDeferredTextureImageMipMapTree(canvas, decodedImage.get(), &params);
210}
211
cblume33e0cb52016-08-30 12:09:23 -0700212#endif