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" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 bool onGetYUVSizes(SkISize sizes[3]) override { | 197 bool onGetYUVSizes(SkISize sizes[3]) override { |
198 return fGen->getYUV8Planes(sizes, nullptr, nullptr, nullptr); | 198 return fGen->getYUV8Planes(sizes, nullptr, nullptr, nullptr); |
199 } | 199 } |
200 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], | 200 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], |
201 SkYUVColorSpace* space) override { | 201 SkYUVColorSpace* space) override { |
202 return fGen->getYUV8Planes(sizes, planes, rowBytes, space); | 202 return fGen->getYUV8Planes(sizes, planes, rowBytes, space); |
203 } | 203 } |
204 }; | 204 }; |
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 if (key.isValid()) { |
| 208 tex->resourcePriv().setUniqueKey(key); |
| 209 } |
208 return tex; | 210 return tex; |
209 } | 211 } |
210 | 212 |
211 /* | 213 /* |
212 * We have a 5 ways to try to return a texture (in sorted order) | 214 * We have a 5 ways to try to return a texture (in sorted order) |
213 * | 215 * |
214 * 1. Check the cache for a pre-existing one | 216 * 1. Check the cache for a pre-existing one |
215 * 2. Ask the generator to natively create one | 217 * 2. Ask the generator to natively create one |
216 * 3. Ask the generator to return a compressed form that the GPU might support | 218 * 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 | 219 * 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 | 220 * 5. Ask the generator to return RGB(A) data, which the GPU can convert |
219 */ | 221 */ |
220 GrTexture* SkImageCacherator::lockUnstretchedTexture(GrContext* ctx, const SkIma
ge* client) { | 222 GrTexture* SkImageCacherator::lockTexture(GrContext* ctx, const GrUniqueKey& key
, |
221 // textures (at least the texture-key) only support 16bit dimensions, so abo
rt early | 223 const SkImage* client) { |
222 // if we're too big. | 224 // 1. Check the cache for a pre-existing one |
223 if (fInfo.width() > 0xFFFF || fInfo.height() > 0xFFFF) { | 225 if (key.isValid()) { |
224 return nullptr; | 226 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKe
y(key)) { |
| 227 return tex; |
| 228 } |
225 } | 229 } |
226 | 230 |
227 GrUniqueKey key; | 231 // 2. Ask the generator to natively create one |
228 const GrTextureParams& noStretchParams = GrTextureParams::ClampNoFilter(); | |
229 GrMakeKeyFromImageID(&key, fUniqueID, SkIRect::MakeWH(fInfo.width(), fInfo.h
eight()), | |
230 *ctx->caps(), noStretchParams); | |
231 | |
232 // 1. Check the cache for a pre-existing one | |
233 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke
y)) { | |
234 return tex; | |
235 } | |
236 | |
237 // 2. Ask the genreator to natively create one | |
238 { | 232 { |
239 ScopedGenerator generator(this); | 233 ScopedGenerator generator(this); |
240 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); | 234 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); |
241 if (GrTexture* tex = generator->generateTexture(ctx, &subset)) { | 235 if (GrTexture* tex = generator->generateTexture(ctx, &subset)) { |
242 return set_key_and_return(tex, key); | 236 return set_key_and_return(tex, key); |
243 } | 237 } |
244 } | 238 } |
245 | 239 |
246 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo); | 240 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo); |
247 | 241 |
(...skipping 12 matching lines...) Expand all Loading... |
260 Generator_GrYUVProvider provider(generator); | 254 Generator_GrYUVProvider provider(generator); |
261 GrTexture* tex = provider.refAsTexture(ctx, desc, true); | 255 GrTexture* tex = provider.refAsTexture(ctx, desc, true); |
262 if (tex) { | 256 if (tex) { |
263 return set_key_and_return(tex, key); | 257 return set_key_and_return(tex, key); |
264 } | 258 } |
265 } | 259 } |
266 | 260 |
267 // 5. Ask the generator to return RGB(A) data, which the GPU can convert | 261 // 5. Ask the generator to return RGB(A) data, which the GPU can convert |
268 SkBitmap bitmap; | 262 SkBitmap bitmap; |
269 if (this->tryLockAsBitmap(&bitmap, client)) { | 263 if (this->tryLockAsBitmap(&bitmap, client)) { |
270 return GrRefCachedBitmapTexture(ctx, bitmap, noStretchParams); | 264 GrTexture* tex = GrUploadBitmapToTexture(ctx, bitmap); |
| 265 if (tex) { |
| 266 return set_key_and_return(tex, key); |
| 267 } |
271 } | 268 } |
272 return nullptr; | 269 return nullptr; |
273 } | 270 } |
274 | 271 |
275 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 272 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
276 | 273 |
277 #include "GrTextureMaker.h" | 274 #include "GrTextureParamsAdjuster.h" |
278 | 275 |
279 class Cacherator_GrTextureMaker : public GrTextureMaker { | 276 class Cacherator_GrTextureParamsAdjuster : public GrTextureParamsAdjuster { |
280 public: | 277 public: |
281 Cacherator_GrTextureMaker(SkImageCacherator* cacher, const SkImage* client, | 278 Cacherator_GrTextureParamsAdjuster(SkImageCacherator* cacher, const SkImage*
client) |
282 const GrUniqueKey& unstretchedKey) | |
283 : INHERITED(cacher->info().width(), cacher->info().height()) | 279 : INHERITED(cacher->info().width(), cacher->info().height()) |
284 , fCacher(cacher) | 280 , fCacher(cacher) |
285 , fClient(client) | 281 , fClient(client) |
286 , fUnstretchedKey(unstretchedKey) | 282 { |
287 {} | 283 if (client) { |
| 284 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), |
| 285 SkIRect::MakeWH(this->width(), this->height()))
; |
| 286 } |
| 287 } |
288 | 288 |
289 protected: | 289 protected: |
| 290 GrTexture* peekOriginalTexture() override { return nullptr; } |
| 291 |
290 // TODO: consider overriding this, for the case where the underlying generat
or might be | 292 // TODO: consider overriding this, for the case where the underlying generat
or might be |
291 // able to efficiently produce a "stretched" texture natively (e.g. pi
cture-backed) | 293 // able to efficiently produce a "stretched" texture natively (e.g. pi
cture-backed) |
292 // GrTexture* onGenerateStretchedTexture(GrContext*, const SkGrStretch&) over
ride; | 294 // GrTexture* generateTextureForParams(GrContext*, const SkGrStretch&) ov
erride; |
293 | 295 |
294 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override { | 296 GrTexture* refOriginalTexture(GrContext* ctx) override { |
295 return fCacher->lockUnstretchedTexture(ctx, fClient); | 297 return fCacher->lockTexture(ctx, fOriginalKey, fClient); |
296 } | 298 } |
297 | 299 |
298 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKe
y) override { | 300 void makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) over
ride { |
299 return GrMakeStretchedKey(fUnstretchedKey, stretch, stretchedKey); | 301 if (fOriginalKey.isValid()) { |
| 302 MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey); |
| 303 } |
300 } | 304 } |
301 | 305 |
302 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override { | 306 void didCacheCopy(const GrUniqueKey& copyKey) override { |
303 if (fClient) { | 307 if (fClient) { |
304 as_IB(fClient)->notifyAddedToCache(); | 308 as_IB(fClient)->notifyAddedToCache(); |
305 } | 309 } |
306 } | 310 } |
307 | 311 |
308 bool onGetROBitmap(SkBitmap* bitmap) override { | 312 bool getROBitmap(SkBitmap* bitmap) override { |
309 return fCacher->lockAsBitmap(bitmap, fClient); | 313 return fCacher->lockAsBitmap(bitmap, fClient); |
310 } | 314 } |
311 | 315 |
312 private: | 316 private: |
313 SkImageCacherator* fCacher; | 317 SkImageCacherator* fCacher; |
314 const SkImage* fClient; | 318 const SkImage* fClient; |
315 const GrUniqueKey fUnstretchedKey; | 319 GrUniqueKey fOriginalKey; |
316 | 320 |
317 typedef GrTextureMaker INHERITED; | 321 typedef GrTextureParamsAdjuster INHERITED; |
318 }; | 322 }; |
319 | 323 |
320 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s& params, | 324 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s& params, |
321 const SkImage* client) { | 325 const SkImage* client) { |
322 if (!ctx) { | 326 if (!ctx) { |
323 return nullptr; | 327 return nullptr; |
324 } | 328 } |
325 | 329 |
326 GrUniqueKey key; | 330 return Cacherator_GrTextureParamsAdjuster(this, client).refTextureForParams(
ctx, params); |
327 GrMakeKeyFromImageID(&key, this->uniqueID(), | |
328 SkIRect::MakeWH(this->info().width(), this->info().heig
ht()), | |
329 *ctx->caps(), GrTextureParams::ClampNoFilter()); | |
330 | |
331 return Cacherator_GrTextureMaker(this, client, key).refCachedTexture(ctx, pa
rams); | |
332 } | 331 } |
333 | 332 |
334 #else | 333 #else |
335 | 334 |
336 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, | 335 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, |
337 const SkImage* client) { | 336 const SkImage* client) { |
338 return nullptr; | 337 return nullptr; |
339 } | 338 } |
340 | 339 |
341 #endif | 340 #endif |
OLD | NEW |