OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
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 "SkImageFilter.h" | 8 #include "SkImageFilter.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkDevice.h" | 11 #include "SkDevice.h" |
12 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
14 #include "SkRect.h" | 14 #include "SkRect.h" |
15 #include "SkTDynamicHash.h" | 15 #include "SkTDynamicHash.h" |
16 #include "SkValidationUtils.h" | 16 #include "SkValidationUtils.h" |
17 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
18 #include "GrContext.h" | 18 #include "GrContext.h" |
19 #include "SkGrPixelRef.h" | 19 #include "SkGrPixelRef.h" |
20 #include "SkGr.h" | 20 #include "SkGr.h" |
21 #endif | 21 #endif |
22 | 22 |
| 23 SkImageFilter::Cache* gExternalCache; |
| 24 |
23 SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropR
ect* cropRect) | 25 SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropR
ect* cropRect) |
24 : fInputCount(inputCount), | 26 : fInputCount(inputCount), |
25 fInputs(new SkImageFilter*[inputCount]), | 27 fInputs(new SkImageFilter*[inputCount]), |
26 fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)) { | 28 fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)) { |
27 for (int i = 0; i < inputCount; ++i) { | 29 for (int i = 0; i < inputCount; ++i) { |
28 fInputs[i] = inputs[i]; | 30 fInputs[i] = inputs[i]; |
29 SkSafeRef(fInputs[i]); | 31 SkSafeRef(fInputs[i]); |
30 } | 32 } |
31 } | 33 } |
32 | 34 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 290 } |
289 | 291 |
290 bool SkImageFilter::asNewEffect(GrEffectRef**, GrTexture*, const SkMatrix&, cons
t SkIRect&) const { | 292 bool SkImageFilter::asNewEffect(GrEffectRef**, GrTexture*, const SkMatrix&, cons
t SkIRect&) const { |
291 return false; | 293 return false; |
292 } | 294 } |
293 | 295 |
294 bool SkImageFilter::asColorFilter(SkColorFilter**) const { | 296 bool SkImageFilter::asColorFilter(SkColorFilter**) const { |
295 return false; | 297 return false; |
296 } | 298 } |
297 | 299 |
| 300 void SkImageFilter::SetExternalCache(Cache* cache) { |
| 301 SkRefCnt_SafeAssign(gExternalCache, cache); |
| 302 } |
| 303 |
| 304 SkImageFilter::Cache* SkImageFilter::GetExternalCache() { |
| 305 return gExternalCache; |
| 306 } |
| 307 |
298 #if SK_SUPPORT_GPU | 308 #if SK_SUPPORT_GPU |
299 | 309 |
300 void SkImageFilter::WrapTexture(GrTexture* texture, int width, int height, SkBit
map* result) { | 310 void SkImageFilter::WrapTexture(GrTexture* texture, int width, int height, SkBit
map* result) { |
301 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | 311 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
302 result->setConfig(info); | 312 result->setConfig(info); |
303 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); | 313 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
304 } | 314 } |
305 | 315 |
306 bool SkImageFilter::getInputResultGPU(SkImageFilter::Proxy* proxy, | 316 bool SkImageFilter::getInputResultGPU(SkImageFilter::Proxy* proxy, |
307 const SkBitmap& src, const Context& ctx, | 317 const SkBitmap& src, const Context& ctx, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 | 366 |
357 return hash; | 367 return hash; |
358 } | 368 } |
359 | 369 |
360 class CacheImpl : public SkImageFilter::Cache { | 370 class CacheImpl : public SkImageFilter::Cache { |
361 public: | 371 public: |
362 explicit CacheImpl(int minChildren) : fMinChildren(minChildren) {} | 372 explicit CacheImpl(int minChildren) : fMinChildren(minChildren) {} |
363 virtual ~CacheImpl(); | 373 virtual ~CacheImpl(); |
364 bool get(const SkImageFilter* key, SkBitmap* result, SkIPoint* offset) SK_OV
ERRIDE; | 374 bool get(const SkImageFilter* key, SkBitmap* result, SkIPoint* offset) SK_OV
ERRIDE; |
365 void set(const SkImageFilter* key, const SkBitmap& result, const SkIPoint& o
ffset) SK_OVERRIDE; | 375 void set(const SkImageFilter* key, const SkBitmap& result, const SkIPoint& o
ffset) SK_OVERRIDE; |
| 376 void remove(const SkImageFilter* key) SK_OVERRIDE; |
366 private: | 377 private: |
367 typedef const SkImageFilter* Key; | 378 typedef const SkImageFilter* Key; |
368 struct Value { | 379 struct Value { |
369 Value(Key key, const SkBitmap& bitmap, const SkIPoint& offset) | 380 Value(Key key, const SkBitmap& bitmap, const SkIPoint& offset) |
370 : fKey(key), fBitmap(bitmap), fOffset(offset) {} | 381 : fKey(key), fBitmap(bitmap), fOffset(offset) {} |
371 Key fKey; | 382 Key fKey; |
372 SkBitmap fBitmap; | 383 SkBitmap fBitmap; |
373 SkIPoint fOffset; | 384 SkIPoint fOffset; |
374 static const Key& GetKey(const Value& v) { | 385 static const Key& GetKey(const Value& v) { |
375 return v.fKey; | 386 return v.fKey; |
376 } | 387 } |
377 static uint32_t Hash(Key key) { | 388 static uint32_t Hash(Key key) { |
378 return compute_hash(reinterpret_cast<const uint32_t*>(&key), sizeof(
Key) / sizeof(uint32_t)); | 389 return compute_hash(reinterpret_cast<const uint32_t*>(&key), sizeof(
Key) / sizeof(uint32_t)); |
379 } | 390 } |
380 }; | 391 }; |
381 SkTDynamicHash<Value, Key> fData; | 392 SkTDynamicHash<Value, Key> fData; |
382 int fMinChildren; | 393 int fMinChildren; |
383 }; | 394 }; |
384 | 395 |
385 bool CacheImpl::get(const SkImageFilter* key, SkBitmap* result, SkIPoint* offset
) { | 396 bool CacheImpl::get(const SkImageFilter* key, SkBitmap* result, SkIPoint* offset
) { |
386 Value* v = fData.find(key); | 397 Value* v = fData.find(key); |
387 if (v) { | 398 if (v) { |
388 *result = v->fBitmap; | 399 *result = v->fBitmap; |
389 *offset = v->fOffset; | 400 *offset = v->fOffset; |
390 return true; | 401 return true; |
391 } | 402 } |
392 return false; | 403 return false; |
393 } | 404 } |
394 | 405 |
| 406 void CacheImpl::remove(const SkImageFilter* key) { |
| 407 Value* v = fData.find(key); |
| 408 if (v) { |
| 409 fData.remove(key); |
| 410 delete v; |
| 411 } |
| 412 } |
| 413 |
395 void CacheImpl::set(const SkImageFilter* key, const SkBitmap& result, const SkIP
oint& offset) { | 414 void CacheImpl::set(const SkImageFilter* key, const SkBitmap& result, const SkIP
oint& offset) { |
396 if (key->getRefCnt() >= fMinChildren) { | 415 if (key->getRefCnt() >= fMinChildren) { |
397 fData.add(new Value(key, result, offset)); | 416 fData.add(new Value(key, result, offset)); |
398 } | 417 } |
399 } | 418 } |
400 | 419 |
401 SkImageFilter::Cache* SkImageFilter::Cache::Create(int minChildren) { | 420 SkImageFilter::Cache* SkImageFilter::Cache::Create(int minChildren) { |
402 return new CacheImpl(minChildren); | 421 return new CacheImpl(minChildren); |
403 } | 422 } |
404 | 423 |
405 CacheImpl::~CacheImpl() { | 424 CacheImpl::~CacheImpl() { |
406 SkTDynamicHash<Value, Key>::Iter iter(&fData); | 425 SkTDynamicHash<Value, Key>::Iter iter(&fData); |
407 | 426 |
408 while (!iter.done()) { | 427 while (!iter.done()) { |
409 Value* v = &*iter; | 428 Value* v = &*iter; |
410 ++iter; | 429 ++iter; |
411 delete v; | 430 delete v; |
412 } | 431 } |
413 } | 432 } |
OLD | NEW |