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

Side by Side Diff: mojo/examples/bitmap_uploader/bitmap_uploader.cc

Issue 684543003: Move //mojo/examples to //examples (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 6 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « mojo/examples/bitmap_uploader/bitmap_uploader.h ('k') | mojo/examples/browser/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "mojo/examples/bitmap_uploader/bitmap_uploader.h"
6
7 #ifndef GL_GLEXT_PROTOTYPES
8 #define GL_GLEXT_PROTOTYPES
9 #endif // GL_GLEXT_PROTOTYPES
10
11 #include "base/bind.h"
12 #include "cc/surfaces/surface_id.h"
13 #include "cc/surfaces/surface_id_allocator.h"
14 #include "gpu/GLES2/gl2chromium.h"
15 #include "gpu/GLES2/gl2extchromium.h"
16 #include "gpu/command_buffer/common/mailbox.h"
17 #include "mojo/converters/geometry/geometry_type_converters.h"
18 #include "mojo/converters/surfaces/surfaces_type_converters.h"
19 #include "mojo/converters/surfaces/surfaces_utils.h"
20 #include "mojo/public/c/gles2/gles2.h"
21 #include "mojo/public/cpp/application/connect.h"
22 #include "mojo/public/interfaces/application/shell.mojom.h"
23 #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h"
24 #include "third_party/khronos/GLES2/gl2.h"
25 #include "ui/gfx/geometry/rect.h"
26
27 namespace mojo {
28
29 namespace {
30 void LostContext(void*) {
31 DCHECK(false);
32 }
33
34 uint32_t TextureFormat() {
35 return SK_B32_SHIFT ? GL_RGBA : GL_BGRA_EXT;
36 }
37 }
38
39 BitmapUploader::BitmapUploader(View* view)
40 : view_(view),
41 color_(SK_ColorTRANSPARENT),
42 next_resource_id_(1u),
43 weak_factory_(this) {
44 }
45
46 void BitmapUploader::Init(Shell* shell) {
47 ServiceProviderPtr surfaces_service_provider;
48 shell->ConnectToApplication("mojo:surfaces_service",
49 GetProxy(&surfaces_service_provider));
50 ConnectToService(surfaces_service_provider.get(), &surfaces_service_);
51 ServiceProviderPtr gpu_service_provider;
52 shell->ConnectToApplication("mojo:native_viewport_service",
53 GetProxy(&gpu_service_provider));
54 ConnectToService(gpu_service_provider.get(), &gpu_service_);
55
56 surfaces_service_->CreateSurfaceConnection(base::Bind(
57 &BitmapUploader::OnSurfaceConnectionCreated, weak_factory_.GetWeakPtr()));
58 CommandBufferPtr gles2_client;
59 gpu_service_->CreateOffscreenGLES2Context(GetProxy(&gles2_client));
60 gles2_context_ =
61 MojoGLES2CreateContext(gles2_client.PassMessagePipe().release().value(),
62 &LostContext,
63 NULL,
64 Environment::GetDefaultAsyncWaiter());
65 MojoGLES2MakeCurrent(gles2_context_);
66 }
67
68 BitmapUploader::~BitmapUploader() {
69 MojoGLES2DestroyContext(gles2_context_);
70 }
71
72 void BitmapUploader::SetColor(SkColor color) {
73 if (color_ == color)
74 return;
75 color_ = color;
76 if (surface_)
77 Upload();
78 }
79
80 void BitmapUploader::SetBitmap(const SkBitmap& bitmap) {
81 bitmap_ = bitmap;
82 if (surface_)
83 Upload();
84 }
85
86 void BitmapUploader::Upload() {
87 const gfx::Size size(view_->bounds().width, view_->bounds().height);
88 if (size.IsEmpty()) {
89 view_->SetSurfaceId(SurfaceId::New());
90 return;
91 }
92 if (!surface_) // Can't upload yet, store for later.
93 return;
94 if (id_.is_null() || size != surface_size_) {
95 if (!id_.is_null())
96 surface_->DestroySurface(SurfaceId::From(id_));
97 id_ = id_allocator_->GenerateId();
98 surface_->CreateSurface(SurfaceId::From(id_), Size::From(size));
99 view_->SetSurfaceId(SurfaceId::From(id_));
100 surface_size_ = size;
101 }
102
103 gfx::Rect bounds(size);
104 PassPtr pass = CreateDefaultPass(1, bounds);
105 FramePtr frame = Frame::New();
106 frame->resources.resize(0u);
107
108 pass->quads.resize(0u);
109 pass->shared_quad_states.push_back(CreateDefaultSQS(size));
110
111 MojoGLES2MakeCurrent(gles2_context_);
112 if (!bitmap_.isNull()) {
113 gfx::Size bitmap_size(bitmap_.width(), bitmap_.height());
114 GLuint texture_id = BindTextureForSize(bitmap_size);
115 bitmap_.lockPixels();
116 glTexSubImage2D(GL_TEXTURE_2D,
117 0,
118 0,
119 0,
120 bitmap_size.width(),
121 bitmap_size.height(),
122 TextureFormat(),
123 GL_UNSIGNED_BYTE,
124 bitmap_.getPixels());
125 bitmap_.unlockPixels();
126
127 gpu::Mailbox mailbox = gpu::Mailbox::Generate();
128 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name);
129 GLuint sync_point = glInsertSyncPointCHROMIUM();
130
131 TransferableResourcePtr resource = TransferableResource::New();
132 resource->id = next_resource_id_++;
133 resource_to_texture_id_map_[resource->id] = texture_id;
134 resource->format = mojo::RESOURCE_FORMAT_RGBA_8888;
135 resource->filter = GL_LINEAR;
136 resource->size = Size::From(bitmap_size);
137 MailboxHolderPtr mailbox_holder = MailboxHolder::New();
138 mailbox_holder->mailbox = Mailbox::From(mailbox);
139 mailbox_holder->texture_target = GL_TEXTURE_2D;
140 mailbox_holder->sync_point = sync_point;
141 resource->mailbox_holder = mailbox_holder.Pass();
142 resource->is_repeated = false;
143 resource->is_software = false;
144
145 QuadPtr quad = Quad::New();
146 quad->material = MATERIAL_TEXTURE_CONTENT;
147 quad->rect = Rect::From(bounds);
148 quad->opaque_rect = Rect::From(bounds);
149 quad->visible_rect = Rect::From(bounds);
150 quad->needs_blending = true;
151 quad->shared_quad_state_index = 0u;
152
153 TextureQuadStatePtr texture_state = TextureQuadState::New();
154 texture_state->resource_id = resource->id;
155 texture_state->premultiplied_alpha = true;
156 texture_state->uv_top_left = PointF::From(gfx::PointF(0.f, 0.f));
157 texture_state->uv_bottom_right = PointF::From(gfx::PointF(1.f, 1.f));
158 texture_state->background_color = Color::From(SkColor(SK_ColorTRANSPARENT));
159 for (int i = 0; i < 4; ++i)
160 texture_state->vertex_opacity.push_back(1.f);
161 texture_state->flipped = false;
162
163 frame->resources.push_back(resource.Pass());
164 quad->texture_quad_state = texture_state.Pass();
165 pass->quads.push_back(quad.Pass());
166 }
167
168 if (color_ != SK_ColorTRANSPARENT) {
169 QuadPtr quad = Quad::New();
170 quad->material = MATERIAL_SOLID_COLOR;
171 quad->rect = Rect::From(bounds);
172 quad->opaque_rect = Rect::From(gfx::Rect());
173 quad->visible_rect = Rect::From(bounds);
174 quad->needs_blending = true;
175 quad->shared_quad_state_index = 0u;
176
177 SolidColorQuadStatePtr color_state = SolidColorQuadState::New();
178 color_state->color = Color::From(color_);
179 color_state->force_anti_aliasing_off = false;
180
181 quad->solid_color_quad_state = color_state.Pass();
182 pass->quads.push_back(quad.Pass());
183 }
184
185 frame->passes.push_back(pass.Pass());
186
187 surface_->SubmitFrame(SurfaceId::From(id_), frame.Pass());
188 }
189
190 void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) {
191 if (!resources.size())
192 return;
193 MojoGLES2MakeCurrent(gles2_context_);
194 // TODO(jamesr): Recycle.
195 for (size_t i = 0; i < resources.size(); ++i) {
196 ReturnedResourcePtr resource = resources[i].Pass();
197 DCHECK_EQ(1, resource->count);
198 glWaitSyncPointCHROMIUM(resource->sync_point);
199 uint32_t texture_id = resource_to_texture_id_map_[resource->id];
200 DCHECK_NE(0u, texture_id);
201 resource_to_texture_id_map_.erase(resource->id);
202 glDeleteTextures(1, &texture_id);
203 }
204 }
205
206 void BitmapUploader::OnSurfaceConnectionCreated(SurfacePtr surface,
207 uint32_t id_namespace) {
208 surface_ = surface.Pass();
209 surface_.set_client(this);
210 id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace));
211 if (color_ != SK_ColorTRANSPARENT || !bitmap_.isNull())
212 Upload();
213 }
214
215 uint32_t BitmapUploader::BindTextureForSize(const gfx::Size size) {
216 // TODO(jamesr): Recycle textures.
217 GLuint texture = 0u;
218 glGenTextures(1, &texture);
219 glBindTexture(GL_TEXTURE_2D, texture);
220 glTexImage2D(GL_TEXTURE_2D,
221 0,
222 TextureFormat(),
223 size.width(),
224 size.height(),
225 0,
226 TextureFormat(),
227 GL_UNSIGNED_BYTE,
228 0);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
230 return texture;
231 }
232
233 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/examples/bitmap_uploader/bitmap_uploader.h ('k') | mojo/examples/browser/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698