Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 17 | #include "PixelBuffer.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 18 | |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 19 | #include "Debug.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 20 | #include "Extensions.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 21 | #include "Properties.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 22 | #include "renderstate/RenderState.h" |
John Reck | 2de7771 | 2016-01-20 11:09:53 -0800 | [diff] [blame] | 23 | #include "utils/GLUtils.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 24 | |
| 25 | #include <utils/Log.h> |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // CPU pixel buffer |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | class CpuPixelBuffer: public PixelBuffer { |
| 35 | public: |
| 36 | CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 37 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 38 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 39 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 40 | void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 41 | |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame] | 42 | protected: |
| 43 | void unmap() override; |
| 44 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 45 | private: |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 46 | std::unique_ptr<uint8_t[]> mBuffer; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 49 | CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height) |
| 50 | : PixelBuffer(format, width, height) |
| 51 | , mBuffer(new uint8_t[width * height * formatSize(format)]) { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | uint8_t* CpuPixelBuffer::map(AccessMode mode) { |
| 55 | if (mAccessMode == kAccessMode_None) { |
| 56 | mAccessMode = mode; |
| 57 | } |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 58 | return mBuffer.get(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void CpuPixelBuffer::unmap() { |
| 62 | mAccessMode = kAccessMode_None; |
| 63 | } |
| 64 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 65 | void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 66 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 67 | mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | // GPU pixel buffer |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | class GpuPixelBuffer: public PixelBuffer { |
| 75 | public: |
| 76 | GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
| 77 | ~GpuPixelBuffer(); |
| 78 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 79 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 80 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 81 | void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 82 | |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame] | 83 | protected: |
| 84 | void unmap() override; |
| 85 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 86 | private: |
| 87 | GLuint mBuffer; |
| 88 | uint8_t* mMappedPointer; |
| 89 | Caches& mCaches; |
| 90 | }; |
| 91 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 92 | GpuPixelBuffer::GpuPixelBuffer(GLenum format, |
| 93 | uint32_t width, uint32_t height) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 94 | : PixelBuffer(format, width, height) |
| 95 | , mMappedPointer(nullptr) |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 96 | , mCaches(Caches::getInstance()){ |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 97 | glGenBuffers(1, &mBuffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 98 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 99 | mCaches.pixelBufferState().bind(mBuffer); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 100 | glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 101 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | GpuPixelBuffer::~GpuPixelBuffer() { |
| 105 | glDeleteBuffers(1, &mBuffer); |
| 106 | } |
| 107 | |
| 108 | uint8_t* GpuPixelBuffer::map(AccessMode mode) { |
| 109 | if (mAccessMode == kAccessMode_None) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 110 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 111 | mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode); |
John Reck | 2de7771 | 2016-01-20 11:09:53 -0800 | [diff] [blame] | 112 | if (CC_UNLIKELY(!mMappedPointer)) { |
| 113 | GLUtils::dumpGLErrors(); |
| 114 | LOG_ALWAYS_FATAL("Failed to map PBO"); |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 115 | } |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 116 | mAccessMode = mode; |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame] | 117 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return mMappedPointer; |
| 121 | } |
| 122 | |
| 123 | void GpuPixelBuffer::unmap() { |
| 124 | if (mAccessMode != kAccessMode_None) { |
| 125 | if (mMappedPointer) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 126 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 127 | GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 128 | if (status == GL_FALSE) { |
| 129 | ALOGE("Corrupted GPU pixel buffer"); |
| 130 | } |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 131 | } |
| 132 | mAccessMode = kAccessMode_None; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 133 | mMappedPointer = nullptr; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 137 | void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 138 | // If the buffer is not mapped, unmap() will not bind it |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 139 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 140 | unmap(); |
| 141 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, |
Kévin PETIT | 73fc558 | 2014-02-13 11:03:40 +0000 | [diff] [blame] | 142 | GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset)); |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame] | 143 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /////////////////////////////////////////////////////////////////////////////// |
| 147 | // Factory |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 150 | PixelBuffer* PixelBuffer::create(GLenum format, |
| 151 | uint32_t width, uint32_t height, BufferType type) { |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 152 | if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) { |
| 153 | return new GpuPixelBuffer(format, width, height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 154 | } |
| 155 | return new CpuPixelBuffer(format, width, height); |
| 156 | } |
| 157 | |
| 158 | }; // namespace uirenderer |
| 159 | }; // namespace android |