| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 if (!surface->isValid()) | 92 if (!surface->isValid()) |
| 93 return nullptr; | 93 return nullptr; |
| 94 return WTF::wrapUnique(new ImageBuffer(std::move(surface))); | 94 return WTF::wrapUnique(new ImageBuffer(std::move(surface))); |
| 95 } | 95 } |
| 96 | 96 |
| 97 ImageBuffer::ImageBuffer(std::unique_ptr<ImageBufferSurface> surface) | 97 ImageBuffer::ImageBuffer(std::unique_ptr<ImageBufferSurface> surface) |
| 98 : m_weakPtrFactory(this), | 98 : m_weakPtrFactory(this), |
| 99 m_snapshotState(InitialSnapshotState), | 99 m_snapshotState(InitialSnapshotState), |
| 100 m_surface(std::move(surface)), | 100 m_surface(std::move(surface)), |
| 101 m_client(0), | 101 m_client(0), |
| 102 m_gpuReadbackInvokedInCurrentFrame(false), |
| 103 m_gpuReadbackSuccessiveFrames(0), |
| 102 m_gpuMemoryUsage(0) { | 104 m_gpuMemoryUsage(0) { |
| 103 m_surface->setImageBuffer(this); | 105 m_surface->setImageBuffer(this); |
| 104 updateGPUMemoryUsage(); | 106 updateGPUMemoryUsage(); |
| 105 } | 107 } |
| 106 | 108 |
| 107 intptr_t ImageBuffer::s_globalGPUMemoryUsage = 0; | 109 intptr_t ImageBuffer::s_globalGPUMemoryUsage = 0; |
| 108 unsigned ImageBuffer::s_globalAcceleratedImageBufferCount = 0; | 110 unsigned ImageBuffer::s_globalAcceleratedImageBufferCount = 0; |
| 109 | 111 |
| 110 ImageBuffer::~ImageBuffer() { | 112 ImageBuffer::~ImageBuffer() { |
| 111 if (m_gpuMemoryUsage) { | 113 if (m_gpuMemoryUsage) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 142 int x, | 144 int x, |
| 143 int y) { | 145 int y) { |
| 144 return m_surface->writePixels(info, pixels, rowBytes, x, y); | 146 return m_surface->writePixels(info, pixels, rowBytes, x, y); |
| 145 } | 147 } |
| 146 | 148 |
| 147 bool ImageBuffer::isSurfaceValid() const { | 149 bool ImageBuffer::isSurfaceValid() const { |
| 148 return m_surface->isValid(); | 150 return m_surface->isValid(); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void ImageBuffer::finalizeFrame() { | 153 void ImageBuffer::finalizeFrame() { |
| 154 if (isAccelerated() && |
| 155 ExpensiveCanvasHeuristicParameters::GPUReadbackForcesNoAcceleration && |
| 156 !RuntimeEnabledFeatures::canvas2dFixedRenderingModeEnabled()) { |
| 157 if (m_gpuReadbackInvokedInCurrentFrame) { |
| 158 m_gpuReadbackSuccessiveFrames++; |
| 159 m_gpuReadbackInvokedInCurrentFrame = false; |
| 160 } else { |
| 161 m_gpuReadbackSuccessiveFrames = 0; |
| 162 } |
| 163 |
| 164 if (m_gpuReadbackSuccessiveFrames >= |
| 165 ExpensiveCanvasHeuristicParameters::GPUReadbackMinSuccessiveFrames) { |
| 166 disableAcceleration(); |
| 167 } |
| 168 } |
| 169 |
| 152 m_surface->finalizeFrame(); | 170 m_surface->finalizeFrame(); |
| 153 } | 171 } |
| 154 | 172 |
| 155 void ImageBuffer::doPaintInvalidation(const FloatRect& dirtyRect) { | 173 void ImageBuffer::doPaintInvalidation(const FloatRect& dirtyRect) { |
| 156 m_surface->doPaintInvalidation(dirtyRect); | 174 m_surface->doPaintInvalidation(dirtyRect); |
| 157 } | 175 } |
| 158 | 176 |
| 159 bool ImageBuffer::restoreSurface() const { | 177 bool ImageBuffer::restoreSurface() const { |
| 160 return m_surface->isValid() || m_surface->restore(); | 178 return m_surface->isValid() || m_surface->restore(); |
| 161 } | 179 } |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 if (!data) | 369 if (!data) |
| 352 return false; | 370 return false; |
| 353 WTF::ArrayBufferContents result(std::move(data), allocSizeInBytes, | 371 WTF::ArrayBufferContents result(std::move(data), allocSizeInBytes, |
| 354 WTF::ArrayBufferContents::NotShared); | 372 WTF::ArrayBufferContents::NotShared); |
| 355 result.transfer(contents); | 373 result.transfer(contents); |
| 356 return true; | 374 return true; |
| 357 } | 375 } |
| 358 | 376 |
| 359 DCHECK(canvas()); | 377 DCHECK(canvas()); |
| 360 | 378 |
| 361 if (ExpensiveCanvasHeuristicParameters::GetImageDataForcesNoAcceleration && | |
| 362 !RuntimeEnabledFeatures::canvas2dFixedRenderingModeEnabled()) { | |
| 363 const_cast<ImageBuffer*>(this)->disableAcceleration(); | |
| 364 } | |
| 365 | |
| 366 sk_sp<SkImage> snapshot = m_surface->newImageSnapshot( | 379 sk_sp<SkImage> snapshot = m_surface->newImageSnapshot( |
| 367 PreferNoAcceleration, SnapshotReasonGetImageData); | 380 PreferNoAcceleration, SnapshotReasonGetImageData); |
| 368 if (!snapshot) | 381 if (!snapshot) |
| 369 return false; | 382 return false; |
| 370 | 383 |
| 371 const bool mayHaveStrayArea = | 384 const bool mayHaveStrayArea = |
| 372 m_surface->isAccelerated() // GPU readback may fail silently | 385 m_surface->isAccelerated() // GPU readback may fail silently |
| 373 || rect.x() < 0 || rect.y() < 0 || | 386 || rect.x() < 0 || rect.y() < 0 || |
| 374 rect.maxX() > m_surface->size().width() || | 387 rect.maxX() > m_surface->size().width() || |
| 375 rect.maxY() > m_surface->size().height(); | 388 rect.maxY() > m_surface->size().height(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 400 sk_sp<SkColorSpace> colorSpace = nullptr; | 413 sk_sp<SkColorSpace> colorSpace = nullptr; |
| 401 if (m_surface->colorSpace()) { | 414 if (m_surface->colorSpace()) { |
| 402 colorSpace = SkColorSpace::MakeSRGB(); | 415 colorSpace = SkColorSpace::MakeSRGB(); |
| 403 } | 416 } |
| 404 | 417 |
| 405 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), colorType, | 418 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), colorType, |
| 406 alphaType, std::move(colorSpace)); | 419 alphaType, std::move(colorSpace)); |
| 407 | 420 |
| 408 snapshot->readPixels(info, result.data(), bytesPerPixel * rect.width(), | 421 snapshot->readPixels(info, result.data(), bytesPerPixel * rect.width(), |
| 409 rect.x(), rect.y()); | 422 rect.x(), rect.y()); |
| 423 m_gpuReadbackInvokedInCurrentFrame = true; |
| 410 | 424 |
| 411 if (useF16Workaround) { | 425 if (useF16Workaround) { |
| 412 uint32_t* pixel = (uint32_t*)result.data(); | 426 uint32_t* pixel = (uint32_t*)result.data(); |
| 413 size_t pixelCount = allocSizeInBytes / sizeof(uint32_t); | 427 size_t pixelCount = allocSizeInBytes / sizeof(uint32_t); |
| 414 // TODO(skbug.com/5853): make readPixels support RGBA output so that we no | 428 // TODO(skbug.com/5853): make readPixels support RGBA output so that we no |
| 415 // longer | 429 // longer |
| 416 // have to do this. | 430 // have to do this. |
| 417 if (kN32_SkColorType == kBGRA_8888_SkColorType) { | 431 if (kN32_SkColorType == kBGRA_8888_SkColorType) { |
| 418 // Convert BGRA to RGBA if necessary on this platform. | 432 // Convert BGRA to RGBA if necessary on this platform. |
| 419 SkSwapRB(pixel, pixel, pixelCount); | 433 SkSwapRB(pixel, pixel, pixelCount); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 DCHECK(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); | 620 DCHECK(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
| 607 | 621 |
| 608 Vector<unsigned char> result; | 622 Vector<unsigned char> result; |
| 609 if (!encodeImage(mimeType, quality, &result)) | 623 if (!encodeImage(mimeType, quality, &result)) |
| 610 return "data:,"; | 624 return "data:,"; |
| 611 | 625 |
| 612 return "data:" + mimeType + ";base64," + base64Encode(result); | 626 return "data:" + mimeType + ";base64," + base64Encode(result); |
| 613 } | 627 } |
| 614 | 628 |
| 615 } // namespace blink | 629 } // namespace blink |
| OLD | NEW |