blob: 9c1fe6a6f282a593f2dc6a124bc97c96427833d6 [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"
Florin Malitaab244f02017-05-03 19:16:58 +000010#include "SkImage.h"
cblume33e0cb52016-08-30 12:09:23 -070011#include "SkMipMap.h"
12#include "Resources.h"
13
14#if SK_SUPPORT_GPU
brianosman1dfec742016-09-15 07:19:53 -070015#include "GrContext.h"
cblume33e0cb52016-08-30 12:09:23 -070016
Robert Phillipsb34727f2017-02-10 14:44:58 -050017// Helper function that uploads the given SkImage using MakeFromDeferredTextureImageData and then
cblume33e0cb52016-08-30 12:09:23 -070018// draws the uploaded version at the specified coordinates.
19static void DrawDeferredTextureImageData(SkCanvas* canvas,
20 SkImage::DeferredTextureImageUsageParams* params) {
21 GrContext* context = canvas->getGrContext();
22 if (!context) {
23 skiagm::GM::DrawGpuOnlyMessage(canvas);
24 return;
25 }
bungeman6bd52842016-10-27 09:30:08 -070026 sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy());
cblume33e0cb52016-08-30 12:09:23 -070027
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 Osman6c15cc72016-10-17 09:51:37 -040037 *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070038 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 Osman6c15cc72016-10-17 09:51:37 -040046 *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070047 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 Osman6c15cc72016-10-17 09:51:37 -040062 *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070063 if (requiredMemoryInBytes == 0) {
64 SkDebugf("\nCould not create DeferredTextureImageData.\n");
65 return;
66 }
67
68 memory.resize(requiredMemoryInBytes);
69 decodedImage->getDeferredTextureImageData(
Brian Osman6c15cc72016-10-17 09:51:37 -040070 *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070071 sk_sp<SkImage> uploadedDecodedImage = SkImage::MakeFromDeferredTextureImageData(
72 context, memory.data(), SkBudgeted::kNo);
73
74 canvas->drawImage(uploadedDecodedImage, 512 + 20, 10);
75}
76
77static 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 }
bungeman6bd52842016-10-27 09:30:08 -070084 sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy());
cblume33e0cb52016-08-30 12:09:23 -070085
86 SkPaint paint;
87 paint.setFilterQuality(params->fQuality);
88
Brian Osmanaaedae72017-01-20 13:21:56 -050089 int mipLevelCount = SkMipMap::ComputeLevelCount(image->width(), image->height());
cblume33e0cb52016-08-30 12:09:23 -070090 size_t requiredMemoryInBytes = image->getDeferredTextureImageData(
Brian Osman6c15cc72016-10-17 09:51:37 -040091 *proxy, params, 1, nullptr, canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -070092 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 Osman6c15cc72016-10-17 09:51:37 -0400100 *proxy, params, 1, memory.data(), canvas->imageInfo().colorSpace());
cblume33e0cb52016-08-30 12:09:23 -0700101 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 Osmanaaedae72017-01-20 13:21:56 -0500111 offsetHeight += image->height() + 10;
cblume33e0cb52016-08-30 12:09:23 -0700112 // handle generated mipmap levels
113 for (int i = 0; i < mipLevelCount; i++) {
Brian Osmanaaedae72017-01-20 13:21:56 -0500114 SkISize mipSize = SkMipMap::ComputeLevelSize(image->width(), image->height(), i);
cblume33e0cb52016-08-30 12:09:23 -0700115 canvas->save();
116 canvas->translate(10.f, offsetHeight);
Brian Osmanaaedae72017-01-20 13:21:56 -0500117 canvas->scale(mipSize.width() / static_cast<float>(image->width()),
118 mipSize.height() / static_cast<float>(image->height()));
cblume33e0cb52016-08-30 12:09:23 -0700119 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 Osmanaaedae72017-01-20 13:21:56 -0500128 canvas->translate(image->width() + 20.f, offsetHeight);
cblume33e0cb52016-08-30 12:09:23 -0700129 canvas->drawImage(image, 0, 0, &paint);
130 canvas->restore();
Brian Osmanaaedae72017-01-20 13:21:56 -0500131 offsetHeight += image->height() + 10;
cblume33e0cb52016-08-30 12:09:23 -0700132 // handle generated mipmap levels
133 for (int i = 0; i < mipLevelCount; i++) {
Brian Osmanaaedae72017-01-20 13:21:56 -0500134 SkISize mipSize = SkMipMap::ComputeLevelSize(image->width(), image->height(), i);
cblume33e0cb52016-08-30 12:09:23 -0700135 canvas->save();
Brian Osmanaaedae72017-01-20 13:21:56 -0500136 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()));
cblume33e0cb52016-08-30 12:09:23 -0700139 canvas->drawImage(image, 0, 0, &paint);
140 canvas->restore();
141 offsetHeight += mipSize.height() + 10;
142 }
143}
144
145DEF_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, &params);
149}
150
151DEF_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, &params);
155}
156
157DEF_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(), &params);
167}
168
169DEF_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(), &params);
180}
181
182DEF_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, &params);
186}
187
Brian Osmanaaedae72017-01-20 13:21:56 -0500188DEF_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(), &params);
198}
199
200DEF_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(), &params);
211}
212
cblume33e0cb52016-08-30 12:09:23 -0700213#endif