Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(400)

Unified Diff: ui/snapshot/snapshot_async.cc

Issue 2592983002: [devtools] Support different encodings for Page.CaptureScreenshot. (Closed)
Patch Set: Encode in ui snapshot methods instead. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/snapshot/snapshot_async.cc
diff --git a/ui/snapshot/snapshot_async.cc b/ui/snapshot/snapshot_async.cc
index a4f0eb07bc44b9abab830015667ef94dd1f36eab..dcee479c103901d776d4b1333bc982ffbb3e1fa5 100644
--- a/ui/snapshot/snapshot_async.cc
+++ b/ui/snapshot/snapshot_async.cc
@@ -11,6 +11,7 @@
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkPixelRef.h"
+#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
@@ -34,27 +35,33 @@ SkBitmap ScaleBitmap(const SkBitmap& input_bitmap,
static_cast<SkBitmap::Allocator*>(NULL));
}
-scoped_refptr<base::RefCountedBytes> EncodeBitmap(const SkBitmap& bitmap) {
- scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
+scoped_refptr<base::RefCountedBytes> EncodeBitmap(const SkBitmap& bitmap,
+ SnapshotEncoding encoding,
+ SnapshotQuality quality) {
+ scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes);
SkAutoLockPixels lock(bitmap);
unsigned char* pixels = reinterpret_cast<unsigned char*>(bitmap.getPixels());
-#if SK_A32_SHIFT == 24 && SK_R32_SHIFT == 16 && SK_G32_SHIFT == 8
- gfx::PNGCodec::ColorFormat kColorFormat = gfx::PNGCodec::FORMAT_BGRA;
-#elif SK_A32_SHIFT == 24 && SK_B32_SHIFT == 16 && SK_G32_SHIFT == 8
- gfx::PNGCodec::ColorFormat kColorFormat = gfx::PNGCodec::FORMAT_RGBA;
-#else
-#error Unknown color format
-#endif
- if (!gfx::PNGCodec::Encode(pixels,
- kColorFormat,
- gfx::Size(bitmap.width(), bitmap.height()),
- base::checked_cast<int>(bitmap.rowBytes()),
- true,
- std::vector<gfx::PNGCodec::Comment>(),
- &png_data->data())) {
- return scoped_refptr<base::RefCountedBytes>();
+ bool encoded = false;
+ switch (encoding) {
+ case SnapshotEncoding::PNG:
+ encoded = gfx::PNGCodec::Encode(
+ pixels, gfx::PNGCodec::FORMAT_SkBitmap,
+ gfx::Size(bitmap.width(), bitmap.height()),
+ base::checked_cast<int>(bitmap.rowBytes()), true,
+ std::vector<gfx::PNGCodec::Comment>(), &data->data());
+ break;
+ case SnapshotEncoding::JPEG:
+ encoded = gfx::JPEGCodec::Encode(
+ pixels, gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(),
+ bitmap.height(), base::checked_cast<int>(bitmap.rowBytes()), quality,
+ &data->data());
+ break;
+ default:
+ NOTREACHED();
}
- return png_data;
+ if (!encoded)
+ return scoped_refptr<base::RefCountedBytes>();
+ return data;
}
} // namespace
@@ -81,8 +88,10 @@ void SnapshotAsync::ScaleCopyOutputResult(
}
void SnapshotAsync::EncodeCopyOutputResult(
- const GrabWindowSnapshotAsyncPNGCallback& callback,
+ const GrabWindowSnapshotAsyncEncodedCallback& callback,
scoped_refptr<base::TaskRunner> background_task_runner,
+ SnapshotEncoding encoding,
+ SnapshotQuality quality,
std::unique_ptr<cc::CopyOutputResult> result) {
if (result->IsEmpty()) {
callback.Run(scoped_refptr<base::RefCountedBytes>());
@@ -94,9 +103,8 @@ void SnapshotAsync::EncodeCopyOutputResult(
// be used here because it's not in content/public. Move the scaling code
// somewhere so that it can be reused here.
base::PostTaskAndReplyWithResult(
- background_task_runner.get(),
- FROM_HERE,
- base::Bind(EncodeBitmap, *result->TakeBitmap()),
+ background_task_runner.get(), FROM_HERE,
+ base::Bind(EncodeBitmap, *result->TakeBitmap(), encoding, quality),
callback);
}

Powered by Google App Engine
This is Rietveld 408576698