diff options
Diffstat (limited to 'chromium/skia/ext')
22 files changed, 578 insertions, 817 deletions
diff --git a/chromium/skia/ext/SkDiscardableMemory_chrome.cc b/chromium/skia/ext/SkDiscardableMemory_chrome.cc index 1a98720bf36..30a7f93c836 100644 --- a/chromium/skia/ext/SkDiscardableMemory_chrome.cc +++ b/chromium/skia/ext/SkDiscardableMemory_chrome.cc @@ -26,6 +26,13 @@ SkDiscardableMemoryChrome::SkDiscardableMemoryChrome( : discardable_(memory.Pass()) { } +base::trace_event::MemoryAllocatorDump* +SkDiscardableMemoryChrome::CreateMemoryAllocatorDump( + const char* name, + base::trace_event::ProcessMemoryDump* pmd) const { + return discardable_->CreateMemoryAllocatorDump(name, pmd); +} + SkDiscardableMemory* SkDiscardableMemory::Create(size_t bytes) { return new SkDiscardableMemoryChrome( base::DiscardableMemoryAllocator::GetInstance() diff --git a/chromium/skia/ext/SkDiscardableMemory_chrome.h b/chromium/skia/ext/SkDiscardableMemory_chrome.h index 1be4516be16..93a7807b5f2 100644 --- a/chromium/skia/ext/SkDiscardableMemory_chrome.h +++ b/chromium/skia/ext/SkDiscardableMemory_chrome.h @@ -10,8 +10,14 @@ namespace base { class DiscardableMemory; + +namespace trace_event { +class MemoryAllocatorDump; +class ProcessMemoryDump; } +} // namespace base + // This class implements the SkDiscardableMemory interface using // base::DiscardableMemory. class SK_API SkDiscardableMemoryChrome : public SkDiscardableMemory { @@ -23,6 +29,10 @@ public: void* data() override; void unlock() override; + base::trace_event::MemoryAllocatorDump* CreateMemoryAllocatorDump( + const char* name, + base::trace_event::ProcessMemoryDump* pmd) const; + private: friend class SkDiscardableMemory; diff --git a/chromium/skia/ext/SkTraceMemoryDump_chrome.cc b/chromium/skia/ext/SkTraceMemoryDump_chrome.cc new file mode 100644 index 00000000000..c4bda0cd672 --- /dev/null +++ b/chromium/skia/ext/SkTraceMemoryDump_chrome.cc @@ -0,0 +1,87 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "skia/ext/SkTraceMemoryDump_chrome.h" + +#include "base/trace_event/memory_allocator_dump.h" +#include "base/trace_event/memory_dump_manager.h" +#include "base/trace_event/process_memory_dump.h" +#include "skia/ext/SkDiscardableMemory_chrome.h" + +namespace skia { + +namespace { +const char kMallocBackingType[] = "malloc"; +} + +SkTraceMemoryDump_Chrome::SkTraceMemoryDump_Chrome( + base::trace_event::MemoryDumpLevelOfDetail level_of_detail, + base::trace_event::ProcessMemoryDump* process_memory_dump) + : SkTraceMemoryDump_Chrome("", level_of_detail, process_memory_dump) {} + +SkTraceMemoryDump_Chrome::SkTraceMemoryDump_Chrome( + const std::string& dump_name_prefix, + base::trace_event::MemoryDumpLevelOfDetail level_of_detail, + base::trace_event::ProcessMemoryDump* process_memory_dump) + : dump_name_prefix_(dump_name_prefix), + process_memory_dump_(process_memory_dump), + request_level_( + level_of_detail == base::trace_event::MemoryDumpLevelOfDetail::LIGHT + ? SkTraceMemoryDump::kLight_LevelOfDetail + : SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail) {} + +SkTraceMemoryDump_Chrome::~SkTraceMemoryDump_Chrome() {} + +void SkTraceMemoryDump_Chrome::dumpNumericValue(const char* dumpName, + const char* valueName, + const char* units, + uint64_t value) { + auto dump = GetOrCreateAllocatorDump(dumpName); + dump->AddScalar(valueName, units, value); +} + +void SkTraceMemoryDump_Chrome::setMemoryBacking(const char* dumpName, + const char* backingType, + const char* backingObjectId) { + if (strcmp(backingType, kMallocBackingType) == 0) { + auto dump = GetOrCreateAllocatorDump(dumpName); + const char* system_allocator_name = + base::trace_event::MemoryDumpManager::GetInstance() + ->system_allocator_pool_name(); + if (system_allocator_name) { + process_memory_dump_->AddSuballocation(dump->guid(), + system_allocator_name); + } + } else { + NOTREACHED(); + } +} + +void SkTraceMemoryDump_Chrome::setDiscardableMemoryBacking( + const char* dumpName, + const SkDiscardableMemory& discardableMemoryObject) { + std::string name = dump_name_prefix_ + dumpName; + DCHECK(!process_memory_dump_->GetAllocatorDump(name)); + const SkDiscardableMemoryChrome& discardable_memory_obj = + static_cast<const SkDiscardableMemoryChrome&>(discardableMemoryObject); + auto dump = discardable_memory_obj.CreateMemoryAllocatorDump( + name.c_str(), process_memory_dump_); + DCHECK(dump); +} + +SkTraceMemoryDump::LevelOfDetail SkTraceMemoryDump_Chrome::getRequestedDetails() + const { + return request_level_; +} + +base::trace_event::MemoryAllocatorDump* +SkTraceMemoryDump_Chrome::GetOrCreateAllocatorDump(const char* dumpName) { + std::string name = dump_name_prefix_ + dumpName; + auto dump = process_memory_dump_->GetAllocatorDump(name); + if (!dump) + dump = process_memory_dump_->CreateAllocatorDump(name); + return dump; +} + +} // namespace skia diff --git a/chromium/skia/ext/SkTraceMemoryDump_chrome.h b/chromium/skia/ext/SkTraceMemoryDump_chrome.h new file mode 100644 index 00000000000..42a0e0e2c86 --- /dev/null +++ b/chromium/skia/ext/SkTraceMemoryDump_chrome.h @@ -0,0 +1,76 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKIA_EXT_SK_TRACE_MEMORY_DUMP_CHROME_H_ +#define SKIA_EXT_SK_TRACE_MEMORY_DUMP_CHROME_H_ + +#include <string> + +#include "base/basictypes.h" +#include "base/macros.h" +#include "base/trace_event/memory_dump_request_args.h" +#include "third_party/skia/include/core/SkTraceMemoryDump.h" + +namespace base { +namespace trace_event { +class MemoryAllocatorDump; +class ProcessMemoryDump; +} +} + +namespace skia { + +class SK_API SkTraceMemoryDump_Chrome : public SkTraceMemoryDump { + public: + // This should never outlive the OnMemoryDump call since the + // ProcessMemoryDump is valid only in that timeframe. Optional + // |dump_name_prefix| argument specifies the prefix appended to the dump + // name skia provides. By default it is taken as empty string. + SkTraceMemoryDump_Chrome( + base::trace_event::MemoryDumpLevelOfDetail level_of_detail, + base::trace_event::ProcessMemoryDump* process_memory_dump); + + SkTraceMemoryDump_Chrome( + const std::string& dump_name_prefix, + base::trace_event::MemoryDumpLevelOfDetail level_of_detail, + base::trace_event::ProcessMemoryDump* process_memory_dump); + + ~SkTraceMemoryDump_Chrome() override; + + // SkTraceMemoryDump implementation: + void dumpNumericValue(const char* dumpName, + const char* valueName, + const char* units, + uint64_t value) override; + void setMemoryBacking(const char* dumpName, + const char* backingType, + const char* backingObjectId) override; + void setDiscardableMemoryBacking( + const char* dumpName, + const SkDiscardableMemory& discardableMemoryObject) override; + LevelOfDetail getRequestedDetails() const override; + + protected: + base::trace_event::ProcessMemoryDump* process_memory_dump() { + return process_memory_dump_; + } + + private: + // Helper to create allocator dumps. + base::trace_event::MemoryAllocatorDump* GetOrCreateAllocatorDump( + const char* dumpName); + + std::string dump_name_prefix_; + + base::trace_event::ProcessMemoryDump* process_memory_dump_; + + // Stores the level of detail for the current dump. + LevelOfDetail request_level_; + + DISALLOW_COPY_AND_ASSIGN(SkTraceMemoryDump_Chrome); +}; + +} // namespace skia + +#endif // SKIA_EXT_SK_TRACE_MEMORY_DUMP_CHROME_H_ diff --git a/chromium/skia/ext/analysis_canvas.cc b/chromium/skia/ext/analysis_canvas.cc index 102c63e1601..ec40825f5b7 100644 --- a/chromium/skia/ext/analysis_canvas.cc +++ b/chromium/skia/ext/analysis_canvas.cc @@ -6,9 +6,9 @@ #include "base/trace_event/trace_event.h" #include "skia/ext/analysis_canvas.h" #include "third_party/skia/include/core/SkDraw.h" +#include "third_party/skia/include/core/SkPath.h" #include "third_party/skia/include/core/SkRRect.h" #include "third_party/skia/include/core/SkShader.h" -#include "third_party/skia/src/core/SkRasterClip.h" namespace { @@ -194,7 +194,7 @@ void AnalysisCanvas::onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint* paint, - DrawBitmapRectFlags flags) { + SrcRectConstraint) { // Call drawRect to determine transparency, // but reset solid color to false. SkPaint tmpPaint; @@ -226,7 +226,8 @@ void AnalysisCanvas::onDrawImage(const SkImage*, void AnalysisCanvas::onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, - const SkPaint* paint) { + const SkPaint* paint, + SrcRectConstraint) { // Call drawRect to determine transparency, // but reset solid color to false. SkPaint tmpPaint; diff --git a/chromium/skia/ext/analysis_canvas.h b/chromium/skia/ext/analysis_canvas.h index 5a596475dfd..4c53e3d8925 100644 --- a/chromium/skia/ext/analysis_canvas.h +++ b/chromium/skia/ext/analysis_canvas.h @@ -47,7 +47,7 @@ class SK_API AnalysisCanvas : public SkCanvas, public SkPicture::AbortCallback { const SkRect* src, const SkRect& dst, const SkPaint* paint, - DrawBitmapRectFlags flags) override; + SrcRectConstraint) override; void onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, @@ -57,9 +57,10 @@ class SK_API AnalysisCanvas : public SkCanvas, public SkPicture::AbortCallback { SkScalar top, const SkPaint* paint = NULL) override; void onDrawImageRect(const SkImage*, - const SkRect* src, - const SkRect& dst, - const SkPaint* paint) override; + const SkRect* src, + const SkRect& dst, + const SkPaint* paint, + SrcRectConstraint) override; void onDrawSprite(const SkBitmap&, int left, int top, diff --git a/chromium/skia/ext/benchmarking_canvas.cc b/chromium/skia/ext/benchmarking_canvas.cc index 904d6bdb0bf..752e398eeec 100644 --- a/chromium/skia/ext/benchmarking_canvas.cc +++ b/chromium/skia/ext/benchmarking_canvas.cc @@ -8,11 +8,14 @@ #include "skia/ext/benchmarking_canvas.h" #include "third_party/skia/include/core/SkColorFilter.h" #include "third_party/skia/include/core/SkImageFilter.h" -#include "third_party/skia/include/core/SkTLazy.h" +#include "third_party/skia/include/core/SkPaint.h" +#include "third_party/skia/include/core/SkPath.h" #include "third_party/skia/include/core/SkPicture.h" #include "third_party/skia/include/core/SkRegion.h" +#include "third_party/skia/include/core/SkRRect.h" #include "third_party/skia/include/core/SkString.h" #include "third_party/skia/include/core/SkTextBlob.h" +#include "third_party/skia/include/core/SkTLazy.h" #include "third_party/skia/include/core/SkXfermode.h" namespace { @@ -307,14 +310,6 @@ scoped_ptr<base::Value> AsValue(const SkRegion& region) { } WARN_UNUSED_RESULT -scoped_ptr<base::Value> AsValue(const SkPicture& picture) { - scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue()); - val->Set("cull-rect", AsValue(picture.cullRect())); - - return val.Pass(); -} - -WARN_UNUSED_RESULT scoped_ptr<base::Value> AsValue(const SkBitmap& bitmap) { scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue()); val->Set("size", AsValue(SkSize::Make(bitmap.width(), bitmap.height()))); @@ -360,15 +355,15 @@ scoped_ptr<base::Value> AsValue(const SkPath& path) { { "move", "line", "quad", "conic", "cubic", "close", "done" }; static const int gPtsPerVerb[] = { 1, 1, 2, 2, 3, 0, 0 }; static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 1, 0, 0 }; - SK_COMPILE_ASSERT( + static_assert( SK_ARRAY_COUNT(gVerbStrings) == static_cast<size_t>(SkPath::kDone_Verb + 1), - gVerbStrings_size_mismatch); - SK_COMPILE_ASSERT( + "gVerbStrings size mismatch"); + static_assert( SK_ARRAY_COUNT(gVerbStrings) == SK_ARRAY_COUNT(gPtsPerVerb), - gPtsPerVerb_size_mismatch); - SK_COMPILE_ASSERT( + "gPtsPerVerb size mismatch"); + static_assert( SK_ARRAY_COUNT(gVerbStrings) == SK_ARRAY_COUNT(gPtOffsetPerVerb), - gPtOffsetPerVerb_size_mismatch); + "gPtOffsetPerVerb size mismatch"); scoped_ptr<base::ListValue> verbs_val(new base::ListValue()); SkPath::Iter iter(const_cast<SkPath&>(path), false); @@ -702,14 +697,14 @@ void BenchmarkingCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, const SkPaint* paint, - DrawBitmapRectFlags flags) { + SrcRectConstraint constraint) { AutoOp op(this, "DrawBitmapRect", paint); op.addParam("bitmap", AsValue(bitmap)); if (src) op.addParam("src", AsValue(*src)); op.addParam("dst", AsValue(dst)); - INHERITED::onDrawBitmapRect(bitmap, src, dst, op.paint(), flags); + INHERITED::onDrawBitmapRect(bitmap, src, dst, op.paint(), constraint); } void BenchmarkingCanvas::onDrawImage(const SkImage* image, @@ -726,7 +721,8 @@ void BenchmarkingCanvas::onDrawImage(const SkImage* image, } void BenchmarkingCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, - const SkRect& dst, const SkPaint* paint) { + const SkRect& dst, const SkPaint* paint, + SrcRectConstraint constraint) { DCHECK(image); AutoOp op(this, "DrawImageRect", paint); op.addParam("image", AsValue(*image)); @@ -734,7 +730,7 @@ void BenchmarkingCanvas::onDrawImageRect(const SkImage* image, const SkRect* src op.addParam("src", AsValue(*src)); op.addParam("dst", AsValue(dst)); - INHERITED::onDrawImageRect(image, src, dst, op.paint()); + INHERITED::onDrawImageRect(image, src, dst, op.paint(), constraint); } void BenchmarkingCanvas::onDrawBitmapNine(const SkBitmap& bitmap, diff --git a/chromium/skia/ext/benchmarking_canvas.h b/chromium/skia/ext/benchmarking_canvas.h index a33c42d9fd1..8fbbae44bfd 100644 --- a/chromium/skia/ext/benchmarking_canvas.h +++ b/chromium/skia/ext/benchmarking_canvas.h @@ -60,10 +60,10 @@ protected: void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override; void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, - const SkPaint*, DrawBitmapRectFlags flags) override; + const SkPaint*, SrcRectConstraint) override; void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override; void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, - const SkPaint*) override; + const SkPaint*, SrcRectConstraint) override; void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst, const SkPaint*) override; void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override; diff --git a/chromium/skia/ext/bitmap_platform_device_mac.cc b/chromium/skia/ext/bitmap_platform_device_mac.cc index 066e93ff198..087cf39d7fd 100644 --- a/chromium/skia/ext/bitmap_platform_device_mac.cc +++ b/chromium/skia/ext/bitmap_platform_device_mac.cc @@ -13,6 +13,7 @@ #include "skia/ext/platform_canvas.h" #include "skia/ext/skia_utils_mac.h" #include "third_party/skia/include/core/SkMatrix.h" +#include "third_party/skia/include/core/SkPath.h" #include "third_party/skia/include/core/SkRegion.h" #include "third_party/skia/include/core/SkTypes.h" #include "third_party/skia/include/core/SkUtils.h" diff --git a/chromium/skia/ext/bitmap_platform_device_win.cc b/chromium/skia/ext/bitmap_platform_device_win.cc index 63108209abc..5712df3eaea 100644 --- a/chromium/skia/ext/bitmap_platform_device_win.cc +++ b/chromium/skia/ext/bitmap_platform_device_win.cc @@ -7,6 +7,7 @@ #include "base/debug/gdi_debug_util_win.h" #include "base/logging.h" +#include "base/win/win_util.h" #include "skia/ext/bitmap_platform_device_win.h" #include "skia/ext/platform_canvas.h" #include "third_party/skia/include/core/SkMatrix.h" @@ -108,7 +109,12 @@ void BitmapPlatformDevice::LoadConfig() { } static void DeleteHBitmapCallback(void* addr, void* context) { - DeleteObject(static_cast<HBITMAP>(context)); + // If context is not NULL then it's a valid HBITMAP to delete. + // Otherwise we just unmap the pixel memory. + if (context) + DeleteObject(static_cast<HBITMAP>(context)); + else + UnmapViewOfFile(addr); } static bool InstallHBitmapPixels(SkBitmap* bitmap, int width, int height, @@ -133,10 +139,25 @@ BitmapPlatformDevice* BitmapPlatformDevice::Create( bool do_clear) { void* data; - HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, shared_section, - &data); - if (!hbitmap) - return NULL; + HBITMAP hbitmap = NULL; + + // This function contains an implementation of a Skia platform bitmap for + // drawing and compositing graphics. The original implementation uses Windows + // GDI to create the backing bitmap memory, however it's possible for a + // process to not have access to GDI which will cause this code to fail. It's + // possible to detect when GDI is unavailable and instead directly map the + // shared memory as the bitmap. + if (base::win::IsUser32AndGdi32Available()) { + hbitmap = CreateHBitmap(width, height, is_opaque, shared_section, &data); + if (!hbitmap) + return NULL; + } else { + DCHECK(shared_section != NULL); + data = MapViewOfFile(shared_section, FILE_MAP_WRITE, 0, 0, + PlatformCanvasStrideForWidth(width) * height); + if (!data) + return NULL; + } SkBitmap bitmap; if (!InstallHBitmapPixels(&bitmap, width, height, is_opaque, data, hbitmap)) @@ -179,13 +200,15 @@ BitmapPlatformDevice::BitmapPlatformDevice( transform_(SkMatrix::I()) { // The data object is already ref'ed for us by create(). SkDEBUGCODE(begin_paint_count_ = 0); - SetPlatformDevice(this, this); - // Initialize the clip region to the entire bitmap. - BITMAP bitmap_data; - if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) { - SkIRect rect; - rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); - clip_region_ = SkRegion(rect); + if (hbitmap) { + SetPlatformDevice(this, this); + // Initialize the clip region to the entire bitmap. + BITMAP bitmap_data; + if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) { + SkIRect rect; + rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); + clip_region_ = SkRegion(rect); + } } } diff --git a/chromium/skia/ext/image_operations_bench.cc b/chromium/skia/ext/image_operations_bench.cc index 4fd81bc0b33..9b2667fec35 100644 --- a/chromium/skia/ext/image_operations_bench.cc +++ b/chromium/skia/ext/image_operations_bench.cc @@ -50,7 +50,7 @@ const StringMethodPair resize_methods[] = { bool StringToMethod(const std::string& arg, skia::ImageOperations::ResizeMethod* method) { for (size_t i = 0; i < arraysize(resize_methods); ++i) { - if (base::strcasecmp(arg.c_str(), resize_methods[i].name) == 0) { + if (base::EqualsCaseInsensitiveASCII(arg, resize_methods[i].name)) { *method = resize_methods[i].method; return true; } @@ -119,8 +119,8 @@ class Dimensions { // On failure, will set its state in such a way that IsValid will return // false. void FromString(const std::string& arg) { - std::vector<std::string> strings; - base::SplitString(std::string(arg), 'x', &strings); + std::vector<base::StringPiece> strings = base::SplitStringPiece( + arg, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); if (strings.size() != 2 || base::StringToInt(strings[0], &width_) == false || base::StringToInt(strings[1], &height_) == false) { diff --git a/chromium/skia/ext/pixel_ref_utils.cc b/chromium/skia/ext/pixel_ref_utils.cc deleted file mode 100644 index 86e2c09b6a3..00000000000 --- a/chromium/skia/ext/pixel_ref_utils.cc +++ /dev/null @@ -1,384 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "skia/ext/pixel_ref_utils.h" - -#include <algorithm> - -#include "third_party/skia/include/core/SkBitmapDevice.h" -#include "third_party/skia/include/core/SkCanvas.h" -#include "third_party/skia/include/core/SkData.h" -#include "third_party/skia/include/core/SkDraw.h" -#include "third_party/skia/include/core/SkPixelRef.h" -#include "third_party/skia/include/core/SkRRect.h" -#include "third_party/skia/include/core/SkRect.h" -#include "third_party/skia/include/core/SkShader.h" -#include "third_party/skia/include/utils/SkNoSaveLayerCanvas.h" -#include "third_party/skia/src/core/SkRasterClip.h" - -namespace skia { - -namespace { - -// URI label for a discardable SkPixelRef. -const char kLabelDiscardable[] = "discardable"; - -class DiscardablePixelRefSet { - public: - DiscardablePixelRefSet( - std::vector<PixelRefUtils::PositionPixelRef>* pixel_refs) - : pixel_refs_(pixel_refs) {} - - void Add(SkPixelRef* pixel_ref, - const SkRect& rect, - const SkMatrix& matrix, - SkFilterQuality filter_quality) { - // Only save discardable pixel refs. - if (pixel_ref->getURI() && - !strcmp(pixel_ref->getURI(), kLabelDiscardable)) { - PixelRefUtils::PositionPixelRef position_pixel_ref; - position_pixel_ref.pixel_ref = pixel_ref; - position_pixel_ref.pixel_ref_rect = rect; - position_pixel_ref.matrix = matrix; - position_pixel_ref.filter_quality = filter_quality; - pixel_refs_->push_back(position_pixel_ref); - } - } - - private: - std::vector<PixelRefUtils::PositionPixelRef>* pixel_refs_; -}; - -class GatherPixelRefDevice : public SkBitmapDevice { - public: - GatherPixelRefDevice(const SkBitmap& bm, - DiscardablePixelRefSet* pixel_ref_set) - : SkBitmapDevice(bm), pixel_ref_set_(pixel_ref_set) {} - - void drawPaint(const SkDraw& draw, const SkPaint& paint) override { - SkBitmap bitmap; - if (GetBitmapFromPaint(paint, &bitmap)) { - SkRect clip_rect = SkRect::Make(draw.fRC->getBounds()); - AddBitmap(bitmap, clip_rect, *draw.fMatrix, paint.getFilterQuality()); - } - } - - void drawPoints(const SkDraw& draw, - SkCanvas::PointMode mode, - size_t count, - const SkPoint points[], - const SkPaint& paint) override { - SkBitmap bitmap; - if (!GetBitmapFromPaint(paint, &bitmap)) - return; - - if (count == 0) - return; - - SkPoint min_point = points[0]; - SkPoint max_point = points[0]; - for (size_t i = 1; i < count; ++i) { - const SkPoint& point = points[i]; - min_point.set(std::min(min_point.x(), point.x()), - std::min(min_point.y(), point.y())); - max_point.set(std::max(max_point.x(), point.x()), - std::max(max_point.y(), point.y())); - } - - SkRect bounds = SkRect::MakeLTRB( - min_point.x(), min_point.y(), max_point.x(), max_point.y()); - - GatherPixelRefDevice::drawRect(draw, bounds, paint); - } - void drawRect(const SkDraw& draw, - const SkRect& rect, - const SkPaint& paint) override { - SkBitmap bitmap; - if (GetBitmapFromPaint(paint, &bitmap)) { - SkRect mapped_rect; - draw.fMatrix->mapRect(&mapped_rect, rect); - if (mapped_rect.intersect(SkRect::Make(draw.fRC->getBounds()))) { - AddBitmap(bitmap, mapped_rect, *draw.fMatrix, paint.getFilterQuality()); - } - } - } - void drawOval(const SkDraw& draw, - const SkRect& rect, - const SkPaint& paint) override { - GatherPixelRefDevice::drawRect(draw, rect, paint); - } - void drawRRect(const SkDraw& draw, - const SkRRect& rect, - const SkPaint& paint) override { - GatherPixelRefDevice::drawRect(draw, rect.rect(), paint); - } - void drawPath(const SkDraw& draw, - const SkPath& path, - const SkPaint& paint, - const SkMatrix* pre_path_matrix, - bool path_is_mutable) override { - SkBitmap bitmap; - if (!GetBitmapFromPaint(paint, &bitmap)) - return; - - SkRect path_bounds = path.getBounds(); - SkRect final_rect; - if (pre_path_matrix != NULL) - pre_path_matrix->mapRect(&final_rect, path_bounds); - else - final_rect = path_bounds; - - GatherPixelRefDevice::drawRect(draw, final_rect, paint); - } - void drawBitmap(const SkDraw& draw, - const SkBitmap& bitmap, - const SkMatrix& matrix, - const SkPaint& paint) override { - SkMatrix total_matrix; - total_matrix.setConcat(*draw.fMatrix, matrix); - - SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); - SkRect mapped_rect; - total_matrix.mapRect(&mapped_rect, bitmap_rect); - AddBitmap(bitmap, mapped_rect, total_matrix, paint.getFilterQuality()); - - SkBitmap paint_bitmap; - if (GetBitmapFromPaint(paint, &paint_bitmap)) { - AddBitmap(paint_bitmap, mapped_rect, total_matrix, - paint.getFilterQuality()); - } - } - void drawBitmapRect(const SkDraw& draw, - const SkBitmap& bitmap, - const SkRect* src_or_null, - const SkRect& dst, - const SkPaint& paint, - SkCanvas::DrawBitmapRectFlags flags) override { - SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); - SkMatrix matrix; - matrix.setRectToRect(bitmap_rect, dst, SkMatrix::kFill_ScaleToFit); - GatherPixelRefDevice::drawBitmap(draw, bitmap, matrix, paint); - } - void drawSprite(const SkDraw& draw, - const SkBitmap& bitmap, - int x, - int y, - const SkPaint& paint) override { - // Sprites aren't affected by current matrix, so we can't reuse drawRect. - SkMatrix matrix; - matrix.setTranslate(x, y); - - SkRect bitmap_rect = SkRect::MakeWH(bitmap.width(), bitmap.height()); - SkRect mapped_rect; - matrix.mapRect(&mapped_rect, bitmap_rect); - - SkMatrix identity; - identity.setIdentity(); - // Sprites aren't affected by current matrix, so use the identity matrix. - AddBitmap(bitmap, mapped_rect, identity, paint.getFilterQuality()); - SkBitmap paint_bitmap; - if (GetBitmapFromPaint(paint, &paint_bitmap)) - AddBitmap(paint_bitmap, mapped_rect, identity, paint.getFilterQuality()); - } - void drawText(const SkDraw& draw, - const void* text, - size_t len, - SkScalar x, - SkScalar y, - const SkPaint& paint) override { - SkBitmap bitmap; - if (!GetBitmapFromPaint(paint, &bitmap)) - return; - - // Math is borrowed from SkBBoxRecord - SkRect bounds; - paint.measureText(text, len, &bounds); - SkPaint::FontMetrics metrics; - paint.getFontMetrics(&metrics); - - if (paint.isVerticalText()) { - SkScalar h = bounds.fBottom - bounds.fTop; - if (paint.getTextAlign() == SkPaint::kCenter_Align) { - bounds.fTop -= h / 2; - bounds.fBottom -= h / 2; - } - bounds.fBottom += metrics.fBottom; - bounds.fTop += metrics.fTop; - } else { - SkScalar w = bounds.fRight - bounds.fLeft; - if (paint.getTextAlign() == SkPaint::kCenter_Align) { - bounds.fLeft -= w / 2; - bounds.fRight -= w / 2; - } else if (paint.getTextAlign() == SkPaint::kRight_Align) { - bounds.fLeft -= w; - bounds.fRight -= w; - } - bounds.fTop = metrics.fTop; - bounds.fBottom = metrics.fBottom; - } - - SkScalar pad = (metrics.fBottom - metrics.fTop) / 2; - bounds.fLeft -= pad; - bounds.fRight += pad; - bounds.fLeft += x; - bounds.fRight += x; - bounds.fTop += y; - bounds.fBottom += y; - - GatherPixelRefDevice::drawRect(draw, bounds, paint); - } - void drawPosText(const SkDraw& draw, - const void* text, - size_t len, - const SkScalar pos[], - int scalars_per_pos, - const SkPoint& offset, - const SkPaint& paint) override { - SkBitmap bitmap; - if (!GetBitmapFromPaint(paint, &bitmap)) - return; - - if (len == 0) - return; - - // Similar to SkDraw asserts. - SkASSERT(scalars_per_pos == 1 || scalars_per_pos == 2); - - SkPoint min_point = SkPoint::Make(offset.x() + pos[0], - offset.y() + (2 == scalars_per_pos ? pos[1] : 0)); - SkPoint max_point = min_point; - - for (size_t i = 0; i < len; ++i) { - SkScalar x = offset.x() + pos[i * scalars_per_pos]; - SkScalar y = offset.y() + (2 == scalars_per_pos ? pos[i * scalars_per_pos + 1] : 0); - - min_point.set(std::min(x, min_point.x()), std::min(y, min_point.y())); - max_point.set(std::max(x, max_point.x()), std::max(y, max_point.y())); - } - - SkRect bounds = SkRect::MakeLTRB( - min_point.x(), min_point.y(), max_point.x(), max_point.y()); - - // Math is borrowed from SkBBoxRecord - SkPaint::FontMetrics metrics; - paint.getFontMetrics(&metrics); - - bounds.fTop += metrics.fTop; - bounds.fBottom += metrics.fBottom; - - SkScalar pad = (metrics.fTop - metrics.fBottom) / 2; - bounds.fLeft += pad; - bounds.fRight -= pad; - - GatherPixelRefDevice::drawRect(draw, bounds, paint); - } - void drawTextOnPath(const SkDraw& draw, - const void* text, - size_t len, - const SkPath& path, - const SkMatrix* matrix, - const SkPaint& paint) override { - SkBitmap bitmap; - if (!GetBitmapFromPaint(paint, &bitmap)) - return; - - // Math is borrowed from SkBBoxRecord - SkRect bounds = path.getBounds(); - SkPaint::FontMetrics metrics; - paint.getFontMetrics(&metrics); - - SkScalar pad = metrics.fTop; - bounds.fLeft += pad; - bounds.fRight -= pad; - bounds.fTop += pad; - bounds.fBottom -= pad; - - GatherPixelRefDevice::drawRect(draw, bounds, paint); - } - void drawVertices(const SkDraw& draw, - SkCanvas::VertexMode, - int vertex_count, - const SkPoint verts[], - const SkPoint texs[], - const SkColor colors[], - SkXfermode* xmode, - const uint16_t indices[], - int index_count, - const SkPaint& paint) override { - GatherPixelRefDevice::drawPoints( - draw, SkCanvas::kPolygon_PointMode, vertex_count, verts, paint); - } - void drawDevice(const SkDraw&, - SkBaseDevice*, - int x, - int y, - const SkPaint&) override {} - - protected: - bool onReadPixels(const SkImageInfo& info, - void* pixels, - size_t rowBytes, - int x, - int y) override { - return false; - } - - bool onWritePixels(const SkImageInfo& info, - const void* pixels, - size_t rowBytes, - int x, - int y) override { - return false; - } - - private: - DiscardablePixelRefSet* pixel_ref_set_; - - void AddBitmap(const SkBitmap& bm, - const SkRect& rect, - const SkMatrix& matrix, - SkFilterQuality filter_quality) { - SkRect canvas_rect = SkRect::MakeWH(width(), height()); - SkRect paint_rect = SkRect::MakeEmpty(); - if (paint_rect.intersect(rect, canvas_rect)) { - pixel_ref_set_->Add(bm.pixelRef(), paint_rect, matrix, - filter_quality); - } - } - - bool GetBitmapFromPaint(const SkPaint& paint, SkBitmap* bm) { - SkShader* shader = paint.getShader(); - if (shader) { - // Check whether the shader is a gradient in order to prevent generation - // of bitmaps from gradient shaders, which implement asABitmap. - if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) - return shader->asABitmap(bm, NULL, NULL); - } - return false; - } -}; - -} // namespace - -void PixelRefUtils::GatherDiscardablePixelRefs( - SkPicture* picture, - std::vector<PositionPixelRef>* pixel_refs) { - pixel_refs->clear(); - DiscardablePixelRefSet pixel_ref_set(pixel_refs); - - SkRect picture_bounds = picture->cullRect(); - SkIRect picture_ibounds = picture_bounds.roundOut(); - SkBitmap empty_bitmap; - empty_bitmap.setInfo(SkImageInfo::MakeUnknown(picture_ibounds.width(), - picture_ibounds.height())); - - GatherPixelRefDevice device(empty_bitmap, &pixel_ref_set); - SkNoSaveLayerCanvas canvas(&device); - - // Draw the picture pinned against our top/left corner. - canvas.translate(-picture_bounds.left(), -picture_bounds.top()); - canvas.drawPicture(picture); -} - -} // namespace skia diff --git a/chromium/skia/ext/pixel_ref_utils.h b/chromium/skia/ext/pixel_ref_utils.h deleted file mode 100644 index f8de4b6e405..00000000000 --- a/chromium/skia/ext/pixel_ref_utils.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef SKIA_EXT_PIXEL_REF_UTILS_H_ -#define SKIA_EXT_PIXEL_REF_UTILS_H_ - -#include <vector> - -#include "SkPicture.h" -#include "SkRect.h" - -namespace skia { - -class SK_API PixelRefUtils { - public: - - struct PositionPixelRef { - SkPixelRef* pixel_ref; - SkRect pixel_ref_rect; - SkMatrix matrix; - SkFilterQuality filter_quality; - }; - - static void GatherDiscardablePixelRefs( - SkPicture* picture, - std::vector<PositionPixelRef>* pixel_refs); -}; - -typedef std::vector<PixelRefUtils::PositionPixelRef> DiscardablePixelRefList; - -} // namespace skia - -#endif diff --git a/chromium/skia/ext/pixel_ref_utils_unittest.cc b/chromium/skia/ext/pixel_ref_utils_unittest.cc index 2e1fa245050..ffe1dc068ea 100644 --- a/chromium/skia/ext/pixel_ref_utils_unittest.cc +++ b/chromium/skia/ext/pixel_ref_utils_unittest.cc @@ -10,6 +10,8 @@ #include "testing/gtest/include/gtest/gtest.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkCanvas.h" +#include "third_party/skia/include/core/SkImage.h" +#include "third_party/skia/include/core/SkImageGenerator.h" #include "third_party/skia/include/core/SkPictureRecorder.h" #include "third_party/skia/include/core/SkPixelRef.h" #include "third_party/skia/include/core/SkPoint.h" @@ -22,52 +24,24 @@ namespace skia { namespace { -void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap); - -class TestDiscardableShader : public SkShader { - public: - TestDiscardableShader() { - CreateBitmap(gfx::Size(50, 50), "discardable", &bitmap_); - } - - TestDiscardableShader(SkReadBuffer& buffer) { - CreateBitmap(gfx::Size(50, 50), "discardable", &bitmap_); - } - - SkShader::BitmapType asABitmap(SkBitmap* bitmap, - SkMatrix* matrix, - TileMode xy[2]) const override { - if (bitmap) - *bitmap = bitmap_; - return SkShader::kDefault_BitmapType; - } - - // not indended to return an actual context. Just need to supply this. - size_t contextSize() const override { return sizeof(SkShader::Context); } - - void flatten(SkWriteBuffer&) const override {} - - // Manual expansion of SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS to - // satisfy Chrome's style checker, since Skia isn't ready to make the C++11 - // leap yet. - private: - static SkFlattenable* CreateProc(SkReadBuffer&); - - public: - Factory getFactory() const override { return CreateProc; } - - private: - SkBitmap bitmap_; +class TestImageGenerator : public SkImageGenerator { + public: + TestImageGenerator(const SkImageInfo& info) + : SkImageGenerator(info) { } }; -SkFlattenable* TestDiscardableShader::CreateProc(SkReadBuffer&) { - return new TestDiscardableShader; +skia::RefPtr<SkImage> CreateDiscardableImage(const gfx::Size& size) { + const SkImageInfo info = + SkImageInfo::MakeN32Premul(size.width(), size.height()); + return skia::AdoptRef( + SkImage::NewFromGenerator(new TestImageGenerator(info))); } -void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) { - bitmap->allocN32Pixels(size.width(), size.height()); - bitmap->pixelRef()->setImmutable(); - bitmap->pixelRef()->setURI(uri); +void SetDiscardableShader(SkPaint* paint) { + skia::RefPtr<SkImage> image = CreateDiscardableImage(gfx::Size(50, 50)); + skia::RefPtr<SkShader> shader = skia::AdoptRef( + image->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)); + paint->setShader(shader.get()); } SkCanvas* StartRecording(SkPictureRecorder* recorder, gfx::Rect layer_rect) { @@ -106,17 +80,14 @@ TEST(PixelRefUtilsTest, DrawPaint) { SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - TestDiscardableShader first_shader; SkPaint first_paint; - first_paint.setShader(&first_shader); + SetDiscardableShader(&first_paint); - TestDiscardableShader second_shader; SkPaint second_paint; - second_paint.setShader(&second_shader); + SetDiscardableShader(&second_paint); - TestDiscardableShader third_shader; SkPaint third_paint; - third_paint.setShader(&third_shader); + SetDiscardableShader(&third_paint); canvas->drawPaint(first_paint); canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67)); @@ -161,17 +132,14 @@ TEST(PixelRefUtilsTest, DrawPoints) { SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - TestDiscardableShader first_shader; SkPaint first_paint; - first_paint.setShader(&first_shader); + SetDiscardableShader(&first_paint); - TestDiscardableShader second_shader; SkPaint second_paint; - second_paint.setShader(&second_shader); + SetDiscardableShader(&second_paint); - TestDiscardableShader third_shader; SkPaint third_paint; - third_paint.setShader(&third_shader); + SetDiscardableShader(&third_paint); SkPoint points[3]; points[0].set(10, 10); @@ -183,7 +151,7 @@ TEST(PixelRefUtilsTest, DrawPoints) { canvas->save(); canvas->clipRect(SkRect::MakeWH(50, 50)); - // (10, 10, 40, 40). + // (10, 10, 90, 90). canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, second_paint); canvas->restore(); @@ -205,7 +173,7 @@ TEST(PixelRefUtilsTest, DrawPoints) { gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90), gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); @@ -221,17 +189,14 @@ TEST(PixelRefUtilsTest, DrawRect) { SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - TestDiscardableShader first_shader; SkPaint first_paint; - first_paint.setShader(&first_shader); + SetDiscardableShader(&first_paint); - TestDiscardableShader second_shader; SkPaint second_paint; - second_paint.setShader(&second_shader); + SetDiscardableShader(&second_paint); - TestDiscardableShader third_shader; SkPaint third_paint; - third_paint.setShader(&third_shader); + SetDiscardableShader(&third_paint); // (10, 20, 30, 40). canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint); @@ -246,7 +211,7 @@ TEST(PixelRefUtilsTest, DrawRect) { canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50)); canvas->translate(20, 20); - // (50, 50, 50, 50) + // (20, 20, 100, 100) canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint); skia::RefPtr<SkPicture> picture = @@ -264,7 +229,7 @@ TEST(PixelRefUtilsTest, DrawRect) { gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 20, 100, 100), gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality); @@ -276,17 +241,14 @@ TEST(PixelRefUtilsTest, DrawRRect) { SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - TestDiscardableShader first_shader; SkPaint first_paint; - first_paint.setShader(&first_shader); + SetDiscardableShader(&first_paint); - TestDiscardableShader second_shader; SkPaint second_paint; - second_paint.setShader(&second_shader); + SetDiscardableShader(&second_paint); - TestDiscardableShader third_shader; SkPaint third_paint; - third_paint.setShader(&third_shader); + SetDiscardableShader(&third_paint); SkRRect rrect; rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40)); @@ -306,7 +268,7 @@ TEST(PixelRefUtilsTest, DrawRRect) { canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50)); canvas->translate(20, 20); rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100)); - // (50, 50, 50, 50) + // (20, 20, 100, 100) canvas->drawRRect(rrect, third_paint); skia::RefPtr<SkPicture> picture = @@ -324,7 +286,7 @@ TEST(PixelRefUtilsTest, DrawRRect) { gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 20, 100, 100), gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality); @@ -336,17 +298,14 @@ TEST(PixelRefUtilsTest, DrawOval) { SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - TestDiscardableShader first_shader; SkPaint first_paint; - first_paint.setShader(&first_shader); + SetDiscardableShader(&first_paint); - TestDiscardableShader second_shader; SkPaint second_paint; - second_paint.setShader(&second_shader); + SetDiscardableShader(&second_paint); - TestDiscardableShader third_shader; SkPaint third_paint; - third_paint.setShader(&third_shader); + SetDiscardableShader(&third_paint); canvas->save(); @@ -365,7 +324,7 @@ TEST(PixelRefUtilsTest, DrawOval) { canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50)); canvas->translate(20, 20); - // (50, 50, 50, 50) + // (20, 20, 100, 100). canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint); skia::RefPtr<SkPicture> picture = @@ -383,7 +342,7 @@ TEST(PixelRefUtilsTest, DrawOval) { gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 20, 100, 100), gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality); @@ -395,13 +354,11 @@ TEST(PixelRefUtilsTest, DrawPath) { SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - TestDiscardableShader first_shader; SkPaint first_paint; - first_paint.setShader(&first_shader); + SetDiscardableShader(&first_paint); - TestDiscardableShader second_shader; SkPaint second_paint; - second_paint.setShader(&second_shader); + SetDiscardableShader(&second_paint); SkPath path; path.moveTo(12, 13); @@ -414,7 +371,8 @@ TEST(PixelRefUtilsTest, DrawPath) { canvas->save(); canvas->clipRect(SkRect::MakeWH(50, 50)); - // (12, 13, 38, 37). + // (12, 13, 38, 88), since clips are ignored as long as the shape is in the + // clip. canvas->drawPath(path, second_paint); canvas->restore(); @@ -430,65 +388,37 @@ TEST(PixelRefUtilsTest, DrawPath) { gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88), gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); } -TEST(PixelRefUtilsTest, DrawBitmap) { +TEST(PixelRefUtilsTest, DrawText) { gfx::Rect layer_rect(0, 0, 256, 256); SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - SkBitmap first; - CreateBitmap(gfx::Size(50, 50), "discardable", &first); - SkBitmap second; - CreateBitmap(gfx::Size(50, 50), "discardable", &second); - SkBitmap third; - CreateBitmap(gfx::Size(50, 50), "discardable", &third); - SkBitmap fourth; - CreateBitmap(gfx::Size(50, 1), "discardable", &fourth); - SkBitmap fifth; - CreateBitmap(gfx::Size(10, 10), "discardable", &fifth); - SkBitmap sixth; - CreateBitmap(gfx::Size(10, 10), "discardable", &sixth); - - canvas->save(); - - // At (0, 0). - canvas->drawBitmap(first, 0, 0); - canvas->translate(25, 0); - // At (25, 0). - canvas->drawBitmap(second, 0, 0); - canvas->translate(0, 50); - // At (50, 50). - canvas->drawBitmap(third, 25, 0); - - canvas->restore(); - canvas->save(); - - canvas->translate(1, 0); - canvas->rotate(90); - // At (1, 0), rotated 90 degrees - canvas->drawBitmap(fourth, 0, 0); - - canvas->restore(); - canvas->save(); - - canvas->scale(5.f, 6.f); - // At (0, 0), scaled by 5 and 6 - canvas->drawBitmap(fifth, 0, 0); - - canvas->restore(); + SkPaint first_paint; + SetDiscardableShader(&first_paint); - canvas->rotate(27); - canvas->scale(3.3f, 0.4f); + SkPoint points[4]; + points[0].set(10, 50); + points[1].set(20, 50); + points[2].set(30, 50); + points[3].set(40, 50); - canvas->drawBitmap(sixth, 0, 0); + SkPath path; + path.moveTo(10, 50); + path.lineTo(20, 50); + path.lineTo(30, 50); + path.lineTo(40, 50); + path.lineTo(50, 50); - canvas->restore(); + canvas->drawText("text", 4, 50, 50, first_paint); + canvas->drawPosText("text", 4, points, first_paint); + canvas->drawTextOnPath("text", 4, path, NULL, first_paint); skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas)); @@ -496,139 +426,140 @@ TEST(PixelRefUtilsTest, DrawBitmap) { std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs; skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs); - EXPECT_EQ(6u, pixel_refs.size()); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50), - gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__); - EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50), - gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); - EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), - gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__); - EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50), - gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__); - EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60), - gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect)); - VerifyScales(5.f, 6.f, pixel_refs[4].matrix, __LINE__); - EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[4].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 29.403214f, 18.545712f), - gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect)); - VerifyScales(3.3f, 0.4f, pixel_refs[5].matrix, __LINE__); - EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[5].filter_quality); + EXPECT_EQ(3u, pixel_refs.size()); } -TEST(PixelRefUtilsTest, DrawBitmapRect) { +TEST(PixelRefUtilsTest, DrawVertices) { gfx::Rect layer_rect(0, 0, 256, 256); SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - SkBitmap first; - CreateBitmap(gfx::Size(50, 50), "discardable", &first); - SkBitmap second; - CreateBitmap(gfx::Size(50, 50), "discardable", &second); - SkBitmap third; - CreateBitmap(gfx::Size(50, 50), "discardable", &third); - - TestDiscardableShader first_shader; SkPaint first_paint; - first_paint.setShader(&first_shader); + SetDiscardableShader(&first_paint); - SkPaint non_discardable_paint; + SkPaint second_paint; + SetDiscardableShader(&second_paint); + + SkPaint third_paint; + SetDiscardableShader(&third_paint); + + SkPoint points[3]; + SkColor colors[3]; + uint16_t indecies[3] = {0, 1, 2}; + points[0].set(10, 10); + points[1].set(100, 20); + points[2].set(50, 100); + // (10, 10, 90, 90). + canvas->drawVertices(SkCanvas::kTriangles_VertexMode, + 3, + points, + points, + colors, + NULL, + indecies, + 3, + first_paint); canvas->save(); - // (0, 0, 100, 100). - canvas->drawBitmapRect( - first, SkRect::MakeWH(100, 100), &non_discardable_paint); - canvas->translate(25, 0); - // (75, 50, 10, 10). - canvas->drawBitmapRect( - second, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint); - canvas->translate(5, 50); - // (0, 30, 100, 100). One from bitmap, one from paint. - canvas->drawBitmapRect( - third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint); + canvas->clipRect(SkRect::MakeWH(50, 50)); + // (10, 10, 90, 90), since clips are ignored as long as the draw object is + // within clip. + canvas->drawVertices(SkCanvas::kTriangles_VertexMode, + 3, + points, + points, + colors, + NULL, + indecies, + 3, + second_paint); canvas->restore(); + points[0].set(50, 55); + points[1].set(50, 55); + points[2].set(200, 200); + // (50, 55, 150, 145). + canvas->drawVertices(SkCanvas::kTriangles_VertexMode, + 3, + points, + points, + colors, + NULL, + indecies, + 3, + third_paint); + skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas)); std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs; skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs); - EXPECT_EQ(4u, pixel_refs.size()); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100), + EXPECT_EQ(3u, pixel_refs.size()); + EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90), gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); - VerifyScales(2.f, 2.f, pixel_refs[0].matrix, __LINE__); + VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90), gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); - VerifyScales(0.2f, 0.2f, pixel_refs[1].matrix, __LINE__); + VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145), gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); - VerifyScales(2.f, 2.f, pixel_refs[2].matrix, __LINE__); + VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100), - gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect)); - VerifyScales(2.f, 2.f, pixel_refs[3].matrix, __LINE__); - EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality); } -TEST(PixelRefUtilsTest, DrawSprite) { +TEST(PixelRefUtilsTest, DrawImage) { gfx::Rect layer_rect(0, 0, 256, 256); SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - SkBitmap first; - CreateBitmap(gfx::Size(50, 50), "discardable", &first); - SkBitmap second; - CreateBitmap(gfx::Size(50, 50), "discardable", &second); - SkBitmap third; - CreateBitmap(gfx::Size(50, 50), "discardable", &third); - SkBitmap fourth; - CreateBitmap(gfx::Size(50, 50), "discardable", &fourth); - SkBitmap fifth; - CreateBitmap(gfx::Size(50, 50), "discardable", &fifth); + skia::RefPtr<SkImage> first = CreateDiscardableImage(gfx::Size(50, 50)); + skia::RefPtr<SkImage> second = CreateDiscardableImage(gfx::Size(50, 50)); + skia::RefPtr<SkImage> third = CreateDiscardableImage(gfx::Size(50, 50)); + skia::RefPtr<SkImage> fourth = CreateDiscardableImage(gfx::Size(50, 1)); + skia::RefPtr<SkImage> fifth = CreateDiscardableImage(gfx::Size(10, 10)); + skia::RefPtr<SkImage> sixth = CreateDiscardableImage(gfx::Size(10, 10)); canvas->save(); - // Sprites aren't affected by the current matrix. - - // (0, 0, 50, 50). - canvas->drawSprite(first, 0, 0); + // At (0, 0). + canvas->drawImage(first.get(), 0, 0); canvas->translate(25, 0); - // (10, 0, 50, 50). - canvas->drawSprite(second, 10, 0); + // At (25, 0). + canvas->drawImage(second.get(), 0, 0); canvas->translate(0, 50); - // (25, 0, 50, 50). - canvas->drawSprite(third, 25, 0); + // At (50, 50). + canvas->drawImage(third.get(), 25, 0); canvas->restore(); canvas->save(); + canvas->translate(1, 0); canvas->rotate(90); - // (0, 0, 50, 50). - canvas->drawSprite(fourth, 0, 0); + // At (1, 0), rotated 90 degrees + canvas->drawImage(fourth.get(), 0, 0); canvas->restore(); - - TestDiscardableShader first_shader; - SkPaint first_paint; - first_paint.setShader(&first_shader); + canvas->save(); canvas->scale(5.f, 6.f); - // (100, 100, 50, 50). - canvas->drawSprite(fifth, 100, 100, &first_paint); + // At (0, 0), scaled by 5 and 6 + canvas->drawImage(fifth.get(), 0, 0); + + canvas->restore(); + + canvas->rotate(27); + canvas->scale(3.3f, 0.4f); + + canvas->drawImage(sixth.get(), 0, 0); + + canvas->restore(); skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas)); @@ -641,148 +572,82 @@ TEST(PixelRefUtilsTest, DrawSprite) { gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50), gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50), gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect)); VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60), gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[4].matrix, __LINE__); + VerifyScales(5.f, 6.f, pixel_refs[4].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[4].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(-1.8159621f, 0, 31.219175f, 18.545712f), gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[5].matrix, __LINE__); + VerifyScales(3.3f, 0.4f, pixel_refs[5].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[5].filter_quality); } -TEST(PixelRefUtilsTest, DrawText) { +TEST(PixelRefUtilsTest, DrawImageRect) { gfx::Rect layer_rect(0, 0, 256, 256); SkPictureRecorder recorder; SkCanvas* canvas = StartRecording(&recorder, layer_rect); - TestDiscardableShader first_shader; - SkPaint first_paint; - first_paint.setShader(&first_shader); - - SkPoint points[4]; - points[0].set(10, 50); - points[1].set(20, 50); - points[2].set(30, 50); - points[3].set(40, 50); - - SkPath path; - path.moveTo(10, 50); - path.lineTo(20, 50); - path.lineTo(30, 50); - path.lineTo(40, 50); - path.lineTo(50, 50); + skia::RefPtr<SkImage> first = CreateDiscardableImage(gfx::Size(50, 50)); + skia::RefPtr<SkImage> second = CreateDiscardableImage(gfx::Size(50, 50)); + skia::RefPtr<SkImage> third = CreateDiscardableImage(gfx::Size(50, 50)); - canvas->drawText("text", 4, 50, 50, first_paint); - canvas->drawPosText("text", 4, points, first_paint); - canvas->drawTextOnPath("text", 4, path, NULL, first_paint); - - skia::RefPtr<SkPicture> picture = - skia::AdoptRef(StopRecording(&recorder, canvas)); - - std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs; - skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs); - - EXPECT_EQ(3u, pixel_refs.size()); -} - -TEST(PixelRefUtilsTest, DrawVertices) { - gfx::Rect layer_rect(0, 0, 256, 256); - - SkPictureRecorder recorder; - SkCanvas* canvas = StartRecording(&recorder, layer_rect); - - TestDiscardableShader first_shader; SkPaint first_paint; - first_paint.setShader(&first_shader); + SetDiscardableShader(&first_paint); - TestDiscardableShader second_shader; - SkPaint second_paint; - second_paint.setShader(&second_shader); - - TestDiscardableShader third_shader; - SkPaint third_paint; - third_paint.setShader(&third_shader); - - SkPoint points[3]; - SkColor colors[3]; - uint16_t indecies[3] = {0, 1, 2}; - points[0].set(10, 10); - points[1].set(100, 20); - points[2].set(50, 100); - // (10, 10, 90, 90). - canvas->drawVertices(SkCanvas::kTriangles_VertexMode, - 3, - points, - points, - colors, - NULL, - indecies, - 3, - first_paint); + SkPaint non_discardable_paint; canvas->save(); - canvas->clipRect(SkRect::MakeWH(50, 50)); - // (10, 10, 40, 40). - canvas->drawVertices(SkCanvas::kTriangles_VertexMode, - 3, - points, - points, - colors, - NULL, - indecies, - 3, - second_paint); + // (0, 0, 100, 100). + canvas->drawImageRect( + first.get(), SkRect::MakeWH(100, 100), &non_discardable_paint); + canvas->translate(25, 0); + // (75, 50, 10, 10). + canvas->drawImageRect( + second.get(), SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint); + canvas->translate(5, 50); + // (0, 30, 100, 100). One from bitmap, one from paint. + canvas->drawImageRect( + third.get(), SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint); canvas->restore(); - points[0].set(50, 55); - points[1].set(50, 55); - points[2].set(200, 200); - // (50, 55, 150, 145). - canvas->drawVertices(SkCanvas::kTriangles_VertexMode, - 3, - points, - points, - colors, - NULL, - indecies, - 3, - third_paint); - skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas)); std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs; skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs); - EXPECT_EQ(3u, pixel_refs.size()); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90), + EXPECT_EQ(4u, pixel_refs.size()); + EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100), gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__); + VerifyScales(2.f, 2.f, pixel_refs[0].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10), gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__); + VerifyScales(0.2f, 0.2f, pixel_refs[1].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145), + EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100), gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); - VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__); + VerifyScales(2.f, 2.f, pixel_refs[2].matrix, __LINE__); EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality); + EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100), + gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect)); + VerifyScales(2.f, 2.f, pixel_refs[3].matrix, __LINE__); + EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality); } } // namespace skia diff --git a/chromium/skia/ext/platform_canvas_unittest.cc b/chromium/skia/ext/platform_canvas_unittest.cc index 530d7220ca7..22a20a1aa33 100644 --- a/chromium/skia/ext/platform_canvas_unittest.cc +++ b/chromium/skia/ext/platform_canvas_unittest.cc @@ -63,7 +63,7 @@ bool VerifyRect(const PlatformCanvas& canvas, return true; } -#if !defined(OS_MACOSX) +#if !defined(USE_AURA) && !defined(OS_MACOSX) // Return true if canvas has something that passes for a rounded-corner // rectangle. Basically, we're just checking to make sure that the pixels in the // middle are of rect_color and pixels in the corners are of canvas_color. @@ -100,10 +100,12 @@ bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) { return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); } +#if !defined(USE_AURA) // http://crbug.com/154358 // Check that every pixel in the canvas is a single color. bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) { return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); } +#endif // !defined(USE_AURA) #if defined(OS_WIN) void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { @@ -208,7 +210,6 @@ TEST(PlatformCanvas, SkLayer) { } #if !defined(USE_AURA) // http://crbug.com/154358 - // Test native clipping. TEST(PlatformCanvas, ClipRegion) { // Initialize a white canvas @@ -233,7 +234,6 @@ TEST(PlatformCanvas, ClipRegion) { } EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); } - #endif // !defined(USE_AURA) // Test the layers get filled properly by native rendering. diff --git a/chromium/skia/ext/platform_device.h b/chromium/skia/ext/platform_device.h index c903c87c08e..f3b2fe34e27 100644 --- a/chromium/skia/ext/platform_device.h +++ b/chromium/skia/ext/platform_device.h @@ -14,7 +14,7 @@ #include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkBitmapDevice.h" -#include "third_party/skia/include/core/SkPreConfig.h" +#include "third_party/skia/include/core/SkTypes.h" class SkMatrix; class SkMetaData; diff --git a/chromium/skia/ext/skia_memory_dump_provider.cc b/chromium/skia/ext/skia_memory_dump_provider.cc new file mode 100644 index 00000000000..aaef11f1a2b --- /dev/null +++ b/chromium/skia/ext/skia_memory_dump_provider.cc @@ -0,0 +1,36 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "skia_memory_dump_provider.h" + +#include "base/trace_event/memory_allocator_dump.h" +#include "base/trace_event/memory_dump_manager.h" +#include "base/trace_event/process_memory_dump.h" +#include "skia/ext/SkTraceMemoryDump_chrome.h" +#include "third_party/skia/include/core/SkGraphics.h" + +namespace skia { + +// static +SkiaMemoryDumpProvider* SkiaMemoryDumpProvider::GetInstance() { + return base::Singleton< + SkiaMemoryDumpProvider, + base::LeakySingletonTraits<SkiaMemoryDumpProvider>>::get(); +} + +SkiaMemoryDumpProvider::SkiaMemoryDumpProvider() {} + +SkiaMemoryDumpProvider::~SkiaMemoryDumpProvider() {} + +bool SkiaMemoryDumpProvider::OnMemoryDump( + const base::trace_event::MemoryDumpArgs& args, + base::trace_event::ProcessMemoryDump* process_memory_dump) { + SkTraceMemoryDump_Chrome skia_dumper(args.level_of_detail, + process_memory_dump); + SkGraphics::DumpMemoryStatistics(&skia_dumper); + + return true; +} + +} // namespace skia diff --git a/chromium/skia/ext/skia_memory_dump_provider.h b/chromium/skia/ext/skia_memory_dump_provider.h new file mode 100644 index 00000000000..c343731165a --- /dev/null +++ b/chromium/skia/ext/skia_memory_dump_provider.h @@ -0,0 +1,35 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKIA_EXT_SKIA_MEMORY_DUMP_PROVIDER_H_ +#define SKIA_EXT_SKIA_MEMORY_DUMP_PROVIDER_H_ + +#include "base/memory/singleton.h" +#include "base/trace_event/memory_dump_provider.h" +#include "third_party/skia/include/core/SkTypes.h" + +namespace skia { + +class SK_API SkiaMemoryDumpProvider + : public base::trace_event::MemoryDumpProvider { + public: + static SkiaMemoryDumpProvider* GetInstance(); + + // base::trace_event::MemoryDumpProvider implementation: + bool OnMemoryDump( + const base::trace_event::MemoryDumpArgs& args, + base::trace_event::ProcessMemoryDump* process_memory_dump) override; + + private: + friend struct base::DefaultSingletonTraits<SkiaMemoryDumpProvider>; + + SkiaMemoryDumpProvider(); + ~SkiaMemoryDumpProvider() override; + + DISALLOW_COPY_AND_ASSIGN(SkiaMemoryDumpProvider); +}; + +} // namespace skia + +#endif // SKIA_EXT_SKIA_MEMORY_DUMP_PROVIDER_H_ diff --git a/chromium/skia/ext/skia_memory_dump_provider_unittest.cc b/chromium/skia/ext/skia_memory_dump_provider_unittest.cc new file mode 100644 index 00000000000..af5c30bd696 --- /dev/null +++ b/chromium/skia/ext/skia_memory_dump_provider_unittest.cc @@ -0,0 +1,23 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/trace_event/process_memory_dump.h" +#include "skia/ext/skia_memory_dump_provider.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace skia { + +// Tests if the skia dump provider dumps without crashing. +TEST(SkiaMemoryDumpProviderTest, OnMemoryDump) { + scoped_ptr<base::trace_event::ProcessMemoryDump> process_memory_dump( + new base::trace_event::ProcessMemoryDump(nullptr)); + base::trace_event::MemoryDumpArgs dump_args = { + base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; + SkiaMemoryDumpProvider::GetInstance()->OnMemoryDump( + dump_args, process_memory_dump.get()); + + ASSERT_TRUE(process_memory_dump->GetAllocatorDump("skia/sk_glyph_cache")); +} + +} // namespace skia diff --git a/chromium/skia/ext/skia_utils_mac_unittest.mm b/chromium/skia/ext/skia_utils_mac_unittest.mm index 27c862bf2fa..ad505c71af8 100644 --- a/chromium/skia/ext/skia_utils_mac_unittest.mm +++ b/chromium/skia/ext/skia_utils_mac_unittest.mm @@ -9,6 +9,7 @@ #include "base/mac/scoped_nsobject.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/skia/include/core/SkCanvas.h" +#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" namespace { @@ -17,14 +18,14 @@ class SkiaUtilsMacTest : public testing::Test { // Creates a red or blue bitmap. SkBitmap CreateSkBitmap(int width, int height, bool isred, bool tfbit); - // Creates a red or blue image. - NSImage* CreateNSImage(int width, int height, bool isred); + // Creates a red image. + NSImage* CreateNSImage(int width, int height); // Checks that the given bitmap rep is actually red or blue. void TestImageRep(NSBitmapImageRep* imageRep, bool isred); - // Checks that the given bitmap is actually red or blue. - void TestSkBitmap(const SkBitmap& bitmap, bool isred); + // Checks that the given bitmap is red. + void TestSkBitmap(const SkBitmap& bitmap); enum BitLockerTest { TestIdentity = 0, @@ -59,16 +60,39 @@ SkBitmap SkiaUtilsMacTest::CreateSkBitmap(int width, int height, return bitmap; } -NSImage* SkiaUtilsMacTest::CreateNSImage(int width, int height, bool isred) { +NSImage* SkiaUtilsMacTest::CreateNSImage(int width, int height) { + base::scoped_nsobject<NSBitmapImageRep> bitmap([[NSBitmapImageRep alloc] + initWithBitmapDataPlanes:nil + pixelsWide:width + pixelsHigh:height + bitsPerSample:8 + samplesPerPixel:4 + hasAlpha:YES + isPlanar:NO + colorSpaceName:NSCalibratedRGBColorSpace + bitmapFormat:0 + bytesPerRow:4 * width + bitsPerPixel:32]); + + { + gfx::ScopedNSGraphicsContextSaveGState scopedGState; + [NSGraphicsContext + setCurrentContext:[NSGraphicsContext + graphicsContextWithBitmapImageRep:bitmap]]; + + CGFloat comps[] = {1.0, 0.0, 0.0, 1.0}; + NSColor* color = + [NSColor colorWithColorSpace:[NSColorSpace genericRGBColorSpace] + components:comps + count:4]; + [color set]; + NSRectFill(NSMakeRect(0, 0, width, height)); + } + base::scoped_nsobject<NSImage> image( [[NSImage alloc] initWithSize:NSMakeSize(width, height)]); - [image lockFocus]; - if (isred) - [[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] set]; - else - [[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:1.0] set]; - NSRectFill(NSMakeRect(0, 0, width, height)); - [image unlockFocus]; + [image addRepresentation:bitmap]; + return [image.release() autorelease]; } @@ -98,18 +122,13 @@ void SkiaUtilsMacTest::TestImageRep(NSBitmapImageRep* imageRep, bool isred) { EXPECT_GT(alpha, 0.95); } -void SkiaUtilsMacTest::TestSkBitmap(const SkBitmap& bitmap, bool isred) { +void SkiaUtilsMacTest::TestSkBitmap(const SkBitmap& bitmap) { int x = bitmap.width() > 17 ? 17 : 0; int y = bitmap.height() > 17 ? 17 : 0; SkColor color = bitmap.getColor(x, y); - if (isred) { - EXPECT_EQ(255u, SkColorGetR(color)); - EXPECT_EQ(0u, SkColorGetB(color)); - } else { - EXPECT_EQ(0u, SkColorGetR(color)); - EXPECT_EQ(255u, SkColorGetB(color)); - } + EXPECT_EQ(255u, SkColorGetR(color)); + EXPECT_EQ(0u, SkColorGetB(color)); EXPECT_EQ(0u, SkColorGetG(color)); EXPECT_EQ(255u, SkColorGetA(color)); } @@ -200,15 +219,14 @@ TEST_F(SkiaUtilsMacTest, BitmapToNSBitmapImageRep_BlueRectangle20x30) { TEST_F(SkiaUtilsMacTest, NSImageRepToSkBitmap) { int width = 10; int height = 15; - bool isred = true; - NSImage* image = CreateNSImage(width, height, isred); + NSImage* image = CreateNSImage(width, height); EXPECT_EQ(1u, [[image representations] count]); NSBitmapImageRep* imageRep = [[image representations] lastObject]; - NSColorSpace* colorSpace = [NSColorSpace deviceRGBColorSpace]; + NSColorSpace* colorSpace = [NSColorSpace genericRGBColorSpace]; SkBitmap bitmap(gfx::NSImageRepToSkBitmapWithColorSpace( imageRep, [image size], false, [colorSpace CGColorSpace])); - TestSkBitmap(bitmap, isred); + TestSkBitmap(bitmap); } TEST_F(SkiaUtilsMacTest, BitLocker_Identity) { diff --git a/chromium/skia/ext/skia_utils_win.cc b/chromium/skia/ext/skia_utils_win.cc index e065b1e403c..3089b242c26 100644 --- a/chromium/skia/ext/skia_utils_win.cc +++ b/chromium/skia/ext/skia_utils_win.cc @@ -13,15 +13,15 @@ namespace { -SK_COMPILE_ASSERT(offsetof(RECT, left) == offsetof(SkIRect, fLeft), o1); -SK_COMPILE_ASSERT(offsetof(RECT, top) == offsetof(SkIRect, fTop), o2); -SK_COMPILE_ASSERT(offsetof(RECT, right) == offsetof(SkIRect, fRight), o3); -SK_COMPILE_ASSERT(offsetof(RECT, bottom) == offsetof(SkIRect, fBottom), o4); -SK_COMPILE_ASSERT(sizeof(RECT().left) == sizeof(SkIRect().fLeft), o5); -SK_COMPILE_ASSERT(sizeof(RECT().top) == sizeof(SkIRect().fTop), o6); -SK_COMPILE_ASSERT(sizeof(RECT().right) == sizeof(SkIRect().fRight), o7); -SK_COMPILE_ASSERT(sizeof(RECT().bottom) == sizeof(SkIRect().fBottom), o8); -SK_COMPILE_ASSERT(sizeof(RECT) == sizeof(SkIRect), o9); +static_assert(offsetof(RECT, left) == offsetof(SkIRect, fLeft), "o1"); +static_assert(offsetof(RECT, top) == offsetof(SkIRect, fTop), "o2"); +static_assert(offsetof(RECT, right) == offsetof(SkIRect, fRight), "o3"); +static_assert(offsetof(RECT, bottom) == offsetof(SkIRect, fBottom), "o4"); +static_assert(sizeof(RECT().left) == sizeof(SkIRect().fLeft), "o5"); +static_assert(sizeof(RECT().top) == sizeof(SkIRect().fTop), "o6"); +static_assert(sizeof(RECT().right) == sizeof(SkIRect().fRight), "o7"); +static_assert(sizeof(RECT().bottom) == sizeof(SkIRect().fBottom), "o8"); +static_assert(sizeof(RECT) == sizeof(SkIRect), "o9"); } // namespace diff --git a/chromium/skia/ext/skia_utils_win.h b/chromium/skia/ext/skia_utils_win.h index 6ac5234b6b0..379cba12004 100644 --- a/chromium/skia/ext/skia_utils_win.h +++ b/chromium/skia/ext/skia_utils_win.h @@ -24,14 +24,14 @@ POINT SkPointToPOINT(const SkPoint& point); SkRect RECTToSkRect(const RECT& rect); // Converts a Windows RECT to a Skia rect. -// Both use same in-memory format. Verified by SK_COMPILE_ASSERT() in +// Both use same in-memory format. Verified by static_assert in // skia_utils_win.cc. inline const SkIRect& RECTToSkIRect(const RECT& rect) { return reinterpret_cast<const SkIRect&>(rect); } // Converts a Skia rect to a Windows RECT. -// Both use same in-memory format. Verified by SK_COMPILE_ASSERT() in +// Both use same in-memory format. Verified by static_assert in // skia_utils_win.cc. inline const RECT& SkIRectToRECT(const SkIRect& rect) { return reinterpret_cast<const RECT&>(rect); |