1 // Copyright 2013 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 #ifndef REMOTING_CLIENT_JNI_JNI_FRAME_CONSUMER_H_ 6 #define REMOTING_CLIENT_JNI_JNI_FRAME_CONSUMER_H_ 7 8 #include <list> 9 10 #include "base/android/scoped_java_ref.h" 11 #include "base/compiler_specific.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "remoting/client/frame_consumer.h" 15 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" 16 17 namespace gfx { 18 class JavaBitmap; 19 } // namespace gfx 20 21 namespace webrtc { 22 class DesktopFrame; 23 } // namespace webrtc 24 25 namespace remoting { 26 class ChromotingJniInstance; 27 class ChromotingJniRuntime; 28 class FrameProducer; 29 30 // FrameConsumer implementation that draws onto a JNI direct byte buffer. 31 class JniFrameConsumer : public FrameConsumer { 32 public: 33 // The instance does not take ownership of |jni_runtime|. 34 explicit JniFrameConsumer(ChromotingJniRuntime* jni_runtime, 35 scoped_refptr<ChromotingJniInstance> jni_instance); 36 37 virtual ~JniFrameConsumer(); 38 39 // This must be called once before the producer's source size is set. 40 void set_frame_producer(FrameProducer* producer); 41 42 // FrameConsumer implementation. 43 virtual void ApplyBuffer(const webrtc::DesktopSize& view_size, 44 const webrtc::DesktopRect& clip_area, 45 webrtc::DesktopFrame* buffer, 46 const webrtc::DesktopRegion& region, 47 const webrtc::DesktopRegion& shape) OVERRIDE; 48 virtual void ReturnBuffer(webrtc::DesktopFrame* buffer) OVERRIDE; 49 virtual void SetSourceSize(const webrtc::DesktopSize& source_size, 50 const webrtc::DesktopVector& dpi) OVERRIDE; 51 virtual PixelFormat GetPixelFormat() OVERRIDE; 52 53 private: 54 // Allocates a new buffer of |source_size|, informs Java about it, and tells 55 // the producer to draw onto it. 56 void AllocateBuffer(const webrtc::DesktopSize& source_size); 57 58 // Frees a frame buffer previously allocated by AllocateBuffer. 59 void FreeBuffer(webrtc::DesktopFrame* buffer); 60 61 // Variables are to be used from the display thread. 62 63 // Used to obtain task runner references and make calls to Java methods. 64 ChromotingJniRuntime* jni_runtime_; 65 66 // Used to record statistics. 67 scoped_refptr<ChromotingJniInstance> jni_instance_; 68 69 FrameProducer* frame_producer_; 70 webrtc::DesktopRect clip_area_; 71 72 // List of allocated image buffers. 73 std::list<webrtc::DesktopFrame*> buffers_; 74 75 // This global reference is required, instead of a local reference, so it 76 // remains valid for the lifetime of |bitmap_| - gfx::JavaBitmap does not 77 // create its own global reference internally. And this global ref must be 78 // destroyed (released) after |bitmap_| is destroyed. 79 base::android::ScopedJavaGlobalRef<jobject> bitmap_global_ref_; 80 81 // Reference to the frame bitmap that is passed to Java when the frame is 82 // allocated. This provides easy access to the underlying pixels. 83 scoped_ptr<gfx::JavaBitmap> bitmap_; 84 85 DISALLOW_COPY_AND_ASSIGN(JniFrameConsumer); 86 }; 87 88 } // namespace remoting 89 90 #endif 91