| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2008, Google Inc. All rights reserved. | 2 * Copyright (c) 2008, Google Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> | 3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> |
| 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. | 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are | 7 * modification, are permitted provided that the following conditions are |
| 8 * met: | 8 * met: |
| 9 * | 9 * |
| 10 * * Redistributions of source code must retain the above copyright | 10 * * Redistributions of source code must retain the above copyright |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 : m_snapshotState(InitialSnapshotState) | 80 : m_snapshotState(InitialSnapshotState) |
| 81 , m_surface(std::move(surface)) | 81 , m_surface(std::move(surface)) |
| 82 , m_client(0) | 82 , m_client(0) |
| 83 , m_gpuMemoryUsage(0) | 83 , m_gpuMemoryUsage(0) |
| 84 { | 84 { |
| 85 m_surface->setImageBuffer(this); | 85 m_surface->setImageBuffer(this); |
| 86 updateGPUMemoryUsage(); | 86 updateGPUMemoryUsage(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 intptr_t ImageBuffer::s_globalGPUMemoryUsage = 0; | 89 intptr_t ImageBuffer::s_globalGPUMemoryUsage = 0; |
| 90 unsigned ImageBuffer::s_globalAcceleratedImageBufferCount = 0; |
| 90 | 91 |
| 91 ImageBuffer::~ImageBuffer() | 92 ImageBuffer::~ImageBuffer() |
| 92 { | 93 { |
| 94 if (m_gpuMemoryUsage) { |
| 95 DCHECK_GT(s_globalAcceleratedImageBufferCount, 0u); |
| 96 s_globalAcceleratedImageBufferCount--; |
| 97 } |
| 93 ImageBuffer::s_globalGPUMemoryUsage -= m_gpuMemoryUsage; | 98 ImageBuffer::s_globalGPUMemoryUsage -= m_gpuMemoryUsage; |
| 94 } | 99 } |
| 95 | 100 |
| 96 SkCanvas* ImageBuffer::canvas() const | 101 SkCanvas* ImageBuffer::canvas() const |
| 97 { | 102 { |
| 98 return m_surface->canvas(); | 103 return m_surface->canvas(); |
| 99 } | 104 } |
| 100 | 105 |
| 101 void ImageBuffer::disableDeferral(DisableDeferralReason reason) const | 106 void ImageBuffer::disableDeferral(DisableDeferralReason reason) const |
| 102 { | 107 { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 size_t allocSizeInBytes = rect.width() * rect.height() * 4; | 297 size_t allocSizeInBytes = rect.width() * rect.height() * 4; |
| 293 void* data; | 298 void* data; |
| 294 WTF::ArrayBufferContents::allocateMemoryOrNull(allocSizeInBytes, WTF::Ar
rayBufferContents::ZeroInitialize, data); | 299 WTF::ArrayBufferContents::allocateMemoryOrNull(allocSizeInBytes, WTF::Ar
rayBufferContents::ZeroInitialize, data); |
| 295 if (!data) | 300 if (!data) |
| 296 return false; | 301 return false; |
| 297 WTF::ArrayBufferContents result(data, allocSizeInBytes, WTF::ArrayBuffer
Contents::NotShared); | 302 WTF::ArrayBufferContents result(data, allocSizeInBytes, WTF::ArrayBuffer
Contents::NotShared); |
| 298 result.transfer(contents); | 303 result.transfer(contents); |
| 299 return true; | 304 return true; |
| 300 } | 305 } |
| 301 | 306 |
| 302 ASSERT(canvas()); | 307 DCHECK(canvas()); |
| 303 RefPtr<SkImage> snapshot = m_surface->newImageSnapshot(PreferNoAcceleration,
SnapshotReasonGetImageData); | 308 RefPtr<SkImage> snapshot = m_surface->newImageSnapshot(PreferNoAcceleration,
SnapshotReasonGetImageData); |
| 304 if (!snapshot) | 309 if (!snapshot) |
| 305 return false; | 310 return false; |
| 306 | 311 |
| 307 const bool mayHaveStrayArea = | 312 const bool mayHaveStrayArea = |
| 308 m_surface->isAccelerated() // GPU readback may fail silently | 313 m_surface->isAccelerated() // GPU readback may fail silently |
| 309 || rect.x() < 0 | 314 || rect.x() < 0 |
| 310 || rect.y() < 0 | 315 || rect.y() < 0 |
| 311 || rect.maxX() > m_surface->size().width() | 316 || rect.maxX() > m_surface->size().width() |
| 312 || rect.maxY() > m_surface->size().height(); | 317 || rect.maxY() > m_surface->size().height(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 void ImageBuffer::updateGPUMemoryUsage() const | 363 void ImageBuffer::updateGPUMemoryUsage() const |
| 359 { | 364 { |
| 360 if (this->isAccelerated()) { | 365 if (this->isAccelerated()) { |
| 361 // If image buffer is accelerated, we should keep track of GPU memory us
age. | 366 // If image buffer is accelerated, we should keep track of GPU memory us
age. |
| 362 int gpuBufferCount = 2; | 367 int gpuBufferCount = 2; |
| 363 CheckedNumeric<intptr_t> checkedGPUUsage = 4 * gpuBufferCount; | 368 CheckedNumeric<intptr_t> checkedGPUUsage = 4 * gpuBufferCount; |
| 364 checkedGPUUsage *= this->size().width(); | 369 checkedGPUUsage *= this->size().width(); |
| 365 checkedGPUUsage *= this->size().height(); | 370 checkedGPUUsage *= this->size().height(); |
| 366 intptr_t gpuMemoryUsage = checkedGPUUsage.ValueOrDefault(std::numeric_li
mits<intptr_t>::max()); | 371 intptr_t gpuMemoryUsage = checkedGPUUsage.ValueOrDefault(std::numeric_li
mits<intptr_t>::max()); |
| 367 | 372 |
| 373 if (!m_gpuMemoryUsage) // was not accelerated before |
| 374 s_globalAcceleratedImageBufferCount++; |
| 375 |
| 368 s_globalGPUMemoryUsage += (gpuMemoryUsage - m_gpuMemoryUsage); | 376 s_globalGPUMemoryUsage += (gpuMemoryUsage - m_gpuMemoryUsage); |
| 369 m_gpuMemoryUsage = gpuMemoryUsage; | 377 m_gpuMemoryUsage = gpuMemoryUsage; |
| 370 } else if (m_gpuMemoryUsage > 0) { | 378 } else if (m_gpuMemoryUsage) { |
| 371 // In case of switching from accelerated to non-accelerated mode, | 379 // In case of switching from accelerated to non-accelerated mode, |
| 372 // the GPU memory usage needs to be updated too. | 380 // the GPU memory usage needs to be updated too. |
| 381 DCHECK_GT(s_globalAcceleratedImageBufferCount, 0u); |
| 382 s_globalAcceleratedImageBufferCount--; |
| 373 s_globalGPUMemoryUsage -= m_gpuMemoryUsage; | 383 s_globalGPUMemoryUsage -= m_gpuMemoryUsage; |
| 374 m_gpuMemoryUsage = 0; | 384 m_gpuMemoryUsage = 0; |
| 375 } | 385 } |
| 376 } | 386 } |
| 377 | 387 |
| 378 bool ImageDataBuffer::encodeImage(const String& mimeType, const double& quality,
Vector<unsigned char>* encodedImage) const | 388 bool ImageDataBuffer::encodeImage(const String& mimeType, const double& quality,
Vector<unsigned char>* encodedImage) const |
| 379 { | 389 { |
| 380 if (mimeType == "image/jpeg") { | 390 if (mimeType == "image/jpeg") { |
| 381 if (!JPEGImageEncoder::encode(*this, quality, encodedImage)) | 391 if (!JPEGImageEncoder::encode(*this, quality, encodedImage)) |
| 382 return false; | 392 return false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 400 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); | 410 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
| 401 | 411 |
| 402 Vector<unsigned char> result; | 412 Vector<unsigned char> result; |
| 403 if (!encodeImage(mimeType, quality, &result)) | 413 if (!encodeImage(mimeType, quality, &result)) |
| 404 return "data:,"; | 414 return "data:,"; |
| 405 | 415 |
| 406 return "data:" + mimeType + ";base64," + base64Encode(result); | 416 return "data:" + mimeType + ";base64," + base64Encode(result); |
| 407 } | 417 } |
| 408 | 418 |
| 409 } // namespace blink | 419 } // namespace blink |
| OLD | NEW |