OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "cc/resources/resource_update_controller.h" | 5 #include "cc/resources/resource_update_controller.h" |
6 | 6 |
7 #include <limits> | 7 #include "base/bind.h" |
8 | |
9 #include "base/debug/trace_event.h" | |
10 #include "cc/base/thread.h" | 8 #include "cc/base/thread.h" |
11 #include "cc/output/context_provider.h" | |
12 #include "cc/output/texture_copier.h" | 9 #include "cc/output/texture_copier.h" |
13 #include "cc/resources/prioritized_resource.h" | 10 #include "cc/resources/prioritized_resource.h" |
14 #include "cc/resources/resource_provider.h" | 11 #include "cc/resources/resource_provider.h" |
15 #include "skia/ext/refptr.h" | |
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" | |
17 #include "third_party/khronos/GLES2/gl2.h" | |
18 #include "third_party/skia/include/gpu/SkGpuDevice.h" | |
19 | |
20 using WebKit::WebGraphicsContext3D; | |
21 | 12 |
22 namespace { | 13 namespace { |
23 | 14 |
24 // Number of partial updates we allow. | 15 // Number of partial updates we allow. |
25 const size_t kPartialTextureUpdatesMax = 12; | 16 const size_t kPartialTextureUpdatesMax = 12; |
26 | 17 |
27 // Measured in seconds. | 18 // Measured in seconds. |
28 const double kTextureUpdateTickRate = 0.004; | 19 const double kTextureUpdateTickRate = 0.004; |
29 | 20 |
30 // Measured in seconds. | 21 // Measured in seconds. |
31 const double kUploaderBusyTickRate = 0.001; | 22 const double kUploaderBusyTickRate = 0.001; |
32 | 23 |
33 // Number of blocking update intervals to allow. | 24 // Number of blocking update intervals to allow. |
34 const size_t kMaxBlockingUpdateIntervals = 4; | 25 const size_t kMaxBlockingUpdateIntervals = 4; |
35 | 26 |
36 skia::RefPtr<SkCanvas> CreateAcceleratedCanvas( | |
37 GrContext* gr_context, gfx::Size canvas_size, unsigned texture_id) { | |
38 GrBackendTextureDesc texture_desc; | |
39 texture_desc.fFlags = kRenderTarget_GrBackendTextureFlag; | |
40 texture_desc.fWidth = canvas_size.width(); | |
41 texture_desc.fHeight = canvas_size.height(); | |
42 texture_desc.fConfig = kSkia8888_GrPixelConfig; | |
43 texture_desc.fTextureHandle = texture_id; | |
44 skia::RefPtr<GrTexture> target = | |
45 skia::AdoptRef(gr_context->wrapBackendTexture(texture_desc)); | |
46 skia::RefPtr<SkDevice> device = | |
47 skia::AdoptRef(new SkGpuDevice(gr_context, target.get())); | |
48 return skia::AdoptRef(new SkCanvas(device.get())); | |
49 } | |
50 | |
51 } // namespace | 27 } // namespace |
52 | 28 |
53 namespace cc { | 29 namespace cc { |
54 | 30 |
55 size_t ResourceUpdateController::MaxPartialTextureUpdates() { | 31 size_t ResourceUpdateController::MaxPartialTextureUpdates() { |
56 return kPartialTextureUpdatesMax; | 32 return kPartialTextureUpdatesMax; |
57 } | 33 } |
58 | 34 |
59 size_t ResourceUpdateController::MaxFullUpdatesPerTick( | 35 size_t ResourceUpdateController::MaxFullUpdatesPerTick( |
60 ResourceProvider* resource_provider) { | 36 ResourceProvider* resource_provider) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 80 } |
105 | 81 |
106 first_update_attempt_ = false; | 82 first_update_attempt_ = false; |
107 } | 83 } |
108 | 84 |
109 void ResourceUpdateController::DiscardUploadsToEvictedResources() { | 85 void ResourceUpdateController::DiscardUploadsToEvictedResources() { |
110 queue_->ClearUploadsToEvictedResources(); | 86 queue_->ClearUploadsToEvictedResources(); |
111 } | 87 } |
112 | 88 |
113 void ResourceUpdateController::UpdateTexture(ResourceUpdate update) { | 89 void ResourceUpdateController::UpdateTexture(ResourceUpdate update) { |
114 if (update.picture) { | 90 update.bitmap->lockPixels(); |
115 PrioritizedResource* texture = update.texture; | 91 update.texture->SetPixels( |
116 gfx::Rect picture_rect = update.content_rect; | 92 resource_provider_, |
117 gfx::Rect source_rect = update.source_rect; | 93 static_cast<const uint8_t*>(update.bitmap->getPixels()), |
118 gfx::Vector2d dest_offset = update.dest_offset; | 94 update.content_rect, |
119 | 95 update.source_rect, |
120 texture->AcquireBackingTexture(resource_provider_); | 96 update.dest_offset); |
121 DCHECK(texture->have_backing_texture()); | 97 update.bitmap->unlockPixels(); |
122 | |
123 DCHECK_EQ(resource_provider_->GetResourceType(texture->resource_id()), | |
124 ResourceProvider::GLTexture); | |
125 | |
126 cc::ContextProvider* offscreen_contexts = | |
127 resource_provider_->offscreen_context_provider(); | |
128 | |
129 ResourceProvider::ScopedWriteLockGL lock( | |
130 resource_provider_, texture->resource_id()); | |
131 | |
132 // Flush the compositor context to ensure that textures there are available | |
133 // in the shared context. Do this after locking/creating the compositor | |
134 // texture. | |
135 resource_provider_->Flush(); | |
136 | |
137 // Make sure skia uses the correct GL context. | |
138 offscreen_contexts->Context3d()->makeContextCurrent(); | |
139 | |
140 // Create an accelerated canvas to draw on. | |
141 skia::RefPtr<SkCanvas> canvas = CreateAcceleratedCanvas( | |
142 offscreen_contexts->GrContext(), texture->size(), lock.texture_id()); | |
143 | |
144 // The compositor expects the textures to be upside-down so it can flip | |
145 // the final composited image. Ganesh renders the image upright so we | |
146 // need to do a y-flip. | |
147 canvas->translate(0.0, texture->size().height()); | |
148 canvas->scale(1.0, -1.0); | |
149 // Clip to the destination on the texture that must be updated. | |
150 canvas->clipRect(SkRect::MakeXYWH(dest_offset.x(), | |
151 dest_offset.y(), | |
152 source_rect.width(), | |
153 source_rect.height())); | |
154 // Translate the origin of picture_rect to dest_offset. | |
155 // Note that dest_offset is defined relative to source_rect. | |
156 canvas->translate( | |
157 picture_rect.x() - source_rect.x() + dest_offset.x(), | |
158 picture_rect.y() - source_rect.y() + dest_offset.y()); | |
159 canvas->drawPicture(*update.picture); | |
160 | |
161 // Flush skia context so that all the rendered stuff appears on the | |
162 // texture. | |
163 offscreen_contexts->GrContext()->flush(); | |
164 | |
165 // Flush the GL context so rendering results from this context are | |
166 // visible in the compositor's context. | |
167 offscreen_contexts->Context3d()->flush(); | |
168 | |
169 // Use the compositor's GL context again. | |
170 resource_provider_->GraphicsContext3D()->makeContextCurrent(); | |
171 } | |
172 | |
173 if (update.bitmap) { | |
174 update.bitmap->lockPixels(); | |
175 update.texture->SetPixels( | |
176 resource_provider_, | |
177 static_cast<const uint8_t*>(update.bitmap->getPixels()), | |
178 update.content_rect, | |
179 update.source_rect, | |
180 update.dest_offset); | |
181 update.bitmap->unlockPixels(); | |
182 } | |
183 } | 98 } |
184 | 99 |
185 void ResourceUpdateController::Finalize() { | 100 void ResourceUpdateController::Finalize() { |
186 while (queue_->FullUploadSize()) | 101 while (queue_->FullUploadSize()) |
187 UpdateTexture(queue_->TakeFirstFullUpload()); | 102 UpdateTexture(queue_->TakeFirstFullUpload()); |
188 | 103 |
189 while (queue_->PartialUploadSize()) | 104 while (queue_->PartialUploadSize()) |
190 UpdateTexture(queue_->TakeFirstPartialUpload()); | 105 UpdateTexture(queue_->TakeFirstPartialUpload()); |
191 | 106 |
192 resource_provider_->FlushUploads(); | 107 resource_provider_->FlushUploads(); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 if (!uploads) | 181 if (!uploads) |
267 return; | 182 return; |
268 | 183 |
269 while (queue_->FullUploadSize() && uploads--) | 184 while (queue_->FullUploadSize() && uploads--) |
270 UpdateTexture(queue_->TakeFirstFullUpload()); | 185 UpdateTexture(queue_->TakeFirstFullUpload()); |
271 | 186 |
272 resource_provider_->FlushUploads(); | 187 resource_provider_->FlushUploads(); |
273 } | 188 } |
274 | 189 |
275 } // namespace cc | 190 } // namespace cc |
OLD | NEW |