OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/tools/test_shell/simple_webpaintsurface_impl.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "gfx/blit.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPaintBuffer.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPaintSurfaceClient
.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPoint.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 14 #include "webkit/glue/webkit_glue.h" |
| 15 |
| 16 using WebKit::WebCanvas; |
| 17 using WebKit::WebPaintBuffer; |
| 18 using WebKit::WebPaintSurfaceClient; |
| 19 using WebKit::WebPoint; |
| 20 using WebKit::WebRect; |
| 21 using WebKit::WebSize; |
| 22 |
| 23 namespace { |
| 24 |
| 25 class SimplePaintBuffer : public WebPaintBuffer { |
| 26 public: |
| 27 SimplePaintBuffer(const gfx::Size& size) |
| 28 : canvas_(size.width(), size.height(), true) { |
| 29 } |
| 30 |
| 31 virtual ~SimplePaintBuffer() { |
| 32 } |
| 33 |
| 34 SkCanvas* skia_canvas() { return &canvas_; } |
| 35 |
| 36 // WebPaintBuffer methods: |
| 37 virtual WebCanvas* canvas() { return webkit_glue::ToWebCanvas(&canvas_); } |
| 38 |
| 39 private: |
| 40 skia::PlatformCanvas canvas_; |
| 41 }; |
| 42 |
| 43 } // namespace |
| 44 |
| 45 SimpleWebPaintSurfaceImpl::SimpleWebPaintSurfaceImpl(Delegate* delegate, |
| 46 const gfx::Size& size) |
| 47 : delegate_(delegate), |
| 48 size_(size), |
| 49 canvas_(size.width(), size.height(), true), |
| 50 client_(NULL), |
| 51 flushing_(false) { |
| 52 } |
| 53 |
| 54 SimpleWebPaintSurfaceImpl::~SimpleWebPaintSurfaceImpl() { |
| 55 flushing_ = false; |
| 56 if (client_) { |
| 57 client_->didReset(); |
| 58 client_->willDestroy(); |
| 59 } |
| 60 } |
| 61 |
| 62 void SimpleWebPaintSurfaceImpl::DidCopyPaintSurface() { |
| 63 if (flushing_) { |
| 64 flushing_ = false; |
| 65 if (client_) |
| 66 client_->didFlush(); |
| 67 } |
| 68 } |
| 69 |
| 70 void SimpleWebPaintSurfaceImpl::setClient(WebPaintSurfaceClient* client) { |
| 71 client_ = client; |
| 72 } |
| 73 |
| 74 WebPaintBuffer* SimpleWebPaintSurfaceImpl::createBuffer(const WebSize& size) { |
| 75 return new SimplePaintBuffer(size); |
| 76 } |
| 77 |
| 78 void SimpleWebPaintSurfaceImpl::paint(WebPaintBuffer* buffer, |
| 79 const WebPoint& destination_point, |
| 80 const WebRect& source_rect) { |
| 81 DCHECK(!flushing_); |
| 82 |
| 83 SimplePaintBuffer* simple_buffer = static_cast<SimplePaintBuffer*>(buffer); |
| 84 SkCanvas* canvas = simple_buffer->skia_canvas(); |
| 85 |
| 86 const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(false); |
| 87 |
| 88 SkPaint paint; |
| 89 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 90 |
| 91 SkIRect src_irect = SkIRect::MakeXYWH(source_rect.x, |
| 92 source_rect.y, |
| 93 source_rect.width, |
| 94 source_rect.height); |
| 95 SkRect dest_rect = SkRect::MakeXYWH(SkIntToScalar(destination_point.x), |
| 96 SkIntToScalar(destination_point.y), |
| 97 SkIntToScalar(source_rect.width), |
| 98 SkIntToScalar(source_rect.height)); |
| 99 canvas_.drawBitmapRect(bitmap, &src_irect, dest_rect, &paint); |
| 100 |
| 101 damage_bounds_ = damage_bounds_.Union( |
| 102 gfx::Rect(destination_point.x, |
| 103 destination_point.y, |
| 104 source_rect.width, |
| 105 source_rect.height)); |
| 106 } |
| 107 |
| 108 void SimpleWebPaintSurfaceImpl::scroll(const WebRect& clip_rect, |
| 109 const WebPoint& scroll_delta) { |
| 110 DCHECK(!flushing_); |
| 111 |
| 112 gfx::ScrollCanvas(&canvas_, clip_rect, scroll_delta); |
| 113 |
| 114 damage_bounds_ = damage_bounds_.Union(clip_rect); |
| 115 } |
| 116 |
| 117 bool SimpleWebPaintSurfaceImpl::flush() { |
| 118 DCHECK(!flushing_); |
| 119 |
| 120 gfx::Rect bounds = damage_bounds_; |
| 121 damage_bounds_ = gfx::Rect(); |
| 122 flushing_ = true; |
| 123 |
| 124 delegate_->DidModifyPaintSurface(bounds); |
| 125 return true; |
| 126 } |
OLD | NEW |