OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "examples/ganesh_app/ganesh_view.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/trace_event/trace_event.h" | |
9 #include "mojo/skia/ganesh_texture_surface.h" | |
10 #include "third_party/skia/include/core/SkCanvas.h" | |
11 #include "third_party/skia/include/core/SkColor.h" | |
12 | |
13 namespace examples { | |
14 | |
15 namespace { | |
16 | |
17 mojo::Size ToSize(const mojo::Rect& rect) { | |
18 mojo::Size size; | |
19 size.width = rect.width; | |
20 size.height = rect.height; | |
21 return size; | |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 GaneshView::GaneshView(mojo::Shell* shell, mojo::View* view) | |
27 : view_(view), | |
28 gl_context_(mojo::GLContext::Create(shell)), | |
29 gr_context_(new mojo::GaneshContext(gl_context_)), | |
30 texture_uploader_(this, shell, gl_context_) { | |
31 view_->AddObserver(this); | |
32 Draw(ToSize(view_->bounds())); | |
33 } | |
34 | |
35 GaneshView::~GaneshView() { | |
36 if (gl_context_) { | |
37 // GaneshContext needs to be destroyed before GLContext. | |
38 gr_context_.reset(); | |
39 gl_context_->Destroy(); | |
40 } | |
41 } | |
42 | |
43 void GaneshView::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) { | |
44 view_->SetSurfaceId(surface_id.Pass()); | |
45 } | |
46 | |
47 void GaneshView::OnViewDestroyed(mojo::View* view) { | |
48 DCHECK(view == view_); | |
49 view_->RemoveObserver(this); | |
50 delete this; | |
51 } | |
52 | |
53 void GaneshView::OnViewInputEvent(mojo::View* view, | |
54 const mojo::EventPtr& event) { | |
55 Draw(ToSize(view_->bounds())); | |
56 } | |
57 | |
58 void GaneshView::OnViewBoundsChanged(mojo::View* view, | |
59 const mojo::Rect& old_bounds, | |
60 const mojo::Rect& new_bounds) { | |
61 Draw(ToSize(new_bounds)); | |
62 } | |
63 | |
64 void GaneshView::Draw(const mojo::Size& size) { | |
65 TRACE_EVENT0("ganesh_app", __func__); | |
66 mojo::GaneshContext::Scope scope(gr_context_.get()); | |
67 mojo::GaneshTextureSurface surface( | |
68 gr_context_.get(), | |
69 std::unique_ptr<mojo::GLTexture>(new mojo::GLTexture(gl_context_, size))); | |
70 | |
71 SkCanvas* canvas = surface.canvas(); | |
72 canvas->clear(SK_ColorCYAN); | |
73 | |
74 SkPaint paint; | |
75 paint.setColor(SK_ColorGREEN); | |
76 SkRect rect = SkRect::MakeWH(size.width, size.height); | |
77 rect.inset(10, 10); | |
78 canvas->drawRect(rect, paint); | |
79 | |
80 paint.setColor(SK_ColorRED); | |
81 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
82 canvas->drawCircle(50, 100, 100, paint); | |
83 | |
84 canvas->flush(); | |
85 | |
86 texture_uploader_.Upload( | |
87 scoped_ptr<mojo::GLTexture>(surface.TakeTexture().release())); | |
88 } | |
89 | |
90 } // namespace examples | |
OLD | NEW |