| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "examples/ganesh_app/ganesh_view.h" | 5 #include "examples/ganesh_app/ganesh_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" |
| 7 #include "mojo/skia/ganesh_surface.h" | 8 #include "mojo/skia/ganesh_surface.h" |
| 8 #include "third_party/skia/include/core/SkCanvas.h" | 9 #include "third_party/skia/include/core/SkCanvas.h" |
| 10 #include "third_party/skia/include/core/SkColor.h" |
| 9 | 11 |
| 10 namespace examples { | 12 namespace examples { |
| 13 |
| 11 namespace { | 14 namespace { |
| 12 | 15 |
| 13 mojo::Size ToSize(const mojo::Rect& rect) { | 16 mojo::Size ToSize(const mojo::Rect& rect) { |
| 14 mojo::Size size; | 17 mojo::Size size; |
| 15 size.width = rect.width; | 18 size.width = rect.width; |
| 16 size.height = rect.height; | 19 size.height = rect.height; |
| 17 return size; | 20 return size; |
| 18 } | 21 } |
| 19 } | 22 |
| 23 } // namespace |
| 20 | 24 |
| 21 GaneshView::GaneshView(mojo::Shell* shell, mojo::View* view) | 25 GaneshView::GaneshView(mojo::Shell* shell, mojo::View* view) |
| 22 : view_(view), | 26 : view_(view), |
| 23 gl_context_(mojo::GLContext::Create(shell)), | 27 gl_context_(mojo::GLContext::Create(shell)), |
| 24 gr_context_(gl_context_), | 28 gr_context_(gl_context_), |
| 25 texture_uploader_(this, shell, gl_context_) { | 29 texture_uploader_(this, shell, gl_context_) { |
| 30 view_->AddObserver(this); |
| 26 Draw(ToSize(view_->bounds())); | 31 Draw(ToSize(view_->bounds())); |
| 27 } | 32 } |
| 28 | 33 |
| 29 GaneshView::~GaneshView() { | 34 GaneshView::~GaneshView() { |
| 30 if (gl_context_) | 35 if (gl_context_) |
| 31 gl_context_->Destroy(); | 36 gl_context_->Destroy(); |
| 32 } | 37 } |
| 33 | 38 |
| 34 void GaneshView::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) { | 39 void GaneshView::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) { |
| 35 view_->SetSurfaceId(surface_id.Pass()); | 40 view_->SetSurfaceId(surface_id.Pass()); |
| 36 } | 41 } |
| 37 | 42 |
| 38 void GaneshView::OnViewDestroyed(mojo::View* view) { | 43 void GaneshView::OnViewDestroyed(mojo::View* view) { |
| 44 DCHECK(view == view_); |
| 45 view_->RemoveObserver(this); |
| 39 delete this; | 46 delete this; |
| 40 } | 47 } |
| 41 | 48 |
| 42 void GaneshView::OnViewBoundsChanged(mojo::View* view, | 49 void GaneshView::OnViewBoundsChanged(mojo::View* view, |
| 43 const mojo::Rect& old_bounds, | 50 const mojo::Rect& old_bounds, |
| 44 const mojo::Rect& new_bounds) { | 51 const mojo::Rect& new_bounds) { |
| 45 Draw(ToSize(new_bounds)); | 52 Draw(ToSize(new_bounds)); |
| 46 } | 53 } |
| 47 | 54 |
| 48 void GaneshView::Draw(const mojo::Size& size) { | 55 void GaneshView::Draw(const mojo::Size& size) { |
| 49 mojo::GaneshContext::Scope scope(&gr_context_); | 56 mojo::GaneshContext::Scope scope(&gr_context_); |
| 50 mojo::GaneshSurface surface( | 57 mojo::GaneshSurface surface( |
| 51 &gr_context_, make_scoped_ptr(new mojo::GLTexture(gl_context_, size))); | 58 &gr_context_, make_scoped_ptr(new mojo::GLTexture(gl_context_, size))); |
| 52 | 59 |
| 53 SkCanvas* canvas = surface.canvas(); | 60 SkCanvas* canvas = surface.canvas(); |
| 61 canvas->clear(SK_ColorCYAN); |
| 54 | 62 |
| 55 SkPaint paint; | 63 SkPaint paint; |
| 64 paint.setColor(SK_ColorGREEN); |
| 65 SkRect rect = SkRect::MakeWH(size.width, size.height); |
| 66 rect.inset(10, 10); |
| 67 canvas->drawRect(rect, paint); |
| 68 |
| 56 paint.setColor(SK_ColorRED); | 69 paint.setColor(SK_ColorRED); |
| 57 paint.setFlags(SkPaint::kAntiAlias_Flag); | 70 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 58 canvas->drawCircle(50, 100, 100, paint); | 71 canvas->drawCircle(50, 100, 100, paint); |
| 72 |
| 59 canvas->flush(); | 73 canvas->flush(); |
| 60 | 74 |
| 61 texture_uploader_.Upload(surface.TakeTexture()); | 75 texture_uploader_.Upload(surface.TakeTexture()); |
| 62 } | 76 } |
| 63 | 77 |
| 64 } // namespace examples | 78 } // namespace examples |
| OLD | NEW |