[Android WebView] Put AndroidStreamReaderURLRequestJob into a namespace
[chromium-blink-merge.git] / cc / resources / resource_provider.h
blob99947e57c68fc793139dc50a3f2605c21d3a5b75
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
3 // found in the LICENSE file.
5 #ifndef CC_RESOURCES_RESOURCE_PROVIDER_H_
6 #define CC_RESOURCES_RESOURCE_PROVIDER_H_
8 #include <deque>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/basictypes.h"
15 #include "base/callback.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/linked_ptr.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/threading/thread_checker.h"
20 #include "base/trace_event/memory_allocator_dump.h"
21 #include "base/trace_event/memory_dump_provider.h"
22 #include "cc/base/cc_export.h"
23 #include "cc/base/resource_id.h"
24 #include "cc/output/context_provider.h"
25 #include "cc/output/output_surface.h"
26 #include "cc/resources/release_callback_impl.h"
27 #include "cc/resources/resource_format.h"
28 #include "cc/resources/return_callback.h"
29 #include "cc/resources/shared_bitmap.h"
30 #include "cc/resources/single_release_callback_impl.h"
31 #include "cc/resources/texture_mailbox.h"
32 #include "cc/resources/transferable_resource.h"
33 #include "third_party/khronos/GLES2/gl2.h"
34 #include "third_party/khronos/GLES2/gl2ext.h"
35 #include "third_party/skia/include/core/SkBitmap.h"
36 #include "third_party/skia/include/core/SkCanvas.h"
37 #include "ui/gfx/geometry/size.h"
38 #include "ui/gfx/gpu_memory_buffer.h"
40 class GrContext;
42 namespace gpu {
43 class GpuMemoryBufferManager;
44 namespace gles {
45 class GLES2Interface;
49 namespace gfx {
50 class Rect;
51 class Vector2d;
54 namespace cc {
55 class BlockingTaskRunner;
56 class IdAllocator;
57 class SharedBitmap;
58 class SharedBitmapManager;
60 // This class is not thread-safe and can only be called from the thread it was
61 // created on (in practice, the impl thread).
62 class CC_EXPORT ResourceProvider
63 : public base::trace_event::MemoryDumpProvider {
64 private:
65 struct Resource;
67 public:
68 typedef std::vector<ResourceId> ResourceIdArray;
69 typedef base::hash_set<ResourceId> ResourceIdSet;
70 typedef base::hash_map<ResourceId, ResourceId> ResourceIdMap;
71 enum TextureHint {
72 TEXTURE_HINT_DEFAULT = 0x0,
73 TEXTURE_HINT_IMMUTABLE = 0x1,
74 TEXTURE_HINT_FRAMEBUFFER = 0x2,
75 TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER =
76 TEXTURE_HINT_IMMUTABLE | TEXTURE_HINT_FRAMEBUFFER
78 enum ResourceType {
79 RESOURCE_TYPE_GL_TEXTURE,
80 RESOURCE_TYPE_BITMAP,
83 static scoped_ptr<ResourceProvider> Create(
84 OutputSurface* output_surface,
85 SharedBitmapManager* shared_bitmap_manager,
86 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
87 BlockingTaskRunner* blocking_main_thread_task_runner,
88 int highp_threshold_min,
89 bool use_rgba_4444_texture_format,
90 size_t id_allocation_chunk_size,
91 const std::vector<unsigned>& use_image_texture_targets);
92 ~ResourceProvider() override;
94 void DidLoseOutputSurface() { lost_output_surface_ = true; }
96 int max_texture_size() const { return max_texture_size_; }
97 ResourceFormat memory_efficient_texture_format() const {
98 return use_rgba_4444_texture_format_ ? RGBA_4444 : best_texture_format_;
100 ResourceFormat best_texture_format() const { return best_texture_format_; }
101 ResourceFormat best_render_buffer_format() const {
102 return best_render_buffer_format_;
104 ResourceFormat yuv_resource_format() const { return yuv_resource_format_; }
105 bool use_sync_query() const { return use_sync_query_; }
106 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager() {
107 return gpu_memory_buffer_manager_;
109 size_t num_resources() const { return resources_.size(); }
111 // Checks whether a resource is in use by a consumer.
112 bool InUseByConsumer(ResourceId id);
114 bool IsLost(ResourceId id);
116 void LoseResourceForTesting(ResourceId id);
117 void EnableReadLockFencesForTesting(ResourceId id);
119 // Producer interface.
121 ResourceType default_resource_type() const { return default_resource_type_; }
122 ResourceType GetResourceType(ResourceId id);
124 // Creates a resource of the default resource type.
125 ResourceId CreateResource(const gfx::Size& size,
126 GLint wrap_mode,
127 TextureHint hint,
128 ResourceFormat format);
130 // Creates a resource which is tagged as being managed for GPU memory
131 // accounting purposes.
132 ResourceId CreateManagedResource(const gfx::Size& size,
133 GLenum target,
134 GLint wrap_mode,
135 TextureHint hint,
136 ResourceFormat format);
138 // You can also explicitly create a specific resource type.
139 ResourceId CreateGLTexture(const gfx::Size& size,
140 GLenum target,
141 GLenum texture_pool,
142 GLint wrap_mode,
143 TextureHint hint,
144 ResourceFormat format);
146 ResourceId CreateBitmap(const gfx::Size& size, GLint wrap_mode);
147 // Wraps an IOSurface into a GL resource.
148 ResourceId CreateResourceFromIOSurface(const gfx::Size& size,
149 unsigned io_surface_id);
151 // Wraps an external texture mailbox into a GL resource.
152 ResourceId CreateResourceFromTextureMailbox(
153 const TextureMailbox& mailbox,
154 scoped_ptr<SingleReleaseCallbackImpl> release_callback_impl);
156 ResourceId CreateResourceFromTextureMailbox(
157 const TextureMailbox& mailbox,
158 scoped_ptr<SingleReleaseCallbackImpl> release_callback_impl,
159 bool read_lock_fences_enabled);
161 void DeleteResource(ResourceId id);
163 // Update pixels from image, copying source_rect (in image) to dest_offset (in
164 // the resource).
165 void CopyToResource(ResourceId id,
166 const uint8_t* image,
167 const gfx::Size& image_size);
169 // Only flush the command buffer if supported.
170 // Returns true if the shallow flush occurred, false otherwise.
171 bool ShallowFlushIfSupported();
173 // Creates accounting for a child. Returns a child ID.
174 int CreateChild(const ReturnCallback& return_callback);
176 // Destroys accounting for the child, deleting all accounted resources.
177 void DestroyChild(int child);
179 // Sets whether resources need sync points set on them when returned to this
180 // child. Defaults to true.
181 void SetChildNeedsSyncPoints(int child, bool needs_sync_points);
183 // Gets the child->parent resource ID map.
184 const ResourceIdMap& GetChildToParentMap(int child) const;
186 // Prepares resources to be transfered to the parent, moving them to
187 // mailboxes and serializing meta-data into TransferableResources.
188 // Resources are not removed from the ResourceProvider, but are marked as
189 // "in use".
190 void PrepareSendToParent(const ResourceIdArray& resources,
191 TransferableResourceArray* transferable_resources);
193 // Receives resources from a child, moving them from mailboxes. Resource IDs
194 // passed are in the child namespace, and will be translated to the parent
195 // namespace, added to the child->parent map.
196 // This adds the resources to the working set in the ResourceProvider without
197 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
198 // after calling this method to do that. All calls to ReceiveFromChild should
199 // be followed by a DeclareUsedResourcesFromChild.
200 // NOTE: if the sync_point is set on any TransferableResource, this will
201 // wait on it.
202 void ReceiveFromChild(
203 int child, const TransferableResourceArray& transferable_resources);
205 // Once a set of resources have been received, they may or may not be used.
206 // This declares what set of resources are currently in use from the child,
207 // releasing any other resources back to the child.
208 void DeclareUsedResourcesFromChild(int child,
209 const ResourceIdSet& resources_from_child);
211 // Receives resources from the parent, moving them from mailboxes. Resource
212 // IDs passed are in the child namespace.
213 // NOTE: if the sync_point is set on any TransferableResource, this will
214 // wait on it.
215 void ReceiveReturnsFromParent(
216 const ReturnedResourceArray& transferable_resources);
218 // The following lock classes are part of the ResourceProvider API and are
219 // needed to read and write the resource contents. The user must ensure
220 // that they only use GL locks on GL resources, etc, and this is enforced
221 // by assertions.
222 class CC_EXPORT ScopedReadLockGL {
223 public:
224 ScopedReadLockGL(ResourceProvider* resource_provider,
225 ResourceId resource_id);
226 virtual ~ScopedReadLockGL();
228 unsigned texture_id() const { return resource_->gl_id; }
229 GLenum target() const { return resource_->target; }
231 protected:
232 ResourceProvider* resource_provider_;
233 ResourceId resource_id_;
235 private:
236 const ResourceProvider::Resource* resource_;
238 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL);
241 class CC_EXPORT ScopedSamplerGL : public ScopedReadLockGL {
242 public:
243 ScopedSamplerGL(ResourceProvider* resource_provider,
244 ResourceId resource_id,
245 GLenum filter);
246 ScopedSamplerGL(ResourceProvider* resource_provider,
247 ResourceId resource_id,
248 GLenum unit,
249 GLenum filter);
250 ~ScopedSamplerGL() override;
252 GLenum target() const { return target_; }
254 private:
255 GLenum unit_;
256 GLenum target_;
258 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL);
261 class CC_EXPORT ScopedWriteLockGL {
262 public:
263 ScopedWriteLockGL(ResourceProvider* resource_provider,
264 ResourceId resource_id);
265 ~ScopedWriteLockGL();
267 unsigned texture_id() const { return texture_id_; }
269 private:
270 ResourceProvider* resource_provider_;
271 ResourceProvider::Resource* resource_;
272 unsigned texture_id_;
274 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL);
277 class CC_EXPORT ScopedReadLockSoftware {
278 public:
279 ScopedReadLockSoftware(ResourceProvider* resource_provider,
280 ResourceId resource_id);
281 ~ScopedReadLockSoftware();
283 const SkBitmap* sk_bitmap() const {
284 DCHECK(valid());
285 return &sk_bitmap_;
287 GLint wrap_mode() const { return wrap_mode_; }
289 bool valid() const { return !!sk_bitmap_.getPixels(); }
291 private:
292 ResourceProvider* resource_provider_;
293 ResourceId resource_id_;
294 SkBitmap sk_bitmap_;
295 GLint wrap_mode_;
297 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware);
300 class CC_EXPORT ScopedWriteLockSoftware {
301 public:
302 ScopedWriteLockSoftware(ResourceProvider* resource_provider,
303 ResourceId resource_id);
304 ~ScopedWriteLockSoftware();
306 SkBitmap& sk_bitmap() { return sk_bitmap_; }
307 bool valid() const { return !!sk_bitmap_.getPixels(); }
309 private:
310 ResourceProvider* resource_provider_;
311 ResourceProvider::Resource* resource_;
312 SkBitmap sk_bitmap_;
313 base::ThreadChecker thread_checker_;
315 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware);
318 class CC_EXPORT ScopedWriteLockGpuMemoryBuffer {
319 public:
320 ScopedWriteLockGpuMemoryBuffer(ResourceProvider* resource_provider,
321 ResourceId resource_id);
322 ~ScopedWriteLockGpuMemoryBuffer();
324 gfx::GpuMemoryBuffer* GetGpuMemoryBuffer();
326 private:
327 ResourceProvider* resource_provider_;
328 ResourceProvider::Resource* resource_;
329 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
330 gfx::GpuMemoryBuffer* gpu_memory_buffer_;
331 gfx::Size size_;
332 ResourceFormat format_;
333 base::ThreadChecker thread_checker_;
335 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGpuMemoryBuffer);
338 class CC_EXPORT ScopedWriteLockGr {
339 public:
340 ScopedWriteLockGr(ResourceProvider* resource_provider,
341 ResourceId resource_id);
342 ~ScopedWriteLockGr();
344 void InitSkSurface(bool use_distance_field_text,
345 bool can_use_lcd_text,
346 int msaa_sample_count);
347 void ReleaseSkSurface();
349 SkSurface* sk_surface() { return sk_surface_.get(); }
350 ResourceProvider::Resource* resource() { return resource_; }
352 private:
353 ResourceProvider* resource_provider_;
354 ResourceProvider::Resource* resource_;
355 base::ThreadChecker thread_checker_;
356 skia::RefPtr<SkSurface> sk_surface_;
358 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGr);
361 class Fence : public base::RefCounted<Fence> {
362 public:
363 Fence() {}
365 virtual void Set() = 0;
366 virtual bool HasPassed() = 0;
367 virtual void Wait() = 0;
369 protected:
370 friend class base::RefCounted<Fence>;
371 virtual ~Fence() {}
373 private:
374 DISALLOW_COPY_AND_ASSIGN(Fence);
377 class SynchronousFence : public ResourceProvider::Fence {
378 public:
379 explicit SynchronousFence(gpu::gles2::GLES2Interface* gl);
381 // Overridden from Fence:
382 void Set() override;
383 bool HasPassed() override;
384 void Wait() override;
386 // Returns true if fence has been set but not yet synchornized.
387 bool has_synchronized() const { return has_synchronized_; }
389 private:
390 ~SynchronousFence() override;
392 void Synchronize();
394 gpu::gles2::GLES2Interface* gl_;
395 bool has_synchronized_;
397 DISALLOW_COPY_AND_ASSIGN(SynchronousFence);
400 // Acquire pixel buffer for resource. The pixel buffer can be used to
401 // set resource pixels without performing unnecessary copying.
402 void AcquirePixelBuffer(ResourceId resource);
403 void ReleasePixelBuffer(ResourceId resource);
404 // Map/unmap the acquired pixel buffer.
405 uint8_t* MapPixelBuffer(ResourceId id, int* stride);
406 void UnmapPixelBuffer(ResourceId id);
407 // Asynchronously update pixels from acquired pixel buffer.
408 void BeginSetPixels(ResourceId id);
409 void ForceSetPixelsToComplete(ResourceId id);
410 bool DidSetPixelsComplete(ResourceId id);
412 // For tests only! This prevents detecting uninitialized reads.
413 // Use SetPixels or LockForWrite to allocate implicitly.
414 void AllocateForTesting(ResourceId id);
416 // For tests only!
417 void CreateForTesting(ResourceId id);
419 GLenum TargetForTesting(ResourceId id);
421 // Sets the current read fence. If a resource is locked for read
422 // and has read fences enabled, the resource will not allow writes
423 // until this fence has passed.
424 void SetReadLockFence(Fence* fence) { current_read_lock_fence_ = fence; }
426 // Indicates if we can currently lock this resource for write.
427 bool CanLockForWrite(ResourceId id);
429 void WaitSyncPointIfNeeded(ResourceId id);
431 static GLint GetActiveTextureUnit(gpu::gles2::GLES2Interface* gl);
433 OutputSurface* output_surface() { return output_surface_; }
435 void ValidateResource(ResourceId id) const;
437 GLenum GetImageTextureTarget(ResourceFormat format);
439 // base::trace_event::MemoryDumpProvider implementation.
440 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
441 base::trace_event::ProcessMemoryDump* pmd) override;
443 int tracing_id() const { return tracing_id_; }
445 protected:
446 ResourceProvider(OutputSurface* output_surface,
447 SharedBitmapManager* shared_bitmap_manager,
448 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
449 BlockingTaskRunner* blocking_main_thread_task_runner,
450 int highp_threshold_min,
451 bool use_rgba_4444_texture_format,
452 size_t id_allocation_chunk_size,
453 const std::vector<unsigned>& use_image_texture_targets);
454 void Initialize();
456 private:
457 struct Resource {
458 enum Origin { INTERNAL, EXTERNAL, DELEGATED };
460 ~Resource();
461 Resource(unsigned texture_id,
462 const gfx::Size& size,
463 Origin origin,
464 GLenum target,
465 GLenum filter,
466 GLenum texture_pool,
467 GLint wrap_mode,
468 TextureHint hint,
469 ResourceFormat format);
470 Resource(uint8_t* pixels,
471 SharedBitmap* bitmap,
472 const gfx::Size& size,
473 Origin origin,
474 GLenum filter,
475 GLint wrap_mode);
476 Resource(const SharedBitmapId& bitmap_id,
477 const gfx::Size& size,
478 Origin origin,
479 GLenum filter,
480 GLint wrap_mode);
482 int child_id;
483 unsigned gl_id;
484 // Pixel buffer used for set pixels without unnecessary copying.
485 unsigned gl_pixel_buffer_id;
486 // Query used to determine when asynchronous set pixels complete.
487 unsigned gl_upload_query_id;
488 // Query used to determine when read lock fence has passed.
489 unsigned gl_read_lock_query_id;
490 TextureMailbox mailbox;
491 ReleaseCallbackImpl release_callback_impl;
492 uint8_t* pixels;
493 int lock_for_read_count;
494 int imported_count;
495 int exported_count;
496 bool dirty_image : 1;
497 bool locked_for_write : 1;
498 bool lost : 1;
499 bool marked_for_deletion : 1;
500 bool allocated : 1;
501 bool read_lock_fences_enabled : 1;
502 bool has_shared_bitmap_id : 1;
503 scoped_refptr<Fence> read_lock_fence;
504 gfx::Size size;
505 Origin origin;
506 GLenum target;
507 // TODO(skyostil): Use a separate sampler object for filter state.
508 GLenum original_filter;
509 GLenum filter;
510 unsigned image_id;
511 unsigned bound_image_id;
512 GLenum texture_pool;
513 GLint wrap_mode;
514 TextureHint hint;
515 ResourceType type;
516 ResourceFormat format;
517 SharedBitmapId shared_bitmap_id;
518 SharedBitmap* shared_bitmap;
519 gfx::GpuMemoryBuffer* gpu_memory_buffer;
521 typedef base::hash_map<ResourceId, Resource> ResourceMap;
523 struct Child {
524 Child();
525 ~Child();
527 ResourceIdMap child_to_parent_map;
528 ResourceIdMap parent_to_child_map;
529 ReturnCallback return_callback;
530 bool marked_for_deletion;
531 bool needs_sync_points;
533 typedef base::hash_map<int, Child> ChildMap;
535 bool ReadLockFenceHasPassed(const Resource* resource) {
536 return !resource->read_lock_fence.get() ||
537 resource->read_lock_fence->HasPassed();
540 Resource* InsertResource(ResourceId id, const Resource& resource);
541 Resource* GetResource(ResourceId id);
542 const Resource* LockForRead(ResourceId id);
543 void UnlockForRead(ResourceId id);
544 Resource* LockForWrite(ResourceId id);
545 void UnlockForWrite(Resource* resource);
547 static void PopulateSkBitmapWithResource(SkBitmap* sk_bitmap,
548 const Resource* resource);
550 void TransferResource(gpu::gles2::GLES2Interface* gl,
551 ResourceId id,
552 TransferableResource* resource);
553 enum DeleteStyle {
554 NORMAL,
555 FOR_SHUTDOWN,
557 void DeleteResourceInternal(ResourceMap::iterator it, DeleteStyle style);
558 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it,
559 DeleteStyle style,
560 const ResourceIdArray& unused);
561 void DestroyChildInternal(ChildMap::iterator it, DeleteStyle style);
562 void LazyCreate(Resource* resource);
563 void LazyAllocate(Resource* resource);
565 void BindImageForSampling(Resource* resource);
566 // Binds the given GL resource to a texture target for sampling using the
567 // specified filter for both minification and magnification. Returns the
568 // texture target used. The resource must be locked for reading.
569 GLenum BindForSampling(ResourceId resource_id, GLenum unit, GLenum filter);
571 // Returns NULL if the output_surface_ does not have a ContextProvider.
572 gpu::gles2::GLES2Interface* ContextGL() const;
573 class GrContext* GrContext(bool worker_context) const;
575 OutputSurface* output_surface_;
576 SharedBitmapManager* shared_bitmap_manager_;
577 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
578 BlockingTaskRunner* blocking_main_thread_task_runner_;
579 bool lost_output_surface_;
580 int highp_threshold_min_;
581 ResourceId next_id_;
582 ResourceMap resources_;
583 int next_child_;
584 ChildMap children_;
586 ResourceType default_resource_type_;
587 bool use_texture_storage_ext_;
588 bool use_texture_format_bgra_;
589 bool use_texture_usage_hint_;
590 bool use_compressed_texture_etc1_;
591 ResourceFormat yuv_resource_format_;
592 int max_texture_size_;
593 ResourceFormat best_texture_format_;
594 ResourceFormat best_render_buffer_format_;
596 base::ThreadChecker thread_checker_;
598 scoped_refptr<Fence> current_read_lock_fence_;
599 bool use_rgba_4444_texture_format_;
601 const size_t id_allocation_chunk_size_;
602 scoped_ptr<IdAllocator> texture_id_allocator_;
603 scoped_ptr<IdAllocator> buffer_id_allocator_;
605 bool use_sync_query_;
606 std::vector<unsigned> use_image_texture_targets_;
608 // A process-unique ID used for disambiguating memory dumps from different
609 // resource providers.
610 int tracing_id_;
612 DISALLOW_COPY_AND_ASSIGN(ResourceProvider);
615 } // namespace cc
617 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_