1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/nativewindow/GonkNativeWindowClientICS.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,446 @@ 1.4 +/* 1.5 + * Copyright (C) 2010 The Android Open Source Project 1.6 + * Copyright (C) 2012 Mozilla Foundation 1.7 + * 1.8 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.9 + * you may not use this file except in compliance with the License. 1.10 + * You may obtain a copy of the License at 1.11 + * 1.12 + * http://www.apache.org/licenses/LICENSE-2.0 1.13 + * 1.14 + * Unless required by applicable law or agreed to in writing, software 1.15 + * distributed under the License is distributed on an "AS IS" BASIS, 1.16 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.17 + * See the License for the specific language governing permissions and 1.18 + * limitations under the License. 1.19 + */ 1.20 + 1.21 +#include "base/basictypes.h" 1.22 + 1.23 +#include "CameraCommon.h" 1.24 +#include "GonkNativeWindow.h" 1.25 +#include "GonkNativeWindowClient.h" 1.26 +#include "nsDebug.h" 1.27 + 1.28 +/** 1.29 + * DOM_CAMERA_LOGI() is enabled in debug builds, and turned on by setting 1.30 + * NSPR_LOG_MODULES=Camera:N environment variable, where N >= 3. 1.31 + * 1.32 + * CNW_LOGE() is always enabled. 1.33 + */ 1.34 +#define CNW_LOGD(...) DOM_CAMERA_LOGI(__VA_ARGS__) 1.35 +#define CNW_LOGE(...) {(void)printf_stderr(__VA_ARGS__);} 1.36 + 1.37 +using namespace android; 1.38 +using namespace mozilla::layers; 1.39 + 1.40 +GonkNativeWindowClient::GonkNativeWindowClient(const sp<GonkNativeWindow>& window) 1.41 + : mNativeWindow(window) { 1.42 + GonkNativeWindowClient::init(); 1.43 +} 1.44 + 1.45 +GonkNativeWindowClient::~GonkNativeWindowClient() { 1.46 + if (mConnectedToCpu) { 1.47 + GonkNativeWindowClient::disconnect(NATIVE_WINDOW_API_CPU); 1.48 + } 1.49 +} 1.50 + 1.51 +void GonkNativeWindowClient::init() { 1.52 + // Initialize the ANativeWindow function pointers. 1.53 + ANativeWindow::setSwapInterval = hook_setSwapInterval; 1.54 + ANativeWindow::dequeueBuffer = hook_dequeueBuffer; 1.55 + ANativeWindow::cancelBuffer = hook_cancelBuffer; 1.56 + ANativeWindow::lockBuffer = hook_lockBuffer; 1.57 + ANativeWindow::queueBuffer = hook_queueBuffer; 1.58 + ANativeWindow::query = hook_query; 1.59 + ANativeWindow::perform = hook_perform; 1.60 + 1.61 + mReqWidth = 0; 1.62 + mReqHeight = 0; 1.63 + mReqFormat = 0; 1.64 + mReqUsage = 0; 1.65 + mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; 1.66 + mDefaultWidth = 0; 1.67 + mDefaultHeight = 0; 1.68 + mTransformHint = 0; 1.69 + mConnectedToCpu = false; 1.70 +} 1.71 + 1.72 + 1.73 +int GonkNativeWindowClient::hook_setSwapInterval(ANativeWindow* window, int interval) { 1.74 + GonkNativeWindowClient* c = getSelf(window); 1.75 + return c->setSwapInterval(interval); 1.76 +} 1.77 + 1.78 +int GonkNativeWindowClient::hook_dequeueBuffer(ANativeWindow* window, 1.79 + ANativeWindowBuffer** buffer) { 1.80 + GonkNativeWindowClient* c = getSelf(window); 1.81 + return c->dequeueBuffer(buffer); 1.82 +} 1.83 + 1.84 +int GonkNativeWindowClient::hook_cancelBuffer(ANativeWindow* window, 1.85 + ANativeWindowBuffer* buffer) { 1.86 + GonkNativeWindowClient* c = getSelf(window); 1.87 + return c->cancelBuffer(buffer); 1.88 +} 1.89 + 1.90 +int GonkNativeWindowClient::hook_lockBuffer(ANativeWindow* window, 1.91 + ANativeWindowBuffer* buffer) { 1.92 + GonkNativeWindowClient* c = getSelf(window); 1.93 + return c->lockBuffer(buffer); 1.94 +} 1.95 + 1.96 +int GonkNativeWindowClient::hook_queueBuffer(ANativeWindow* window, 1.97 + ANativeWindowBuffer* buffer) { 1.98 + GonkNativeWindowClient* c = getSelf(window); 1.99 + return c->queueBuffer(buffer); 1.100 +} 1.101 + 1.102 +int GonkNativeWindowClient::hook_query(const ANativeWindow* window, 1.103 + int what, int* value) { 1.104 + const GonkNativeWindowClient* c = getSelf(window); 1.105 + return c->query(what, value); 1.106 +} 1.107 + 1.108 +int GonkNativeWindowClient::hook_perform(ANativeWindow* window, int operation, ...) { 1.109 + va_list args; 1.110 + va_start(args, operation); 1.111 + GonkNativeWindowClient* c = getSelf(window); 1.112 + return c->perform(operation, args); 1.113 +} 1.114 + 1.115 +int GonkNativeWindowClient::setSwapInterval(int interval) { 1.116 + return NO_ERROR; 1.117 +} 1.118 + 1.119 +int GonkNativeWindowClient::dequeueBuffer(android_native_buffer_t** buffer) { 1.120 + CNW_LOGD("GonkNativeWindowClient::dequeueBuffer"); 1.121 + Mutex::Autolock lock(mMutex); 1.122 + int buf = -1; 1.123 + status_t result = mNativeWindow->dequeueBuffer(&buf, mReqWidth, mReqHeight, 1.124 + mReqFormat, mReqUsage); 1.125 + if (result < 0) { 1.126 + CNW_LOGD("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)" 1.127 + "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage, 1.128 + result); 1.129 + return result; 1.130 + } 1.131 + sp<GraphicBuffer>& gbuf(mSlots[buf]); 1.132 + if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) { 1.133 + freeAllBuffers(); 1.134 + } 1.135 + 1.136 + if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) { 1.137 + result = mNativeWindow->requestBuffer(buf, &gbuf); 1.138 + if (result != NO_ERROR) { 1.139 + CNW_LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d", 1.140 + result); 1.141 + return result; 1.142 + } 1.143 + } 1.144 + *buffer = gbuf.get(); 1.145 + return OK; 1.146 +} 1.147 + 1.148 +int GonkNativeWindowClient::cancelBuffer(ANativeWindowBuffer* buffer) { 1.149 + CNW_LOGD("GonkNativeWindowClient::cancelBuffer"); 1.150 + Mutex::Autolock lock(mMutex); 1.151 + int i = getSlotFromBufferLocked(buffer); 1.152 + if (i < 0) { 1.153 + return i; 1.154 + } 1.155 + mNativeWindow->cancelBuffer(i); 1.156 + return OK; 1.157 +} 1.158 + 1.159 +int GonkNativeWindowClient::getSlotFromBufferLocked( 1.160 + android_native_buffer_t* buffer) const { 1.161 + bool dumpedState = false; 1.162 + for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { 1.163 + // XXX: Dump the slots whenever we hit a NULL entry while searching for 1.164 + // a buffer. 1.165 + if (mSlots[i] == NULL) { 1.166 + if (!dumpedState) { 1.167 + CNW_LOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d " 1.168 + "looking for buffer %p", i, buffer->handle); 1.169 + for (int j = 0; j < NUM_BUFFER_SLOTS; j++) { 1.170 + if (mSlots[j] == NULL) { 1.171 + CNW_LOGD("getSlotFromBufferLocked: %02d: NULL", j); 1.172 + } else { 1.173 + CNW_LOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle); 1.174 + } 1.175 + } 1.176 + dumpedState = true; 1.177 + } 1.178 + } 1.179 + 1.180 + if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) { 1.181 + return i; 1.182 + } 1.183 + } 1.184 + CNW_LOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); 1.185 + return BAD_VALUE; 1.186 +} 1.187 + 1.188 + 1.189 +int GonkNativeWindowClient::lockBuffer(android_native_buffer_t* buffer) { 1.190 + CNW_LOGD("GonkNativeWindowClient::lockBuffer"); 1.191 + Mutex::Autolock lock(mMutex); 1.192 + return OK; 1.193 +} 1.194 + 1.195 +int GonkNativeWindowClient::queueBuffer(android_native_buffer_t* buffer) { 1.196 + CNW_LOGD("GonkNativeWindowClient::queueBuffer"); 1.197 + Mutex::Autolock lock(mMutex); 1.198 + int64_t timestamp; 1.199 + if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { 1.200 + timestamp = systemTime(SYSTEM_TIME_MONOTONIC); 1.201 + CNW_LOGD("GonkNativeWindowClient::queueBuffer making up timestamp: %.2f ms", 1.202 + timestamp / 1000000.f); 1.203 + } else { 1.204 + timestamp = mTimestamp; 1.205 + } 1.206 + int i = getSlotFromBufferLocked(buffer); 1.207 + if (i < 0) { 1.208 + return i; 1.209 + } 1.210 + status_t err = mNativeWindow->queueBuffer(i, timestamp, 1.211 + &mDefaultWidth, &mDefaultHeight, &mTransformHint); 1.212 + if (err != OK) { 1.213 + CNW_LOGE("queueBuffer: error queuing buffer to GonkNativeWindow, %d", err); 1.214 + } 1.215 + return err; 1.216 +} 1.217 + 1.218 +int GonkNativeWindowClient::query(int what, int* value) const { 1.219 + CNW_LOGD("query"); 1.220 + { // scope for the lock 1.221 + Mutex::Autolock lock(mMutex); 1.222 + switch (what) { 1.223 + case NATIVE_WINDOW_FORMAT: 1.224 + if (mReqFormat) { 1.225 + *value = mReqFormat; 1.226 + return NO_ERROR; 1.227 + } 1.228 + break; 1.229 + case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: 1.230 + *value = 0; 1.231 + return NO_ERROR; 1.232 + case NATIVE_WINDOW_CONCRETE_TYPE: 1.233 + *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT; 1.234 + return NO_ERROR; 1.235 + case NATIVE_WINDOW_DEFAULT_WIDTH: 1.236 + *value = mDefaultWidth; 1.237 + return NO_ERROR; 1.238 + case NATIVE_WINDOW_DEFAULT_HEIGHT: 1.239 + *value = mDefaultHeight; 1.240 + return NO_ERROR; 1.241 + case NATIVE_WINDOW_TRANSFORM_HINT: 1.242 + *value = mTransformHint; 1.243 + return NO_ERROR; 1.244 + } 1.245 + } 1.246 + return mNativeWindow->query(what, value); 1.247 +} 1.248 + 1.249 +int GonkNativeWindowClient::perform(int operation, va_list args) 1.250 +{ 1.251 + int res = NO_ERROR; 1.252 + switch (operation) { 1.253 + case NATIVE_WINDOW_CONNECT: 1.254 + // deprecated. must return NO_ERROR. 1.255 + break; 1.256 + case NATIVE_WINDOW_DISCONNECT: 1.257 + // deprecated. must return NO_ERROR. 1.258 + break; 1.259 + case NATIVE_WINDOW_SET_SCALING_MODE: 1.260 + return NO_ERROR; 1.261 + case NATIVE_WINDOW_SET_USAGE: 1.262 + res = dispatchSetUsage(args); 1.263 + break; 1.264 + case NATIVE_WINDOW_SET_CROP: 1.265 + //not implemented 1.266 + break; 1.267 + case NATIVE_WINDOW_SET_BUFFER_COUNT: 1.268 + res = dispatchSetBufferCount(args); 1.269 + break; 1.270 + case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: 1.271 + res = dispatchSetBuffersGeometry(args); 1.272 + break; 1.273 + case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: 1.274 + //not implemented 1.275 + break; 1.276 + case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: 1.277 + res = dispatchSetBuffersTimestamp(args); 1.278 + break; 1.279 + case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: 1.280 + res = dispatchSetBuffersDimensions(args); 1.281 + break; 1.282 + case NATIVE_WINDOW_SET_BUFFERS_FORMAT: 1.283 + res = dispatchSetBuffersFormat(args); 1.284 + break; 1.285 + case NATIVE_WINDOW_LOCK: 1.286 + res = INVALID_OPERATION;// not supported 1.287 + break; 1.288 + case NATIVE_WINDOW_UNLOCK_AND_POST: 1.289 + res = INVALID_OPERATION;// not supported 1.290 + break; 1.291 + case NATIVE_WINDOW_API_CONNECT: 1.292 + res = dispatchConnect(args); 1.293 + break; 1.294 + case NATIVE_WINDOW_API_DISCONNECT: 1.295 + res = dispatchDisconnect(args); 1.296 + break; 1.297 + case NATIVE_WINDOW_SET_BUFFERS_SIZE: 1.298 + //not implemented 1.299 + break; 1.300 + default: 1.301 + res = NAME_NOT_FOUND; 1.302 + break; 1.303 + } 1.304 + return res; 1.305 +} 1.306 + 1.307 +int GonkNativeWindowClient::dispatchConnect(va_list args) { 1.308 + int api = va_arg(args, int); 1.309 + return connect(api); 1.310 +} 1.311 + 1.312 +int GonkNativeWindowClient::dispatchDisconnect(va_list args) { 1.313 + int api = va_arg(args, int); 1.314 + return disconnect(api); 1.315 +} 1.316 + 1.317 +int GonkNativeWindowClient::dispatchSetUsage(va_list args) { 1.318 + int usage = va_arg(args, int); 1.319 + return setUsage(usage); 1.320 +} 1.321 + 1.322 +int GonkNativeWindowClient::dispatchSetBufferCount(va_list args) { 1.323 + size_t bufferCount = va_arg(args, size_t); 1.324 + return setBufferCount(bufferCount); 1.325 +} 1.326 + 1.327 +int GonkNativeWindowClient::dispatchSetBuffersGeometry(va_list args) { 1.328 + int w = va_arg(args, int); 1.329 + int h = va_arg(args, int); 1.330 + int f = va_arg(args, int); 1.331 + int err = setBuffersDimensions(w, h); 1.332 + if (err != 0) { 1.333 + return err; 1.334 + } 1.335 + return setBuffersFormat(f); 1.336 +} 1.337 + 1.338 +int GonkNativeWindowClient::dispatchSetBuffersDimensions(va_list args) { 1.339 + int w = va_arg(args, int); 1.340 + int h = va_arg(args, int); 1.341 + return setBuffersDimensions(w, h); 1.342 +} 1.343 + 1.344 +int GonkNativeWindowClient::dispatchSetBuffersFormat(va_list args) { 1.345 + int f = va_arg(args, int); 1.346 + return setBuffersFormat(f); 1.347 +} 1.348 + 1.349 +int GonkNativeWindowClient::dispatchSetBuffersTimestamp(va_list args) { 1.350 + int64_t timestamp = va_arg(args, int64_t); 1.351 + return setBuffersTimestamp(timestamp); 1.352 +} 1.353 + 1.354 +int GonkNativeWindowClient::connect(int api) { 1.355 + CNW_LOGD("GonkNativeWindowClient::connect"); 1.356 + Mutex::Autolock lock(mMutex); 1.357 + int err = mNativeWindow->connect(api, 1.358 + &mDefaultWidth, &mDefaultHeight, &mTransformHint); 1.359 + if (!err && api == NATIVE_WINDOW_API_CPU) { 1.360 + mConnectedToCpu = true; 1.361 + } 1.362 + return err; 1.363 +} 1.364 + 1.365 +int GonkNativeWindowClient::disconnect(int api) { 1.366 + CNW_LOGD("GonkNativeWindowClient::disconnect"); 1.367 + Mutex::Autolock lock(mMutex); 1.368 + freeAllBuffers(); 1.369 + int err = mNativeWindow->disconnect(api); 1.370 + if (!err) { 1.371 + mReqFormat = 0; 1.372 + mReqWidth = 0; 1.373 + mReqHeight = 0; 1.374 + mReqUsage = 0; 1.375 + if (api == NATIVE_WINDOW_API_CPU) { 1.376 + mConnectedToCpu = false; 1.377 + } 1.378 + } 1.379 + return err; 1.380 +} 1.381 + 1.382 +int GonkNativeWindowClient::setUsage(uint32_t reqUsage) 1.383 +{ 1.384 + CNW_LOGD("GonkNativeWindowClient::setUsage"); 1.385 + Mutex::Autolock lock(mMutex); 1.386 + mReqUsage = reqUsage; 1.387 + return OK; 1.388 +} 1.389 + 1.390 +int GonkNativeWindowClient::setBufferCount(int bufferCount) 1.391 +{ 1.392 + CNW_LOGD("GonkNativeWindowClient::setBufferCount"); 1.393 + Mutex::Autolock lock(mMutex); 1.394 + 1.395 + status_t err = mNativeWindow->setBufferCount(bufferCount); 1.396 + if (err == NO_ERROR) { 1.397 + freeAllBuffers(); 1.398 + } 1.399 + 1.400 + return err; 1.401 +} 1.402 + 1.403 +int GonkNativeWindowClient::setBuffersDimensions(int w, int h) 1.404 +{ 1.405 + CNW_LOGD("GonkNativeWindowClient::setBuffersDimensions"); 1.406 + Mutex::Autolock lock(mMutex); 1.407 + 1.408 + if (w<0 || h<0) 1.409 + return BAD_VALUE; 1.410 + 1.411 + if ((w && !h) || (!w && h)) 1.412 + return BAD_VALUE; 1.413 + 1.414 + mReqWidth = w; 1.415 + mReqHeight = h; 1.416 + 1.417 + status_t err = OK; 1.418 + 1.419 + return err; 1.420 +} 1.421 + 1.422 +int GonkNativeWindowClient::setBuffersFormat(int format) 1.423 +{ 1.424 + CNW_LOGD("GonkNativeWindowClient::setBuffersFormat"); 1.425 + Mutex::Autolock lock(mMutex); 1.426 + 1.427 + if (format<0) 1.428 + return BAD_VALUE; 1.429 + 1.430 + mReqFormat = format; 1.431 + 1.432 + return NO_ERROR; 1.433 +} 1.434 + 1.435 + 1.436 +int GonkNativeWindowClient::setBuffersTimestamp(int64_t timestamp) 1.437 +{ 1.438 + CNW_LOGD("GonkNativeWindowClient::setBuffersTimestamp"); 1.439 + Mutex::Autolock lock(mMutex); 1.440 + mTimestamp = timestamp; 1.441 + return NO_ERROR; 1.442 +} 1.443 + 1.444 +void GonkNativeWindowClient::freeAllBuffers() { 1.445 + for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { 1.446 + mSlots[i] = 0; 1.447 + } 1.448 +} 1.449 +