ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / cc / resources / resource_provider.h
blob95e12944a3a896579bf90e6ea3776688bc5b8c4a
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 "cc/base/cc_export.h"
21 #include "cc/output/context_provider.h"
22 #include "cc/output/output_surface.h"
23 #include "cc/resources/release_callback_impl.h"
24 #include "cc/resources/resource_format.h"
25 #include "cc/resources/return_callback.h"
26 #include "cc/resources/shared_bitmap.h"
27 #include "cc/resources/single_release_callback_impl.h"
28 #include "cc/resources/texture_mailbox.h"
29 #include "cc/resources/transferable_resource.h"
30 #include "third_party/khronos/GLES2/gl2.h"
31 #include "third_party/khronos/GLES2/gl2ext.h"
32 #include "third_party/skia/include/core/SkBitmap.h"
33 #include "third_party/skia/include/core/SkCanvas.h"
34 #include "ui/gfx/geometry/size.h"
36 class GrContext;
38 namespace gpu {
39 class GpuMemoryBufferManager;
40 namespace gles {
41 class GLES2Interface;
45 namespace gfx {
46 class GpuMemoryBuffer;
47 class Rect;
48 class Vector2d;
51 namespace cc {
52 class BlockingTaskRunner;
53 class IdAllocator;
54 class SharedBitmap;
55 class SharedBitmapManager;
56 class TextureUploader;
58 // This class is not thread-safe and can only be called from the thread it was
59 // created on (in practice, the impl thread).
60 class CC_EXPORT ResourceProvider {
61 private:
62 struct Resource;
64 public:
65 typedef unsigned ResourceId;
66 typedef std::vector<ResourceId> ResourceIdArray;
67 typedef std::set<ResourceId> ResourceIdSet;
68 typedef base::hash_map<ResourceId, ResourceId> ResourceIdMap;
69 enum TextureHint {
70 TEXTURE_HINT_DEFAULT = 0x0,
71 TEXTURE_HINT_IMMUTABLE = 0x1,
72 TEXTURE_HINT_FRAMEBUFFER = 0x2,
73 TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER =
74 TEXTURE_HINT_IMMUTABLE | TEXTURE_HINT_FRAMEBUFFER
76 enum ResourceType {
77 RESOURCE_TYPE_INVALID = 0,
78 RESOURCE_TYPE_GL_TEXTURE = 1,
79 RESOURCE_TYPE_BITMAP,
82 static scoped_ptr<ResourceProvider> Create(
83 OutputSurface* output_surface,
84 SharedBitmapManager* shared_bitmap_manager,
85 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
86 BlockingTaskRunner* blocking_main_thread_task_runner,
87 int highp_threshold_min,
88 bool use_rgba_4444_texture_format,
89 size_t id_allocation_chunk_size);
90 virtual ~ResourceProvider();
92 void InitializeSoftware();
93 void InitializeGL();
95 void DidLoseOutputSurface() { lost_output_surface_ = true; }
97 int max_texture_size() const { return max_texture_size_; }
98 ResourceFormat memory_efficient_texture_format() const {
99 return use_rgba_4444_texture_format_ ? RGBA_4444 : best_texture_format_;
101 ResourceFormat best_texture_format() const { return best_texture_format_; }
102 ResourceFormat yuv_resource_format() const { return yuv_resource_format_; }
103 bool use_sync_query() const { return use_sync_query_; }
104 size_t num_resources() const { return resources_.size(); }
106 // Checks whether a resource is in use by a consumer.
107 bool InUseByConsumer(ResourceId id);
109 bool IsLost(ResourceId id);
110 bool AllowOverlay(ResourceId id);
112 // Producer interface.
114 ResourceType default_resource_type() const { return default_resource_type_; }
115 ResourceType GetResourceType(ResourceId id);
117 // Creates a resource of the default resource type.
118 ResourceId CreateResource(const gfx::Size& size,
119 GLint wrap_mode,
120 TextureHint hint,
121 ResourceFormat format);
123 // Creates a resource which is tagged as being managed for GPU memory
124 // accounting purposes.
125 ResourceId CreateManagedResource(const gfx::Size& size,
126 GLenum target,
127 GLint wrap_mode,
128 TextureHint hint,
129 ResourceFormat format);
131 // You can also explicitly create a specific resource type.
132 ResourceId CreateGLTexture(const gfx::Size& size,
133 GLenum target,
134 GLenum texture_pool,
135 GLint wrap_mode,
136 TextureHint hint,
137 ResourceFormat format);
139 ResourceId CreateBitmap(const gfx::Size& size, GLint wrap_mode);
140 // Wraps an IOSurface into a GL resource.
141 ResourceId CreateResourceFromIOSurface(const gfx::Size& size,
142 unsigned io_surface_id);
144 // Wraps an external texture mailbox into a GL resource.
145 ResourceId CreateResourceFromTextureMailbox(
146 const TextureMailbox& mailbox,
147 scoped_ptr<SingleReleaseCallbackImpl> release_callback_impl);
149 void DeleteResource(ResourceId id);
151 // Update pixels from image, copying source_rect (in image) to dest_offset (in
152 // the resource).
153 // NOTE: DEPRECATED. Use CopyToResource() instead.
154 void SetPixels(ResourceId id,
155 const uint8_t* image,
156 const gfx::Rect& image_rect,
157 const gfx::Rect& source_rect,
158 const gfx::Vector2d& dest_offset);
159 void CopyToResource(ResourceId id,
160 const uint8_t* image,
161 const gfx::Size& image_size);
163 // Check upload status.
164 size_t NumBlockingUploads();
165 void MarkPendingUploadsAsNonBlocking();
166 size_t EstimatedUploadsPerTick();
167 void FlushUploads();
168 void ReleaseCachedData();
169 base::TimeTicks EstimatedUploadCompletionTime(size_t uploads_per_tick);
171 // Only flush the command buffer if supported.
172 // Returns true if the shallow flush occurred, false otherwise.
173 bool ShallowFlushIfSupported();
175 // Creates accounting for a child. Returns a child ID.
176 int CreateChild(const ReturnCallback& return_callback);
178 // Destroys accounting for the child, deleting all accounted resources.
179 void DestroyChild(int child);
181 // Gets the child->parent resource ID map.
182 const ResourceIdMap& GetChildToParentMap(int child) const;
184 // Prepares resources to be transfered to the parent, moving them to
185 // mailboxes and serializing meta-data into TransferableResources.
186 // Resources are not removed from the ResourceProvider, but are marked as
187 // "in use".
188 void PrepareSendToParent(const ResourceIdArray& resources,
189 TransferableResourceArray* transferable_resources);
191 // Receives resources from a child, moving them from mailboxes. Resource IDs
192 // passed are in the child namespace, and will be translated to the parent
193 // namespace, added to the child->parent map.
194 // This adds the resources to the working set in the ResourceProvider without
195 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
196 // after calling this method to do that. All calls to ReceiveFromChild should
197 // be followed by a DeclareUsedResourcesFromChild.
198 // NOTE: if the sync_point is set on any TransferableResource, this will
199 // wait on it.
200 void ReceiveFromChild(
201 int child, const TransferableResourceArray& transferable_resources);
203 // Once a set of resources have been received, they may or may not be used.
204 // This declares what set of resources are currently in use from the child,
205 // releasing any other resources back to the child.
206 void DeclareUsedResourcesFromChild(
207 int child,
208 const ResourceIdArray& resources_from_child);
210 // Receives resources from the parent, moving them from mailboxes. Resource
211 // IDs passed are in the child namespace.
212 // NOTE: if the sync_point is set on any TransferableResource, this will
213 // wait on it.
214 void ReceiveReturnsFromParent(
215 const ReturnedResourceArray& transferable_resources);
217 // The following lock classes are part of the ResourceProvider API and are
218 // needed to read and write the resource contents. The user must ensure
219 // that they only use GL locks on GL resources, etc, and this is enforced
220 // by assertions.
221 class CC_EXPORT ScopedReadLockGL {
222 public:
223 ScopedReadLockGL(ResourceProvider* resource_provider,
224 ResourceProvider::ResourceId resource_id);
225 virtual ~ScopedReadLockGL();
227 unsigned texture_id() const { return texture_id_; }
229 protected:
230 ResourceProvider* resource_provider_;
231 ResourceProvider::ResourceId resource_id_;
233 private:
234 unsigned texture_id_;
236 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL);
239 class CC_EXPORT ScopedSamplerGL : public ScopedReadLockGL {
240 public:
241 ScopedSamplerGL(ResourceProvider* resource_provider,
242 ResourceProvider::ResourceId resource_id,
243 GLenum filter);
244 ScopedSamplerGL(ResourceProvider* resource_provider,
245 ResourceProvider::ResourceId resource_id,
246 GLenum unit,
247 GLenum filter);
248 ~ScopedSamplerGL() override;
250 GLenum target() const { return target_; }
252 private:
253 GLenum unit_;
254 GLenum target_;
256 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL);
259 class CC_EXPORT ScopedWriteLockGL {
260 public:
261 ScopedWriteLockGL(ResourceProvider* resource_provider,
262 ResourceProvider::ResourceId resource_id);
263 ~ScopedWriteLockGL();
265 unsigned texture_id() const { return texture_id_; }
267 private:
268 ResourceProvider* resource_provider_;
269 ResourceProvider::Resource* resource_;
270 unsigned texture_id_;
272 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL);
275 class CC_EXPORT ScopedReadLockSoftware {
276 public:
277 ScopedReadLockSoftware(ResourceProvider* resource_provider,
278 ResourceProvider::ResourceId resource_id);
279 ~ScopedReadLockSoftware();
281 const SkBitmap* sk_bitmap() const {
282 DCHECK(valid());
283 return &sk_bitmap_;
285 GLint wrap_mode() const { return wrap_mode_; }
287 bool valid() const { return !!sk_bitmap_.getPixels(); }
289 private:
290 ResourceProvider* resource_provider_;
291 ResourceProvider::ResourceId resource_id_;
292 SkBitmap sk_bitmap_;
293 GLint wrap_mode_;
295 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware);
298 class CC_EXPORT ScopedWriteLockSoftware {
299 public:
300 ScopedWriteLockSoftware(ResourceProvider* resource_provider,
301 ResourceProvider::ResourceId resource_id);
302 ~ScopedWriteLockSoftware();
304 SkBitmap& sk_bitmap() { return sk_bitmap_; }
305 bool valid() const { return !!sk_bitmap_.getPixels(); }
307 private:
308 ResourceProvider* resource_provider_;
309 ResourceProvider::Resource* resource_;
310 SkBitmap sk_bitmap_;
311 base::ThreadChecker thread_checker_;
313 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware);
316 class CC_EXPORT ScopedWriteLockGpuMemoryBuffer {
317 public:
318 ScopedWriteLockGpuMemoryBuffer(ResourceProvider* resource_provider,
319 ResourceProvider::ResourceId resource_id);
320 ~ScopedWriteLockGpuMemoryBuffer();
322 gfx::GpuMemoryBuffer* GetGpuMemoryBuffer();
324 private:
325 ResourceProvider* resource_provider_;
326 ResourceProvider::Resource* resource_;
327 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
328 gfx::GpuMemoryBuffer* gpu_memory_buffer_;
329 gfx::Size size_;
330 ResourceFormat format_;
331 base::ThreadChecker thread_checker_;
333 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGpuMemoryBuffer);
336 class CC_EXPORT ScopedWriteLockGr {
337 public:
338 ScopedWriteLockGr(ResourceProvider* resource_provider,
339 ResourceProvider::ResourceId resource_id);
340 ~ScopedWriteLockGr();
342 void InitSkSurface(bool use_worker_context,
343 bool use_distance_field_text,
344 bool can_use_lcd_text,
345 int msaa_sample_count);
346 void ReleaseSkSurface();
348 SkSurface* sk_surface() { return sk_surface_.get(); }
349 ResourceProvider::Resource* resource() { return resource_; }
351 private:
352 ResourceProvider* resource_provider_;
353 ResourceProvider::Resource* resource_;
354 base::ThreadChecker thread_checker_;
355 skia::RefPtr<SkSurface> sk_surface_;
357 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGr);
360 class Fence : public base::RefCounted<Fence> {
361 public:
362 Fence() {}
364 virtual void Set() = 0;
365 virtual bool HasPassed() = 0;
366 virtual void Wait() = 0;
368 protected:
369 friend class base::RefCounted<Fence>;
370 virtual ~Fence() {}
372 private:
373 DISALLOW_COPY_AND_ASSIGN(Fence);
376 class SynchronousFence : public ResourceProvider::Fence {
377 public:
378 explicit SynchronousFence(gpu::gles2::GLES2Interface* gl);
380 // Overridden from Fence:
381 void Set() override;
382 bool HasPassed() override;
383 void Wait() override;
385 // Returns true if fence has been set but not yet synchornized.
386 bool has_synchronized() const { return has_synchronized_; }
388 private:
389 ~SynchronousFence() override;
391 void Synchronize();
393 gpu::gles2::GLES2Interface* gl_;
394 bool has_synchronized_;
396 DISALLOW_COPY_AND_ASSIGN(SynchronousFence);
399 // Acquire pixel buffer for resource. The pixel buffer can be used to
400 // set resource pixels without performing unnecessary copying.
401 void AcquirePixelBuffer(ResourceId resource);
402 void ReleasePixelBuffer(ResourceId resource);
403 // Map/unmap the acquired pixel buffer.
404 uint8_t* MapPixelBuffer(ResourceId id, int* stride);
405 void UnmapPixelBuffer(ResourceId id);
406 // Asynchronously update pixels from acquired pixel buffer.
407 void BeginSetPixels(ResourceId id);
408 void ForceSetPixelsToComplete(ResourceId id);
409 bool DidSetPixelsComplete(ResourceId id);
411 // For tests only! This prevents detecting uninitialized reads.
412 // Use SetPixels or LockForWrite to allocate implicitly.
413 void AllocateForTesting(ResourceId id);
415 // For tests only!
416 void CreateForTesting(ResourceId id);
418 GLenum TargetForTesting(ResourceId id);
420 // Sets the current read fence. If a resource is locked for read
421 // and has read fences enabled, the resource will not allow writes
422 // until this fence has passed.
423 void SetReadLockFence(Fence* fence) { current_read_lock_fence_ = fence; }
425 // Indicates if we can currently lock this resource for write.
426 bool CanLockForWrite(ResourceId id);
428 // Copy pixels from source to destination.
429 void CopyResource(ResourceId source_id, ResourceId dest_id);
431 void WaitSyncPointIfNeeded(ResourceId id);
433 void WaitReadLockIfNeeded(ResourceId id);
435 static GLint GetActiveTextureUnit(gpu::gles2::GLES2Interface* gl);
437 OutputSurface* output_surface() { return output_surface_; }
439 private:
440 struct Resource {
441 enum Origin { INTERNAL, EXTERNAL, DELEGATED };
443 Resource();
444 ~Resource();
445 Resource(unsigned texture_id,
446 const gfx::Size& size,
447 Origin origin,
448 GLenum target,
449 GLenum filter,
450 GLenum texture_pool,
451 GLint wrap_mode,
452 TextureHint hint,
453 ResourceFormat format);
454 Resource(uint8_t* pixels,
455 SharedBitmap* bitmap,
456 const gfx::Size& size,
457 Origin origin,
458 GLenum filter,
459 GLint wrap_mode);
460 Resource(const SharedBitmapId& bitmap_id,
461 const gfx::Size& size,
462 Origin origin,
463 GLenum filter,
464 GLint wrap_mode);
466 int child_id;
467 unsigned gl_id;
468 // Pixel buffer used for set pixels without unnecessary copying.
469 unsigned gl_pixel_buffer_id;
470 // Query used to determine when asynchronous set pixels complete.
471 unsigned gl_upload_query_id;
472 // Query used to determine when read lock fence has passed.
473 unsigned gl_read_lock_query_id;
474 TextureMailbox mailbox;
475 ReleaseCallbackImpl release_callback_impl;
476 uint8_t* pixels;
477 int lock_for_read_count;
478 int imported_count;
479 int exported_count;
480 bool dirty_image : 1;
481 bool locked_for_write : 1;
482 bool lost : 1;
483 bool marked_for_deletion : 1;
484 bool pending_set_pixels : 1;
485 bool set_pixels_completion_forced : 1;
486 bool allocated : 1;
487 bool read_lock_fences_enabled : 1;
488 bool has_shared_bitmap_id : 1;
489 bool allow_overlay : 1;
490 scoped_refptr<Fence> read_lock_fence;
491 gfx::Size size;
492 Origin origin;
493 GLenum target;
494 // TODO(skyostil): Use a separate sampler object for filter state.
495 GLenum original_filter;
496 GLenum filter;
497 unsigned image_id;
498 unsigned bound_image_id;
499 GLenum texture_pool;
500 GLint wrap_mode;
501 TextureHint hint;
502 ResourceType type;
503 ResourceFormat format;
504 SharedBitmapId shared_bitmap_id;
505 SharedBitmap* shared_bitmap;
506 gfx::GpuMemoryBuffer* gpu_memory_buffer;
508 typedef base::hash_map<ResourceId, Resource> ResourceMap;
510 static bool CompareResourceMapIteratorsByChildId(
511 const std::pair<ReturnedResource, ResourceMap::iterator>& a,
512 const std::pair<ReturnedResource, ResourceMap::iterator>& b);
514 struct Child {
515 Child();
516 ~Child();
518 ResourceIdMap child_to_parent_map;
519 ResourceIdMap parent_to_child_map;
520 ReturnCallback return_callback;
521 ResourceIdSet in_use_resources;
522 bool marked_for_deletion;
524 typedef base::hash_map<int, Child> ChildMap;
526 bool ReadLockFenceHasPassed(const Resource* resource) {
527 return !resource->read_lock_fence.get() ||
528 resource->read_lock_fence->HasPassed();
531 ResourceProvider(OutputSurface* output_surface,
532 SharedBitmapManager* shared_bitmap_manager,
533 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
534 BlockingTaskRunner* blocking_main_thread_task_runner,
535 int highp_threshold_min,
536 bool use_rgba_4444_texture_format,
537 size_t id_allocation_chunk_size);
539 void CleanUpGLIfNeeded();
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 scoped_ptr<TextureUploader> texture_uploader_;
593 int max_texture_size_;
594 ResourceFormat best_texture_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 // Fence used for CopyResource if CHROMIUM_sync_query is not supported.
607 scoped_refptr<SynchronousFence> synchronous_fence_;
609 DISALLOW_COPY_AND_ASSIGN(ResourceProvider);
612 // TODO(epenner): Move these format conversions to resource_format.h
613 // once that builds on mac (npapi.h currently #includes OpenGL.h).
614 inline unsigned BitsPerPixel(ResourceFormat format) {
615 switch (format) {
616 case BGRA_8888:
617 case RGBA_8888:
618 return 32;
619 case RGBA_4444:
620 case RGB_565:
621 return 16;
622 case ALPHA_8:
623 case LUMINANCE_8:
624 case RED_8:
625 return 8;
626 case ETC1:
627 return 4;
629 NOTREACHED();
630 return 0;
633 inline GLenum GLDataType(ResourceFormat format) {
634 DCHECK_LE(format, RESOURCE_FORMAT_MAX);
635 static const unsigned format_gl_data_type[RESOURCE_FORMAT_MAX + 1] = {
636 GL_UNSIGNED_BYTE, // RGBA_8888
637 GL_UNSIGNED_SHORT_4_4_4_4, // RGBA_4444
638 GL_UNSIGNED_BYTE, // BGRA_8888
639 GL_UNSIGNED_BYTE, // ALPHA_8
640 GL_UNSIGNED_BYTE, // LUMINANCE_8
641 GL_UNSIGNED_SHORT_5_6_5, // RGB_565,
642 GL_UNSIGNED_BYTE, // ETC1
643 GL_UNSIGNED_BYTE // RED_8
645 return format_gl_data_type[format];
648 inline GLenum GLDataFormat(ResourceFormat format) {
649 DCHECK_LE(format, RESOURCE_FORMAT_MAX);
650 static const unsigned format_gl_data_format[RESOURCE_FORMAT_MAX + 1] = {
651 GL_RGBA, // RGBA_8888
652 GL_RGBA, // RGBA_4444
653 GL_BGRA_EXT, // BGRA_8888
654 GL_ALPHA, // ALPHA_8
655 GL_LUMINANCE, // LUMINANCE_8
656 GL_RGB, // RGB_565
657 GL_ETC1_RGB8_OES, // ETC1
658 GL_RED_EXT // RED_8
660 return format_gl_data_format[format];
663 inline GLenum GLInternalFormat(ResourceFormat format) {
664 return GLDataFormat(format);
667 } // namespace cc
669 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_