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

Side by Side Diff: mojo/services/public/cpp/view_manager/lib/bitmap_uploader.cc

Issue 534843002: Convert view manager to surfaces with uploading shim in client lib (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months 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
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/services/public/cpp/view_manager/lib/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/services/public/cpp/geometry/geometry_type_converters.h"
18 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h"
19 #include "mojo/services/public/cpp/surfaces/surfaces_utils.h"
20 #include "third_party/khronos/GLES2/gl2.h"
21 #include "ui/gfx/geometry/rect.h"
22
23 namespace mojo {
24
25 namespace {
26 void LostContext(void*) {
27 DCHECK(false);
28 }
29 }
30
31 BitmapUploader::BitmapUploader(SurfacesServicePtr surfaces_service,
32 GpuPtr gpu_service)
33 : surfaces_service_(surfaces_service.Pass()),
34 gpu_service_(gpu_service.Pass()),
35 next_resource_id_(1u),
36 weak_factory_(this) {
37 surfaces_service_->CreateSurfaceConnection(base::Bind(
38 &BitmapUploader::OnSurfaceConnectionCreated, weak_factory_.GetWeakPtr()));
39 CommandBufferPtr gles2_client;
40 gpu_service_->CreateOffscreenGLES2Context(Get(&gles2_client));
41 gles2_context_ =
42 MojoGLES2CreateContext(gles2_client.PassMessagePipe().release().value(),
43 &LostContext,
44 NULL,
45 Environment::GetDefaultAsyncWaiter());
46 MojoGLES2MakeCurrent(gles2_context_);
47 }
48
49 BitmapUploader::~BitmapUploader() {
50 MojoGLES2DestroyContext(gles2_context_);
51 }
52
53 void BitmapUploader::OnSurfaceConnectionCreated(SurfacePtr surface,
54 uint32_t id_namespace) {
55 surface_ = surface.Pass();
56 surface_.set_client(this);
57 id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace));
58 if (!bitmap_.isNull()) {
59 Upload(bitmap_, done_callback_);
60 bitmap_.reset();
61 done_callback_.Reset();
62 }
63 }
64 void BitmapUploader::BindTextureForSize(const gfx::Size size) {
65 // TODO(jamesr): Recycle textures.
66 GLuint texture = 0u;
67 glGenTextures(1, &texture);
68 glBindTexture(GL_TEXTURE_2D, texture);
69 glTexImage2D(GL_TEXTURE_2D,
70 0,
71 GL_RGBA,
72 size.width(),
73 size.height(),
74 0,
75 GL_RGBA,
76 GL_UNSIGNED_BYTE,
77 0);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
79 }
80
81 void BitmapUploader::Upload(
82 SkBitmap bitmap,
83 const base::Callback<void(SurfaceIdPtr)>& done_callback) {
84 if (bitmap.width() == 0 || bitmap.height() == 0) {
85 done_callback.Run(SurfaceId::New());
86 return;
87 }
88 if (!surface_) { // Can't upload yet, store for later.
89 bitmap_ = bitmap;
90 done_callback_ = done_callback;
91 return;
92 }
93 gfx::Size bitmap_size(bitmap.width(), bitmap.height());
94 if (id_.is_null() || bitmap_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(bitmap_size));
99 surface_size_ = bitmap_size;
100 }
101
102 BindTextureForSize(bitmap_size);
103 bitmap.lockPixels();
104 glTexSubImage2D(GL_TEXTURE_2D,
105 0,
106 0,
107 0,
108 bitmap_size.width(),
109 bitmap_size.height(),
110 GL_RGBA,
111 GL_UNSIGNED_BYTE,
112 bitmap.getPixels());
113 bitmap.unlockPixels();
114
115 gpu::Mailbox mailbox = gpu::Mailbox::Generate();
116 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name);
117 GLuint sync_point = glInsertSyncPointCHROMIUM();
118
119 TransferableResourcePtr resource = TransferableResource::New();
120 resource->id = next_resource_id_++;
121 resource->format = mojo::RESOURCE_FORMAT_RGBA_8888;
122 resource->filter = GL_LINEAR;
123 resource->size = Size::From(bitmap_size);
124 MailboxHolderPtr mailbox_holder = MailboxHolder::New();
125 mailbox_holder->mailbox = Mailbox::From(mailbox);
126 mailbox_holder->texture_target = GL_TEXTURE_2D;
127 mailbox_holder->sync_point = sync_point;
128 resource->mailbox_holder = mailbox_holder.Pass();
129 resource->is_repeated = false;
130 resource->is_software = false;
131
132 gfx::Rect bitmap_rect(bitmap_size);
133
134 QuadPtr quad = Quad::New();
135 quad->material = MATERIAL_TEXTURE_CONTENT;
136 quad->rect = Rect::From(bitmap_rect);
137 quad->opaque_rect = Rect::From(bitmap_rect);
138 quad->visible_rect = Rect::From(bitmap_rect);
139 quad->needs_blending = true;
140 quad->shared_quad_state_index = 0u;
141
142 TextureQuadStatePtr texture_state = TextureQuadState::New();
143 texture_state->resource_id = resource->id;
144 texture_state->premultiplied_alpha = true;
145 texture_state->uv_top_left = PointF::From(gfx::PointF(0.f, 0.f));
146 texture_state->uv_bottom_right = PointF::From(gfx::PointF(1.f, 1.f));
147 texture_state->background_color = Color::From(SK_ColorRED);
148 for (int i = 0; i < 4; ++i)
149 texture_state->vertex_opacity.push_back(1.f);
150 texture_state->flipped = false;
151
152 quad->texture_quad_state = texture_state.Pass();
153
154 SharedQuadStatePtr sqs = CreateDefaultSQS(bitmap_size);
155
156 PassPtr pass = CreateDefaultPass(1, bitmap_rect);
157
158 pass->quads.push_back(quad.Pass());
159 pass->shared_quad_states.push_back(sqs.Pass());
160
161 FramePtr frame = Frame::New();
162 frame->resources.push_back(resource.Pass());
163 frame->passes.push_back(pass.Pass());
164
165 surface_->SubmitFrame(SurfaceId::From(id_), frame.Pass());
166 done_callback.Run(SurfaceId::From(id_));
167 }
168
169 void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) {
170 // TODO(jamesr): Recycle.
171 for (size_t i = 0; i < resources.size(); ++i) {
172 ReturnedResourcePtr resource = resources[i].Pass();
173 DCHECK_EQ(1, resource->count);
174 glWaitSyncPointCHROMIUM(resource->sync_point);
175 glDeleteTextures(1, &resource->id);
176 }
177 }
178
179 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698