OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
10 #include "SkImage_Base.h" | 10 #include "SkImage_Base.h" |
11 #include "SkImageCacherator.h" | 11 #include "SkImageCacherator.h" |
12 #include "SkMallocPixelRef.h" | 12 #include "SkMallocPixelRef.h" |
13 #include "SkNextID.h" | 13 #include "SkNextID.h" |
14 #include "SkPixelRef.h" | 14 #include "SkPixelRef.h" |
15 #include "SkResourceCache.h" | 15 #include "SkResourceCache.h" |
16 | 16 |
17 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
18 #include "GrContext.h" | 18 #include "GrContext.h" |
19 #include "GrGpuResourcePriv.h" | 19 #include "GrGpuResourcePriv.h" |
20 #include "GrResourceKey.h" | 20 #include "GrResourceKey.h" |
21 #include "GrTextureAccess.h" | 21 #include "GrTextureParams.h" |
22 #include "GrYUVProvider.h" | 22 #include "GrYUVProvider.h" |
23 #include "SkGr.h" | 23 #include "SkGr.h" |
24 #include "SkGrPriv.h" | 24 #include "SkGrPriv.h" |
25 #endif | 25 #endif |
26 | 26 |
27 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, | 27 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, |
28 const SkIRect* subset) { | 28 const SkIRect* subset) { |
29 if (!gen) { | 29 if (!gen) { |
30 return nullptr; | 30 return nullptr; |
31 } | 31 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 return check_output_bitmap(*bitmap, fUniqueID); | 133 return check_output_bitmap(*bitmap, fUniqueID); |
134 } | 134 } |
135 | 135 |
136 #if SK_SUPPORT_GPU | 136 #if SK_SUPPORT_GPU |
137 // Try to get a texture and read it back to raster (and then cache that with
our ID) | 137 // Try to get a texture and read it back to raster (and then cache that with
our ID) |
138 SkAutoTUnref<GrTexture> tex; | 138 SkAutoTUnref<GrTexture> tex; |
139 | 139 |
140 { | 140 { |
141 ScopedGenerator generator(this); | 141 ScopedGenerator generator(this); |
142 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); | 142 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); |
143 tex.reset(generator->generateTexture(nullptr, kUntiled_SkImageUsageType,
&subset)); | 143 tex.reset(generator->generateTexture(nullptr, GrTextureParams::ClampNoFi
lter(), &subset)); |
144 } | 144 } |
145 if (!tex) { | 145 if (!tex) { |
146 bitmap->reset(); | 146 bitmap->reset(); |
147 return false; | 147 return false; |
148 } | 148 } |
149 | 149 |
150 if (!bitmap->tryAllocPixels(fInfo)) { | 150 if (!bitmap->tryAllocPixels(fInfo)) { |
151 bitmap->reset(); | 151 bitmap->reset(); |
152 return false; | 152 return false; |
153 } | 153 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 205 |
206 static GrTexture* set_key_and_return(GrTexture* tex, const GrUniqueKey& key) { | 206 static GrTexture* set_key_and_return(GrTexture* tex, const GrUniqueKey& key) { |
207 tex->resourcePriv().setUniqueKey(key); | 207 tex->resourcePriv().setUniqueKey(key); |
208 return tex; | 208 return tex; |
209 } | 209 } |
210 | 210 |
211 /* | 211 /* |
212 * We have a 5 ways to try to return a texture (in sorted order) | 212 * We have a 5 ways to try to return a texture (in sorted order) |
213 * | 213 * |
214 * 1. Check the cache for a pre-existing one | 214 * 1. Check the cache for a pre-existing one |
215 * 2. Ask the genreator to natively create one | 215 * 2. Ask the generator to natively create one |
216 * 3. Ask the generator to return a compressed form that the GPU might support | 216 * 3. Ask the generator to return a compressed form that the GPU might support |
217 * 4. Ask the generator to return YUV planes, which the GPU can convert | 217 * 4. Ask the generator to return YUV planes, which the GPU can convert |
218 * 5. Ask the generator to return RGB(A) data, which the GPU can convert | 218 * 5. Ask the generator to return RGB(A) data, which the GPU can convert |
219 */ | 219 */ |
220 GrTexture* SkImageCacherator::lockUnstretchedTexture(GrContext* ctx, SkImageUsag
eType usage, | 220 GrTexture* SkImageCacherator::lockUnstretchedTexture(GrContext* ctx, const GrTex
tureParams& params, |
221 const SkImage* client) { | 221 const SkImage* client) { |
222 // textures (at least the texture-key) only support 16bit dimensions, so abo
rt early | 222 // textures (at least the texture-key) only support 16bit dimensions, so abo
rt early |
223 // if we're too big. | 223 // if we're too big. |
224 if (fInfo.width() > 0xFFFF || fInfo.height() > 0xFFFF) { | 224 if (fInfo.width() > 0xFFFF || fInfo.height() > 0xFFFF) { |
225 return nullptr; | 225 return nullptr; |
226 } | 226 } |
227 | 227 |
228 GrUniqueKey key; | 228 GrUniqueKey key; |
229 GrMakeKeyFromImageID(&key, fUniqueID, SkIRect::MakeWH(fInfo.width(), fInfo.h
eight()), | 229 GrMakeKeyFromImageID(&key, fUniqueID, SkIRect::MakeWH(fInfo.width(), fInfo.h
eight()), |
230 *ctx->caps(), usage); | 230 *ctx->caps(), params); |
231 | 231 |
232 // 1. Check the cache for a pre-existing one | 232 // 1. Check the cache for a pre-existing one |
233 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke
y)) { | 233 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke
y)) { |
234 return tex; | 234 return tex; |
235 } | 235 } |
236 | 236 |
237 // 2. Ask the genreator to natively create one | 237 // 2. Ask the genreator to natively create one |
238 { | 238 { |
239 ScopedGenerator generator(this); | 239 ScopedGenerator generator(this); |
240 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); | 240 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); |
241 if (GrTexture* tex = generator->generateTexture(ctx, usage, &subset)) { | 241 if (GrTexture* tex = generator->generateTexture(ctx, params, &subset)) { |
242 return set_key_and_return(tex, key); | 242 return set_key_and_return(tex, key); |
243 } | 243 } |
244 } | 244 } |
245 | 245 |
246 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo); | 246 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo); |
247 | 247 |
248 // 3. Ask the generator to return a compressed form that the GPU might suppo
rt | 248 // 3. Ask the generator to return a compressed form that the GPU might suppo
rt |
249 SkAutoTUnref<SkData> data(this->refEncoded()); | 249 SkAutoTUnref<SkData> data(this->refEncoded()); |
250 if (data) { | 250 if (data) { |
251 GrTexture* tex = load_compressed_into_texture(ctx, data, desc); | 251 GrTexture* tex = load_compressed_into_texture(ctx, data, desc); |
252 if (tex) { | 252 if (tex) { |
253 return set_key_and_return(tex, key); | 253 return set_key_and_return(tex, key); |
254 } | 254 } |
255 } | 255 } |
256 | 256 |
257 // 4. Ask the generator to return YUV planes, which the GPU can convert | 257 // 4. Ask the generator to return YUV planes, which the GPU can convert |
258 { | 258 { |
259 ScopedGenerator generator(this); | 259 ScopedGenerator generator(this); |
260 Generator_GrYUVProvider provider(generator); | 260 Generator_GrYUVProvider provider(generator); |
261 GrTexture* tex = provider.refAsTexture(ctx, desc, true); | 261 GrTexture* tex = provider.refAsTexture(ctx, desc, true); |
262 if (tex) { | 262 if (tex) { |
263 return set_key_and_return(tex, key); | 263 return set_key_and_return(tex, key); |
264 } | 264 } |
265 } | 265 } |
266 | 266 |
267 // 5. Ask the generator to return RGB(A) data, which the GPU can convert | 267 // 5. Ask the generator to return RGB(A) data, which the GPU can convert |
268 SkBitmap bitmap; | 268 SkBitmap bitmap; |
269 if (this->tryLockAsBitmap(&bitmap, client)) { | 269 if (this->tryLockAsBitmap(&bitmap, client)) { |
270 return GrRefCachedBitmapTexture(ctx, bitmap, usage); | 270 return GrRefCachedBitmapTexture(ctx, bitmap, params); |
271 } | 271 } |
272 return nullptr; | 272 return nullptr; |
273 } | 273 } |
274 | 274 |
275 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 275 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
276 | 276 |
277 #include "GrTextureMaker.h" | 277 #include "GrTextureMaker.h" |
278 | 278 |
279 class Cacherator_GrTextureMaker : public GrTextureMaker { | 279 class Cacherator_GrTextureMaker : public GrTextureMaker { |
280 public: | 280 public: |
281 Cacherator_GrTextureMaker(SkImageCacherator* cacher, SkImageUsageType usage, | 281 Cacherator_GrTextureMaker(SkImageCacherator* cacher, const GrTextureParams&
params, |
282 const SkImage* client, const GrUniqueKey& unstretc
hedKey) | 282 const SkImage* client, const GrUniqueKey& unstretc
hedKey) |
283 : INHERITED(cacher->info().width(), cacher->info().height()) | 283 : INHERITED(cacher->info().width(), cacher->info().height()) |
284 , fCacher(cacher) | 284 , fCacher(cacher) |
285 , fUsage(usage) | 285 , fParams(params) |
286 , fClient(client) | 286 , fClient(client) |
287 , fUnstretchedKey(unstretchedKey) | 287 , fUnstretchedKey(unstretchedKey) |
288 {} | 288 {} |
289 | 289 |
290 protected: | 290 protected: |
291 // TODO: consider overriding this, for the case where the underlying generat
or might be | 291 // TODO: consider overriding this, for the case where the underlying generat
or might be |
292 // able to efficiently produce a "stretched" texture natively (e.g. pi
cture-backed) | 292 // able to efficiently produce a "stretched" texture natively (e.g. pi
cture-backed) |
293 // GrTexture* onGenerateStretchedTexture(GrContext*, const SkGrStretch&) over
ride; | 293 // GrTexture* onGenerateStretchedTexture(GrContext*, const SkGrStretch&) over
ride; |
294 | 294 |
295 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override { | 295 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override { |
296 return fCacher->lockUnstretchedTexture(ctx, fUsage, fClient); | 296 return fCacher->lockUnstretchedTexture(ctx, fParams, fClient); |
297 } | 297 } |
298 | 298 |
299 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKe
y) override { | 299 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKe
y) override { |
300 return GrMakeStretchedKey(fUnstretchedKey, stretch, stretchedKey); | 300 return GrMakeStretchedKey(fUnstretchedKey, stretch, stretchedKey); |
301 } | 301 } |
302 | 302 |
303 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override { | 303 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override { |
304 if (fClient) { | 304 if (fClient) { |
305 as_IB(fClient)->notifyAddedToCache(); | 305 as_IB(fClient)->notifyAddedToCache(); |
306 } | 306 } |
307 } | 307 } |
308 | 308 |
309 bool onGetROBitmap(SkBitmap* bitmap) override { | 309 bool onGetROBitmap(SkBitmap* bitmap) override { |
310 return fCacher->lockAsBitmap(bitmap, fClient); | 310 return fCacher->lockAsBitmap(bitmap, fClient); |
311 } | 311 } |
312 | 312 |
313 private: | 313 private: |
314 SkImageCacherator* fCacher; | 314 SkImageCacherator* fCacher; |
315 const SkImageUsageType fUsage; | 315 const GrTextureParams& fParams; |
316 const SkImage* fClient; | 316 const SkImage* fClient; |
317 const GrUniqueKey fUnstretchedKey; | 317 const GrUniqueKey fUnstretchedKey; |
318 | 318 |
319 typedef GrTextureMaker INHERITED; | 319 typedef GrTextureMaker INHERITED; |
320 }; | 320 }; |
321 | 321 |
322 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa
ge, | 322 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s& params, |
323 const SkImage* client) { | 323 const SkImage* client) { |
324 if (!ctx) { | 324 if (!ctx) { |
325 return nullptr; | 325 return nullptr; |
326 } | 326 } |
327 | 327 |
328 GrUniqueKey key; | 328 GrUniqueKey key; |
329 GrMakeKeyFromImageID(&key, this->uniqueID(), | 329 GrMakeKeyFromImageID(&key, this->uniqueID(), |
330 SkIRect::MakeWH(this->info().width(), this->info().heig
ht()), | 330 SkIRect::MakeWH(this->info().width(), this->info().heig
ht()), |
331 *ctx->caps(), usage); | 331 *ctx->caps(), params); |
332 | 332 |
333 return Cacherator_GrTextureMaker(this, usage, client, key).refCachedTexture(
ctx, usage); | 333 return Cacherator_GrTextureMaker(this, params, client, key).refCachedTexture
(ctx, params); |
334 } | 334 } |
335 | 335 |
336 #else | 336 #else |
337 | 337 |
338 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa
ge, | 338 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, |
339 const SkImage* client) { | 339 const SkImage* client) { |
340 return nullptr; | 340 return nullptr; |
341 } | 341 } |
342 | 342 |
343 #endif | 343 #endif |
OLD | NEW |